velocityjs
Advanced tools
Comparing version 0.5.0-alpha.5 to 0.5.0-alpha.6
{ | ||
"name": "velocityjs", | ||
"description": "Velocity Template Language(VTL) for JavaScript", | ||
"version": "0.5.0-alpha.5", | ||
"version": "0.5.0-alpha.6", | ||
"keywords": [ | ||
@@ -6,0 +6,0 @@ "velocity template" |
@@ -1,3 +0,22 @@ | ||
module.exports = function(Velocity, utils){ | ||
'use strict'; | ||
module.exports = function(Velocity, utils) { | ||
function step(arr, i) { | ||
var result = ''; | ||
i = i || 0; | ||
if (!arr[i]) { | ||
return Promise.resolve(''); | ||
} | ||
return new Promise(function(resolve) { | ||
arr[i]().then(function(ret) { | ||
result += ret; | ||
step(arr, i + 1).then(function(ret) { | ||
result += ret; | ||
resolve(result); | ||
}); | ||
}); | ||
}); | ||
} | ||
/** | ||
@@ -7,3 +26,3 @@ * compile | ||
utils.mixin(Velocity.prototype, { | ||
init: function(){ | ||
init: function() { | ||
this.context = {}; | ||
@@ -24,3 +43,3 @@ this.macros = {}; | ||
*/ | ||
render: function(context, macros, silence){ | ||
render: function(context, macros, silence) { | ||
@@ -30,10 +49,15 @@ this.silence = !!silence; | ||
this.jsmacros = macros || {}; | ||
var t1 = utils.now(); | ||
var str = this._render(); | ||
var t2 = utils.now(); | ||
var cost = t2 - t1; | ||
this.cost = cost; | ||
if (!this.config.isAsync) { | ||
var t1 = utils.now(); | ||
var str = this._render(); | ||
var t2 = utils.now(); | ||
var cost = t2 - t1; | ||
return str; | ||
this.cost = cost; | ||
return str; | ||
} | ||
return this.asyncRender(this.asts); | ||
}, | ||
@@ -48,5 +72,4 @@ | ||
*/ | ||
_render: function(asts, contextId){ | ||
_render: function(asts, contextId) { | ||
var str = ''; | ||
asts = asts || this.asts; | ||
@@ -56,3 +79,3 @@ | ||
if (contextId !== this.condition && | ||
if (contextId !== this.condition && | ||
utils.indexOf(contextId, this.conditions) === -1) { | ||
@@ -68,33 +91,48 @@ this.conditions.unshift(contextId); | ||
utils.forEach(asts, function(ast){ | ||
return utils.map(asts, this.compute, this).join(''); | ||
}, | ||
switch(ast.type) { | ||
case 'references': | ||
str += this.getReferences(ast, true); | ||
break; | ||
asyncRender: function(asts) { | ||
return step(this.toPromises(asts)); | ||
}, | ||
case 'set': | ||
this.setValue(ast); | ||
break; | ||
compute: function(ast) { | ||
switch (ast.type) { | ||
case 'references': | ||
return this.getReferences(ast, true); | ||
case 'break': | ||
this.setBreak = true; | ||
break; | ||
case 'set': | ||
this.setValue(ast); | ||
return ''; | ||
case 'macro_call': | ||
str += this.getMacro(ast); | ||
break; | ||
case 'break': | ||
this.setBreak = true; | ||
return ''; | ||
case 'comment': | ||
break; | ||
case 'macro_call': | ||
return this.getMacro(ast); | ||
default: | ||
str += typeof ast == 'string' ? ast : this.getBlock(ast); | ||
break; | ||
} | ||
}, this); | ||
case 'comment': | ||
return ''; | ||
return str; | ||
default: | ||
return typeof ast === 'string' ? ast : this.getBlock(ast); | ||
} | ||
}, | ||
toPromises: function(asts) { | ||
var self = this; | ||
return utils.map(asts, function(ast) { | ||
return function() { | ||
var ret = self.compute(ast); | ||
if (typeof ret === 'string') { | ||
return Promise.resolve(ret); | ||
} | ||
// 否则是promise对象 | ||
return ret; | ||
}; | ||
}); | ||
} | ||
}); | ||
}; |
@@ -0,1 +1,2 @@ | ||
'use strict'; | ||
var utils = require('../utils'); | ||
@@ -9,3 +10,5 @@ var Helper = require('../helper/index'); | ||
// 不需要转义的白名单 | ||
unescape: {} | ||
unescape: {}, | ||
// 是否支持异步宏 | ||
isAsync: false | ||
}; | ||
@@ -12,0 +15,0 @@ utils.mixin(this.config, config); |
@@ -167,2 +167,21 @@ var Velocity = require('../src/velocity') | ||
}) | ||
it('async render support', function(done) { | ||
var vm = 'abc#parse("hello")11' | ||
var macros = { | ||
parse: function(word) { | ||
return new Promise(function(resolve) { | ||
setTimeout(function() { | ||
resolve(word); | ||
}, 30); | ||
}); | ||
} | ||
}; | ||
var compile = new Compile(Parser.parse(vm), { isAsync: true }) | ||
compile.render({}, macros).then(function(ret) { | ||
assert.equal(ret, 'abchello11'); | ||
done(); | ||
}); | ||
}) | ||
}) | ||
@@ -771,3 +790,4 @@ | ||
}) | ||
}) | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1650364
14097