mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-15 20:30:39 -07:00
pref: remove await/async from internal async calls
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { assert } from '../util/assert'
|
||||
import { isRange, rangeValue, rangeValueSync } from './range'
|
||||
import { isRange, rangeValue } from './range'
|
||||
import { Value } from './value'
|
||||
import { Context } from '../context/context'
|
||||
import { toValue } from '../util/underscore'
|
||||
@@ -12,38 +12,20 @@ export class Expression {
|
||||
public constructor (str = '') {
|
||||
this.postfix = [...toPostfix(str)]
|
||||
}
|
||||
public async evaluate (ctx: Context): Promise<any> {
|
||||
public * evaluate (ctx: Context) {
|
||||
assert(ctx, 'unable to evaluate: context not defined')
|
||||
|
||||
for (const token of this.postfix) {
|
||||
if (isOperator(token)) {
|
||||
this.evaluateOnce(token)
|
||||
} else if (isRange(token)) {
|
||||
this.operands.push(await rangeValue(token, ctx))
|
||||
} else this.operands.push(await new Value(token).evaluate(ctx))
|
||||
this.operands.push(yield rangeValue(token, ctx))
|
||||
} else this.operands.push(yield new Value(token).evaluate(ctx))
|
||||
}
|
||||
return this.operands[0]
|
||||
}
|
||||
public evaluateSync (ctx: Context): any {
|
||||
assert(ctx, 'unable to evaluate: context not defined')
|
||||
|
||||
for (const token of this.postfix) {
|
||||
if (isOperator(token)) {
|
||||
this.evaluateOnce(token)
|
||||
} else if (isRange(token)) {
|
||||
this.operands.push(rangeValueSync(token, ctx))
|
||||
} else {
|
||||
const val = new Value(token).evaluateSync(ctx)
|
||||
this.operands.push(val)
|
||||
}
|
||||
}
|
||||
return this.operands[0]
|
||||
}
|
||||
public async value (ctx: Context): Promise<any> {
|
||||
return toValue(await this.evaluate(ctx))
|
||||
}
|
||||
public valueSync (ctx: Context): any {
|
||||
return toValue(this.evaluateSync(ctx))
|
||||
public * value (ctx: Context) {
|
||||
return toValue(yield this.evaluate(ctx))
|
||||
}
|
||||
private evaluateOnce (token: string) {
|
||||
const r = this.operands.pop()
|
||||
|
||||
+3
-12
@@ -7,20 +7,11 @@ export function isRange (token: string) {
|
||||
return token[0] === '(' && token[token.length - 1] === ')'
|
||||
}
|
||||
|
||||
export async function rangeValue (token: string, ctx: Context) {
|
||||
export function * rangeValue (token: string, ctx: Context) {
|
||||
let match
|
||||
if ((match = token.match(rangeLine))) {
|
||||
const low = await new Value(match[1]).value(ctx)
|
||||
const high = await new Value(match[2]).value(ctx)
|
||||
return range(+low, +high + 1)
|
||||
}
|
||||
}
|
||||
|
||||
export function rangeValueSync (token: string, ctx: Context) {
|
||||
let match
|
||||
if ((match = token.match(rangeLine))) {
|
||||
const low = new Value(match[1]).valueSync(ctx)
|
||||
const high = new Value(match[2]).valueSync(ctx)
|
||||
const low = yield new Value(match[1]).value(ctx)
|
||||
const high = yield new Value(match[2]).value(ctx)
|
||||
return range(+low, +high + 1)
|
||||
}
|
||||
}
|
||||
|
||||
+4
-15
@@ -4,26 +4,15 @@ import { ITemplate } from '../template/itemplate'
|
||||
import { Emitter } from './emitter'
|
||||
|
||||
export class Render {
|
||||
public async renderTemplates (templates: ITemplate[], ctx: Context, emitter = new Emitter()): Promise<string> {
|
||||
public * renderTemplates (templates: ITemplate[], ctx: Context, emitter = new Emitter()): IterableIterator<string> {
|
||||
for (const tpl of templates) {
|
||||
try {
|
||||
const html = await tpl.render(ctx, emitter)
|
||||
const html = yield tpl.render(ctx, emitter)
|
||||
html && emitter.write(html)
|
||||
if (emitter.break || emitter.continue) break
|
||||
} catch (e) {
|
||||
throw RenderError.is(e) ? e : new RenderError(e, tpl)
|
||||
}
|
||||
}
|
||||
return emitter.html
|
||||
}
|
||||
public renderTemplatesSync (templates: ITemplate[], ctx: Context, emitter = new Emitter()): string {
|
||||
for (const tpl of templates) {
|
||||
try {
|
||||
const html = tpl.renderSync(ctx, emitter)
|
||||
html && !(html instanceof Promise) && emitter.write(html)
|
||||
if (emitter.break || emitter.continue) break
|
||||
} catch (e) {
|
||||
throw RenderError.is(e) ? e : new RenderError(e, tpl)
|
||||
const err = RenderError.is(e) ? e : new RenderError(e, tpl)
|
||||
throw err
|
||||
}
|
||||
}
|
||||
return emitter.html
|
||||
|
||||
+3
-11
@@ -9,11 +9,7 @@ export class Value {
|
||||
this.str = str
|
||||
}
|
||||
|
||||
public async evaluate (ctx: Context) {
|
||||
return this.evaluateSync(ctx)
|
||||
}
|
||||
|
||||
public evaluateSync (ctx: Context) {
|
||||
public evaluate (ctx: Context) {
|
||||
const literalValue = parseLiteral(this.str)
|
||||
if (literalValue !== undefined) {
|
||||
return literalValue
|
||||
@@ -21,11 +17,7 @@ export class Value {
|
||||
return ctx.get(this.str)
|
||||
}
|
||||
|
||||
public async value (ctx: Context) {
|
||||
return toValue(await this.evaluate(ctx))
|
||||
}
|
||||
|
||||
public valueSync (ctx: Context) {
|
||||
return toValue(this.evaluateSync(ctx))
|
||||
public value (ctx: Context) {
|
||||
return toValue(this.evaluate(ctx))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user