-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathexample.clock.ts
61 lines (49 loc) · 1.47 KB
/
example.clock.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import {
SimpleEventDispatcher,
SignalDispatcher,
EventDispatcher,
IEvent
} from "strongly-typed-events";
declare var window: any;
declare var alert: any;
declare var console: any;
declare var Image: any;
/*
* This clock example shows how to use Strongly Typed Events
* with interfaces.
*/
interface IClock {
OnTick(): IEvent<IClock, number>;
}
class Clock {
private _onTick = new SignalDispatcher();
private _onSequenceTick = new SimpleEventDispatcher<number>();
private _onClockTick = new EventDispatcher<Clock, number>();
private _ticks: number = 0;
constructor(public name: string, timeout: number) {
setInterval(() => {
this._ticks += 1;
this._onTick.dispatch();
this._onSequenceTick.dispatch(this._ticks);
this._onClockTick.dispatch(this, this._ticks);
}, timeout);
}
public get onTick() {
return this._onTick.asEvent();
}
public get onSequenceTick() {
return this._onSequenceTick.asEvent();
}
public get onClockTick() {
return this._onClockTick.asEvent();
}
}
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.`)
);