Skip to content

Commit

Permalink
fix requirements of internal slots in Observable / Subscription /…
Browse files Browse the repository at this point in the history
… `SubscriptionObserver`

close #1017
  • Loading branch information
zloirock committed Dec 3, 2021
1 parent 66e8263 commit bfa8fe1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
@@ -1,5 +1,6 @@
## Changelog
##### Unreleased
- Fixed requirements of internal slots in `Observable` / `Subscription` / `SubscriptionObserver`, [#1017](https://github.com/zloirock/core-js/issues/1017)
- Added NodeJS 17.2 compat data mapping

##### 3.19.2 - 2021.11.29
Expand Down
41 changes: 27 additions & 14 deletions packages/core-js/modules/esnext.observable.js
Expand Up @@ -21,9 +21,15 @@ var hostReportErrors = require('../internals/host-report-errors');
var wellKnownSymbol = require('../internals/well-known-symbol');
var InternalStateModule = require('../internals/internal-state');

var OBSERVABLE = wellKnownSymbol('observable');
var getInternalState = InternalStateModule.get;
var $$OBSERVABLE = wellKnownSymbol('observable');
var OBSERVABLE = 'Observable';
var SUBSCRIPTION = 'Subscription';
var SUBSCRIPTION_OBSERVER = 'SubscriptionObserver';
var getterFor = InternalStateModule.getterFor;
var setInternalState = InternalStateModule.set;
var getObservableInternalState = getterFor(OBSERVABLE);
var getSubscriptionInternalState = getterFor(SUBSCRIPTION);
var getSubscriptionObserverInternalState = getterFor(SUBSCRIPTION_OBSERVER);
var Array = global.Array;

var cleanupSubscription = function (subscriptionState) {
Expand Down Expand Up @@ -53,6 +59,7 @@ var close = function (subscriptionState) {

var Subscription = function (observer, subscriber) {
var subscriptionState = setInternalState(this, {
type: SUBSCRIPTION,
cleanup: undefined,
observer: anObject(observer),
subscriptionObserver: undefined
Expand Down Expand Up @@ -80,7 +87,7 @@ var Subscription = function (observer, subscriber) {

Subscription.prototype = redefineAll({}, {
unsubscribe: function unsubscribe() {
var subscriptionState = getInternalState(this);
var subscriptionState = getSubscriptionInternalState(this);
if (!subscriptionClosed(subscriptionState)) {
close(subscriptionState);
cleanupSubscription(subscriptionState);
Expand All @@ -91,18 +98,21 @@ Subscription.prototype = redefineAll({}, {
if (DESCRIPTORS) defineProperty(Subscription.prototype, 'closed', {
configurable: true,
get: function () {
return subscriptionClosed(getInternalState(this));
return subscriptionClosed(getSubscriptionInternalState(this));
}
});

var SubscriptionObserver = function (subscription) {
setInternalState(this, { subscription: subscription });
setInternalState(this, {
type: SUBSCRIPTION_OBSERVER,
subscription: subscription
});
if (!DESCRIPTORS) this.closed = false;
};

SubscriptionObserver.prototype = redefineAll({}, {
next: function next(value) {
var subscriptionState = getInternalState(getInternalState(this).subscription);
var subscriptionState = getSubscriptionInternalState(getSubscriptionObserverInternalState(this).subscription);
if (!subscriptionClosed(subscriptionState)) {
var observer = subscriptionState.observer;
try {
Expand All @@ -114,7 +124,7 @@ SubscriptionObserver.prototype = redefineAll({}, {
}
},
error: function error(value) {
var subscriptionState = getInternalState(getInternalState(this).subscription);
var subscriptionState = getSubscriptionInternalState(getSubscriptionObserverInternalState(this).subscription);
if (!subscriptionClosed(subscriptionState)) {
var observer = subscriptionState.observer;
close(subscriptionState);
Expand All @@ -128,7 +138,7 @@ SubscriptionObserver.prototype = redefineAll({}, {
}
},
complete: function complete() {
var subscriptionState = getInternalState(getInternalState(this).subscription);
var subscriptionState = getSubscriptionInternalState(getSubscriptionObserverInternalState(this).subscription);
if (!subscriptionClosed(subscriptionState)) {
var observer = subscriptionState.observer;
close(subscriptionState);
Expand All @@ -145,13 +155,16 @@ SubscriptionObserver.prototype = redefineAll({}, {
if (DESCRIPTORS) defineProperty(SubscriptionObserver.prototype, 'closed', {
configurable: true,
get: function () {
return subscriptionClosed(getInternalState(getInternalState(this).subscription));
return subscriptionClosed(getSubscriptionInternalState(getSubscriptionObserverInternalState(this).subscription));
}
});

var $Observable = function Observable(subscriber) {
anInstance(this, ObservablePrototype);
setInternalState(this, { subscriber: aCallable(subscriber) });
setInternalState(this, {
type: OBSERVABLE,
subscriber: aCallable(subscriber)
});
};

var ObservablePrototype = $Observable.prototype;
Expand All @@ -163,14 +176,14 @@ redefineAll(ObservablePrototype, {
next: observer,
error: length > 1 ? arguments[1] : undefined,
complete: length > 2 ? arguments[2] : undefined
} : isObject(observer) ? observer : {}, getInternalState(this).subscriber);
} : isObject(observer) ? observer : {}, getObservableInternalState(this).subscriber);
}
});

redefineAll($Observable, {
from: function from(x) {
var C = isConstructor(this) ? this : $Observable;
var observableMethod = getMethod(anObject(x), OBSERVABLE);
var observableMethod = getMethod(anObject(x), $$OBSERVABLE);
if (observableMethod) {
var observable = anObject(call(observableMethod, x));
return observable.constructor === C ? observable : new C(function (observer) {
Expand Down Expand Up @@ -201,10 +214,10 @@ redefineAll($Observable, {
}
});

redefine(ObservablePrototype, OBSERVABLE, function () { return this; });
redefine(ObservablePrototype, $$OBSERVABLE, function () { return this; });

$({ global: true }, {
Observable: $Observable
});

setSpecies('Observable');
setSpecies(OBSERVABLE);

0 comments on commit bfa8fe1

Please sign in to comment.