-
-
Save jordaaash/2ae5584718cfae5724b1f163a1a23fce to your computer and use it in GitHub Desktop.
Patch for making the Bluebird library aware of Zone.js
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
'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; |
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
'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