Skip to content
This repository has been archived by the owner on Jun 2, 2020. It is now read-only.

Commit

Permalink
It Works
Browse files Browse the repository at this point in the history
  • Loading branch information
Félix Saparelli committed Oct 20, 2011
1 parent 12c8aed commit 00b7205
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 29 deletions.
52 changes: 27 additions & 25 deletions README.mkd
Original file line number Diff line number Diff line change
Expand Up @@ -58,37 +58,39 @@ The Code
--------

Enough talk already! Where's the code?

var worker = new DynWorker();

worker.inject(function(arg) {
var result = "Do something awesome here";
DynWorker.send(result); // Send it up
}, "fooBar");
// If the library is in the current dir and
// is named dynworker.js, this works:
var worker = new DynWorker();

worker.receive(function(message) {
// Display and strike awe
});
// Otherwise you need to specify a filename,
// but it needs to be on the same domain.
var worker = new DynWorker("/js/lib/dynworker.min.js");

// You can also modify the default path once and for all:
DynWorker.path("path/to/dynworker.js");
var worker = new DynWorker();

worker.run("fooBar", ["argument"]);

Cool! But what if my function's already named? Well, it depends:

// We'll parse that name:

function fooBar(arg) {
// ...
}
// The function is namespaced under DynWorker.ns
// inside the worker.
worker.inject("funcName", function(arg1, arg2) {
var result = "Do something awesome here";

return result;
});

// The callback gets back the raw event
worker.receive(function(e) {
e.data; // Display and strike awe
});

// But not that one:

var fooBar = function(arg) {
// ...
}
// The #run function wraps the function call in a
// DynWorker.send() so the return value of the
// function is sent up.
worker.run("funcName", arg1, arg2);

// Yeah, we're as sorry as you :(

Enjoy!
// Hence, these two are equivalent:
worker.run("funcName");
worker.eval("DynWorker.send(DynWorker.ns['funcName']());");
32 changes: 28 additions & 4 deletions dynworker.coffee
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
###
DynWorker is Copyright 2011- Félix "passcod" Saparelli
and licensed under MIT: http://passcod.mit-license.org
###

"use strict";

self.onmessage = (e) ->
switch e.data['DynWorkerAction']
when 'eval' then eval e.data['code']



DynWorker = (path = "dynworker.js") ->
DynWorker = (path = DynWorker.libpath) ->
worker = new Worker(path)

receive = (callback) ->
Expand All @@ -13,18 +19,36 @@ DynWorker = (path = "dynworker.js") ->
send = (msg) ->
worker.postMessage(msg)

runCode = (code) ->
weval = (code) ->
send {
DynWorkerAction: 'eval'
code: code
}

inject = (name, func) ->
sfunc = func.toString()
unless /^function \(/.test(sfunc)
sfunc.replace(/^function [^\(]+\(/, 'function (')

weval "DynWorker.ns['#{name}']=#{sfunc};"

run = (name, args...) ->
weval "DynWorker.send(DynWorker.ns['#{name}'].apply(null, #{JSON.stringify args}));"

return {
receive: receive
send: send
run: runCode
eval: weval
inject: inject
run: run
}

DynWorker.ns = {}

DynWorker.libpath = "dynworker.js"
DynWorker.path = (path) ->
if path then DynWorker.libpath = path;

DynWorker.send = (msg) ->
self.postMessage(msg)

Expand Down

0 comments on commit 00b7205

Please sign in to comment.