client/ui/perf: process ipc messages (incl. osc in a threaded queue) to enhance ui reactivity
This commit is contained in:
parent
5aefbd5125
commit
78dd3bc89d
5 changed files with 91 additions and 11 deletions
57
src/browser/js/app/ipc-worker.js
Normal file
57
src/browser/js/app/ipc-worker.js
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
var io = require('socket.io-client/dist/socket.io.slim.js')
|
||||
|
||||
var IpcWorker = class IpcWorker {
|
||||
|
||||
constructor(worker) {
|
||||
|
||||
this.worker = worker
|
||||
this.queue = []
|
||||
this.socket = null
|
||||
|
||||
this.worker.addEventListener('message', (e)=>{
|
||||
|
||||
var [name, data] = e.data
|
||||
|
||||
if (name == 'on') {
|
||||
|
||||
this.socket.on(data, (message)=>{
|
||||
this.worker.postMessage([data, message])
|
||||
})
|
||||
|
||||
} else if (name == 'send'){
|
||||
|
||||
this.queue.push(data)
|
||||
|
||||
} else if (name == 'connect') {
|
||||
|
||||
this.socket = io.connect(data)
|
||||
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
setInterval(this.flush.bind(this), 20)
|
||||
|
||||
}
|
||||
|
||||
flush() {
|
||||
|
||||
if (this.queue.length && this.socket !== null) {
|
||||
|
||||
for (let msg of this.queue) {
|
||||
|
||||
this.socket.emit(...msg)
|
||||
}
|
||||
this.queue = []
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
module.exports = (worker)=>{
|
||||
|
||||
return new IpcWorker(worker)
|
||||
|
||||
}
|
||||
|
|
@ -1,18 +1,38 @@
|
|||
var socket = require('socket.io-client/dist/socket.io.slim.js')
|
||||
var worker = require('webworkify')
|
||||
|
||||
var Ipc = function(){
|
||||
var Ipc = class Ipc {
|
||||
|
||||
this.socket = socket.connect()
|
||||
constructor() {
|
||||
|
||||
this.callbacks = {}
|
||||
|
||||
this.worker = worker(require('./ipc-worker'))
|
||||
this.worker.addEventListener('message', (ev)=>{
|
||||
|
||||
var [name, data] = ev.data
|
||||
|
||||
if (this.callbacks[name]) this.callbacks[name](data)
|
||||
|
||||
})
|
||||
|
||||
this.worker.postMessage(['connect', window.location.href])
|
||||
|
||||
}
|
||||
|
||||
send(name, data) {
|
||||
|
||||
this.worker.postMessage(['send', [name, data]])
|
||||
|
||||
}
|
||||
|
||||
on(name, fn) {
|
||||
|
||||
this.callbacks[name] = fn
|
||||
this.worker.postMessage(['on', name])
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Ipc.prototype.send = function(){
|
||||
return this.socket.emit.apply(this.socket, arguments)
|
||||
}
|
||||
|
||||
Ipc.prototype.on = function(){
|
||||
return this.socket.on.apply(this.socket, arguments)
|
||||
}
|
||||
|
||||
var ipc = new Ipc()
|
||||
|
||||
|
|
|
|||
|
|
@ -41,7 +41,7 @@ var WidgetManager = function(){
|
|||
})
|
||||
})
|
||||
|
||||
ipc.socket.on('reconnect', ()=>{
|
||||
ipc.on('reconnect', ()=>{
|
||||
for (var hash in this.widgets) {
|
||||
ipc.send('addWidget', {
|
||||
hash:hash,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue