Git Product home page Git Product logo

observer-pattern-js's Introduction

Observer pattern implementation in js.

Installation.

npm i observer-pattern-js

Usage example.

import {IEventEmitter, EventEmitter, IEmitterSubscriber} from "observer-pattern-js";

const emitter: IEventEmitter = new EventEmitter();

const subscriber: IEmitterSubscriber = emitter.subscribe(
  'event-name',
  (name, surname, age): void => {
    console.log(name); // Ikrom.
    console.log(surname); // Murodov.
    console.log(age); // 18.
  },
);

emitter.emit('event-name', 'Ikrom', 'Murodov', 18);

// If you want to unsubscribe from an event you can call the unsubscribe method.
subscriber.unsubscribe();

EventEmitter (API).

subscribe - Adds the listener function to the end of the listeners array for the event named eventName.

  • Adds the listener function to the end of the listeners array for the event named eventName.
  • @param { string } eventName - name of events to subscribe.
  • @param { IEmitterCallBack } cb - listener function.
  • @public - This method is available to all instances of the EventEmitter class.
  • @return { IEmitterSubscriber } - Will return the subscriber object.
  • method with which you can unsubscribe from an event.
import {IEventEmitter, EventEmitter, IEmitterSubscriber} from "observer-pattern-js";

const emitter: IEventEmitter = new EventEmitter();

const subscriber: IEmitterSubscriber = emitter.subscribe('event-name', (...args) => {});

emit - Synchronously calls each of the listeners registered for the event named eventName.

  • @param { string } eventName.
  • @param { ...* } args - You can pass as many arguments as you like.
  • @public - This method is available to all instances of the EventEmitter class.
  • @throws Throws an error if no listener has been registered for an event named eventName.
  • @return { void } - This method returns nothing.
import {IEventEmitter, EventEmitter} from "observer-pattern-js";

const emitter: IEventEmitter = new EventEmitter();

emitter.emit('eventName', '...args');

getListeners - This method returns a list of listeners.

  • @return { IListeners } - list of listeners
  • @public - This method is available to all instances of the EventEmitter class.
import {IEventEmitter, EventEmitter, IListeners} from "observer-pattern-js";

const emitter: IEventEmitter = new EventEmitter();

const listeners: IListeners = emitter.getListeners();

console.log(listeners);

observer-pattern-js's People

Contributors

ikrom-murodov avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.