Skip to content

Commit

Permalink
Add zone.js compat. before/after task hooks (#16)
Browse files Browse the repository at this point in the history
kraman committed Jul 11, 2014

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
1 parent 5f608c2 commit 61301e3
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 additions & 9 deletions lib/Zone.js
Original file line number Diff line number Diff line change
@@ -601,6 +601,15 @@ Zone.prototype._apply = function _apply(thisArg, fn, args) {
}
};

function _runTask(thisArg, fn) {
try {
fn.apply(thisArg);
return true;
} catch (err) {
return false;
}
}


/**
* The apply() method calls a function with a given this value and arguments
@@ -618,7 +627,16 @@ Zone.prototype.apply = function apply(thisArg, fn, args) {
} else {
var previousZone = zone;
global.zone = this;
var result = this._apply(thisArg, fn, args);
var beforeTaskSucceeded = true;
if (this._beforeTask) {
beforeTaskSucceeded = _runTask(thisArg, this._beforeTask);
}
if (beforeTaskSucceeded) {
var result = this._apply(thisArg, fn, args);
if (this._afterTask) {
_runTask(thisArg, this._afterTask);
}
}
global.zone = previousZone;
return result;
}
@@ -683,18 +701,11 @@ Zone.prototype.callAsync = function callAsync(thisArg, fn) {
* @private
*/
Zone.prototype._run = function run(fn) {
if (this._beforeTask) {
this.apply(this, this._beforeTask);
}
var args = new Array(arguments.length - 1);
for (var i = 1; i < arguments.length; i++) {
args[i - 1] = arguments[i];
}
var result = this.apply(this, fn, args);
if (this._afterTask) {
this.apply(this, this._afterTask);
}
return result;
return result = this.apply(this, fn, args);
};


@@ -1199,12 +1210,16 @@ exports.Zone = Zone;
* @callback Zone~beforeTask
*
* Before a function invoked within the zone, this hook runs.
* Note: This hook must never cause any async tasks as this would lead to an
* infinite loop.
*/

/**
* @callback Zone~afterTask
*
* After a function in a zone runs, the afterTask hook runs.
* Note: This hook must never cause any async tasks as this would lead to an
* infinite loop.
*/

/**

0 comments on commit 61301e3

Please sign in to comment.