Skip to content

Observable

Classes

Subscriber
Observable

Typedefs

Observer : Object

The observer object or function.

Subscriber

Kind: global class

new Subscriber(observer)

Class representing a Subscriber.

Param Type Description
observer Observer | function The observer object or function.

subscriber.next(result)

Notifies the observer of a new value.

Kind: instance method of Subscriber

Param Type Description
result any The result to pass to the observer's next method.

Example

subscriber.next('Hello, world!');

subscriber.complete()

Notifies the observer that the observable has completed and no more data will be emitted.

Kind: instance method of Subscriber
Example

subscriber.complete();

subscriber.error(error)

Notifies the observer that an error has occurred.

Kind: instance method of Subscriber

Param Type Description
error Error The error to pass to the observer's error method.

Example

subscriber.error(new Error('Something went wrong'));

subscriber.addTeardown(teardown)

Adds a teardown function to the teardowns array.

Kind: instance method of Subscriber

Param Type Description
teardown function The teardown function to add to the teardowns array.

subscriber.unsubscribe()

Unsubscribes from the observable, preventing any further notifications to the observer and triggering any teardown logic.

Kind: instance method of Subscriber
Example

subscriber.unsubscribe();

Observable

Kind: global class

new Observable(subscribeCallback)

Class representing an Observable.

Param Type Description
subscribeCallback function The callback function to call when a new observer subscribes.

observable.subscribe(observerOrNext, error, complete) ⇒ Object

Subscribes an observer to the observable.

Kind: instance method of Observable
Returns: Object - An object containing an unsubscribe method to stop receiving updates.

Param Type Description
observerOrNext Observer | function The observer to subscribe or the next function. Default is an empty function.
error function The error function. Default is an empty function.
complete function The complete function. Default is an empty function.

Example

const observable = new Observable();
const subscription = observable.subscribe({
  next: value => console.log(value),
  error: err => console.error(err),
  complete: () => console.log('Completed'),
});

observable.next(value)

Passes a value to the observer's next method.

Kind: instance method of Observable

Param Type Description
value * The value to be passed to the observer's next method.

Example

const observable = new Observable();
observable.next('Hello, world!');

observable.error(error)

Passes an error to the observer's error method.

Kind: instance method of Observable

Param Type Description
error * The error to be passed to the observer's error method.

Example

const observable = new Observable();
observable.error(new Error('Something went wrong'));

observable.complete()

Calls the complete method on all observers.

Kind: instance method of Observable
Example

const observable = new Observable();
observable.complete();

observable.onValue(callbackFn) ⇒ Object

Subscribes an observer with a next function to the observable.

Kind: instance method of Observable
Returns: Object - An object containing an unsubscribe method to stop receiving updates.

Param Type Description
callbackFn function The callback function to call when a new value is emitted.

Example

const observable = new Observable();
const subscription = observable.onValue(value => console.log(value));

observable.onError(callbackFn) ⇒ Object

Subscribes an observer with an error function to the observable.

Kind: instance method of Observable
Returns: Object - An object containing an unsubscribe method to stop receiving updates.

Param Type Description
callbackFn function The callback function to call when an error is emitted.

Example

const observable = new Observable();
const subscription = observable.onError(err => console.error(err));

observable.onEnd(callbackFn) ⇒ Object

Subscribes an observer with a complete function to the observable.

Kind: instance method of Observable
Returns: Object - An object containing an unsubscribe method to stop receiving updates.

Param Type Description
callbackFn function The callback function to call when the observable completes.

Example

const observable = new Observable();
const subscription = observable.onEnd(() => console.log('Completed'));

Observer : Object

The observer object or function.

Kind: global typedef
Properties

Name Type Description
next function Function to handle new values.
error function Function to handle errors.
complete function Function to handle completion.