From bfa8fe111be1329e401fbee22e8ff77bbcc32c96 Mon Sep 17 00:00:00 2001
From: Denis Pushkarev <zloirock@zloirock.ru>
Date: Fri, 3 Dec 2021 12:48:12 +0700
Subject: [PATCH] fix requirements of internal slots in `Observable` /
 `Subscription` / `SubscriptionObserver`

close #1017
---
 CHANGELOG.md                                  |  1 +
 packages/core-js/modules/esnext.observable.js | 41 ++++++++++++-------
 2 files changed, 28 insertions(+), 14 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9306f4d202a9..54914edc1690 100644
--- a/CHANGELOG.md
+++ b/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
diff --git a/packages/core-js/modules/esnext.observable.js b/packages/core-js/modules/esnext.observable.js
index f3bff30fa17c..e6098a81bc85 100644
--- a/packages/core-js/modules/esnext.observable.js
+++ b/packages/core-js/modules/esnext.observable.js
@@ -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) {
@@ -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
@@ -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);
@@ -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 {
@@ -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);
@@ -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);
@@ -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;
@@ -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) {
@@ -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);