diff --git a/Effector/Store.swift b/Effector/Store.swift index e49922d..42f2a7f 100644 --- a/Effector/Store.swift +++ b/Effector/Store.swift @@ -14,7 +14,7 @@ public final class Store: Unit, ObservableObject { graphite = Node(name: name, kind: .store, priority: .child) - let filter = Node.Step.filter("\name:filter") { state in + let filter = Node.Step.filter("\(name):filter") { state in !areEqual(state, self.currentState) } @@ -32,6 +32,7 @@ public final class Store: Unit, ObservableObject { return newState } + graphite.seq.append(contentsOf: [filter, assign]) graphite.appendNext(updates.graphite) @@ -178,13 +179,14 @@ public final class Store: Unit, ObservableObject { } } - public func areEqual(_ lhs: Any, _ rhs: Any) -> Bool { +public func areEqual(_ lhs: Any, _ rhs: Any) -> Bool { guard lhs is AnyHashable else { return false } + guard rhs is AnyHashable else { return false } return (lhs as! AnyHashable) == (rhs as! AnyHashable) - } +} diff --git a/Package.swift b/Package.swift index 18bc899..f84c948 100644 --- a/Package.swift +++ b/Package.swift @@ -1,4 +1,4 @@ -// swift-tools-version: 5.6 +// swift-tools-version: 5.8 import PackageDescription diff --git a/README.md b/README.md index f6710de..43e00be 100644 --- a/README.md +++ b/README.md @@ -6,47 +6,51 @@ import Effector import SwiftUI -let store = Store(0) -let inc = Event() -let dec = Event() +enum CounterFeature { + static let counter = Store(0) + static let inc = Event() + static let dec = Event() -let logFx = Effect { n in print("n: \(n)") } + static let logFx = Effect { n in print("n: \(n)") } -let incAsync: Effect = attach(store: store) { store, _ in - try? await Task.sleep(nanoseconds: 2_000_000_000) - return store + 10 -} - -func bootstrap() { - store - .on(inc) { n, _ in n + 1 } - .on(dec) { n, _ in n - 1 } + static let incAsync: Effect = attach(store: counter) { store, _ in + try? await Task.sleep(nanoseconds: 2_000_000_000) + return store + 10 + } - sample( - trigger: store.updates, - source: store, - map: { s, _ in s + 10 }, - target: logFx - ) + static func bind() { + counter + .on(inc) { n, _ in n + 1 } + .on(dec) { n, _ in n - 1 } + .on(incAsync.doneData) { _, n in n } - forward(from: [incAsync.doneData], to: [store]) + sample( + trigger: counter.updates, + target: logFx + ) + } } struct ContentView: View { + // MARK: Lifecycle + init() { - bootstrap() + CounterFeature.bind() } - @UseStore(store) var value + // MARK: Internal + + @Use(CounterFeature.counter) + var counter var body: some View { VStack { - Text("\(value)") + Text("\(counter)") - Button("dec", action: dec.run) - Button("inc", action: inc.run) - Button("inc 10 async", action: { Task { try await incAsync() }}) - Button("set 100") { value = 100 } + Button("dec", action: CounterFeature.dec.run) + Button("inc", action: CounterFeature.inc.run) + Button("inc 10 async", action: { Task { try await CounterFeature.incAsync() }}) + Button("set 100") { counter = 100 } } } } @@ -56,4 +60,5 @@ struct ContentView_Previews: PreviewProvider { ContentView() } } + ```