improve error handling in worker

This commit is contained in:
nightwing 2014-10-05 22:01:01 +04:00
commit 6c0bc56500
2 changed files with 27 additions and 10 deletions

View file

@ -17,7 +17,13 @@ window.window = window;
window.ace = window;
window.onerror = function(message, file, line, col, err) {
console.error("Worker " + (err ? err.stack : message));
postMessage({type: "error", data: {
message: message,
file: file,
line: line,
col: col,
stack: err.stack
}});
};
window.normalizeModule = function(parentId, moduleName) {
@ -84,15 +90,20 @@ window.define = function(id, deps, factory) {
deps = [];
id = window.require.id;
}
if (typeof factory != "function") {
window.require.modules[id] = {
exports: factory,
initialized: true
};
return;
}
if (!deps.length)
// If there is no dependencies, we inject 'require', 'exports' and
// 'module' as dependencies, to provide CommonJS compatibility.
deps = ['require', 'exports', 'module'];
if (id.indexOf("text!") === 0)
return;
var req = function(childId) {
return window.require(id, childId);
};

View file

@ -92,14 +92,9 @@ var WorkerClient = function(topLevelNamespaces, mod, classname, workerUrl) {
this.onMessage = function(e) {
var msg = e.data;
switch(msg.type) {
case "log":
window.console && console.log && console.log.apply(console, msg.data);
break;
case "event":
this._signal(msg.name, {data: msg.data});
break;
case "call":
var callback = this.callbacks[msg.id];
if (callback) {
@ -107,8 +102,18 @@ var WorkerClient = function(topLevelNamespaces, mod, classname, workerUrl) {
delete this.callbacks[msg.id];
}
break;
case "error":
this.reportError(msg.data);
break;
case "log":
window.console && console.log && console.log.apply(console, msg.data);
break;
}
};
this.reportError = function(err) {
window.console && console.error && console.error(err);
};
this.$normalizePath = function(path) {
return net.qualifyURL(path);
@ -119,7 +124,8 @@ var WorkerClient = function(topLevelNamespaces, mod, classname, workerUrl) {
this.deltaQueue = null;
this.$worker.terminate();
this.$worker = null;
this.$doc.removeEventListener("change", this.changeListener);
if (this.$doc)
this.$doc.off("change", this.changeListener);
this.$doc = null;
};