1.1.1 • Published 8 years ago

observer-subject v1.1.1

Weekly downloads
5
License
ISC
Repository
github
Last release
8 years ago

Observer Design Pattern Subject

An implementation of the subject in an Observer Design Pattern.

Example

let Observable = require('observer-subject'),
  subject = new Observable(),
  observer1 = {
    notify(...args) {

      console.log(`#1`, ...args);

    }

  },
  observer2 = {

    notify(...args) {

      console.log(`#2`, ...args);

    }

  };

subject.registerObserver(observer1);
subject.registerObserver(observer2);
subject.notifyObservers(42);
// #1 42
// #2 42

subject.unregisterObserver(observer1);
subject.notifyObservers(32);
// #2 32

Installation

$ npm install observer-subject

API

let Observer = require('observer-subject');

.registerObserver(observer)

Will start notifying the observer.

typedata typenameDescription
parameterobjectobserverThe object to be notified.
throwsType Exceptionn/aWill throw an error if observer does not have a function named notify.

.unregisterObserver(observer)

Will no longer notify the specified observer.

typedata typenameDescription
parameterobjectobserverThe object to be notified.
returnsbooleann/atrue if the observer was found and removed, otherwise false

.notifyObservers(..args)

Calls each observers notify function with the arguments provided.

typedata typenameDescription
returnsbooleann/atrue if game has ended, otherwise false.

observer.notify(...args)

Callback that the observable object uses to notify the observer.

typedata typenameDescription
parameter...*argsArguments from the observable object.