UNPKG

recipe-ts-runtime

Version:

TypeScript run-time library for the Recipe framework

46 lines 1.73 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const AbstractOven_1 = require("./AbstractOven"); const Payload_1 = require("./Payload"); const Cake_1 = require("./Cake"); const Recipe_1 = require("./Recipe"); class BackendOven extends AbstractOven_1.AbstractOven { constructor() { super(...arguments); this.hooks = {}; this.ingredientTypes = {}; } bake(json) { const payload = Payload_1.Payload.fromJSON(JSON.parse(json), this.ingredientTypes); const cake = this.createCake(payload.getCake()); this.bakeIngredient(payload.getRecipe(), cake); const cakeToSerialize = new Cake_1.Cake(cake); return JSON.stringify(cakeToSerialize); } registerHook(hook) { this.hooks[hook.getIngredientName()] = hook; this.ingredientTypes[hook.getIngredientName()] = hook.getSnapshotClass(); } bakeIngredient(ingredient, cake) { if (ingredient instanceof Recipe_1.Recipe) { const recipe = ingredient; const bakeRecipeIngredients = () => { for (let i of recipe.getIngredients()) { this.bakeIngredient(i, cake); } }; if (recipe.getContext() !== null) { cake.inNamespace(recipe.getContext(), bakeRecipeIngredients); } else { bakeRecipeIngredients(); } } else { const ingredientData = ingredient.toJSON()[ingredient.getIngredientType()]; this.hooks[ingredient.getIngredientType()].bake(ingredientData, cake); } } } exports.BackendOven = BackendOven; //# sourceMappingURL=BackendOven.js.map