Skip to content

Commit 5dade13

Browse files
alurmarijnh
authored andcommitted
Reduce setTimeout/clearTimeout calls from Delayed
This reduces the time the operation I mentioned in codemirror#5992 takes to ~450ms.
1 parent b5e77b4 commit 5dade13

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

src/util/misc.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,27 @@ export function countColumn(string, end, tabSize, startIndex, startValue) {
2929
}
3030

3131
export class Delayed {
32-
constructor() {this.id = null}
32+
constructor() {
33+
this.id = null
34+
this.f = null
35+
this.time = 0
36+
this.handler = bind(this.onTimeout, this)
37+
}
38+
onTimeout(self) {
39+
self.id = 0
40+
if (self.time < Date.now()) {
41+
self.f()
42+
} else {
43+
setTimeout(self.handler, self.time - Date.now())
44+
}
45+
}
3346
set(ms, f) {
34-
clearTimeout(this.id)
35-
this.id = setTimeout(f, ms)
47+
this.f = f
48+
const time = Date.now() + ms
49+
if (!this.id || time < this.time) {
50+
clearTimeout(this.id)
51+
this.id = setTimeout(this.handler, ms)
52+
}
3653
}
3754
}
3855

0 commit comments

Comments
 (0)