Last active
November 28, 2020 09:02
-
-
Save muukii/10c3e5513a63cfebf2eca790d6a2f767 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final class StoreA: Store<StateA, Never> { | |
} | |
struct StateB: Equatable { | |
// Changesに包まれたままStateBに埋め込むとStateAの暗黙のコピーが発生しないのでパフォーマンスに優しいです。 | |
var stateA: Changes<StateA> | |
// StateAのものを読み出したい場合はcomputed-propertyで実現可能 | |
var something: String { | |
stateA.foo.bar | |
} | |
... | |
} | |
final class StoreB: Store<StateB, Never> { | |
init(parent: RootStore) { | |
super.init(...) | |
// 無難なやりかた | |
parent.sinkState { [weak self] state in | |
guard let self = self else { return } | |
self.commit { | |
$0.stateA = state | |
} | |
} | |
.store(in: &cancellables) | |
// assignをつかった少し短い書き方 | |
parent.assign(assignee(\.stateA)) | |
.store(in: &cancellables) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment