Skip to content

add reserved EventListener

Latest
Compare
Choose a tag to compare
@apollo79 apollo79 released this 17 Sep 16:13
· 5 commits to main since this release

This release introduces a "reserved EventEmitter".

It is based on version 1.x.x and not compatible with version 2.x.x. Because of this, you can't use emit with multiple detail args like the following: emit("foo", arg1, arg2), but you must use it like emit("foo", [arg1, arg2]) The upside of version 1.x.x is, that you can use removeEventListener with events added through on or once, which is not possible with version 2.x.x. If you used version 1.x.x before, you can just use your code as it is now, it will work.

There are now different ways to use this EventEmitter:

  • You can use it without typed events, like an untyped EventEmitter:

    import { EventEmitter } from "https://deno.land/x/evtemitter@2.0.0/mod.ts";
    
    const target = new EventEmitter();
    
    target.on("foo", (detail) => {
        console.log(detail); // undefined
    });
    
    target.emit("foo");
    
    target.on("bar", (detail) => {
        console.log(detail); // hello world
    });
    
    target.emit("bar", "hello world");
  • You can use it as typed EventEmitter that provides strongly typed events and
    methods with autocompletion in you code editor.

    import { EventEmitter } from "https://deno.land/x/evtemitter@2.0.0/mod.ts";
    
    type Events = {
        foo: undefined;
        bar: string;
    };
    
    const emitter = new EventEmitter<Events>();
    
    target.on("foo", (detail) => {
        console.log("Foo has been emitted");
    });
    
    // works
    target.emit("foo");
    
    // would throw an exception
    // target.emit("foo", "hello world");
    
    target.once("bar", (detail) => {
        console.log("Bar has been emitted");
    });
    
    // works
    target.emit("bar", "hello world");
    
    // would throw an exception
    // target.emit("bar", 123);
  • And you can use it with reserved events, which is for example useful if you
    want to only allow to emit message events from anyone but in your class you
    want to emit a connection event. Of course, this type of emitter is also
    strongly typed and provides autocompletion:

    import { EventEmitter } from "https://deno.land/x/evtemitter@2.0.0/mod.ts";
    
    // Events that can be emitted via `emit`, `dispatch` and `publish` and that can be listened to
    type UserEvents = {
        message: string;
    };
    
    // Events that can only be emitted via the protected `emitReserved` method. It is also possible to listen to these events
    type ReservedEvents = {
        connection: { name: string };
    };
    
    class Implementing extends EventEmitter<
        UserEvents
        ReservedEvents
    > {
        constructor() {
            super();
    
            // your logic here
        }
    
        onConnect(name: string) {
            // logic here...
            this.emitReserved("connection", { name });
        }
    }
    
    const target = new Implementing();
    
    target.addEventListener("connection", (event) => {
        const name = event.detail.name; // this is typed as a string in this example
    
        console.log(`${name} has connected!`);
    });
    
    // of course, this makes no sense in reality, it's just for showing
    target.onConnect("Apollo");
    
    target.pull("message").then((message) => {
        console.log(message);
    });
    
    target.emit("message", "hello world");
    
    // this would throw an exception
    // target.emit("connection", "Apollo");

Full Changelog: 1.3.1.1...3.0.0