Does TypeScript support events on classes?

后端 未结 8 741
小鲜肉
小鲜肉 2021-01-30 01:53

I am just wondering if in TypeScript you can define custom events on your classes or interfaces?

What would this look like?

8条回答
  •  孤街浪徒
    2021-01-30 02:59

    The NPM package Strongly Typed Events for TypeScript (GitHub) implements 3 types of events: IEvent, ISimpleEvent and ISignal. This makes it easier to use the right kind of event for your project. It also hides the dispatch method from your event, as good information hiding should do.

    Event Types / Interfaces - The definitions of the events:

    interface IEventHandler {
        (sender: TSender, args: TArgs): void
    }
    
    interface ISimpleEventHandler {
        (args: TArgs): void
    }
    
    interface ISignalHandler {
        (): void;
    }
    

    Example - This example shows how the 3 types of events can be implemented using a ticking clock:

    class Clock {
    
        //implement events as private dispatchers:
        private _onTick = new SignalDispatcher();
        private _onSequenceTick = new SimpleEventDispatcher();
        private _onClockTick = new EventDispatcher();
    
        private _ticks: number = 0;
    
        constructor(public name: string, timeout: number) {
            window.setInterval( () => { 
                this.Tick(); 
            }, timeout);
        }
    
        private Tick(): void {
            this._ticks += 1;
    
            //trigger event by calling the dispatch method and provide data
            this._onTick.dispatch();
            this._onSequenceTick.dispatch(this._ticks);
            this._onClockTick.dispatch(this, this._ticks);
        }
    
        //expose the events through the interfaces - use the asEvent
        //method to prevent exposure of the dispatch method:
        public get onTick(): ISignal {
            return this._onTick.asEvent();
        }
    
        public get onSequenceTick() : ISimpleEvent{
            return this._onSequenceTick.asEvent();
        }
    
        public get onClockTick(): IEvent {
            return this._onClockTick.asEvent();
        }
    }
    

    Usage - It can be used like this:

    let clock = new Clock('Smu', 1000);
    
    //log the ticks to the console
    clock.onTick.subscribe(()=> console.log('Tick!'));
    
    //log the sequence parameter to the console
    clock.onSequenceTick.subscribe((s) => console.log(`Sequence: ${s}`));
    
    //log the name of the clock and the tick argument to the console
    clock.onClockTick.subscribe((c, n) => console.log(`${c.name} ticked ${n} times.`))
    

    Read more here: On events, dispatchers and lists (a general explanation of the system)

    Tutorials
    I've written a few tutorials on the subject:

    • Strongly typed event handlers in TypeScript (Part 1)
    • Using strongly typed events in TypeScript with interfaces (Part 2)
    • Strongly Typed Events in TypeScript using an event list (Part 3)
    • Adding named events to your classes (Part 4)
    • 0.2.0 Simple event support
    • 0.3.0 Signal support

提交回复
热议问题