Skip to content
This repository was archived by the owner on Dec 21, 2018. It is now read-only.

Commit

Permalink
Add priority sorting for runAfter.
Browse files Browse the repository at this point in the history
  • Loading branch information
jheer committed Feb 9, 2018
1 parent b97ec18 commit d2a6c6a
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/dataflow/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,18 @@ export function run(encode) {
if (df._postrun.length) {
var postrun = df._postrun;
df._postrun = [];
postrun.forEach(function(f) {
try { f(df); } catch (err) { df.error(err); }
});
postrun
.sort(function(a, b) { return b.priority - a.priority; })
.forEach(function(_) { invokeCallback(df, _.callback); });
}

return count;
}

function invokeCallback(df, callback) {
try { callback(df); } catch (err) { df.error(err); }
}

/**
* Runs the dataflow and returns a Promise that resolves when the
* propagation cycle completes. The standard run method may exit early
Expand All @@ -127,16 +131,19 @@ export function runAsync() {
* sole argument.
* @param {boolean} enqueue - A boolean flag indicating that the
* callback should be queued up to run after the next propagation
* cycle, suppressing immediate invovation when propagation is not
* cycle, suppressing immediate invocation when propagation is not
* currently occurring.
*/
export function runAfter(callback, enqueue) {
export function runAfter(callback, enqueue, priority) {
if (this._pulse || enqueue) {
// pulse propagation is currently running, queue to run after
this._postrun.push(callback);
this._postrun.push({
priority: priority || 0,
callback: callback
});
} else {
// pulse propagation already complete, invoke immediately
try { callback(this); } catch (err) { this.error(err); }
invokeCallback(this, callback);
}
}

Expand Down

0 comments on commit d2a6c6a

Please sign in to comment.