Skip to content

Commit

Permalink
doc
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Usbergo committed Mar 22, 2020
1 parent d875ebd commit a5f497c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -393,3 +393,46 @@ RunGroup {
}
}
```

### Combined Stores

Support for children store (similar to Redux `combineStores`).

```swift
struct Root {
struct Todo {
var name: String = "Untitled"
var done: Bool = false
}
struct Note {
var text: String = ""
var upvotes: Int = 0
}
var todo: Todo = Todo()
var note: Note = Note()
}

class RootStore: Store<Root> {
lazy var todoStore = makeChildStore(keyPath: \.todo)
lazy var noteStore = makeChildStore(keyPath: \.note)
}

extension Root.Todo {
struct Action_MarkAsDone: ActionProtocol {
func reduce(context: TransactionContext<Store<Root.Todo>, Self>) {
defer { context.fulfill() }
context.reduceModel { $0.done = true }
}
}
}

extension Root.Note {
struct Action_IncreaseUpvotes: ActionProtocol {
func reduce(context: TransactionContext<Store<Root.Note>, Self>) {
defer { context.fulfill() }
context.reduceModel { $0.upvotes += 1 }
}
}
}

```
4 changes: 2 additions & 2 deletions Tests/StoreTests/CombinedStores.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class RootStore: Store<Root> {
extension Root.Todo {
struct Action_MarkAsDone: ActionProtocol {
func reduce(context: TransactionContext<Store<Root.Todo>, Self>) {
defer { context.fulfill() }
defer { context.fulfill() }
context.reduceModel { $0.done = true }
}
}
Expand All @@ -34,7 +34,7 @@ extension Root.Todo {
extension Root.Note {
struct Action_IncreaseUpvotes: ActionProtocol {
func reduce(context: TransactionContext<Store<Root.Note>, Self>) {
defer { context.fulfill() }
defer { context.fulfill() }
context.reduceModel { $0.upvotes += 1 }
}
}
Expand Down

0 comments on commit a5f497c

Please sign in to comment.