Skip to content

Commit

Permalink
fix(electron): chunk message data (#2053)
Browse files Browse the repository at this point in the history
Co-authored-by: Nicolas D'Amours <nicolas@folkshr.com>
  • Loading branch information
nicDamours and Nicolas D'Amours committed Feb 4, 2024
1 parent 9985d3e commit e275579
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
20 changes: 20 additions & 0 deletions packages/shared-utils/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -790,3 +790,23 @@ export function copyToClipboard (state) {
export function isEmptyObject (obj) {
return obj === UNDEFINED || !obj || Object.keys(obj).length === 0
}

/**
* chunk an array into smaller chunk of given size.
* @see https://stackoverflow.com/a/37826698
* @param array
* @param size
*/
export function chunk (array: unknown[], size: number): unknown[][] {
return array.reduce((resultArray, item, index) => {
const chunkIndex = Math.floor(index / size)

if (!resultArray[chunkIndex]) {
resultArray[chunkIndex] = [] // start a new chunk
}

resultArray[chunkIndex].push(item)

return resultArray
}, []) as unknown[][]
}
9 changes: 7 additions & 2 deletions packages/shell-electron/src/backend.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import io from 'socket.io-client'
import { initBackend } from '@back'
import { installToast } from '@back/toast'
import { Bridge, target } from '@vue-devtools/shared-utils'
import { Bridge, target, chunk } from '@vue-devtools/shared-utils'

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

const connectedMessage = () => {
if (target.__VUE_DEVTOOLS_TOAST__) {
Expand Down Expand Up @@ -45,7 +46,11 @@ const bridge = new Bridge({
socket.on('vue-message', data => fn(data))
},
send (data) {
socket.emit('vue-message', data)
const chunks = chunk(data, MAX_DATA_CHUNK)

for (const chunk of chunks) {
socket.emit('vue-message', chunk)
}
},
})

Expand Down

0 comments on commit e275579

Please sign in to comment.