Skip to content

[Typescript SimpleEvents] Simple Event

Face of Cat edited this page Apr 30, 2017 · 1 revision

This is the simplest type of all the provided events.

Class Information

export interface Event {
    on(callback: () => void): number;
    off(handle: number): void;
    trigger(): void;
}

export class SimpleEvent extends BaseEvent<void, void> implements Event {
    on= (callback: () => void): number => super.add(callback);
    off= (handler: number): void => super.remove(handler);
    trigger= (): void => super.getResult(undefined);
}
  • on: adds a callback to the invoke list and returns its handle;
  • off: removes a callback from the invoke list based on its handle;
  • trigger: invokes all the callback in order;

Example Usage

the following test.ts compiled into test.js:

import { SimpleEvent } from "typescript-simpleevents";

class EventTest {
    readonly action = new SimpleEvent();

    doAction() {
        this.action.trigger();
    }
}

const test = new EventTest();
test.action.on(() => { console.log('Action!'); });
test.doAction();

wil have the output:

> node test.js
Action!

Clone this wiki locally