Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
stephencelis committed Apr 18, 2024
1 parent 26e5995 commit fa2bdb2
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,18 @@ struct CounterTabView: View {
forKey: "isOn"
)
}
}
Button("Write directly to stats.json") {
try? JSONEncoder().encode(
Stats(
count: .random(in: 1...1_000),
maxCount: .random(in: 1...1_000),
minCount: .random(in: 1...1_000),
numberOfCounts: .random(in: 1...1_000)
)
)
.write(to: .stats)
}
}
}
.buttonStyle(.borderless)
.navigationTitle("Shared State Demo")
Expand Down Expand Up @@ -252,12 +263,12 @@ struct SharedStateView: View {
var body: some View {
TabView(selection: $store.currentTab.sending(\.selectTab)) {
NavigationStack {
let _ = print(
CounterTabView(
store: self.store.scope(state: \.counter, action: \.counter)
)
.defaultAppStorage(UserDefaults(suiteName: "pointfree")!)
)
// let _ = print(
// CounterTabView(
// store: self.store.scope(state: \.counter, action: \.counter)
// )
// .defaultAppStorage(UserDefaults(suiteName: "pointfree")!)
// )
CounterTabView(
store: self.store.scope(state: \.counter, action: \.counter)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,33 @@ public final class FileStorageKey<Value: Codable>: PersistenceKey {
self.saveWorkItem?.cancel()
self.saveWorkItem = DispatchWorkItem { [weak self] in
guard let self else { return }
print("SAVE!!!!")
try? JSONEncoder().encode(value).write(to: self.url)
self.saveWorkItem = nil
}
saveQueue.asyncAfter(deadline: .now() + 5, execute: self.saveWorkItem!)
}

public var updates: AsyncStream<Value> {
.finished
AsyncStream { continuation in
let source = DispatchSource.makeFileSystemObjectSource(
fileDescriptor: open(self.url.path, O_EVTONLY),
eventMask: .write,
queue: self.saveQueue
)
source.setEventHandler {
guard let value = self.load()
else {
return
}
continuation.yield(value)
}
source.resume()
continuation.onTermination = { _ in
source.cancel()
close(source.handle)
}
}
}

public static func == (lhs: FileStorageKey<Value>, rhs: FileStorageKey<Value>) -> Bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,28 @@ public struct Shared<Value> {
@Perceptible
final class Storage {
let persistenceKey: (any PersistenceKey<Value>)?
var _currentValue: Value

var currentValue: Value {
didSet {
self.persistenceKey?.save(self.currentValue)
get {
_currentValue
}
set {
_currentValue = newValue
self.persistenceKey?.save(newValue)
}
}
var snapshot: Value?
init(
value: Value,
persistenceKey: (any PersistenceKey<Value>)? = nil
) {
self.currentValue = persistenceKey?.load() ?? value
self._currentValue = persistenceKey?.load() ?? value
self.persistenceKey = persistenceKey
if let updates = persistenceKey?.updates {
Task { @MainActor in
for await value in updates {
self.currentValue = value
self._currentValue = value
}
}
}
Expand Down

0 comments on commit fa2bdb2

Please sign in to comment.