1 |
|
2 | const timeoutCallbacks = new Map();
|
3 | let timerId = 0;
|
4 | var TimerTargetImpl = (function (_super) {
|
5 | __extends(TimerTargetImpl, _super);
|
6 | function TimerTargetImpl() {
|
7 | return _super !== null && _super.apply(this, arguments) || this;
|
8 | }
|
9 | TimerTargetImpl.initWithCallback = function (callback, id, shouldRepeat) {
|
10 | var handler = TimerTargetImpl.new();
|
11 | handler.callback = callback;
|
12 | handler.id = id;
|
13 | handler.shouldRepeat = shouldRepeat;
|
14 | return handler;
|
15 | };
|
16 | TimerTargetImpl.prototype.tick = function (timer) {
|
17 | if (!this.disposed) {
|
18 | this.callback();
|
19 | }
|
20 | if (!this.shouldRepeat) {
|
21 | this.unregister();
|
22 | }
|
23 | };
|
24 | TimerTargetImpl.prototype.unregister = function () {
|
25 | if (!this.disposed) {
|
26 | this.disposed = true;
|
27 | var timer = timeoutCallbacks.get(this.id).k;
|
28 | timer.invalidate();
|
29 | timeoutCallbacks.delete(this.id);
|
30 | }
|
31 | };
|
32 | TimerTargetImpl.ObjCExposedMethods = {
|
33 | tick: { returns: interop.types.void, params: [NSTimer] },
|
34 | };
|
35 | return TimerTargetImpl;
|
36 | }(NSObject));
|
37 | function createTimerAndGetId(callback, milliseconds, shouldRepeat) {
|
38 |
|
39 | milliseconds += 0;
|
40 | timerId++;
|
41 | const id = timerId;
|
42 | const timerTarget = TimerTargetImpl.initWithCallback(callback, id, shouldRepeat);
|
43 | const timer = NSTimer.scheduledTimerWithTimeIntervalTargetSelectorUserInfoRepeats(milliseconds / 1000, timerTarget, 'tick', null, shouldRepeat);
|
44 |
|
45 | NSRunLoop.currentRunLoop.addTimerForMode(timer, NSRunLoopCommonModes);
|
46 | const pair = {
|
47 | k: timer,
|
48 | v: timerTarget,
|
49 | };
|
50 | timeoutCallbacks.set(id, pair);
|
51 | return id;
|
52 | }
|
53 | export function setTimeout(callback, milliseconds = 0, ...args) {
|
54 | const invoke = () => callback(...args);
|
55 | return createTimerAndGetId(zonedCallback(invoke), milliseconds, false);
|
56 | }
|
57 | export function clearTimeout(id) {
|
58 | const pair = timeoutCallbacks.get(id);
|
59 | if (pair && pair.v) {
|
60 | pair.v.unregister();
|
61 | }
|
62 | }
|
63 | export function setInterval(callback, milliseconds = 0, ...args) {
|
64 | const invoke = () => callback(...args);
|
65 | return createTimerAndGetId(zonedCallback(invoke), milliseconds, true);
|
66 | }
|
67 | export const clearInterval = clearTimeout;
|
68 |
|
\ | No newline at end of file |