Skip to content

Commit 2799804

Browse files
perf(overmind-devtools-client): throttle messages to avoid unnecessary flushes and refactor inspecto
1 parent c6fb42f commit 2799804

File tree

7 files changed

+308
-244
lines changed

7 files changed

+308
-244
lines changed

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,14 @@
3232
"color-hash": "1.0.3",
3333
"electron": "2.0.8",
3434
"electron-json-storage": "4.1.0",
35+
"electron-prompt": "1.3.1",
3536
"emotion": "9.2.12",
3637
"express": "4.16.3",
3738
"install": "0.12.1",
3839
"is-plain-obj": "1.1.0",
40+
"lodash.throttle": "4.1.1",
3941
"marksy": "6.1.0",
42+
"npm": "6.3.0",
4043
"page": "1.8.6",
4144
"petit-dom": "0.2.2",
4245
"preact": "8.3.1",
@@ -53,9 +56,7 @@
5356
"vue": "2.5.16",
5457
"vue-hot-reload-api": "2.3.0",
5558
"vue-styled-components": "1.3.0",
56-
"ws": "7.0.0",
57-
"electron-prompt": "1.3.1",
58-
"npm": "6.3.0"
59+
"ws": "7.0.0"
5960
},
6061
"devDependencies": {
6162
"@babel/core": "7.4.5",

packages/node_modules/overmind-devtools-client/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"react-dom": "16.8.1",
2525
"overmind-themes": "next",
2626
"ws": "^7.0.0",
27-
"emotion": "^9.2.12"
27+
"emotion": "^9.2.12",
28+
"lodash.throttle": "^4.1.1"
2829
},
2930
"devDependencies": {
3031
"@types/node": "^10.12.21",

packages/node_modules/overmind-devtools-client/src/BackendConnector.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { AppMessage, AppMessageType } from './overmind/types'
1+
import { AppMessage } from './overmind/types'
2+
import * as throttle from 'lodash.throttle'
23

34
type Message = {
45
appName: string
56
messages: AppMessage<any>[]
67
}
78

8-
type MessageCallback = (message: Message) => void
9+
type MessageCallback = (message: Message[]) => void
910

1011
class WebsocketConnector {
1112
private nextEvaluationId = 0
@@ -15,6 +16,7 @@ class WebsocketConnector {
1516
private socket: WebSocket
1617
private messagesBeforeConnected: Array<[string, any, any]> = []
1718
private isOpen = false
19+
buffer = []
1820

1921
public connect(
2022
port: string | number // TODO: return Promise so we can wait for it
@@ -85,8 +87,22 @@ class WebsocketConnector {
8587
}
8688

8789
export class BackendConnector extends WebsocketConnector {
90+
private throttledOnMessage = throttle(
91+
(onMessage: MessageCallback) => {
92+
const messages = this.buffer.slice()
93+
this.buffer.length = 0
94+
onMessage(messages)
95+
},
96+
500,
97+
{
98+
leading: false,
99+
}
100+
)
88101
onMessage = (onMessage: MessageCallback) => {
89-
this.on('message', (message) => onMessage(message))
102+
this.on('message', (message) => {
103+
this.buffer.push(message)
104+
this.throttledOnMessage(onMessage)
105+
})
90106
}
91107
onDisconnect = (onDisconnect: (name: string) => void) => {
92108
this.on('disconnect', (name) => onDisconnect(name))

0 commit comments

Comments
 (0)