-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tech(observer): Use registry to observe arrays (#57)
- Loading branch information
Showing
8 changed files
with
200 additions
and
77 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,15 @@ | ||
/// A protocol allowing to observe a value returned from the `IdentityMap` | ||
public protocol Observer { | ||
associatedtype T | ||
|
||
/// The value at the time the observer creation. | ||
/// If you want **realtime** value use `observe to get notified of changes | ||
var value: T { get } | ||
|
||
/// Add an observer being notified when entity change. | ||
/// Alternatively you can use `asPublisher` to observe using Combine. | ||
/// - Parameter onChange: a closure called when value changed | ||
/// - Returns: a subscription to cancel observation. Observation is automatically cancelled if subscription is deinit. | ||
/// As long as the subscription is alived the entity should be kept in `IdentityMap`. | ||
func observe(onChange: @escaping (T) -> Void) -> Subscription | ||
} | ||
|
||
extension Array: Observer where Element: Observer { | ||
public var value: [Element.T] { map(\.value) } | ||
|
||
public func observe(onChange: @escaping ([Element.T]) -> Void) -> Subscription { | ||
var value = value | ||
|
||
let subscriptions = indices.map { index in | ||
self[index].observe { | ||
value[index] = $0 | ||
onChange(value) | ||
} | ||
} | ||
|
||
return Subscription { | ||
subscriptions.forEach { $0.unsubscribe() } | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.