Skip to content

Commit

Permalink
Change worker command API
Browse files Browse the repository at this point in the history
  • Loading branch information
ai committed Dec 14, 2010
1 parent afff1a3 commit 3954e0d
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 16 deletions.
8 changes: 5 additions & 3 deletions lib/drivers/common.js
Expand Up @@ -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' })
}
}
Expand Down Expand Up @@ -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 })
}

/**
Expand Down
8 changes: 3 additions & 5 deletions lib/evoplus-steam.js
Expand Up @@ -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)
},

/**
Expand Down
5 changes: 3 additions & 2 deletions spec/commonSpec.js
Expand Up @@ -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 }
Expand Down
5 changes: 3 additions & 2 deletions 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))
Expand Down
7 changes: 3 additions & 4 deletions spec/runnerSpec.js
Expand Up @@ -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 }
Expand All @@ -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 }])
})
})

Expand Down

0 comments on commit 3954e0d

Please sign in to comment.