client/ui/perf: process ipc messages (incl. osc in a threaded queue) to enhance ui reactivity

This commit is contained in:
jean-emmanuel 2017-06-12 23:07:14 +02:00
parent 5aefbd5125
commit 78dd3bc89d
5 changed files with 91 additions and 11 deletions

View 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)
}

View file

@ -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()

View file

@ -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,