Skip to content

Commit e275579

Browse files
nicDamoursNicolas D'Amours
and
Nicolas D'Amours
authored
fix(electron): chunk message data (#2053)
Co-authored-by: Nicolas D'Amours <[email protected]>
1 parent 9985d3e commit e275579

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

packages/shared-utils/src/util.ts

+20
Original file line numberDiff line numberDiff line change
@@ -790,3 +790,23 @@ export function copyToClipboard (state) {
790790
export function isEmptyObject (obj) {
791791
return obj === UNDEFINED || !obj || Object.keys(obj).length === 0
792792
}
793+
794+
/**
795+
* chunk an array into smaller chunk of given size.
796+
* @see https://stackoverflow.com/a/37826698
797+
* @param array
798+
* @param size
799+
*/
800+
export function chunk (array: unknown[], size: number): unknown[][] {
801+
return array.reduce((resultArray, item, index) => {
802+
const chunkIndex = Math.floor(index / size)
803+
804+
if (!resultArray[chunkIndex]) {
805+
resultArray[chunkIndex] = [] // start a new chunk
806+
}
807+
808+
resultArray[chunkIndex].push(item)
809+
810+
return resultArray
811+
}, []) as unknown[][]
812+
}

packages/shell-electron/src/backend.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import io from 'socket.io-client'
22
import { initBackend } from '@back'
33
import { installToast } from '@back/toast'
4-
import { Bridge, target } from '@vue-devtools/shared-utils'
4+
import { Bridge, target, chunk } from '@vue-devtools/shared-utils'
55

66
const host = target.__VUE_DEVTOOLS_HOST__ || 'http://localhost'
77
const port = target.__VUE_DEVTOOLS_PORT__ !== undefined ? target.__VUE_DEVTOOLS_PORT__ : 8098
88
const fullHost = port ? host + ':' + port : host
99
const createSocket = target.__VUE_DEVTOOLS_SOCKET__ || io
1010
const socket = createSocket(fullHost)
11+
const MAX_DATA_CHUNK = 2000
1112

1213
const connectedMessage = () => {
1314
if (target.__VUE_DEVTOOLS_TOAST__) {
@@ -45,7 +46,11 @@ const bridge = new Bridge({
4546
socket.on('vue-message', data => fn(data))
4647
},
4748
send (data) {
48-
socket.emit('vue-message', data)
49+
const chunks = chunk(data, MAX_DATA_CHUNK)
50+
51+
for (const chunk of chunks) {
52+
socket.emit('vue-message', chunk)
53+
}
4954
},
5055
})
5156

0 commit comments

Comments
 (0)