Skip to content

Latest commit

 

History

History
326 lines (230 loc) · 8.05 KB

notification.md

File metadata and controls

326 lines (230 loc) · 8.05 KB

Notification object

Represents a notification to an observer.

Notification Methods

Notification Instance Methods

Notification Properties

Notification Methods

Rx.Notification.createOnCompleted()

#

Creates an object that represents an OnCompleted notification to an observer.

Returns

(Notification): The OnCompleted notification.

Example

var source = Rx.Observable
    .from([
        Rx.Notification.createOnCompleted()
    ])
    .dematerialize();

var subscription = source.subscribe(
    function (x) {
        console.log('Next: ' + x.toString());
    },
    function (err) {
        console.log('Error: ' + err);
    },
    function () {
        console.log('Completed');
    });

// => Completed

Location

  • rx.js

Rx.Notification.createOnError(exception)

#

Creates an object that represents an OnError notification to an observer.

Arguments

  1. exception (Any): The exception contained in the notification.

Returns

(Notification): The OnError notification containing the exception.

Example

var source = Rx.Observable
    .from([
        Rx.Notification.createOnError(new Error('woops'))
    ])
    .dematerialize();

var subscription = source.subscribe(
    function (x) {
        console.log('Next: ' + x.toString());
    },
    function (err) {
        console.log('Error: ' + err);
    },
    function () {
        console.log('Completed');
    });

// => Error: Error: woops

Location

  • rx.js

Rx.Notification.createOnNext(value)

#

Creates an object that represents an OnNext notification to an observer.

Arguments

  1. value (Any): The value contained in the notification.

Returns

(Notification): The OnNext notification containing the value.

Example

var source = Rx.Observable
    .from([
        Rx.Notification.createOnNext(42),
        Rx.Notification.createOnCompleted()
    ])
    .dematerialize();

var subscription = source.subscribe(
    function (x) {
        console.log('Next: ' + x.toString());
    },
    function (err) {
        console.log('Error: ' + err);
    },
    function () {
        console.log('Completed');
    });

// => Next: 42
// => Completed

Location

  • rx.js

Notification Instance Methods

Rx.Notification.prototype.accept([observer] | [onNext], [onError], [onCompleted])

#

Invokes the delegate corresponding to the notification or the observer's method corresponding to the notification and returns the produced result.

Arguments

  1. [observer] (Observer): Observer to invoke the notification on.
  2. [onNext] (Function): Function to invoke for an OnNext notification.
  3. [onError] (Function): Function to invoke for an OnError notification.
  4. [OnCompleted] (Function): Function to invoke for an OnCompleted notification.

Returns

(Any): Result produced by the observation.

Example

/* Using an observer */
var observer = Rx.Observer.create(function (x) { return x; });

var notification = Rx.Notification.createOnNext(42);

console.log(notification.accept(observer));

// => 42

/* Using a function */
var notification = Rx.Notification.createOnNext(42);

console.log(notification.accept(function (x) { return x; }))
// => 42

Location

  • rx.js

Rx.Notification.prototype.toObservable([scheduler])

#

Returns an observable sequence with a single notification.

Arguments

  1. [scheduler = Rx.Scheduler.immediate] (Scheduler): Scheduler to send out the notification calls on.

Returns

(Observable): The observable sequence that surfaces the behavior of the notification upon subscription.

Example

/* Without a scheduler */
var source = Rx.Notification.createOnNext(42)
    .toObservable();

var subscription = source.subscribe(
    function (x) {
        console.log('Next: ' + x.toString());
    },
    function (err) {
        console.log('Error: ' + err);
    },
    function () {
        console.log('Completed');
    });

// => Next: 42
// => Completed

/* With a scheduler */
var source = Rx.Notification.createOnError(new Error('error!'))
    .toObservable(Rx.Scheduler.timeout);

var subscription = source.subscribe(
    function (x) {
        console.log('Next: ' + x.toString());
    },
    function (err) {
        console.log('Error: ' + err);
    },
    function () {
        console.log('Completed');
    });

// => Error: Error: error!

Location

  • rx.js

Notification Properties

exception

#

Gets the exception from the OnError notification.

Returns

(Any): The Exception from the OnError notification.

Example

var notification = Rx.Notification.createOnError(new Error('invalid'));
console.log(notification.exception);

// => Error: invalid

Location

  • rx.js

hasValue

#

Determines whether the Notification has a value. Returns true for OnNext Notifications, and false for OnError and OnCompleted Notifications.

Returns

(Bool): Returns true for OnNext Notifications, and false for OnError and OnCompleted Notifications.

Example

var onNext = Rx.Notification.createOnNext(42);
console.log(onNext.hasValue);

// => true

var onCompleted = Rx.Notification.createOnCompleted();
console.log(onCompleted.hasValue);

// => false

Location

  • rx.js

kind

#

Gets the kind from the notification which denotes 'N' for OnNext, 'E' for OnError and 'C' for OnCompleted.

Returns

(String): The kind from the notification which denotes 'N' for OnNext, 'E' for OnError and 'C' for OnCompleted.

Example

var notification = Rx.Notification.createOnCompleted();
console.log(notification.kind);

// => C

Location

  • rx.js

value

#

Gets the value from the OnNext notification.

Returns

(Any): The value from the OnNext notification.

Example

var notification = Rx.Notification.createOnNext(42);
console.log(notification.value);

// => 42

Location

  • rx.js