File tree Expand file tree Collapse file tree 1 file changed +15
-10
lines changed
packages/node_modules/overmind/src Expand file tree Collapse file tree 1 file changed +15
-10
lines changed Original file line number Diff line number Diff 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 )
You can’t perform that action at this time.
0 commit comments