Skip to content

Commit

Permalink
[refactoring] introduce metadata.facade
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Nov 5, 2020
1 parent 5a5a9f2 commit 8f75f24
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
2 changes: 2 additions & 0 deletions packages/core-js/internals/internal-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ if (NATIVE_WEAK_MAP) {
var wmhas = store.has;
var wmset = store.set;
set = function (it, metadata) {
metadata.facade = it;
wmset.call(store, it, metadata);
return metadata;
};
Expand All @@ -41,6 +42,7 @@ if (NATIVE_WEAK_MAP) {
var STATE = sharedKey('state');
hiddenKeys[STATE] = true;
set = function (it, metadata) {
metadata.facade = it;
createNonEnumerableProperty(it, STATE, metadata);
return metadata;
};
Expand Down
44 changes: 23 additions & 21 deletions packages/core-js/modules/es.promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ var isThenable = function (it) {
return isObject(it) && typeof (then = it.then) == 'function' ? then : false;
};

var notify = function (promise, state, isReject) {
var notify = function (state, isReject) {
if (state.notified) return;
state.notified = true;
var chain = state.reactions;
Expand All @@ -105,7 +105,7 @@ var notify = function (promise, state, isReject) {
try {
if (handler) {
if (!ok) {
if (state.rejection === UNHANDLED) onHandleUnhandled(promise, state);
if (state.rejection === UNHANDLED) onHandleUnhandled(state);
state.rejection = HANDLED;
}
if (handler === true) result = value;
Expand All @@ -130,7 +130,7 @@ var notify = function (promise, state, isReject) {
}
state.reactions = [];
state.notified = false;
if (isReject && !state.rejection) onUnhandled(promise, state);
if (isReject && !state.rejection) onUnhandled(state);
});
};

Expand All @@ -147,8 +147,9 @@ var dispatchEvent = function (name, promise, reason) {
else if (name === UNHANDLED_REJECTION) hostReportErrors('Unhandled promise rejection', reason);
};

var onUnhandled = function (promise, state) {
var onUnhandled = function (state) {
task.call(global, function () {
var promise = state.facade;
var value = state.value;
var IS_UNHANDLED = isUnhandled(state);
var result;
Expand All @@ -169,55 +170,56 @@ var isUnhandled = function (state) {
return state.rejection !== HANDLED && !state.parent;
};

var onHandleUnhandled = function (promise, state) {
var onHandleUnhandled = function (state) {
task.call(global, function () {
var promise = state.facade;
if (IS_NODE) {
process.emit('rejectionHandled', promise);
} else dispatchEvent(REJECTION_HANDLED, promise, state.value);
});
};

var bind = function (fn, promise, state, unwrap) {
var bind = function (fn, state, unwrap) {
return function (value) {
fn(promise, state, value, unwrap);
fn(state, value, unwrap);
};
};

var internalReject = function (promise, state, value, unwrap) {
var internalReject = function (state, value, unwrap) {
if (state.done) return;
state.done = true;
if (unwrap) state = unwrap;
state.value = value;
state.state = REJECTED;
notify(promise, state, true);
notify(state, true);
};

var internalResolve = function (promise, state, value, unwrap) {
var internalResolve = function (state, value, unwrap) {
if (state.done) return;
state.done = true;
if (unwrap) state = unwrap;
try {
if (promise === value) throw TypeError("Promise can't be resolved itself");
if (state.facade === value) throw TypeError("Promise can't be resolved itself");
var then = isThenable(value);
if (then) {
microtask(function () {
var wrapper = { done: false };
try {
then.call(value,
bind(internalResolve, promise, wrapper, state),
bind(internalReject, promise, wrapper, state)
bind(internalResolve, wrapper, state),
bind(internalReject, wrapper, state)
);
} catch (error) {
internalReject(promise, wrapper, error, state);
internalReject(wrapper, error, state);
}
});
} else {
state.value = value;
state.state = FULFILLED;
notify(promise, state, false);
notify(state, false);
}
} catch (error) {
internalReject(promise, { done: false }, error, state);
internalReject({ done: false }, error, state);
}
};

Expand All @@ -230,9 +232,9 @@ if (FORCED) {
Internal.call(this);
var state = getInternalState(this);
try {
executor(bind(internalResolve, this, state), bind(internalReject, this, state));
executor(bind(internalResolve, state), bind(internalReject, state));
} catch (error) {
internalReject(this, state, error);
internalReject(state, error);
}
};
// eslint-disable-next-line no-unused-vars
Expand All @@ -259,7 +261,7 @@ if (FORCED) {
reaction.domain = IS_NODE ? process.domain : undefined;
state.parent = true;
state.reactions.push(reaction);
if (state.state != PENDING) notify(this, state, false);
if (state.state != PENDING) notify(state, false);
return reaction.promise;
},
// `Promise.prototype.catch` method
Expand All @@ -272,8 +274,8 @@ if (FORCED) {
var promise = new Internal();
var state = getInternalState(promise);
this.promise = promise;
this.resolve = bind(internalResolve, promise, state);
this.reject = bind(internalReject, promise, state);
this.resolve = bind(internalResolve, state);
this.reject = bind(internalReject, state);
};
newPromiseCapabilityModule.f = newPromiseCapability = function (C) {
return C === PromiseConstructor || C === PromiseWrapper
Expand Down

0 comments on commit 8f75f24

Please sign in to comment.