Repository navigation
[Typescript SimpleEvents] Simple Data Event
Face of Cat edited this page Apr 30, 2017
·
1 revision
This event type provides data to its callabacks.
export interface DataEvent<D> {
on(callback: (data: D) => void): number;
off(handle: number): void;
trigger(data: D): void;
}
export class SimpleDataEvent<D> extends BaseEvent<D, void> implements DataEvent<D> {
on= (callback: (data: D) => void): number => super.add(callback);
off= (handle: number): void => super.remove(handle);
trigger= (data: D): void => super.getResult(data);
}- 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;
the following test.ts compiled into test.js:
import { SimpleDataEvent } from "typescript-simpleevents";
interface EventData {
name: string;
location: string;
}
class EventTest {
readonly action = new SimpleDataEvent<EventData>();
doAction(name: string, location: string) {
this.action.trigger({ name: name, location: location });
}
}
const test = new EventTest();
test.action.on(d => { console.log(`${d.name} was ${d.location}!`); });
test.doAction('Gigi', 'here');wil have the output:
> node test.js
Gigi was here!