car-conductor
Advanced tools
Comparing version 0.1.1 to 0.1.2
38
index.js
@@ -1,20 +0,26 @@ | ||
module.exports = function registerConductor(filename, domain){ | ||
var worker = new Worker(filename) | ||
var domain = domain || filename.slice(filename.lastIndexOf('/') , -2) | ||
module.exports = function registerConductor(path, domain){ | ||
var worker = new Worker(path) | ||
var filenameStart = path.lastIndexOf('/') | ||
var domain = domain || path.slice(filenameStart !== -1 ? filenameStart + 1 : 0, -3) | ||
function passMessageFromChooBus(eventName, data){ | ||
if(eventName.indexOf(domain + ':') === 0){ | ||
console.log(eventName.indexOf(domain + ':')) | ||
worker.postMessage([eventName.slice(domain.length), data]) | ||
} | ||
} | ||
function passDataBackToChoo(msg){ | ||
var data = msg.data | ||
if(Array.isArray(data) && data.length === 2 && typeof data[0] === 'string' && typeof data[1] === 'object' ){ | ||
return emitter.emit(data[0], data[1]) | ||
} | ||
state[domain] = data | ||
emitter.emit('render') | ||
} | ||
return function conductorMiddleware(state, emitter){ | ||
emitter.on('*', function(eventName, data){ | ||
console.log(eventName) | ||
if(eventName.indexOf(domain + ':')){ | ||
worker.write([eventName.slice(domain.length), data]) | ||
} | ||
}) | ||
worker.on('data', function(data){ | ||
if(Array.isArray(data) && data.length === 2 && typeof data[0] === 'string' && typeof data[1] === 'object' ){ | ||
return emitter.emit(data[0], data[1]) | ||
} | ||
state[domain] = data | ||
emitter.emit('render') | ||
}) | ||
emitter.addListener('*', passMessageFromChooBus) | ||
worker.onmessage = passDataBackToChoo | ||
} | ||
} |
{ | ||
"name": "car-conductor", | ||
"version": "0.1.1", | ||
"description": "choo webworker middleware", | ||
"version": "0.1.2", | ||
"description": "choo web-worker middleware", | ||
"main": "index.js", | ||
@@ -6,0 +6,0 @@ "scripts": { |
@@ -15,4 +15,3 @@ # car-conductor | ||
app.use(conductor('passengers.js')) | ||
app.use(conductor('passengers.js', 'cows')) | ||
app.use(conductor('./passengers.js')) | ||
@@ -23,4 +22,3 @@ app.route('/' function mainView(state, emit) { | ||
Passenger count ${state.passengers.count} | ||
Cow count ${cows.count} | ||
<a onclick=${emit('passengers:increment')}>Add</a> | ||
<a onclick=${emit('passengers:increment')}>Add passenger</a> | ||
</div> | ||
@@ -32,7 +30,7 @@ ` | ||
A typical `passengers.js` file | ||
A typical `passengers.js` file can look like this | ||
```js | ||
// Yes, worker will be browserified tnx 💪-ify | ||
var dream = require("intensive-task") | ||
// Yes, worker will be browserified | ||
@@ -43,2 +41,5 @@ var state = { | ||
// To hydrate state | ||
self.postMessage(state) | ||
self.onmessage = function(msg){ | ||
@@ -50,3 +51,2 @@ var eventName = msg[0] | ||
case 'nothing to do': state.subconscious = dream() | ||
break | ||
default: self.postMessage(state) | ||
@@ -59,2 +59,2 @@ } | ||
Cars suck because they isolate the state, for a choo that has all it's state in a webworker check out my fork. | ||
Cars suck because they isolate the state. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2682
23
55