Skip to content

Commit 215562c

Browse files
perf(overmind-devtools): throttle instead of debounce
1 parent 27dbb9e commit 215562c

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

packages/node_modules/overmind/src/Devtools.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,24 @@ export function safeValues(values) {
3030
return values.map(safeValue)
3131
}
3232

33-
function debounce(func, wait) {
34-
let timeout
33+
const throttle = (func, limit) => {
34+
let lastFunc
35+
let lastRan
3536
return function() {
3637
const context = this
3738
const args = arguments
38-
39-
const later = function() {
40-
timeout = null
39+
if (!lastRan) {
4140
func.apply(context, args)
41+
lastRan = Date.now()
42+
} else {
43+
clearTimeout(lastFunc)
44+
lastFunc = setTimeout(function() {
45+
if (Date.now() - lastRan >= limit) {
46+
func.apply(context, args)
47+
lastRan = Date.now()
48+
}
49+
}, limit - (Date.now() - lastRan))
4250
}
43-
44-
clearTimeout(timeout)
45-
timeout = setTimeout(later, wait)
4651
}
4752
}
4853

@@ -98,8 +103,8 @@ export class Devtools {
98103
this.sendBuffer()
99104
})
100105
}
101-
private sendBuffer = debounce(function() {
102-
if (this.isConnected) {
106+
private sendBuffer = throttle(function() {
107+
if (this.isConnected && this.buffer.length) {
103108
this.ws.send(
104109
`{ "appName": "${this.name}", "messages": [${this.buffer.join(',')}] }`
105110
)

0 commit comments

Comments
 (0)