Skip to content

Commit

Permalink
Added pause/resume to renderer. When the renderer is paused, it will …
Browse files Browse the repository at this point in the history
…buffer up the results until resumed.
  • Loading branch information
RayMorgan committed Jan 29, 2010
1 parent 6f7b1a8 commit ee72f78
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 20 deletions.
15 changes: 12 additions & 3 deletions demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,21 @@ var ctx = {
in_ca: true
};

Mu.render('simple.html', ctx)
sys.puts("Outputting examples/simple.html.mu with a chunkSize of 10\n");

Mu.render('simple.html', ctx, {chunkSize: 10})
.addCallback(function (output) {
var buffer = '';

output.addListener('data', function (c) {buffer += c; })
.addListener('eof', function () { sys.puts(buffer); });
output
.addListener('data', function (c) {
sys.print(c); // output chunk
output.pause(); // pause for demo
setTimeout(function () { // wait 500ms and resume for demo
output.resume();
}, 500);
})
.addListener('eof', function () { sys.puts("\n\nDONE"); });
})
.addErrback(function (e) {
sys.puts(e.stack);
Expand Down
6 changes: 3 additions & 3 deletions lib/mu.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ Mu.compile = function Mu_compile(filename) {
* @returns {Promise} Returns a promise that will emit when the parse/compile/rendering
* is completed. Emits the final result.
*/
Mu.render = function Mu_render(filename, context) {
Mu.render = function Mu_render(filename, context, options) {
var promise = new process.Promise();

if (Mu.cache[filename]) {
process.nextTick(function () {
try {
promise.emit('success', Mu.cache[filename](context));
promise.emit('success', Mu.cache[filename](context, options));
} catch (e) {
promise.emit('error', e);
}
Expand All @@ -85,7 +85,7 @@ Mu.render = function Mu_render(filename, context) {
Mu.compile(filename)
.addCallback(function (compiled) {
try {
promise.emit('success', compiled(context));
promise.emit('success', compiled(context, options));
} catch (e) {
promise.emit('error', e);
}
Expand Down
59 changes: 45 additions & 14 deletions lib/mu/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,11 @@ exports.compilePartial = compilePartial;
function compile(parsed) {
var M = Mu;

var code = '(function COMPILED(context) {\n var result = "";\n';
code += 'var Mu = M;\n';
code += compilePartial(parsed);
code += '\n return result;\n})';

var code =
'(function COMPILED(context) {\n' +
'(function COMPILED(context, options) {\n' +
' options = options || {};\n' +
' var Mu = M;\n' +
' var REE = new RenderEventEmitter();\n' +
' var REE = new RenderEventEmitter(options);\n' +
' process.nextTick(function () {\n' +
compilePartial(parsed) + '\n' +
' REE.close();\n' +
Expand Down Expand Up @@ -67,24 +63,59 @@ function compilePartial(parsed) {
}


function RenderEventEmitter() {
function RenderEventEmitter(options) {
this.chunkSize = options.chunkSize || 1024;
this.buffer = '';
this.paused = false;
this.closed = false;
this.emittedEOF = false;
}
process.inherits(RenderEventEmitter, process.EventEmitter);

process.mixin(RenderEventEmitter.prototype, {
write: function (chunk) {
this.buffer += chunk;
if (this.buffer.length >= 1024) {
this.emit('data', this.buffer);
this.buffer = '';
if (this.closed) {
return;
}

this.buffer += chunk;
this.emitChunks();
},

close: function () {
if (this.buffer.length > 0) {
this.closed = true;
this.emitChunks();
},

pause: function () {
this.paused = true;
},

resume: function () {
this.paused = false;
this.emitChunks();
},

emitChunks: function () {
while (this.buffer.length >= this.chunkSize) {
if (this.paused) {
return;
}

var chunk = this.buffer.substring(0, this.chunkSize);
this.buffer = this.buffer.substring(this.chunkSize);

this.emit('data', chunk);
}

if (this.closed && !this.paused && this.buffer.length) {
this.emit('data', this.buffer);
this.buffer = '';
}

if (this.closed && !this.emittedEOF && !this.buffer.length) {
this.emittedEOF = true;
this.emit('eof');
}
this.emit('eof');
}
});

0 comments on commit ee72f78

Please sign in to comment.