observable-spec icon indicating copy to clipboard operation
observable-spec copied to clipboard

Specification for Observable

Observable

An open standard for sound, interoperable JavaScript Observables.

An Observable is an object that can send multiple values to a consumer. An Observable has a subscribe method which accepts a generator and can send it any number of values.

This specification details the behavior of the subscribe method, providing an interoperable base which all conformant Observable implementations must provide. As such, the specification should be considered very stable. Although the Observable organization may occasionally revise this specification with minor backward-compatible changes to address newly-discovered corner cases, we will integrate large or backward-incompatible changes only after careful consideration, discussion, and testing.

The core Observable specification does not deal with how to create Observables or Generators, choosing instead to focus on providing an interoperable subscribe method. Future work in companion specifications may touch on these subjects.

Terminology

  • observable is an object or function with a subscribe method whose behavior conforms to this specification.

Requirements

The subscribe Method

An observable must provide a subscribe method to allow consumers to receive its values. An observable's subscribe method accepts one argument and returns one value:

let subscription = observable.subscribe(generator);
  1. generator is a required argument.
    1. It must be an object.
  2. If generator is an object with a next method:
    1. It must never be called after the unsubscribe method of subscription has been called.
    2. It may optionally be passed a value.
    3. It may optionally return a value when called.
    4. If the method returns an object with a "done" property whose value is true, the generator should receive no further values.
  3. If generator is an object with a throw method:
    1. It must never be called after the unsubscribe method of subscription has been called.
    2. It must be called if an error is thrown anywhere in code either executed synchronously or asynchronously by observable. The error must be passed as the argument to the method.
    3. It may optionally return a value when called.
    4. If the method returns an object with a "done" property whose value is true, the generator should receive no further values.
  4. If generator is an object with a return method:
    1. It must never be called after the unsubscribe method of subscription has been called.
    2. It should be called with a value designated as the return value.
    3. It must be called to indicate that the observable will send no further values.
    4. It should optionally return a value when called.
    5. If the method returns an object with a "done" property whose value is true, the generator should receive no further values.
  5. subscribe must return a subscription object.
    1. It must be an object with an unsubscribe method.
    2. A call to the unsubscribe method should free all references to the generator held by the observable.
    3. The unsubscribe method may be called multiple times. However, any subsequent calls after the first one should not have any side effects.

CC0
To the extent possible under law, the Observable organization has waived all copyright and related or neighboring rights to Observable Specification. This work is published from: United States.