2011-09-06 09:42:58 +09:00
|
|
|
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- /
|
|
|
|
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
2011-09-06 10:12:03 +09:00
|
|
|
function MessageHandler(name, actionHandler, comObj, scope) {
|
|
|
|
this.name = name;
|
|
|
|
|
|
|
|
actionHandler["console_log"] = function(data) {
|
|
|
|
console.log.apply(console, data);
|
|
|
|
}
|
|
|
|
actionHandler["console_error"] = function(data) {
|
|
|
|
console.error.apply(console, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
comObj.onmessage = function(event) {
|
2011-09-06 09:42:58 +09:00
|
|
|
var data = event.data;
|
|
|
|
if (data.action in actionHandler) {
|
|
|
|
actionHandler[data.action].call(scope, data.data);
|
|
|
|
} else {
|
|
|
|
throw 'Unkown action from worker: ' + data.action;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.send = function(actionName, data) {
|
2011-09-06 10:12:03 +09:00
|
|
|
try {
|
|
|
|
comObj.postMessage({
|
|
|
|
action: actionName,
|
|
|
|
data: data
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
console.error("FAILED to send data from", this.name);
|
|
|
|
throw e;
|
|
|
|
}
|
2011-09-06 09:42:58 +09:00
|
|
|
}
|
|
|
|
}
|