summaryrefslogtreecommitdiffstats
path: root/toolkit/modules/Timer.sys.mjs
blob: c77fe90e11abf1c7f22b79e5af4ab3ca27c6b2ca (plain)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/**
 * JS module implementation of setTimeout and clearTimeout.
 */

// This gives us >=2^30 unique timer IDs, enough for 1 per ms for 12.4 days.
var gNextId = 1; // setTimeout and setInterval must return a positive integer

var gTimerTable = new Map(); // int -> nsITimer or idleCallback

// Don't generate this for every timer.
var setTimeout_timerCallbackQI = ChromeUtils.generateQI([
  "nsITimerCallback",
  "nsINamed",
]);

function _setTimeoutOrIsInterval(
  aCallback,
  aMilliseconds,
  aIsInterval,
  aTarget,
  aArgs
) {
  if (typeof aCallback !== "function") {
    throw new Error(
      `callback is not a function in ${
        aIsInterval ? "setInterval" : "setTimeout"
      }`
    );
  }
  let id = gNextId++;
  let timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);

  if (aTarget) {
    timer.target = aTarget;
  }

  let callback = {
    QueryInterface: setTimeout_timerCallbackQI,

    // nsITimerCallback
    notify() {
      if (!aIsInterval) {
        gTimerTable.delete(id);
      }
      aCallback.apply(null, aArgs);
    },

    // nsINamed
    get name() {
      return `${
        aIsInterval ? "setInterval" : "setTimeout"
      }() for ${Cu.getDebugName(aCallback)}`;
    },
  };

  timer.initWithCallback(
    callback,
    aMilliseconds,
    aIsInterval ? timer.TYPE_REPEATING_SLACK : timer.TYPE_ONE_SHOT
  );

  gTimerTable.set(id, timer);
  return id;
}

export function setTimeout(aCallback, aMilliseconds, ...aArgs) {
  return _setTimeoutOrIsInterval(aCallback, aMilliseconds, false, null, aArgs);
}

export function setTimeoutWithTarget(
  aCallback,
  aMilliseconds,
  aTarget,
  ...aArgs
) {
  return _setTimeoutOrIsInterval(
    aCallback,
    aMilliseconds,
    false,
    aTarget,
    aArgs
  );
}

export function setInterval(aCallback, aMilliseconds, ...aArgs) {
  return _setTimeoutOrIsInterval(aCallback, aMilliseconds, true, null, aArgs);
}

export function setIntervalWithTarget(
  aCallback,
  aMilliseconds,
  aTarget,
  ...aArgs
) {
  return _setTimeoutOrIsInterval(
    aCallback,
    aMilliseconds,
    true,
    aTarget,
    aArgs
  );
}

function clear(aId) {
  if (gTimerTable.has(aId)) {
    gTimerTable.get(aId).cancel();
    gTimerTable.delete(aId);
  }
}
export var clearInterval = clear;
export var clearTimeout = clear;

export function requestIdleCallback(aCallback, aOptions) {
  if (typeof aCallback !== "function") {
    throw new Error("callback is not a function in requestIdleCallback");
  }
  let id = gNextId++;

  let callback = (...aArgs) => {
    if (gTimerTable.has(id)) {
      gTimerTable.delete(id);
      aCallback(...aArgs);
    }
  };

  ChromeUtils.idleDispatch(callback, aOptions);
  gTimerTable.set(id, callback);
  return id;
}

export function cancelIdleCallback(aId) {
  if (gTimerTable.has(aId)) {
    gTimerTable.delete(aId);
  }
}