-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
78 additions
and
37 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
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#if canImport(Combine) | ||
|
||
import Combine | ||
import ReactiveSwift | ||
|
||
@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) | ||
internal final class SwiftUIHotSwappableSubscription<State, Event>: ObservableObject { | ||
|
||
@Published private var latestValue: State! | ||
private weak var attachedLoop: Loop<State, Event>? | ||
private var disposable: Disposable? | ||
|
||
init() {} | ||
|
||
deinit { | ||
disposable?.dispose() | ||
} | ||
|
||
func currentState(in loop: Loop<State, Event>) -> State { | ||
if attachedLoop !== loop { | ||
disposable?.dispose() | ||
|
||
latestValue = loop.box._current | ||
|
||
disposable = loop.producer | ||
.observe(on: UIScheduler()) | ||
.startWithValues { [weak self] state in | ||
guard let self = self else { return } | ||
self.latestValue = state | ||
} | ||
} | ||
|
||
return latestValue | ||
} | ||
} | ||
|
||
#endif |
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