| 1 | "use strict";
|
|---|
| 2 | module.exports = function(Promise, INTERNAL, tryConvertToPromise, debug) {
|
|---|
| 3 | var calledBind = false;
|
|---|
| 4 | var rejectThis = function(_, e) {
|
|---|
| 5 | this._reject(e);
|
|---|
| 6 | };
|
|---|
| 7 |
|
|---|
| 8 | var targetRejected = function(e, context) {
|
|---|
| 9 | context.promiseRejectionQueued = true;
|
|---|
| 10 | context.bindingPromise._then(rejectThis, rejectThis, null, this, e);
|
|---|
| 11 | };
|
|---|
| 12 |
|
|---|
| 13 | var bindingResolved = function(thisArg, context) {
|
|---|
| 14 | if (((this._bitField & 50397184) === 0)) {
|
|---|
| 15 | this._resolveCallback(context.target);
|
|---|
| 16 | }
|
|---|
| 17 | };
|
|---|
| 18 |
|
|---|
| 19 | var bindingRejected = function(e, context) {
|
|---|
| 20 | if (!context.promiseRejectionQueued) this._reject(e);
|
|---|
| 21 | };
|
|---|
| 22 |
|
|---|
| 23 | Promise.prototype.bind = function (thisArg) {
|
|---|
| 24 | if (!calledBind) {
|
|---|
| 25 | calledBind = true;
|
|---|
| 26 | Promise.prototype._propagateFrom = debug.propagateFromFunction();
|
|---|
| 27 | Promise.prototype._boundValue = debug.boundValueFunction();
|
|---|
| 28 | }
|
|---|
| 29 | var maybePromise = tryConvertToPromise(thisArg);
|
|---|
| 30 | var ret = new Promise(INTERNAL);
|
|---|
| 31 | ret._propagateFrom(this, 1);
|
|---|
| 32 | var target = this._target();
|
|---|
| 33 | ret._setBoundTo(maybePromise);
|
|---|
| 34 | if (maybePromise instanceof Promise) {
|
|---|
| 35 | var context = {
|
|---|
| 36 | promiseRejectionQueued: false,
|
|---|
| 37 | promise: ret,
|
|---|
| 38 | target: target,
|
|---|
| 39 | bindingPromise: maybePromise
|
|---|
| 40 | };
|
|---|
| 41 | target._then(INTERNAL, targetRejected, undefined, ret, context);
|
|---|
| 42 | maybePromise._then(
|
|---|
| 43 | bindingResolved, bindingRejected, undefined, ret, context);
|
|---|
| 44 | ret._setOnCancel(maybePromise);
|
|---|
| 45 | } else {
|
|---|
| 46 | ret._resolveCallback(target);
|
|---|
| 47 | }
|
|---|
| 48 | return ret;
|
|---|
| 49 | };
|
|---|
| 50 |
|
|---|
| 51 | Promise.prototype._setBoundTo = function (obj) {
|
|---|
| 52 | if (obj !== undefined) {
|
|---|
| 53 | this._bitField = this._bitField | 2097152;
|
|---|
| 54 | this._boundTo = obj;
|
|---|
| 55 | } else {
|
|---|
| 56 | this._bitField = this._bitField & (~2097152);
|
|---|
| 57 | }
|
|---|
| 58 | };
|
|---|
| 59 |
|
|---|
| 60 | Promise.prototype._isBound = function () {
|
|---|
| 61 | return (this._bitField & 2097152) === 2097152;
|
|---|
| 62 | };
|
|---|
| 63 |
|
|---|
| 64 | Promise.bind = function (thisArg, value) {
|
|---|
| 65 | return Promise.resolve(value).bind(thisArg);
|
|---|
| 66 | };
|
|---|
| 67 | };
|
|---|