Skip to content

Commit 8f75f24

Browse files
committed
[refactoring] introduce metadata.facade
1 parent 5a5a9f2 commit 8f75f24

File tree

2 files changed

+25
-21
lines changed

2 files changed

+25
-21
lines changed

packages/core-js/internals/internal-state.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ if (NATIVE_WEAK_MAP) {
2828
var wmhas = store.has;
2929
var wmset = store.set;
3030
set = function (it, metadata) {
31+
metadata.facade = it;
3132
wmset.call(store, it, metadata);
3233
return metadata;
3334
};
@@ -41,6 +42,7 @@ if (NATIVE_WEAK_MAP) {
4142
var STATE = sharedKey('state');
4243
hiddenKeys[STATE] = true;
4344
set = function (it, metadata) {
45+
metadata.facade = it;
4446
createNonEnumerableProperty(it, STATE, metadata);
4547
return metadata;
4648
};

packages/core-js/modules/es.promise.js

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ var isThenable = function (it) {
8686
return isObject(it) && typeof (then = it.then) == 'function' ? then : false;
8787
};
8888

89-
var notify = function (promise, state, isReject) {
89+
var notify = function (state, isReject) {
9090
if (state.notified) return;
9191
state.notified = true;
9292
var chain = state.reactions;
@@ -105,7 +105,7 @@ var notify = function (promise, state, isReject) {
105105
try {
106106
if (handler) {
107107
if (!ok) {
108-
if (state.rejection === UNHANDLED) onHandleUnhandled(promise, state);
108+
if (state.rejection === UNHANDLED) onHandleUnhandled(state);
109109
state.rejection = HANDLED;
110110
}
111111
if (handler === true) result = value;
@@ -130,7 +130,7 @@ var notify = function (promise, state, isReject) {
130130
}
131131
state.reactions = [];
132132
state.notified = false;
133-
if (isReject && !state.rejection) onUnhandled(promise, state);
133+
if (isReject && !state.rejection) onUnhandled(state);
134134
});
135135
};
136136

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

150-
var onUnhandled = function (promise, state) {
150+
var onUnhandled = function (state) {
151151
task.call(global, function () {
152+
var promise = state.facade;
152153
var value = state.value;
153154
var IS_UNHANDLED = isUnhandled(state);
154155
var result;
@@ -169,55 +170,56 @@ var isUnhandled = function (state) {
169170
return state.rejection !== HANDLED && !state.parent;
170171
};
171172

172-
var onHandleUnhandled = function (promise, state) {
173+
var onHandleUnhandled = function (state) {
173174
task.call(global, function () {
175+
var promise = state.facade;
174176
if (IS_NODE) {
175177
process.emit('rejectionHandled', promise);
176178
} else dispatchEvent(REJECTION_HANDLED, promise, state.value);
177179
});
178180
};
179181

180-
var bind = function (fn, promise, state, unwrap) {
182+
var bind = function (fn, state, unwrap) {
181183
return function (value) {
182-
fn(promise, state, value, unwrap);
184+
fn(state, value, unwrap);
183185
};
184186
};
185187

186-
var internalReject = function (promise, state, value, unwrap) {
188+
var internalReject = function (state, value, unwrap) {
187189
if (state.done) return;
188190
state.done = true;
189191
if (unwrap) state = unwrap;
190192
state.value = value;
191193
state.state = REJECTED;
192-
notify(promise, state, true);
194+
notify(state, true);
193195
};
194196

195-
var internalResolve = function (promise, state, value, unwrap) {
197+
var internalResolve = function (state, value, unwrap) {
196198
if (state.done) return;
197199
state.done = true;
198200
if (unwrap) state = unwrap;
199201
try {
200-
if (promise === value) throw TypeError("Promise can't be resolved itself");
202+
if (state.facade === value) throw TypeError("Promise can't be resolved itself");
201203
var then = isThenable(value);
202204
if (then) {
203205
microtask(function () {
204206
var wrapper = { done: false };
205207
try {
206208
then.call(value,
207-
bind(internalResolve, promise, wrapper, state),
208-
bind(internalReject, promise, wrapper, state)
209+
bind(internalResolve, wrapper, state),
210+
bind(internalReject, wrapper, state)
209211
);
210212
} catch (error) {
211-
internalReject(promise, wrapper, error, state);
213+
internalReject(wrapper, error, state);
212214
}
213215
});
214216
} else {
215217
state.value = value;
216218
state.state = FULFILLED;
217-
notify(promise, state, false);
219+
notify(state, false);
218220
}
219221
} catch (error) {
220-
internalReject(promise, { done: false }, error, state);
222+
internalReject({ done: false }, error, state);
221223
}
222224
};
223225

@@ -230,9 +232,9 @@ if (FORCED) {
230232
Internal.call(this);
231233
var state = getInternalState(this);
232234
try {
233-
executor(bind(internalResolve, this, state), bind(internalReject, this, state));
235+
executor(bind(internalResolve, state), bind(internalReject, state));
234236
} catch (error) {
235-
internalReject(this, state, error);
237+
internalReject(state, error);
236238
}
237239
};
238240
// eslint-disable-next-line no-unused-vars
@@ -259,7 +261,7 @@ if (FORCED) {
259261
reaction.domain = IS_NODE ? process.domain : undefined;
260262
state.parent = true;
261263
state.reactions.push(reaction);
262-
if (state.state != PENDING) notify(this, state, false);
264+
if (state.state != PENDING) notify(state, false);
263265
return reaction.promise;
264266
},
265267
// `Promise.prototype.catch` method
@@ -272,8 +274,8 @@ if (FORCED) {
272274
var promise = new Internal();
273275
var state = getInternalState(promise);
274276
this.promise = promise;
275-
this.resolve = bind(internalResolve, promise, state);
276-
this.reject = bind(internalReject, promise, state);
277+
this.resolve = bind(internalResolve, state);
278+
this.reject = bind(internalReject, state);
277279
};
278280
newPromiseCapabilityModule.f = newPromiseCapability = function (C) {
279281
return C === PromiseConstructor || C === PromiseWrapper

0 commit comments

Comments
 (0)