Skip to content

Commit

Permalink
split esnext.observable
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Feb 23, 2022
1 parent e2e8a5f commit 5fd933a
Show file tree
Hide file tree
Showing 16 changed files with 318 additions and 260 deletions.
12 changes: 11 additions & 1 deletion packages/core-js-compat/src/data.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,7 @@ export const data = {
safari: '7.1',
rhino: '1.7.13',
},
// TODO: remove this module from `core-js@4` since it's split to below modules
// TODO: Remove this module from `core-js@4` since it's split to modules listed below
'es.promise': {
// V8 6.6 has a serious bug
chrome: '67', // '51',
Expand Down Expand Up @@ -1704,8 +1704,15 @@ export const data = {
// TODO: Remove from `core-js@4`
'esnext.object.iterate-values': {
},
// TODO: Remove this module from `core-js@4` since it's split to modules listed below
'esnext.observable': {
},
'esnext.observable.constructor': {
},
'esnext.observable.from': {
},
'esnext.observable.of': {
},
// TODO: Remove from `core-js@4`
'esnext.promise.all-settled': null,
// TODO: Remove from `core-js@4`
Expand Down Expand Up @@ -1978,5 +1985,8 @@ delete data['es.promise.catch'];
delete data['es.promise.race'];
delete data['es.promise.reject'];
delete data['es.promise.resolve'];
delete data['esnext.observable.constructor'];
delete data['esnext.observable.from'];
delete data['esnext.observable.of'];

export const modules = Object.keys(data);
13 changes: 13 additions & 0 deletions packages/core-js/internals/observable-forced.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var global = require('../internals/global');
var isCallable = require('../internals/is-callable');
var wellKnownSymbol = require('../internals/well-known-symbol');

var $$OBSERVABLE = wellKnownSymbol('observable');
var NativeObservable = global.Observable;
var NativeObservablePrototype = NativeObservable && NativeObservable.prototype;

module.exports = !isCallable(NativeObservable)
|| !isCallable(NativeObservable.from)
|| !isCallable(NativeObservable.of)
|| !isCallable(NativeObservablePrototype.subscribe)
|| !isCallable(NativeObservablePrototype[$$OBSERVABLE]);
2 changes: 1 addition & 1 deletion packages/core-js/modules/es.promise.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// TODO: remove this module from `core-js@4` since it's split to below modules
// TODO: Remove this module from `core-js@4` since it's split to modules listed below
require('../modules/es.promise.constructor');
require('../modules/es.promise.all');
require('../modules/es.promise.catch');
Expand Down
187 changes: 187 additions & 0 deletions packages/core-js/modules/esnext.observable.constructor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
'use strict';
// https://github.com/tc39/proposal-observable
var $ = require('../internals/export');
var call = require('../internals/function-call');
var DESCRIPTORS = require('../internals/descriptors');
var setSpecies = require('../internals/set-species');
var aCallable = require('../internals/a-callable');
var isCallable = require('../internals/is-callable');
var anObject = require('../internals/an-object');
var isObject = require('../internals/is-object');
var anInstance = require('../internals/an-instance');
var getMethod = require('../internals/get-method');
var defineProperty = require('../internals/object-define-property').f;
var redefine = require('../internals/redefine');
var redefineAll = require('../internals/redefine-all');
var hostReportErrors = require('../internals/host-report-errors');
var wellKnownSymbol = require('../internals/well-known-symbol');
var InternalStateModule = require('../internals/internal-state');
var OBSERVABLE_FORCED = require('../internals/observable-forced');

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 SubscriptionState = function (observer) {
this.observer = anObject(observer);
this.cleanup = undefined;
this.subscriptionObserver = undefined;
};

SubscriptionState.prototype = {
type: SUBSCRIPTION,
clean: function () {
var cleanup = this.cleanup;
if (cleanup) {
this.cleanup = undefined;
try {
cleanup();
} catch (error) {
hostReportErrors(error);
}
}
},
close: function () {
if (!DESCRIPTORS) {
var subscription = this.facade;
var subscriptionObserver = this.subscriptionObserver;
subscription.closed = true;
if (subscriptionObserver) subscriptionObserver.closed = true;
} this.observer = undefined;
},
isClosed: function () {
return this.observer === undefined;
}
};

var Subscription = function (observer, subscriber) {
var subscriptionState = setInternalState(this, new SubscriptionState(observer));
var start;
if (!DESCRIPTORS) this.closed = false;
try {
if (start = getMethod(observer, 'start')) call(start, observer, this);
} catch (error) {
hostReportErrors(error);
}
if (subscriptionState.isClosed()) return;
var subscriptionObserver = subscriptionState.subscriptionObserver = new SubscriptionObserver(subscriptionState);
try {
var cleanup = subscriber(subscriptionObserver);
var subscription = cleanup;
if (cleanup != null) subscriptionState.cleanup = isCallable(cleanup.unsubscribe)
? function () { subscription.unsubscribe(); }
: aCallable(cleanup);
} catch (error) {
subscriptionObserver.error(error);
return;
} if (subscriptionState.isClosed()) subscriptionState.clean();
};

Subscription.prototype = redefineAll({}, {
unsubscribe: function unsubscribe() {
var subscriptionState = getSubscriptionInternalState(this);
if (!subscriptionState.isClosed()) {
subscriptionState.close();
subscriptionState.clean();
}
}
});

if (DESCRIPTORS) defineProperty(Subscription.prototype, 'closed', {
configurable: true,
get: function () {
return getSubscriptionInternalState(this).isClosed();
}
});

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

SubscriptionObserver.prototype = redefineAll({}, {
next: function next(value) {
var subscriptionState = getSubscriptionObserverInternalState(this).subscriptionState;
if (!subscriptionState.isClosed()) {
var observer = subscriptionState.observer;
try {
var nextMethod = getMethod(observer, 'next');
if (nextMethod) call(nextMethod, observer, value);
} catch (error) {
hostReportErrors(error);
}
}
},
error: function error(value) {
var subscriptionState = getSubscriptionObserverInternalState(this).subscriptionState;
if (!subscriptionState.isClosed()) {
var observer = subscriptionState.observer;
subscriptionState.close();
try {
var errorMethod = getMethod(observer, 'error');
if (errorMethod) call(errorMethod, observer, value);
else hostReportErrors(value);
} catch (err) {
hostReportErrors(err);
} subscriptionState.clean();
}
},
complete: function complete() {
var subscriptionState = getSubscriptionObserverInternalState(this).subscriptionState;
if (!subscriptionState.isClosed()) {
var observer = subscriptionState.observer;
subscriptionState.close();
try {
var completeMethod = getMethod(observer, 'complete');
if (completeMethod) call(completeMethod, observer);
} catch (error) {
hostReportErrors(error);
} subscriptionState.clean();
}
}
});

if (DESCRIPTORS) defineProperty(SubscriptionObserver.prototype, 'closed', {
configurable: true,
get: function () {
return getSubscriptionObserverInternalState(this).subscriptionState.isClosed();
}
});

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

var ObservablePrototype = $Observable.prototype;

redefineAll(ObservablePrototype, {
subscribe: function subscribe(observer) {
var length = arguments.length;
return new Subscription(isCallable(observer) ? {
next: observer,
error: length > 1 ? arguments[1] : undefined,
complete: length > 2 ? arguments[2] : undefined
} : isObject(observer) ? observer : {}, getObservableInternalState(this).subscriber);
}
});

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

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

setSpecies(OBSERVABLE);
36 changes: 36 additions & 0 deletions packages/core-js/modules/esnext.observable.from.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';
var $ = require('../internals/export');
var getBuiltIn = require('../internals/get-built-in');
var call = require('../internals/function-call');
var anObject = require('../internals/an-object');
var isConstructor = require('../internals/is-constructor');
var getIterator = require('../internals/get-iterator');
var getMethod = require('../internals/get-method');
var iterate = require('../internals/iterate');
var wellKnownSymbol = require('../internals/well-known-symbol');
var OBSERVABLE_FORCED = require('../internals/observable-forced');

var $$OBSERVABLE = wellKnownSymbol('observable');

// `Observable.from` method
// https://github.com/tc39/proposal-observable
$({ target: 'Observable', stat: true, forced: OBSERVABLE_FORCED }, {
from: function from(x) {
var C = isConstructor(this) ? this : getBuiltIn('Observable');
var observableMethod = getMethod(anObject(x), $$OBSERVABLE);
if (observableMethod) {
var observable = anObject(call(observableMethod, x));
return observable.constructor === C ? observable : new C(function (observer) {
return observable.subscribe(observer);
});
}
var iterator = getIterator(x);
return new C(function (observer) {
iterate(iterator, function (it, stop) {
observer.next(it);
if (observer.closed) return stop();
}, { IS_ITERATOR: true, INTERRUPTED: true });
observer.complete();
});
}
});
Loading

0 comments on commit 5fd933a

Please sign in to comment.