Skip to content

Commit

Permalink
docs: update example
Browse files Browse the repository at this point in the history
  • Loading branch information
r13v committed Jul 5, 2023
1 parent cba1fd2 commit dbe6db9
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 31 deletions.
8 changes: 5 additions & 3 deletions Effector/Store.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public final class Store<State>: 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)
}

Expand All @@ -32,6 +32,7 @@ public final class Store<State>: Unit, ObservableObject {

return newState
}

graphite.seq.append(contentsOf: [filter, assign])
graphite.appendNext(updates.graphite)

Expand Down Expand Up @@ -178,13 +179,14 @@ public final class Store<State>: 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)
}
}
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version: 5.6
// swift-tools-version: 5.8

import PackageDescription

Expand Down
59 changes: 32 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,51 @@
import Effector
import SwiftUI

let store = Store(0)
let inc = Event<Void>()
let dec = Event<Void>()
enum CounterFeature {
static let counter = Store(0)
static let inc = Event<Void>()
static let dec = Event<Void>()

let logFx = Effect<Int, Void, Error> { n in print("n: \(n)") }
static let logFx = Effect<Int, Void, Error> { n in print("n: \(n)") }

let incAsync: Effect<Void, Int, Error> = 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<Void, Int, Error> = 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 }
}
}
}
Expand All @@ -56,4 +60,5 @@ struct ContentView_Previews: PreviewProvider {
ContentView()
}
}

```

0 comments on commit dbe6db9

Please sign in to comment.