Skip to content

Add purgeStorage to InMemoryStorage #162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion Sources/Sharing/SharedKeys/InMemoryKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ extension SharedReaderKey {
///
/// See ``SharedReaderKey/inMemory(_:)`` to create values of this type.
public struct InMemoryKey<Value: Sendable>: SharedKey {
private let key: String
private let store: InMemoryStorage
fileprivate let key: String
fileprivate init(_ key: String) {
@Dependency(\.defaultInMemoryStorage) var defaultInMemoryStorage
self.key = key
Expand Down Expand Up @@ -80,6 +80,15 @@ public struct InMemoryStorage: Hashable, Sendable {
public func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
// public func removeValue(for key: String) {
// removeValue(for: )
// }
public func removeValue<Value>(for key: InMemoryKey<Value>, default defaultValue: Value) {
values.removeValue(for: key, default: defaultValue)
}
public func removeValue<Value>(for key: InMemoryKey<Value>.Default) {
removeValue(for: key.base, default: key.initialValue)
}
fileprivate final class Values: Sendable {
let storage = Mutex<[String: any Sendable]>([:])

Expand All @@ -99,6 +108,18 @@ public struct InMemoryStorage: Hashable, Sendable {
}
}
}

func removeValue<Value>(for key: InMemoryKey<Value>, default defaultValue: Value) {
@Dependency(PersistentReferences.self) var persistentReferences
let reference = persistentReferences.value(
forKey: .inMemory(key.key),
default: defaultValue,
skipInitialLoad: false
)
reference.withLock { value in
value = defaultValue
}
}
}
}

Expand Down
24 changes: 24 additions & 0 deletions Tests/SharingTests/InMemoryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,30 @@ import Testing
#expect(count == 2)
}

@Test func removeValue() async {
@Dependency(\.defaultInMemoryStorage) var storage

do {
@Shared(.inMemory("count")) var count = 0

#expect(count == 0)

$count.withLock { $0 += 1 }

#expect(count == 1)
}

do {
@Shared(.inMemory("count")) var count = 0

#expect(count == 1)

storage.removeValue(for: InMemoryKey<Int>.inMemory("count"), default: 0)

#expect(count == 0)
}
}

@Test func referenceCounting() async throws {
do {
@Shared(.inMemory("count")) var count = 0
Expand Down
27 changes: 27 additions & 0 deletions Tests/SharingTests/ObservationTests.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Dependencies
import PerceptionCore
import Sharing
import Testing
Expand Down Expand Up @@ -29,4 +30,30 @@ import Testing
$count.withLock { $0 += 1 }
}
}

@Test func removeValueShared() async {
@Dependency(\.defaultInMemoryStorage) var storage
@Shared(.inMemory("count")) var count = 0
await confirmation { confirm in
withPerceptionTracking {
_ = count
} onChange: {
confirm()
}
storage.removeValue(for: InMemoryKey<Int>.inMemory("count"), default: 0)
}
}

@Test func removeValueSharedReader() async {
@Dependency(\.defaultInMemoryStorage) var storage
@SharedReader(.inMemory("count")) var count = 0
await confirmation { confirm in
withPerceptionTracking {
_ = count
} onChange: {
confirm()
}
storage.removeValue(for: InMemoryKey<Int>.inMemory("count"), default: 0)
}
}
}