feat: promise support for drops, working on #65

This commit is contained in:
Jun Yang
2019-03-10 18:05:52 +08:00
parent ad0930f152
commit 4a8088d4e4
25 changed files with 260 additions and 250 deletions
+7 -3
View File
@@ -19,9 +19,13 @@ export class Filter {
this.impl = impl || (x => x)
this.args = args
}
render (value: any, scope: Scope): any {
const args = this.args.map(arg => isArray(arg) ? [arg[0], evalValue(arg[1], scope)] : evalValue(arg, scope))
return this.impl.apply(null, [value, ...args])
async render (value: any, scope: Scope) {
const argv: any[] = []
for(let arg of this.args) {
if (isArray(arg)) argv.push([arg[0], await evalValue(arg[1], scope)])
else argv.push(await evalValue(arg, scope))
}
return this.impl.apply(null, [value, ...argv])
}
static register (name: string, filter: FilterImpl) {
Filter.impls[name] = filter
+4 -2
View File
@@ -10,13 +10,15 @@ import Scope from '../../scope/scope'
*/
export default class Hash {
[key: string]: any
constructor (markup: string, scope: Scope) {
static async create (markup: string, scope: Scope) {
const instance = new Hash()
let match
hashCapture.lastIndex = 0
while ((match = hashCapture.exec(markup))) {
const k = match[1]
const v = match[2]
this[k] = evalValue(v, scope)
instance[k] = await evalValue(v, scope)
}
return instance
}
}
+1 -1
View File
@@ -28,7 +28,7 @@ export default class Tag extends Template<TagToken> implements ITemplate {
}
}
async render (scope: Scope) {
const hash = new Hash(this.token.args, scope)
const hash = await Hash.create(this.token.args, scope)
const impl = this.impl
if (typeof impl.render !== 'function') {
return ''
+6 -4
View File
@@ -47,10 +47,12 @@ export default class Value {
}
this.filters.push(new Filter(name, args, this.strictFilters))
}
value (scope: Scope) {
return this.filters.reduce(
(prev, filter) => filter.render(prev, scope),
evalExp(this.initial, scope))
async value (scope: Scope) {
let val = await evalExp(this.initial, scope)
for (let filter of this.filters) {
val = await filter.render(val, scope)
}
return val
}
static tokenize (str: string): Array<'|' | ',' | ':' | string> {
const tokens = []