-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
318 additions
and
260 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
187 changes: 187 additions & 0 deletions
187
packages/core-js/modules/esnext.observable.constructor.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
} | ||
}); |
Oops, something went wrong.