UNPKG

@b-side/parser

Version:

HTML Parser used to parse HTML string and bind data to it.

81 lines 2.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Expression = void 0; const side_effects_1 = require("./side-effects"); const utils_1 = require("./utils"); const AsyncFunction = Object.getPrototypeOf(async function () { }).constructor; const getContext = (data, dataModifier) => { return Object.assign((0, utils_1.getConstructorMethods)(data), dataModifier); }; class Expression { constructor(expression) { this.data = {}; this.firstExecution = true; this.resultHasChanged = false; this.raw = expression.trim(); this.dependancies = new side_effects_1.Effects(this.raw); this.expression = this.withReturn(this.raw); } withReturn(expression) { if (!expression.trim()) { return `return;`; } return ` try { with (this) { with ([...arguments][0]) { return await (${expression}); } } } catch(e) { if (!(e instanceof ReferenceError)) { console.error(e); } } `; } noReturn(expression) { return ` with (this) { with ([...arguments][0]) { ${expression} } } `; } setExpressionNoReturn(expression) { this.expression = this.noReturn(expression === undefined ? this.raw : expression); } eval(data, dataModifier) { this.data = data; this.dependancies.eval(getContext(this.data, dataModifier)); try { this.fn = new AsyncFunction(this.expression); } catch (error) { console.error(error); console.log(this.expression); } } async execute(dataModifier) { if (!this.fn) { throw new Error(`Should eval first`); } this.dependancies.activateProcessingChanges(); const result = await this.fn?.call(this.data, dataModifier); this.resultHasChanged = this.firstExecution || result !== this.lastResult; this.firstExecution = false; this.lastResult = result; this.dependancies.doneProcessingChanges(); return result; } clone() { const expression = new Expression(this.raw); expression.expression = this.expression; expression.dependancies = this.dependancies.clone(); expression.eval(this.data, {}); return expression; } } exports.Expression = Expression; //# sourceMappingURL=expression.js.map