liquidjs icon indicating copy to clipboard operation
liquidjs copied to clipboard

Promise support in expressions

Open almousa1990 opened this issue 4 years ago • 2 comments

Hi,

This library support promises in context, however, this support seems to be missing when evaluating an expression

engine
    .parseAndRender('{%if name== blank %}it is promise, not blank!{%endif%}', {name: Promise.resolve('alice')})
    .then(console.log);

almousa1990 avatar Nov 29 '20 18:11 almousa1990

Was able to resolve the issue by modifying Expression class, i am not sure if i am correct (yields and generators are new concepts that i am not familiar with). Anyway, this is my slightly modified version of the class:

export class Expression {
  private operands: any[] = []
  private postfix: IterableIterator<Token>

  public constructor (str: string) {
    const tokenizer = new Tokenizer(str)
    this.postfix = toPostfix(tokenizer.readExpression())
  }
  public * evaluate (ctx: Context): any {
    for (const token of this.postfix) {
      if (TypeGuards.isOperatorToken(token)) {
        const r = yield this.operands.pop()
        const l = yield this.operands.pop()
        const result = evalOperatorToken(token, l, r, ctx)
        this.operands.push(result)
      } else {
        this.operands.push(evalToken(token, ctx))
      }
    }
    return this.operands[0]
  }
  public * value (ctx: Context) {
    return toValue(yield this.evaluate(ctx))
  }
}

almousa1990 avatar Nov 29 '20 20:11 almousa1990

This feature need substantial effort (I tried this too), essentially will change all expression evaluation to async. Also prone to performance issues.

Will leave this issue open to see how many people want this. Feel free to LIKE this.

harttle avatar Sep 30 '21 14:09 harttle