add old worker experiments
This commit is contained in:
parent
7644522783
commit
3cedb3bef6
4 changed files with 290 additions and 0 deletions
33
experiments/worker.html
Normal file
33
experiments/worker.html
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
|
||||
"http://www.w3.org/TR/html4/strict.dtd">
|
||||
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
||||
<title>worker</title>
|
||||
<meta name="generator" content="TextMate http://macromates.com/">
|
||||
<meta name="author" content="Fabian Jakobs">
|
||||
<!-- Date: 2010-04-08 -->
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<script type="text/javascript" charset="utf-8">
|
||||
|
||||
var worker = new Worker("worker.js");
|
||||
|
||||
worker.onmessage = function(e) {
|
||||
console.log(e.data);
|
||||
};
|
||||
|
||||
worker.onerror = function(e) {
|
||||
console.log(e.data);
|
||||
};
|
||||
|
||||
worker.postMessage("postMessage('juhu')");
|
||||
worker.postMessage("postMessage('juhu')");
|
||||
|
||||
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
3
experiments/worker.js
Normal file
3
experiments/worker.js
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
onmessage = function(e) {
|
||||
onmessage = new Function("e", e.data);
|
||||
};
|
||||
91
lib/ace/WorkerTokenizer.js
Normal file
91
lib/ace/WorkerTokenizer.js
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
/**
|
||||
* Ajax.org Code Editor (ACE)
|
||||
*
|
||||
* @copyright 2010, Ajax.org Services B.V.
|
||||
* @license LGPLv3 <http://www.gnu.org/licenses/lgpl-3.0.txt>
|
||||
* @author Fabian Jakobs <fabian AT ajax DOT org>
|
||||
*/
|
||||
require.def("ace/WorkerTokenizer", ["ace/lib/oop", "ace/MEventEmitter"], function(oop, MEventEmitter) {
|
||||
|
||||
var WorkerTokenizer = function(tokenizer) {
|
||||
var workerUrl = require.nameToUrl("ace/worker", null, "_");
|
||||
this.$worker = new Worker(workerUrl);
|
||||
|
||||
this.callbackId = 1;
|
||||
this.callbacks = {};
|
||||
|
||||
var _self = this;
|
||||
this.$worker.onerror = function(e) {
|
||||
throw e;
|
||||
};
|
||||
this.$worker.onmessage = function(e) {
|
||||
var msg = e.data;
|
||||
//console.log("rec", msg)
|
||||
switch(msg.type) {
|
||||
case "log":
|
||||
console.log(msg.data);
|
||||
break;
|
||||
|
||||
case "event":
|
||||
_self.$dispatchEvent(msg.name, {data: msg.data});
|
||||
break;
|
||||
|
||||
case "call":
|
||||
var callback = _self.callbacks[msg.id];
|
||||
if (callback) {
|
||||
callback(msg.data);
|
||||
delete _self.callbacks[msg.id];
|
||||
}
|
||||
break;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
(function(){
|
||||
|
||||
oop.implement(this, MEventEmitter);
|
||||
|
||||
this.$send = function(cmd, args) {
|
||||
this.$worker.postMessage({command: cmd, args: args});
|
||||
};
|
||||
|
||||
this.$call = function(cmd, args, callback) {
|
||||
var id = this.callbackId++;
|
||||
this.callbacks[id] = callback;
|
||||
args.push(id);
|
||||
this.$send(cmd, args);
|
||||
};
|
||||
|
||||
this.setTokenizer = function(tokenizer) {
|
||||
this.$send("setRules", ["ace/mode/JavaScriptHighlightRules"]);
|
||||
};
|
||||
|
||||
this.setLines = function(textLines) {
|
||||
this.lines = textLines;
|
||||
this.$send("setLines", [textLines]);
|
||||
};
|
||||
|
||||
this.start = function(startRow) {
|
||||
// TODO don't send all lines on each update!!
|
||||
this.$send("setLines", [this.lines]);
|
||||
this.$send("start", [startRow])
|
||||
};
|
||||
|
||||
this.stop = function() {
|
||||
this.$send("stop", [])
|
||||
};
|
||||
|
||||
this.getTokens = function(firstRow, lastRow, callback) {
|
||||
this.$call("getTokens", [firstRow, lastRow], function(tokens) {
|
||||
callback(tokens)
|
||||
});
|
||||
};
|
||||
|
||||
this.getState = function(row, callback) {
|
||||
this.$call("getState", [row], callback);
|
||||
};
|
||||
|
||||
}).call(WorkerTokenizer.prototype);
|
||||
|
||||
return WorkerTokenizer;
|
||||
});
|
||||
163
lib/ace/worker.js
Normal file
163
lib/ace/worker.js
Normal file
|
|
@ -0,0 +1,163 @@
|
|||
postMessage("Juhu Kinners");
|
||||
|
||||
var console = {
|
||||
log: function(msg) {
|
||||
postMessage({type: "log", data: msg});
|
||||
}
|
||||
};
|
||||
var window = {
|
||||
console: console
|
||||
};
|
||||
|
||||
var require = function(name) {
|
||||
if (require.modules[name])
|
||||
return require.modules[name];
|
||||
|
||||
importScripts(require.baseUrl + "/" + name + ".js");
|
||||
return require.modules[name];
|
||||
};
|
||||
|
||||
require.def = function(name, deps, callback) {
|
||||
if (!callback) {
|
||||
callback = deps;
|
||||
deps = [];
|
||||
}
|
||||
var modules = deps.map(function(dep) {
|
||||
return require(dep);
|
||||
});
|
||||
require.modules[name] = callback.apply(this, modules);
|
||||
};
|
||||
|
||||
require.baseUrl = "..";
|
||||
require.modules = {};
|
||||
|
||||
var Tokenizer = require("ace/Tokenizer");
|
||||
|
||||
var bgtokenizer = {
|
||||
running: false,
|
||||
textLines: [],
|
||||
lines: [],
|
||||
currentLine: 0,
|
||||
tokenizer: null,
|
||||
|
||||
init: function() {
|
||||
var self = this;
|
||||
|
||||
this.$worker = function() {
|
||||
if (!self.running) { return; }
|
||||
|
||||
var workerStart = new Date();
|
||||
var startLine = self.currentLine;
|
||||
var textLines = self.textLines;
|
||||
|
||||
var processedLines = 0;
|
||||
|
||||
while (self.currentLine < textLines.length) {
|
||||
self.lines[self.currentLine] = self.$tokenizeRows(self.currentLine, self.currentLine)[0];
|
||||
self.currentLine++;
|
||||
|
||||
// only check every 5 lines
|
||||
processedLines += 1;
|
||||
if ((processedLines % 5 == 0) && (new Date() - workerStart) > 40) {
|
||||
self.$event("update", {first: startLine, last: self.currentLine-1});
|
||||
self.running = setTimeout(self.$worker, 0);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
self.running = false;
|
||||
self.$event("update", {first: startLine, last: textLines.length - 1});
|
||||
};
|
||||
},
|
||||
|
||||
setRules: function(rules) {
|
||||
var Rules = require(rules);
|
||||
this.tokenizer = new Tokenizer(new Rules().getRules());
|
||||
this.lines = [];
|
||||
|
||||
this.start(0);
|
||||
},
|
||||
|
||||
setLines: function(textLines) {
|
||||
this.textLines = textLines;
|
||||
this.lines = [];
|
||||
|
||||
this.stop();
|
||||
},
|
||||
|
||||
start: function(startRow) {
|
||||
this.currentLine = Math.min(startRow || 0, this.currentLine,
|
||||
this.textLines.length);
|
||||
|
||||
// remove all cached items below this line
|
||||
this.lines.splice(this.currentLine, this.lines.length);
|
||||
|
||||
this.stop();
|
||||
this.running = setTimeout(this.$worker, 0);
|
||||
},
|
||||
|
||||
stop: function() {
|
||||
if (this.running)
|
||||
clearTimeout(this.running);
|
||||
|
||||
this.running = false;
|
||||
},
|
||||
|
||||
getTokens: function(firstRow, lastRow, callbackId) {
|
||||
this.$callback(this.$tokenizeRows(firstRow, lastRow), callbackId);
|
||||
},
|
||||
|
||||
getState: function(row, callbackId) {
|
||||
this.$callback(this.$tokenizeRows(row, row)[0].state, callbackId);
|
||||
},
|
||||
|
||||
$tokenizeRows: function(firstRow, lastRow) {
|
||||
var rows = [];
|
||||
|
||||
// determin start state
|
||||
var state = "start";
|
||||
var doCache = false;
|
||||
if (firstRow > 0 && this.lines[firstRow - 1]) {
|
||||
state = this.lines[firstRow - 1].state;
|
||||
doCache = true;
|
||||
}
|
||||
|
||||
for (var row=firstRow; row<=lastRow; row++) {
|
||||
if (!this.lines[row]) {
|
||||
var tokens = this.tokenizer.getLineTokens(this.textLines[row] || "", state);
|
||||
var state = tokens.state;
|
||||
rows.push(tokens);
|
||||
|
||||
if (doCache) {
|
||||
this.lines[row] = tokens;
|
||||
}
|
||||
}
|
||||
else
|
||||
rows.push(this.lines[row]);
|
||||
}
|
||||
return rows;
|
||||
},
|
||||
|
||||
$callback: function(data, callbackId) {
|
||||
postMessage({
|
||||
type: "call",
|
||||
id: callbackId,
|
||||
data: data
|
||||
});
|
||||
},
|
||||
|
||||
$event: function(name, data) {
|
||||
postMessage({
|
||||
type: "event",
|
||||
name: name,
|
||||
data: data
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
bgtokenizer.init();
|
||||
|
||||
var onmessage = function(e) {
|
||||
var msg = e.data;
|
||||
bgtokenizer[msg.command].apply(bgtokenizer, msg.args);
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue