Skip to content

Releases: apollo79/evtemitter

add reserved EventListener

17 Sep 16:13
Compare
Choose a tag to compare

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

Pass multiple args to listener

21 Apr 12:10
Compare
Choose a tag to compare

This version has the number 2.0.0, because it works different like the other versions before. It has its own branch and there will be improvements on 1.x.x and 2.x.x in the future. Chose the technology that you like the most.

You can now use on like the following:

target.on("message", (sender, message) => {
  // ...
})

and emit:

target.emit("message", "kevin", "Hi Detlef");

The type of EventMap is the following:

const target = new EventEmitter<{
  message: [string, string];
}>();

!!! WARNING !!!:
removeEventListener can't anymore be used with listeners added with on, once and subscribe and off is no more compatible with listeners added with addEventListener!
emit / publish / dispatch does not call listeners added with addEventListener, the same at dispatchEvent and on, once, subscribe!

EventEmitter only extends EventTarget, meaning you can use either EventTarget methods or EventEmitter methods or both, but separated!

Full Changelog: 1.3.1.1...2.0.0

Change type of `detail` in untyped EventListener from `unknown` to `any`

26 Mar 13:42
Compare
Choose a tag to compare
1.3.1.1

change type of standard event record: `any` instead of `unknown`

Use without typed events

26 Mar 13:22
Compare
Choose a tag to compare
1.3.1

update to allow use without types

1.3.0

23 Mar 18:56
Compare
Choose a tag to compare
auto run `deno fmt ${file}` on save

1.3.0.dev: make `emit` async and add emitSync method

19 Mar 17:27
Compare
Choose a tag to compare

Braking Change: Only the event detail is now passed to the listener functions. But you can also use the native methods (addEventListener, removeEventListener, dispatchEvent and a little helper: dispatch) to work with CustomEvents and their detail prop.

1.2.0-dev

06 Mar 10:25
Compare
Choose a tag to compare
1.2.0-dev Pre-release
Pre-release

Add publish / subscribe methods and fix once method

Update emit method

01 Mar 17:19
Compare
Choose a tag to compare
Update emit method Pre-release
Pre-release

Update to the emit method. The type of ...[detail] is now [detail: T[K]["detail"]].

That makes it possible to extend the EventEmitter with allowing custom events, but comes with some other problems. Now, you must pass undefined to emit: emit("<event>", undefined) if you don't want to pass any arguments to the emitted event.

First release of the eventemitter

25 Feb 15:16
Compare
Choose a tag to compare
1.0.0

move EventEmitter to EventEmitter.ts and add tests