add UIWorkerClient for debugging

This commit is contained in:
nightwing 2012-10-28 23:55:22 +04:00
commit e7339ac321
2 changed files with 72 additions and 28 deletions

View file

@ -36,8 +36,6 @@ var Mirror = require("../worker/mirror").Mirror;
var JSONParseTreeHandler = require("./xquery/JSONParseTreeHandler").JSONParseTreeHandler;
var XQueryParser = require("./xquery/XQueryParser").XQueryParser;
var SyntaxHighlighter = require("../mode/xquery/visitors/SyntaxHighlighter").SyntaxHighlighter;
window.addEventListener = function() {};
var XQueryWorker = exports.XQueryWorker = function(sender) {
Mirror.call(this, sender);

View file

@ -3,7 +3,7 @@
*
* Copyright (c) 2010, Ajax.org B.V.
* All rights reserved.
*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
@ -14,7 +14,7 @@
* * Neither the name of Ajax.org B.V. nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
@ -36,14 +36,15 @@ var EventEmitter = require("../lib/event_emitter").EventEmitter;
var config = require("../config");
var WorkerClient = function(topLevelNamespaces, mod, classname) {
this.changeListener = this.changeListener.bind(this);
this.onMessage = this.onMessage.bind(this);
this.onError = this.onError.bind(this);
var workerUrl;
if (config.get("packaged")) {
this.$worker = new Worker(config.moduleUrl(mod, "worker"));
}
else {
var workerUrl;
workerUrl = config.moduleUrl(mod, "worker");
} else {
var normalizePath = this.$normalizePath;
if (typeof require.supports !== "undefined" && require.supports.indexOf("ucjs2-pinf-0") >= 0) {
// We are running in the sourcemint loader.
workerUrl = require.nameToUrl("ace/worker/worker_sourcemint");
@ -52,19 +53,16 @@ var WorkerClient = function(topLevelNamespaces, mod, classname) {
// nameToUrl is renamed to toUrl in requirejs 2
if (require.nameToUrl && !require.toUrl)
require.toUrl = require.nameToUrl;
workerUrl = this.$normalizePath(require.toUrl("ace/worker/worker", null, "_"));
workerUrl = normalizePath(require.toUrl("ace/worker/worker", null, "_"));
}
this.$worker = new Worker(workerUrl);
var tlns = {};
for (var i=0; i<topLevelNamespaces.length; i++) {
var ns = topLevelNamespaces[i];
var path = this.$normalizePath(require.toUrl(ns, null, "_").replace(/.js(\?.*)?$/, ""));
tlns[ns] = path;
}
topLevelNamespaces.forEach(function(ns) {
tlns[ns] = normalizePath(require.toUrl(ns, null, "_").replace(/.js(\?.*)?$/, ""));
});
}
this.$worker = new Worker(workerUrl);
this.$worker.postMessage({
init : true,
tlns: tlns,
@ -75,12 +73,20 @@ var WorkerClient = function(topLevelNamespaces, mod, classname) {
this.callbackId = 1;
this.callbacks = {};
var _self = this;
this.$worker.onerror = function(e) {
this.$worker.onerror = this.onError;
this.$worker.onmessage = this.onMessage;
};
(function(){
oop.implement(this, EventEmitter);
this.onError = function(e) {
window.console && console.log && console.log(e);
throw e;
};
this.$worker.onmessage = function(e) {
this.onMessage = function(e) {
var msg = e.data;
switch(msg.type) {
case "log":
@ -88,23 +94,18 @@ var WorkerClient = function(topLevelNamespaces, mod, classname) {
break;
case "event":
_self._emit(msg.name, {data: msg.data});
this._emit(msg.name, {data: msg.data});
break;
case "call":
var callback = _self.callbacks[msg.id];
var callback = this.callbacks[msg.id];
if (callback) {
callback(msg.data);
delete _self.callbacks[msg.id];
delete this.callbacks[msg.id];
}
break;
}
};
};
(function(){
oop.implement(this, EventEmitter);
this.$normalizePath = function(path) {
if (!location.host) // needed for file:// protocol
@ -166,6 +167,51 @@ var WorkerClient = function(topLevelNamespaces, mod, classname) {
}).call(WorkerClient.prototype);
var UIWorkerClient = function(topLevelNamespaces, mod, classname) {
this.changeListener = this.changeListener.bind(this);
this.callbackId = 1;
this.callbacks = {};
this.messageBuffer = [];
var main = null;
var sender = Object.create(EventEmitter);
var _self = this;
this.$worker = {}
this.$worker.postMessage = function(e) {
_self.messageBuffer.push(e);
main && setTimeout(processNext);
};
var processNext = function() {
var msg = _self.messageBuffer.shift();
if (msg.command)
main[msg.command].apply(main, msg.args);
else if (msg.event)
sender._emit(msg.event, msg.data);
};
sender.postMessage = function(msg) {
_self.onMessage({data: msg});
};
sender.callback = function(data, callbackId) {
this.postMessage({type: "call", id: callbackId, data: data});
};
sender.emit = function(name, data) {
this.postMessage({type: "event", name: name, data: data});
};
require([mod], function(Main) {
main = new Main[classname](sender);
while (_self.messageBuffer.length)
processNext();
});
};
UIWorkerClient.prototype = WorkerClient.prototype;
exports.UIWorkerClient = UIWorkerClient;
exports.WorkerClient = WorkerClient;
});