ipc: remove woker, just handle queue in a separate loop, use a better io-parser, update deps

This commit is contained in:
jean-emmanuel 2017-06-13 14:57:25 +02:00
parent 7770d01698
commit 547f66a2a2
6 changed files with 36 additions and 84 deletions

View file

@ -1,57 +0,0 @@
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,34 +1,44 @@
var worker = require('webworkify')
var io = require('socket.io-client/dist/socket.io.slim.js'),
customParser = require('socket.io-msgpack-parser')
var Ipc = class Ipc {
constructor() {
this.callbacks = {}
this.queue = []
this.socket = io.connect('/', {parser:customParser})
this.worker = worker(require('./ipc-worker'))
this.worker.addEventListener('message', (ev)=>{
setInterval(this.flush.bind(this), 5)
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]])
flush() {
if (this.queue.length) {
for (let msg of this.queue) {
this.socket.emit(...msg)
}
this.queue = []
}
}
send() {
this.queue.push(arguments)
}
on(name, fn) {
this.callbacks[name] = fn
this.worker.postMessage(['on', name])
this.socket.on(name, fn)
}
}