diff --git a/lib/drivers/common.js b/lib/drivers/common.js index fe85d6b..cffcee8 100644 --- a/lib/drivers/common.js +++ b/lib/drivers/common.js @@ -8,10 +8,12 @@ onmessage = function(e) { /** * Set _callback_ as worker constructor. It will be call on _init_ command and * send _initialized_ command after _callback_ finish. + + * @param callback {Function} will be receive worker name and command object */ function init(callback) { listeners.init = function(command) { - callback(command) + callback(command.name, command) postMessage({ command: 'initialized' }) } } @@ -62,8 +64,8 @@ function out(data) { * @param name {String} worker recipient name * @param data {Object} any variables, to communicate */ -function worker(name, data) { - postMessage({ command: 'worker', to: name, data: data }) +function worker(name, command) { + postMessage({ command: 'worker', to: name, content: command }) } /** diff --git a/lib/evoplus-steam.js b/lib/evoplus-steam.js index 3451b3d..1ebc087 100644 --- a/lib/evoplus-steam.js +++ b/lib/evoplus-steam.js @@ -273,11 +273,9 @@ evoplus.steam.Runner.prototype = { * input _worker_ command. */ _onworker: function(from, msg) { - this.workers[msg.to].postMessage({ - command: 'worker', - from: from, - data: msg.data - }) + var command = msg.content + command.from = from + this.workers[msg.to].postMessage(command) }, /** diff --git a/spec/commonSpec.js b/spec/commonSpec.js index 58e59d6..ac891ab 100644 --- a/spec/commonSpec.js +++ b/spec/commonSpec.js @@ -12,14 +12,15 @@ describe('drivers/common.js', function() { run.workers[0].postMessage({ command: 'talk', data: 'test' }) - waitsFor(function() { return log.length >= 4 }) + waitsFor(function() { return log.length >= 9 }) runs(function() { expect(log).toEqual([ + { command: 'log', data: 'I am 0 from 1' }, { command: 'getter', name: 'answer' }, { command: 'initialized' }, { command: 'log', data: 'test' }, { command: 'load', name: 'A', params: { a: 1 } }, - { command: 'worker', data: { b: 2 }, to: 'A' }, + { command: 'worker', to: 'A', content: { command: 'b', b: 2 } }, { command: 'out', data: { c: 3 } }, { command: 'log', data: 2 }, { command: 'log', data: 3 } diff --git a/spec/commoner.js b/spec/commoner.js index 96435dd..da740bb 100644 --- a/spec/commoner.js +++ b/spec/commoner.js @@ -1,13 +1,14 @@ importScripts('../drivers/common.js') -init(function() { +init(function(name, command) { + log('I am ' + name + ' from ' + command.count) getter('answer', function() { return 42 }) }) on('talk', function(msg) { log(msg.data) load('A', { a: 1 }) - worker('A', { b: 2 }) + worker('A', { command: 'b', b: 2 }) out({ c: 3 }) log(options.a) log(options.sum(1, 2)) diff --git a/spec/runnerSpec.js b/spec/runnerSpec.js index d5532ef..de669e3 100644 --- a/spec/runnerSpec.js +++ b/spec/runnerSpec.js @@ -125,7 +125,8 @@ describe('evoplus.steam.Runner', function() { run.workers[0].postMessage('clearLog') run.workers[1].postMessage('clearLog') - run._onworker(0, { to: 1, data: 'test' }) + run._onworker(0, { command: 'worker', to: 1, + content: { command: 'test', a: 1 } }) var log = {} run._onmessage = function(name, msg) { log[name] = msg } @@ -135,9 +136,7 @@ describe('evoplus.steam.Runner', function() { waitsFor(function() { return log[0] && log[1] }) runs(function() { expect(log[0]).toEqual([]) - expect(log[1]).toEqual([{ - command: 'worker', from: 0, data: 'test' - }]) + expect(log[1]).toEqual([{ command: 'test', from: 0, a: 1 }]) }) })