-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into muukii/accumulation
- Loading branch information
Showing
30 changed files
with
265 additions
and
212 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,4 +72,6 @@ fastlane/test_output | |
Derived | ||
*.xcodeproj | ||
|
||
Workspace.xcworkspace/ | ||
|
||
# End of https://www.gitignore.io/api/swift |
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,2 @@ | ||
[tools] | ||
tuist = "4.10.2" |
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
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,130 @@ | ||
import Foundation | ||
|
||
extension Store { | ||
|
||
/// Push an event to run event loop. | ||
public func updateMainLoop() { | ||
RunLoop.main.perform(inModes: [.common]) {} | ||
} | ||
|
||
/** | ||
Subscribes state updates in given run-loop. | ||
*/ | ||
public func pollMainLoop(receive: @escaping @MainActor (Changes<State>) -> Void) -> VergeAnyCancellable { | ||
|
||
var latestState: Changes<State>? = nil | ||
|
||
let subscription = RunLoopActivityObserver.addObserver(acitivity: .beforeWaiting, in: .main) { | ||
|
||
let newState = self.state | ||
|
||
guard (latestState?.version ?? 0) < newState.version else { | ||
return | ||
} | ||
|
||
latestState = newState | ||
|
||
let state: Changes<State> | ||
|
||
if let latestState { | ||
state = newState.replacePrevious(latestState) | ||
} else { | ||
state = newState.droppedPrevious() | ||
} | ||
|
||
MainActor.assumeIsolated { | ||
receive(state) | ||
} | ||
|
||
} | ||
|
||
return .init { | ||
RunLoopActivityObserver.remove(subscription) | ||
} | ||
} | ||
|
||
} | ||
|
||
private enum RunLoopActivityObserver { | ||
|
||
struct Subscription { | ||
let mode: CFRunLoopMode | ||
let observer: CFRunLoopObserver? | ||
weak var targetRunLoop: RunLoop? | ||
} | ||
|
||
static func addObserver(acitivity: CFRunLoopActivity, in runLoop: RunLoop, callback: @escaping () -> Void) -> Subscription { | ||
|
||
let o = CFRunLoopObserverCreateWithHandler(kCFAllocatorDefault, acitivity.rawValue, true, Int.max, { observer, activity in | ||
callback() | ||
}); | ||
|
||
assert(o != nil) | ||
|
||
let mode = CFRunLoopMode.commonModes! | ||
let cfRunLoop = runLoop.getCFRunLoop() | ||
|
||
CFRunLoopAddObserver(cfRunLoop, o, mode); | ||
|
||
return .init(mode: mode, observer: o, targetRunLoop: runLoop) | ||
} | ||
|
||
static func remove(_ subscription: Subscription) { | ||
|
||
guard let observer = subscription.observer, let targetRunLoop = subscription.targetRunLoop else { | ||
return | ||
} | ||
|
||
CFRunLoopRemoveObserver(targetRunLoop.getCFRunLoop(), observer, subscription.mode); | ||
} | ||
|
||
} | ||
|
||
#if DEBUG && canImport(SwiftUI) | ||
import SwiftUI | ||
|
||
#Preview { | ||
Content() | ||
} | ||
|
||
private struct StoreState: StateType { | ||
var count: Int = 0 | ||
} | ||
|
||
private struct Content: View { | ||
|
||
let store = Store<_, Never>(initialState: StoreState()) | ||
|
||
@State var subscription: VergeAnyCancellable? | ||
@State var timer: Timer? | ||
|
||
var body: some View { | ||
VStack { | ||
Button("Up") { | ||
store.commit { | ||
$0.count += 1 | ||
} | ||
} | ||
Button("Background Up") { | ||
|
||
for _ in 0..<10 { | ||
store.commit { | ||
$0.count += 1 | ||
} | ||
} | ||
|
||
} | ||
Button("Run") { | ||
RunLoop.main.run(until: Date(timeIntervalSinceNow: 19)) | ||
} | ||
} | ||
.onAppear { | ||
subscription = store.pollMainLoop { state in | ||
print(state.count) | ||
} | ||
} | ||
} | ||
|
||
} | ||
#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
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
Oops, something went wrong.