/
EventDispatcher.ts
40 lines (38 loc) · 1.01 KB
/
EventDispatcher.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import {EventDispatcherBase, Subscribable} from './EventDispatcherBase';
/**
* Dispatches a {@link SubscribableEvent}.
*
* @example
* ```ts
* class Example {
* // expose the event to external classes
* public get onValueChanged {
* return this.value.subscribable;
* }
* // create a private dispatcher
* private value = new EventDispatcher<number>();
*
* private dispatchExample() {
* // dispatching will notify all subscribers.
* this.value.dispatch(0);
* }
* }
* ```
*
* @typeParam T - The type of the value argument to subscribers.
*/
export class EventDispatcher<T> extends EventDispatcherBase<T> {
public dispatch(value: T) {
this.notifySubscribers(value);
}
}
/**
* Provides safe access to the public interface of {@link EventDispatcher}.
*
* @remarks
* External classes can use it to subscribe to an event without being able to
* dispatch it.
*
* @typeParam T - The type of the argument passed to subscribers.
*/
export type SubscribableEvent<T> = Subscribable<T>;