Skip to content

Commit

Permalink
Merge pull request #31 from zaphod1984/nextTick
Browse files Browse the repository at this point in the history
process.nextTick instead of setTimeout
  • Loading branch information
vybs committed May 21, 2012
2 parents a204699 + 90a8049 commit 4cdb119
Showing 1 changed file with 26 additions and 20 deletions.
46 changes: 26 additions & 20 deletions lib/dust.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,15 @@ if (Array.isArray) {
};
}

dust.nextTick = function(callback) {
setTimeout(callback, 0);
}
dust.nextTick = (function() {
if (process) {
return process.nextTick;
} else {
return function(callback) {
setTimeout(callback,0);
}
}
} )();

dust.isEmpty = function(value) {
if (dust.isArray(value) && !value.length) return true;
Expand Down Expand Up @@ -105,7 +111,7 @@ dust.filters = {
j: function(value) { return dust.escapeJs(value); },
u: encodeURI,
uc: encodeURIComponent
}
};

function Context(stack, global, blocks) {
this.stack = stack;
Expand All @@ -115,14 +121,14 @@ function Context(stack, global, blocks) {

dust.makeBase = function(global) {
return new Context(new Stack(), global);
}
};

Context.wrap = function(context) {
if (context instanceof Context) {
return context;
}
return new Context(new Stack(context));
}
};

Context.prototype.get = function(key) {
var ctx = this.stack, value;
Expand Down Expand Up @@ -181,7 +187,7 @@ Context.prototype.getBlock = function(key) {
fn = blocks[len][key];
if (fn) return fn;
}
}
};

Context.prototype.shiftBlocks = function(locals) {
var blocks = this.blocks;
Expand All @@ -195,7 +201,7 @@ Context.prototype.shiftBlocks = function(locals) {
return new Context(this.stack, this.global, newBlocks);
}
return this;
}
};

function Stack(head, tail, idx, len) {
this.tail = tail;
Expand Down Expand Up @@ -228,7 +234,7 @@ Stub.prototype.flush = function() {
this.head = chunk;
}
this.callback(null, this.out);
}
};

function Stream() {
this.head = new Chunk(this);
Expand All @@ -251,23 +257,23 @@ Stream.prototype.flush = function() {
this.head = chunk;
}
this.emit('end');
}
};

Stream.prototype.emit = function(type, data) {
var events = this.events;

if (events && events[type]) {
events[type](data);
}
}
};

Stream.prototype.on = function(type, callback) {
if (!this.events) {
this.events = {};
}
this.events[type] = callback;
return this;
}
};

function Chunk(root, next, taps) {
this.root = root;
Expand All @@ -285,7 +291,7 @@ Chunk.prototype.write = function(data) {
}
this.data += data;
return this;
}
};

Chunk.prototype.end = function(data) {
if (data) {
Expand All @@ -294,7 +300,7 @@ Chunk.prototype.end = function(data) {
this.flushable = true;
this.root.flush();
return this;
}
};

Chunk.prototype.map = function(callback) {
var cursor = new Chunk(this.root, this.next, this.taps),
Expand All @@ -304,7 +310,7 @@ Chunk.prototype.map = function(callback) {
this.flushable = true;
callback(branch);
return cursor;
}
};

Chunk.prototype.tap = function(tap) {
var taps = this.taps;
Expand All @@ -315,16 +321,16 @@ Chunk.prototype.tap = function(tap) {
this.taps = new Tap(tap);
}
return this;
}
};

Chunk.prototype.untap = function() {
this.taps = this.taps.tail;
return this;
}
};

Chunk.prototype.render = function(body, context) {
return body(this, context);
}
};

Chunk.prototype.reference = function(elem, context, auto, filters) {
if (typeof elem === "function") {
Expand Down Expand Up @@ -383,7 +389,7 @@ Chunk.prototype.exists = function(elem, context, bodies) {
return skip(this, context);
}
return this;
}
};

Chunk.prototype.notexists = function(elem, context, bodies) {
var body = bodies.block,
Expand All @@ -395,7 +401,7 @@ Chunk.prototype.notexists = function(elem, context, bodies) {
return skip(this, context);
}
return this;
}
};

Chunk.prototype.block = function(elem, context, bodies) {
var body = bodies.block;
Expand Down

0 comments on commit 4cdb119

Please sign in to comment.