observer-pattern-js
    TypeScript icon, indicating that this package has built-in type declarations

    1.1.0 • Public • Published

    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);

    Install

    npm i observer-pattern-js

    DownloadsWeekly Downloads

    1

    Version

    1.1.0

    License

    ISC

    Unpacked Size

    8.05 kB

    Total Files

    4

    Last publish

    Collaborators

    • avatar