-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
⚡️ Implement a custom
done
and waterfall
static method for the `S…
…wiftPledge` class
- Loading branch information
Showing
2 changed files
with
79 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1382,20 +1382,49 @@ var Timeout = Fn.inherits('Pledge', function TimeoutPledge(executor, timeout, me | |
* | ||
* @author Jelle De Loecker <[email protected]> | ||
* @since 0.8.15 | ||
* @version 0.8.15 | ||
* @version 0.9.3 | ||
* | ||
* @param {Function} executor | ||
*/ | ||
const Swift = Fn.inherits(Pledge, function SwiftPledge(executor) { | ||
this[EXECUTOR] = executor; | ||
this[START_EXECUTOR](false); | ||
if (executor != null) { | ||
this[EXECUTOR] = executor; | ||
this[START_EXECUTOR](false); | ||
} | ||
}); | ||
|
||
const SwiftObject = Object.create(Bound.Function); | ||
SwiftObject[Blast.flowPledgeClass] = Swift; | ||
SwiftObject[Blast.asyncScheduler] = Blast.callNow; | ||
Swift[FNC] = SwiftObject; | ||
|
||
/** | ||
* Handle an old-style callback for a pledge or promise | ||
* | ||
* @author Jelle De Loecker <[email protected]> | ||
* @since 0.9.3 | ||
* @version 0.9.3 | ||
*/ | ||
Swift.setStatic(function done(value, callback) { | ||
|
||
// Pass through falsy or non-object values | ||
if (!value || typeof value != 'object') { | ||
return callback(null, value); | ||
} | ||
|
||
// Pass non-thenables through | ||
if (!value.then) { | ||
return callback(null, value); | ||
} | ||
|
||
// See if we can rip the values out of Pledge instances | ||
if (value[STATE] == RESOLVED) { | ||
return callback(null, value[RESOLVED_VALUE]); | ||
} | ||
|
||
value.then(val => callback(null, val), callback); | ||
}); | ||
|
||
/** | ||
* Do the given tasks in parallel | ||
* | ||
|
@@ -1502,18 +1531,61 @@ Swift.setStatic(function execute(value) { | |
return value; | ||
}); | ||
|
||
/** | ||
* Do a single waterfall task | ||
* | ||
* @author Jelle De Loecker <[email protected]> | ||
* @since 0.9.3 | ||
* @version 0.9.3 | ||
*/ | ||
const doWaterfallTask = (pledge, tasks, index, max_index, previous_value) => { | ||
|
||
let task = tasks[index], | ||
next_value; | ||
|
||
if (typeof task == 'function') { | ||
try { | ||
next_value = task(previous_value); | ||
} catch (err) { | ||
return pledge.reject(err); | ||
} | ||
} else { | ||
next_value = task; | ||
} | ||
|
||
Swift.done(next_value, (err, result) => { | ||
|
||
if (err) { | ||
return pledge.reject(err); | ||
} | ||
|
||
if (index == max_index) { | ||
return pledge.resolve(result); | ||
} | ||
|
||
doWaterfallTask(pledge, tasks, index + 1, max_index, result); | ||
}); | ||
}; | ||
|
||
/** | ||
* Perform a swift waterfall | ||
* | ||
* @author Jelle De Loecker <[email protected]> | ||
* @since 0.8.15 | ||
* @version 0.8.15 | ||
* @version 0.9.3 | ||
* | ||
* @return {SwiftPledge|Mixed} | ||
*/ | ||
Swift.setStatic(function waterfall(...tasks) { | ||
let result = SwiftObject.waterfall(...tasks); | ||
return Swift.execute(result); | ||
let pledge = new Swift(); | ||
|
||
doWaterfallTask(pledge, tasks, 0, tasks.length - 1, null); | ||
|
||
if (pledge[STATE] == RESOLVED) { | ||
return pledge[RESOLVED_VALUE]; | ||
} | ||
|
||
return pledge; | ||
}); | ||
|
||
/** | ||
|