Skip to content

Instantly share code, notes, and snippets.

@jordaaash
Forked from lorenzodallavecchia/bluebird-zone.js
Last active February 15, 2017 21:52
Show Gist options
  • Save jordaaash/2ae5584718cfae5724b1f163a1a23fce to your computer and use it in GitHub Desktop.
Save jordaaash/2ae5584718cfae5724b1f163a1a23fce to your computer and use it in GitHub Desktop.
Patch for making the Bluebird library aware of Zone.js
'use strict';
import Promise from 'bluebird';
import {
zonifyCall,
zonifyCatch,
zonifyCoroutine,
zonifyFirst,
zonifyLast,
zonifyMiddle,
zonifyOne,
zonifySecond,
zonifyTwo
} from './zonify';
Promise.coroutine = zonifyCoroutine(Promise.coroutine);
Promise.each = zonifySecond(Promise.each);
Promise.filter = zonifyMiddle(Promise.filter);
Promise.fromCallback = zonifyFirst(Promise.fromCallback);
Promise.fromNode = Promise.fromCallback;
Promise.join = zonifyLast(Promise.join);
Promise.map = zonifyMiddle(Promise.map);
Promise.mapSeries = zonifySecond(Promise.mapSeries);
Promise.onPossiblyUnhandledRejection = zonifyOne(Promise.onPossiblyUnhandledRejection);
Promise.onUnhandledRejectionHandled = zonifyOne(Promise.onUnhandledRejectionHandled);
Promise.reduce = zonifyMiddle(Promise.reduce);
Promise.using = zonifyLast(Promise.using);
Promise.prototype.asCallback = zonifyFirst(Promise.prototype.asCallback);
Promise.prototype.call = zonifyCall(Promise.prototype.call);
Promise.prototype.catch = zonifyCatch(Promise.prototype.catch);
Promise.prototype.caught = Promise.prototype.catch;
Promise.prototype.disposer = zonifyOne(Promise.prototype.disposer);
Promise.prototype.done = zonifyTwo(Promise.prototype.done);
Promise.prototype.each = zonifyOne(Promise.prototype.each);
Promise.prototype.error = zonifyOne(Promise.prototype.error);
Promise.prototype.filter = zonifyFirst(Promise.prototype.filter);
Promise.prototype.finally = zonifyOne(Promise.prototype.finally);
Promise.prototype.lastly = Promise.prototype.finally;
Promise.prototype.map = zonifyFirst(Promise.prototype.map);
Promise.prototype.mapSeries = zonifyOne(Promise.prototype.mapSeries);
Promise.prototype.nodeify = Promise.prototype.asCallback;
Promise.prototype.reduce = zonifyFirst(Promise.prototype.reduce);
Promise.prototype.spread = zonifyOne(Promise.prototype.spread);
Promise.prototype.tap = zonifyOne(Promise.prototype.tap);
Promise.prototype.then = zonifyTwo(Promise.prototype.then);
Zone.assertZonePatched = function () {};
window.Promise = Promise;
export default Promise;
'use strict';
import Zone from 'zone.js';
export const zonify = function (callback) {
return (typeof callback === 'function') ? Zone.current.wrap(callback) : callback;
};
export const zonifyOne = function (method) {
return function (one) {
return method.call(this, zonify(one));
};
};
export const zonifyTwo = function (method) {
return function (first, second) {
return method.call(this, zonify(first), zonify(second));
};
};
export const zonifyFirst = function (method) {
return function (first, second) {
return method.call(this, zonify(first), second);
};
};
export const zonifySecond = function (method) {
return function (first, second) {
return method.call(this, first, zonify(second));
};
};
export const zonifyMiddle = function (method) {
return function (first, second, third) {
return method.call(this, first, zonify(second), third);
};
};
export const zonifyLast = function (method) {
return function (...rest) {
const length = rest.length;
if (length > 0) {
const i = rest.length - 1;
rest[i] = zonify(rest[i]);
}
return method.apply(this, rest);
};
};
export const zonifyCall = function (method) {
return function (methodName, ...rest) {
return this.then(function (result) {
return result[methodName].apply(result, rest);
});
};
};
export const zonifyCatch = function (method) {
return function (...rest) {
for (let i = 0, length = rest.length, argument; i < length; i++) {
argument = rest[i];
if (!(argument instanceof Error)) {
rest[i] = zonify(argument);
}
}
return method.apply(this, rest);
};
};
export const zonifyCoroutine = function (method) {
const coroutine = zonifyFirst(method);
coroutine.addYieldHandler = zonifyOne(method.addYieldHandler);
return coroutine;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment