Created
June 5, 2018 08:57
-
-
Save anthonyvitocuaderno/16253b572043fa089c9cdc3ec459c72e to your computer and use it in GitHub Desktop.
Promise Prototype for iOS Swift 3.3
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
// | |
// Promise.swift | |
// R2S | |
// | |
// Created by Vito Cuaderno on 5/9/18. | |
// Copyright © 2018 Total Integrated Resources. All rights reserved. | |
// | |
import Foundation | |
/** | |
Basic PROMISE prototype | |
*/ | |
class Promise { | |
/** sample implementation | |
_ = Promise{ | |
printD("try promise...") | |
return arc4random_uniform(2) == 0 | |
}.success { | |
printD("try promise...success") | |
}.failed { | |
printE("try promise...failed") | |
}.completed { | |
printD("try promise.........completed") | |
}.retry{ | |
printD("retry promise...") | |
return arc4random_uniform(2) == 0 | |
}.success { | |
printD("retry promise...success") | |
}.failed { | |
printE("retry promise...failed") | |
}.completed { | |
printD("retry promise.........completed") | |
} | |
*/ | |
private var fulfillment:(()->Void)? | |
private var falseHope:(()->Void)? | |
private var completed:(()->Void)? | |
private var retry:Promise? | |
var pendingProcess:(()->Bool)? | |
/** | |
Runs a function in background thread "then" on main thread | |
- Parameter backgroundProcess: your heavy operation | |
- Returns: a Promise that may be fulfilled | |
*/ | |
init(runImmediately:Bool = true, backgroundProcess:@escaping ()->Bool) { | |
if runImmediately { | |
self.run(backgroundProcess: backgroundProcess) | |
} else { | |
self.pendingProcess = backgroundProcess | |
} | |
} | |
func run(backgroundProcess:@escaping ()->Bool) { | |
DispatchQueue.global(qos: .background).async { | |
if backgroundProcess() { | |
DispatchQueue.main.async { | |
self.fulfillment?() | |
} | |
} else { | |
DispatchQueue.main.async { | |
self.falseHope?() | |
} | |
} | |
DispatchQueue.main.async { | |
self.completed?() | |
if let retry = self.retry { | |
retry.run(backgroundProcess: (self.retry?.pendingProcess)!) | |
} | |
} | |
} | |
} | |
func retry(backgroundProcess:@escaping ()->Bool) -> Promise { | |
self.retry = Promise(runImmediately: false, backgroundProcess: backgroundProcess) | |
return self.retry! | |
} | |
/** | |
Runs the function if promise is fulfilled | |
- Parameter onCompleted: result process | |
- Returns: a Promise that may be fulfilled | |
*/ | |
func success(onCompleted:@escaping ()->Void) -> Promise { | |
self.fulfillment = onCompleted | |
return self | |
} | |
/** | |
Runs the function if promise is broken | |
- Parameter backgroundProcess: result process | |
- Returns: a Promise that may be fulfilled | |
*/ | |
func failed(onBroken:@escaping ()->Void) -> Promise { | |
self.falseHope = onBroken | |
return self | |
} | |
func completed(onCompleted:@escaping ()->Void) -> Promise { | |
self.completed = onCompleted | |
return self | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment