1.3.0 • Published 1 month ago

@mdaemon/emitter v1.3.0

Weekly downloads
-
License
LGPL-2.1
Repository
github
Last release
1 month ago

Dynamic JSON Badge Static Badge install size Dynamic JSON Badge Node.js CI

@mdaemon/emitter, A Dependency Free event emitter library

[ @mdaemon/emitter on npm ]

The "emitter" provides pub/sub options

Install

  $ npm install @mdaemon/emitter --save  

Node CommonJS

    const Emitter = require("@mdaemon/emitter/dist/emitter.cjs");

Node Modules

    import Emitter from "@mdaemon/emitter/dist/emitter.mjs";  

Web

    <script type="text/javascript" src="/path_to_modules/dist/emitter.umd.js">

Emitter

    /* this is more typically used as a prototype for a class or object
     * class Messages extends Emitter {}
     *
     * or
     * 
     * function Messages() { 
     *   Object.assign(this, new Emitter());
     * }
     */

    // maxListeners and maxOnceListeners default to 50 and are immutable once set
    const emitter = new Emitter({
        maxListeners: 20,
        maxOnceListeners: 40
    });

    emitter.on("test", "namespace", (input) => {
        console.log(input); 
    });

    emitter.emit("test", "this is a test");
    // this is a test

    emitter.off("test", "namespace");

    emitter.emit("test", "this will go nowhere");

    // if you pass a function as the second parameter, the function will be registered as part of an "all" namespace
    emitter.on("test", (input) => {
        console.log(input, "this gets called"); 
    });

    emitter.emit("test", "calling all");
    // calling all this gets called

    // your flavor of pub/sub may vary
    // emitter.register === emitter.on === emitter.subscribe 
    // emitter.unregister === emitter.off === emitter.unsubscribe
    // emitter.trigger === emitter.emit === emitter.publish

    emitter.once("only-receive-this-once", (input) => {
        console.log(input); 
    });

    emitter.trigger("only-receive-this-once", "this was received once, and then removed from memory");
    // this was received once, and then removed from memory

    // onMany allows you to register/subscribe multiple items at once
    emitter.onMany("namespace", {
        "test": (input) => { console.log("test", input); },
        "test2": (input) => { console.log("test2", input); }
    });

    emitter.emit("test", "my test");
    // test my test

    emitter.emit("test2", "another test");
    // test2 another test

    // isRegistered checks if an event+namespace combination is registered
    emitter.isRegistered("test2", "namespace");
    // true

    // propagate is a reverse parameter of trigger/emit/publish
    emitter.propagate("another test", "test2");
    // test2 another test
    
    // offAll will remove all subscriptions for a given namespace
    emitter.offAll("namespace");

    emitter.emit("test", "nothing will be logged");

    // a priority property can be added to the end of the paramters
    emitter.on("test3", "namespace", () => {
        console.log("this will be logged last");
    }, Emitter.LOW_PRIORITY);

    emitter.on("test3", "namespace", () => {
        console.log("this will be logged first");
    }, Emitter.HIGH_PRIORITY);

    emitter.emit("test3");
    // this will be logged first
    // this will be logged last

    // once registrations do not have a priority, because the event will only execute once
    

License

Published under the LGPL-2.1 license.

Published by MDaemon Technologies, Ltd. Simple Secure Email https://www.mdaemon.com

1.3.0

1 month ago

1.2.1

2 months ago

1.2.0

4 months ago

1.1.2

7 months ago

1.1.1

1 year ago

1.1.0

1 year ago

1.0.13

1 year ago

1.0.12

1 year ago

1.0.11

1 year ago

1.0.10

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago