The Observable constructor is the %Observable% intrinsic object and the initial value of the Observable property of the global object. When called as a constructor it creates and initializes a new Observable object. Observable is not intended to be called as a function and will throw an exception when called in that manner.
The Observable constructor is designed to be subclassable. It may be used as the value in an extends clause of a class definition. Subclass constructors that intend to inherit the specified Observable behaviour must include a super call to the Observable constructor to create and initialize the subclass instance with the internal state necessary to support the Observable and Observable.prototype built-in methods.
1.1Observable ( subscriber )
The Observable constructor initializes a new Observable object. It is not intended to be called as a function and will throw an exception when called in that manner.
The subscriber argument must be a function object. It is called each time the subscribe method of the Observable object is invoked. The subscriber function is called with a wrapped observer object and may optionally return a function which will cancel the subscription.
The Observable constructor performs the following steps:
If NewTarget is undefined, throw a TypeError exception.
If IsCallable(subscriber) is false, throw a TypeError exception.
The length property of an Observable.of subscriber function is 1.
3Properties of the Observable Prototype Object
The Observable prototype object is the intrinsic object %ObservablePrototype%. The value of the [[Prototype]] internal slot of the Observable prototype object is the intrinsic object %ObjectPrototype%. The Observable prototype object is an ordinary object.
3.1Observable.prototype.subscribe ( observer )
The subscribe function begins sending values to the supplied observer object by executing the Observable object's subscriber function. It returns a Subscription object which may be used to cancel the subscription.
The subscribe function performs the following steps:
Let O be the this value.
If Type(O) is not Object, throw a TypeError exception.
If O does not have an [[Subscriber]] internal slot, throw a TypeError exception.
The abstract operation SubscriptionClosed with argument subscription performs the following steps:
Assert: subscription is a Subscription object.
If the value of subscription's [[Observer]] internal slot is undefined, return true.
Else, return false.
4.2The %SubscriptionPrototype% Object
All Subscription objects inherit properties from the %SubscriptionPrototype% intrinsic object. The %SubscriptionPrototype% object is an ordinary object and its [[Prototype]] internal slot is the %ObjectPrototype% intrinsic object. In addition, %SubscriptionPrototype% has the following properties:
4.2.1get %SubscriptionPrototype%.closed
Let subscription be the this value.
If Type(subscription) is not Object, throw a TypeError exception.
If subscription does not have all of the internal slots of a Subscription instance, throw a TypeError exception.
A Subscription Observer is an object which wraps the observer argument supplied to the subscribe method of Observable objects. Subscription Observer objects are passed as the single parameter to an observable's subscriber function. They enforce the following guarantees:
If the observer's error method is called, the observer will not be invoked again and the observable's cleanup function will be called.
If the observer's complete method is called, the observer will not be invoked again and the observable's cleanup function will be called.
When the subscription is canceled, the observer will not be invoked again.
In addition, Subscription Observer objects provide default behaviors when the observer does not implement next, error or complete.
5.1Subscription Observer Abstract Operations
5.1.1CreateSubscriptionObserver ( subscription )
The abstract operation CreateSubscriptionObserver with argument observer is used to create a normalized observer which can be supplied to an observable's subscriber function. It performs the following steps:
Let subscriptionObserver be ObjectCreate(%SubscriptionObserverPrototype%, « [[Subscription]] »).
Set subscriptionObserver's [[Subscription]] internal slot to subscription.
Return subscriptionObserver.
5.2The %SubscriptionObserverPrototype% Object
All Subscription Observer objects inherit properties from the %SubscriptionObserverPrototype% intrinsic object. The %SubscriptionObserverPrototype% object is an ordinary object and its [[Prototype]] internal slot is the %ObjectPrototype% intrinsic object. In addition, %SubscriptionObserverPrototype% has the following properties:
5.2.1get %SubscriptionObserverPrototype%.closed
Let O be the this value.
If Type(O) is not Object, throw a TypeError exception.
If O does not have all of the internal slots of a Subscription Observer instance, throw a TypeError exception.
Let subscription be the value of O's [[Subscription]] internal.