Created
April 27, 2020 09:43
-
-
Save cipolleschi/5f9b0ab33fe39b39d3c3235bb09f30c7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| static func waitForStateUpdate(context: SideEffectContext<AppState, DependenciesContainer>, | |
| attempt: Int = 50, | |
| shouldPass: @escaping (_ context: SideEffectContext<AppState, DependenciesContainer>) -> Bool) -> Promise<Void> { | |
| func recursiveWait(context: SideEffectContext<AppState, DependenciesContainer>, | |
| attempt: Int, | |
| shouldPass: @escaping (_ context: SideEffectContext<AppState, DependenciesContainer>) -> Bool, | |
| resolve: @escaping (())->(), | |
| reject: @escaping (Error)->()) { | |
| guard shouldPass(context) else { | |
| if attempt == 0 { | |
| reject(WaitStateError.timeout) | |
| } else { | |
| DispatchQueue.global().asyncAfter(deadline: .now() + .milliseconds(100)) { | |
| recursiveWait(context: context, attempt: attempt - 1, shouldPass: shouldPass, resolve: resolve, reject: reject) | |
| } | |
| } | |
| return | |
| } | |
| resolve(()) | |
| } | |
| return Promise<Void> { (resolve, reject, _) in | |
| recursiveWait(context: context, attempt: attempt, shouldPass: shouldPass, resolve: resolve, reject: reject) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment