From 9fc3f77b4ce980362e7780a97fc68cd20f4df334 Mon Sep 17 00:00:00 2001 From: Fabian Jakobs Date: Fri, 16 Apr 2010 15:36:21 +0200 Subject: [PATCH] Use events for the backgroundtokenizer --- src/BackgroundTokenizer.js | 23 ++++++++++++++--------- src/Editor.js | 15 +++++++++------ 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/src/BackgroundTokenizer.js b/src/BackgroundTokenizer.js index f2481584..ff681786 100644 --- a/src/BackgroundTokenizer.js +++ b/src/BackgroundTokenizer.js @@ -1,17 +1,12 @@ ace.provide("ace.BackgroundTokenizer"); -ace.BackgroundTokenizer = function(tokenizer, onUpdate, onComplete) { +ace.BackgroundTokenizer = function(tokenizer) { this.running = false; this.textLines = []; this.lines = []; this.currentLine = 0; this.tokenizer = tokenizer; - this.onUpdate = onUpdate || function(firstLine, lastLine) { - }; - this.onComplete = onComplete || function() { - }; - var self = this; this._worker = function() { if (!self.running) { return; } @@ -33,7 +28,7 @@ ace.BackgroundTokenizer = function(tokenizer, onUpdate, onComplete) { // only check every 30 lines processedLines += 1; if ((processedLines % 30 == 0) && (new Date() - workerStart) > 20) { - self.onUpdate(startLine, self.currentLine); + self.fireUpdateEvent(startLine, self.currentLine); return setTimeout(self._worker, 10); } @@ -42,10 +37,12 @@ ace.BackgroundTokenizer = function(tokenizer, onUpdate, onComplete) { self.running = false; - self.onUpdate(startLine, textLines.length - 1); - self.onComplete(); + self.fireUpdateEvent(startLine, textLines.length - 1); }; + + this.$initEvents(); }; +ace.mixin(ace.BackgroundTokenizer.prototype, ace.MEventEmitter); ace.BackgroundTokenizer.prototype.setTokenizer = function(tokenizer) { this.tokenizer = tokenizer; @@ -61,6 +58,14 @@ ace.BackgroundTokenizer.prototype.setLines = function(textLines) { this.stop(); }; +ace.BackgroundTokenizer.prototype.fireUpdateEvent = function(firstRow, lastRow) { + var data = { + first: firstRow, + last: lastRow + }; + this.$dispatchEvent("update", {data: data}); +}; + ace.BackgroundTokenizer.prototype.start = function(startRow) { this.currentLine = Math.min(startRow || 0, this.currentLine, this.textLines.length); diff --git a/src/Editor.js b/src/Editor.js index abee186f..72ef38a3 100644 --- a/src/Editor.js +++ b/src/Editor.js @@ -66,7 +66,8 @@ ace.Editor.prototype.setMode = function(mode) { if (!this.bgTokenizer) { var onUpdate = ace.bind(this.onTokenizerUpdate, this); - this.bgTokenizer = new ace.BackgroundTokenizer(tokenizer, onUpdate); + this.bgTokenizer = new ace.BackgroundTokenizer(tokenizer); + this.bgTokenizer.addEventListener("update", onUpdate); } else { this.bgTokenizer.setTokenizer(tokenizer); } @@ -126,13 +127,15 @@ ace.Editor.prototype.onBlur = function() { this.renderer.visualizeBlur(); }; -ace.Editor.prototype.onDocumentChange = function(startRow, endRow) { - this.bgTokenizer.start(startRow); - this.renderer.updateLines(startRow, endRow); +ace.Editor.prototype.onDocumentChange = function(e) { + var data = e.data; + this.bgTokenizer.start(data.startRow); + this.renderer.updateLines(data.startRow, data.endRow); }; -ace.Editor.prototype.onTokenizerUpdate = function(startRow, endRow) { - this.renderer.updateLines(startRow, endRow); +ace.Editor.prototype.onTokenizerUpdate = function(e) { + var rows = e.data; + this.renderer.updateLines(rows.first, rows.last); }; ace.Editor.prototype.onCursorChange = function() {