Permalink
Browse files

Apply change suggested by Suor in #4 that will ensure more efficient …

…template execution.
  • Loading branch information...
1 parent 011d0a0 commit 16563dcd5333a21fedcd2c8299501840056feefb Thomas Lee committed Jan 19, 2011
Showing with 11 additions and 2 deletions.
  1. +11 −2 lib/jazz/compiler.js
View
@@ -81,18 +81,26 @@ Compiler_JS.prototype._prologue = function(globals) {
//
code += "var __jazz_output = [];\n";
+ //
+ // Set to true when the template has been executed. We may still be waiting
+ // on async calls (see __jazz_check_complete()).
+ //
+ code += "var __jazz_ready = false;\n";
+
//
// This function is used to detect when any background process might have
// completed. May be inefficient?
//
code += "function __jazz_check_complete() {\n";
- code += " if (__jazz_expected == __jazz_seen) __jazz_cb(__jazz_output.join(''));\n";
- code += " else process.nextTick(__jazz_check_complete);\n";
+ code += " if (__jazz_ready && __jazz_expected == __jazz_seen) {\n";
+ code += " __jazz_cb(__jazz_output.join(''));\n";
+ code += " }\n";
code += "}\n";
code += "__jazz_emitter.addListener('data', function(pos, chunk) {\n";
code += " __jazz_output[pos] = chunk;\n";
code += " __jazz_seen++;\n";
+ code += " __jazz_check_complete();\n";
code += "});\n";
for (var i = 0; i < globals.length; i++) {
@@ -104,6 +112,7 @@ Compiler_JS.prototype._prologue = function(globals) {
Compiler_JS.prototype._epilogue = function() {
var code = "";
+ code += "__jazz_ready = true;\n";
code += "__jazz_check_complete();\n";
code += "})";
return code;

0 comments on commit 16563dc

Please sign in to comment.