async-vdom-builder
Advanced tools
Comparing version 1.0.1 to 1.0.2
{ | ||
"name": "async-vdom-builder", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -16,7 +16,8 @@ var EventEmitter = require('events').EventEmitter; | ||
this.finished = false; | ||
this.last = undefined; | ||
this.lastFired = false; | ||
this.lastCount = 0; | ||
} | ||
function AsyncVDOMBuilder(globalData, rootNode, state) { | ||
var parentNode = rootNode; | ||
function AsyncVDOMBuilder(globalData, parentNode, state) { | ||
if (!parentNode) { | ||
@@ -26,14 +27,13 @@ parentNode = createDocumentFragment(); | ||
var finalState = state; | ||
if (finalState) { | ||
finalState.remaining++; | ||
if (state) { | ||
state.remaining++; | ||
} else { | ||
finalState = new State(parentNode); | ||
state = new State(parentNode); | ||
} | ||
this._state = finalState; | ||
this._state = state; | ||
this._parent = parentNode; | ||
this.global = globalData || {}; | ||
this._stack = [parentNode]; | ||
this._sync = false; | ||
} | ||
@@ -44,4 +44,4 @@ | ||
var proto = AsyncVDOMBuilder.prototype = { | ||
element: function(name, attr, childCount) { | ||
var element = createElement(name, attr, childCount); | ||
element: function(name, attrs, childCount) { | ||
var element = createElement(name, attrs, childCount); | ||
@@ -54,3 +54,3 @@ var parent = this._parent; | ||
return childCount > 0 ? element : this; | ||
return childCount === 0 ? this : element; | ||
}, | ||
@@ -149,3 +149,12 @@ | ||
this._parent = null; | ||
if(!--state.remaining) { | ||
var remaining = --state.remaining; | ||
if (!state.lastFired && (remaining - state.lastCount === 0)) { | ||
state.lastFired = true; | ||
state.lastCount = 0; | ||
state.events.emit('last'); | ||
} | ||
if (!remaining) { | ||
state.finished = true; | ||
@@ -156,11 +165,23 @@ state.events.emit('finish', state.tree); | ||
beginAsync: function() { | ||
if (this._isSync) { | ||
beginAsync: function(options) { | ||
if (this._sync) { | ||
throw new Error('beginAsync() not allowed when using renderSync()'); | ||
} | ||
var state = this._state; | ||
if (options) { | ||
if (options.last === true) { | ||
state.lastCount++; | ||
} | ||
} | ||
var documentFragment = this._parent.appendDocumentFragment(); | ||
return new AsyncVDOMBuilder(this.global, documentFragment, this._state); | ||
return new AsyncVDOMBuilder(this.global, documentFragment, state); | ||
}, | ||
createOut: function(callback) { | ||
return new AsyncVDOMBuilder(this.global); | ||
}, | ||
flush: function() { | ||
@@ -228,6 +249,34 @@ var state = this._state; | ||
sync: function() { | ||
this._isSync = true; | ||
this._sync = true; | ||
}, | ||
document: typeof document !== 'undefined' && document | ||
onLast: function(callback) { | ||
var state = this._state; | ||
var lastArray = state.last; | ||
if (!lastArray) { | ||
lastArray = state.last = []; | ||
var i = 0; | ||
var next = function next() { | ||
if (i === lastArray.length) { | ||
return; | ||
} | ||
var _next = lastArray[i++]; | ||
_next(next); | ||
}; | ||
this.once('last', function() { | ||
next(); | ||
}); | ||
} | ||
lastArray.push(callback); | ||
return this; | ||
}, | ||
document: typeof document !== 'undefined' && document, | ||
isVDOM: true | ||
}; | ||
@@ -234,0 +283,0 @@ |
12323
319