UNPKG

@foxpage/foxpage-core

Version:

foxpage core

99 lines (98 loc) 3.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ParserImpl = void 0; const foxpage_shared_1 = require("@foxpage/foxpage-shared"); const mocker_1 = require("../mocker"); const condition_1 = require("./condition"); const library_1 = require("./library"); const main_1 = require("./main"); const material_1 = require("./material"); const plugin_1 = require("./plugin"); const variable_1 = require("./variable"); /** * parser implements * * @export * @class ParserImpl * @implements {Parser} */ class ParserImpl { constructor() { this.mainParsers = {}; this.variableParser = new variable_1.VariableParserImpl(); this.conditionParser = new condition_1.ConditionParserImpl(); this.libraryParser = new library_1.LibraryParser(); this.pluginParser = new plugin_1.PluginParser(); this.materialParser = new material_1.MaterialParser(); } /** * prepare * @param opt parserOption */ async prepare(opt) { var _a; const { hooks } = opt || {}; await ((_a = this.variableParser) === null || _a === void 0 ? void 0 : _a.registerDynamic(hooks === null || hooks === void 0 ? void 0 : hooks.variable)); } /** * pre parse * first step: preParse content * second step: preParse template */ preParse(content, ctx, opt) { var _a; this.mainParsers[opt.sessionId] = new main_1.MainParser({ content }, ctx); (_a = this.variableParser) === null || _a === void 0 ? void 0 : _a.preParse(); } /** * parse * first: variable(functions) * second: condition * last: content */ async parse(sessionId, ctx) { var _a, _b, _c, _d, _e, _f; if (!this.mainParsers[sessionId]) { throw new Error('parser instance is invalid.'); } // with variable mock if (ctx.isMock) { (0, mocker_1.withVariableMock)(ctx.getOrigin(foxpage_shared_1.ContentType.VARIABLE), ctx); } // libraries (_a = this.libraryParser) === null || _a === void 0 ? void 0 : _a.parse(ctx); // csr plugins (_b = this.pluginParser) === null || _b === void 0 ? void 0 : _b.parse(ctx); // material (_c = this.materialParser) === null || _c === void 0 ? void 0 : _c.parse(ctx); // variable const variableCost = ctx.performanceLogger('variableTime'); await ((_d = this.variableParser) === null || _d === void 0 ? void 0 : _d.parse(ctx)); variableCost(); // condition const conditionCost = ctx.performanceLogger('conditionTime'); (_e = this.conditionParser) === null || _e === void 0 ? void 0 : _e.parse(ctx); conditionCost(); // content(page\template\block) const structureCost = ctx.performanceLogger('structureTime'); const result = await ((_f = this.mainParsers[sessionId]) === null || _f === void 0 ? void 0 : _f.parse(ctx)); structureCost(); return result; } /** * get parse status * * @return {*} {boolean} true:parsed,false:no parse */ isParsed() { return false; } /** * clear instance * */ reset(opt) { delete this.mainParsers[opt.sessionId]; } } exports.ParserImpl = ParserImpl;