The observer pattern
is a software design pattern in which an object, called the subject, maintains
a list of its dependents, called observers, and notifies them automatically of
state changes. Observables get their name from the
Observer design pattern. The Observable
sends notifications while the Observer
receives them.
Observables are declarative—that is, you define a function for publishing values, but it is not executed until a consumer subscribes to it. The subscribed consumer then receives notifications until the function completes, or until they unsubscribe.
As a publisher, you create an Observable instance that defines a subscriber function. This is the function that is executed when a consumer calls the subscribe() method. The subscriber function defines how to obtain or generate values or messages to be published.
Observers are handlers that receive notifications from observables. It is an object that defines callback methods to handle the three types of notifications that an observable can send:
Observables are declarative—that is, you define a function for publishing values, but it is not executed until a consumer subscribes to it. The subscribed consumer then receives notifications until the function completes, or until they unsubscribe.
As a publisher, you create an Observable instance that defines a subscriber function. This is the function that is executed when a consumer calls the subscribe() method. The subscriber function defines how to obtain or generate values or messages to be published.
Observers are handlers that receive notifications from observables. It is an object that defines callback methods to handle the three types of notifications that an observable can send:
next - A handler
for each delivered value. Called zero or more times after execution starts.
error - A handler for an error notification. An error halts execution of the observable instance.
complete - A handler for the execution-complete notification.
const myObserver = {
next: x => console.log('Observer got a next value: ' + x),
error: err => console.error('Observer got an error: ' + err),
complete: () => console.log('Observer got a complete notification'),
};
error - A handler for an error notification. An error halts execution of the observable instance.
complete - A handler for the execution-complete notification.
const myObserver = {
next: x => console.log('Observer got a next value: ' + x),
error: err => console.error('Observer got an error: ' + err),
complete: () => console.log('Observer got a complete notification'),
};
No comments:
Post a Comment