pref: remove await/async from internal async calls

This commit is contained in:
harttle
2019-10-26 08:57:33 -05:00
committed by Jun Yang
parent d4ad4b8a8c
commit b82fa9ed2b
40 changed files with 416 additions and 380 deletions
+3 -11
View File
@@ -21,19 +21,11 @@ export class Filter {
this.impl = impl || (x => x)
this.args = args
}
public async render (value: any, context: Context) {
public * render (value: any, context: Context) {
const argv: any[] = []
for (const arg of this.args) {
if (isKeyValuePair(arg)) argv.push([arg[0], await new Expression(arg[1]).evaluate(context)])
else argv.push(await new Expression(arg).evaluate(context))
}
return this.impl.apply({ context }, [value, ...argv])
}
public renderSync (value: any, context: Context) {
const argv: any[] = []
for (const arg of this.args) {
if (isKeyValuePair(arg)) argv.push([arg[0], new Expression(arg[1]).evaluateSync(context)])
else argv.push(new Expression(arg).evaluateSync(context))
if (isKeyValuePair(arg)) argv.push([arg[0], yield new Expression(arg[1]).evaluate(context)])
else argv.push(yield new Expression(arg).evaluate(context))
}
return this.impl.apply({ context }, [value, ...argv])
}
+1 -4
View File
@@ -10,10 +10,7 @@ export class HTML extends Template<HTMLToken> implements ITemplate {
super(token)
this.str = token.value
}
public renderSync (ctx: Context, emitter: Emitter) {
public * render (ctx: Context, emitter: Emitter): IterableIterator<void> {
emitter.write(this.str)
}
public async render (ctx: Context, emitter: Emitter) {
this.renderSync(ctx, emitter)
}
}
+1 -2
View File
@@ -4,6 +4,5 @@ import { Emitter } from '../render/emitter'
export interface ITemplate {
token: Token;
render(ctx: Context, emitter: Emitter): Promise<any>;
renderSync(ctx: Context, emitter: Emitter): any;
render(ctx: Context, emitter: Emitter): any;
}
+2 -6
View File
@@ -12,12 +12,8 @@ export class Output extends Template<OutputToken> implements ITemplate {
super(token)
this.value = new Value(token.value, strictFilters)
}
public renderSync (ctx: Context, emitter: Emitter) {
const val = this.value.valueSync(ctx)
emitter.write(stringify(toValue(val)))
}
public async render (ctx: Context, emitter: Emitter) {
const val = await this.value.value(ctx)
public * render (ctx: Context, emitter: Emitter) {
const val = yield this.value.value(ctx)
emitter.write(stringify(toValue(val)))
}
}
+2 -9
View File
@@ -21,17 +21,10 @@ export class Hash {
}
return instance
}
public static createSync (markup: string, ctx: Context) {
public static * create (markup: string, ctx: Context) {
const instance = Hash.parse(markup)
for (const key of Object.keys(instance)) {
instance[key] = new Expression(instance[key]).evaluateSync(ctx)
}
return instance
}
public static async create (markup: string, ctx: Context) {
const instance = Hash.parse(markup)
for (const key of Object.keys(instance)) {
instance[key] = await new Expression(instance[key]).evaluate(ctx)
instance[key] = yield new Expression(instance[key]).evaluate(ctx)
}
return instance
}
+1 -2
View File
@@ -7,6 +7,5 @@ import { Emitter } from '../../render/emitter'
export interface ITagImplOptions {
parse?: (this: ITagImpl, token: TagToken, remainingTokens: Token[]) => void;
render: (this: ITagImpl, ctx: Context, hash: Hash, emitter: Emitter) => void;
renderSync?: (this: ITagImpl, ctx: Context, hash: Hash, emitter: Emitter) => void;
render: (this: ITagImpl, ctx: Context, hash: Hash, emitter: Emitter) => any;
}
+3 -9
View File
@@ -23,16 +23,10 @@ export class Tag extends Template<TagToken> implements ITemplate {
this.impl.parse(token, tokens)
}
}
public renderSync (ctx: Context, emitter: Emitter) {
const hash = Hash.createSync(this.token.args, ctx)
public * render (ctx: Context, emitter: Emitter) {
const hash = yield Hash.create(this.token.args, ctx)
const impl = this.impl
if (isFunction(impl.renderSync)) return impl.renderSync(ctx, hash, emitter)
if (isFunction(impl.render)) return impl.render(ctx, hash, emitter)
}
public async render (ctx: Context, emitter: Emitter) {
const hash = await Hash.create(this.token.args, ctx)
const impl = this.impl
if (isFunction(impl.render)) return impl.render(ctx, hash, emitter)
if (isFunction(impl.render)) return yield impl.render(ctx, hash, emitter)
}
public static register (name: string, tag: ITagImplOptions) {
Tag.impls[name] = tag
+3 -10
View File
@@ -47,17 +47,10 @@ export class Value {
}
this.filters.push(new Filter(name, args, this.strictFilters))
}
public async value (ctx: Context) {
let val = await new Expression(this.initial).evaluate(ctx)
public * value (ctx: Context) {
let val = yield new Expression(this.initial).evaluate(ctx)
for (const filter of this.filters) {
val = await filter.render(val, ctx)
}
return val
}
public valueSync (ctx: Context) {
let val = new Expression(this.initial).evaluateSync(ctx)
for (const filter of this.filters) {
val = filter.renderSync(val, ctx)
val = yield filter.render(val, ctx)
}
return val
}