Skip to content

Commit

Permalink
Reduce setTimeout/clearTimeout calls from Delayed
Browse files Browse the repository at this point in the history
This reduces the time the operation I mentioned in #5992 takes to ~450ms.
  • Loading branch information
alur committed Sep 6, 2019
1 parent 538c4dc commit b33187e
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/util/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,27 @@ export function countColumn(string, end, tabSize, startIndex, startValue) {
}

export class Delayed {
constructor() {this.id = null}
constructor() {
this.id = null
this.f = null
this.time = 0
this.handler = bind(onTimeout, this)
}
onTimeout(self) {
self.id = 0;
if (self.time < Date.now()) {
self.f()
} else {
setTimeout(self.handler, self.time - Date.now())
}
}
set(ms, f) {
clearTimeout(this.id)
this.id = setTimeout(f, ms)
this.f = f
const time = Date.now() + ms
if (!this.id || time < this.time) {
clearTimeout(this.id)
this.id = setTimeout(this.handler, ms)
}
}
}

Expand Down

0 comments on commit b33187e

Please sign in to comment.