From 079e5437371f51cac15550baa5b09aff4c0f2164 Mon Sep 17 00:00:00 2001 From: d98762625 Date: Fri, 20 Mar 2020 13:54:18 +0000 Subject: [PATCH] linting on recipeState work --- package-lock.json | 79 ++++++++---------------------- src/core/Chef.mjs | 6 +-- src/core/Recipe.mjs | 69 ++++++++++++++------------ src/core/lib/Magic.mjs | 2 +- src/core/operations/Fork.mjs | 2 +- src/core/operations/Subsection.mjs | 2 +- 6 files changed, 65 insertions(+), 95 deletions(-) diff --git a/package-lock.json b/package-lock.json index 42004724..4da118c1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3258,7 +3258,6 @@ "resolved": "https://registry.npmjs.org/cliui/-/cliui-5.0.0.tgz", "integrity": "sha512-PYeGSEmmHM6zvoef2w8TPzlrnNpXIjTipYK780YswmIP9vjxmd6Y2a3CB2Ks6/AU8NHjZugXvo8w3oWM2qnwXA==", "dev": true, - "optional": true, "requires": { "string-width": "^3.1.0", "strip-ansi": "^5.2.0", @@ -3269,22 +3268,19 @@ "version": "4.1.0", "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-4.1.0.tgz", "integrity": "sha512-1apePfXM1UOSqw0o9IiFAovVz9M5S1Dg+4TrDwfMewQ6p/rmMueb7tWZjQ1rx4Loy1ArBggoqGpfqqdI4rondg==", - "dev": true, - "optional": true + "dev": true }, "emoji-regex": { "version": "7.0.3", "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-7.0.3.tgz", "integrity": "sha512-CwBLREIQ7LvYFB0WyRvwhq5N5qPhc6PMjD6bYggFlI5YyDgl+0vxq5VHbMOFqLg7hfWzmu8T5Z1QofhmTIhItA==", - "dev": true, - "optional": true + "dev": true }, "string-width": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/string-width/-/string-width-3.1.0.tgz", "integrity": "sha512-vafcv6KjVZKSgz06oM/H6GDBrAtz8vdhQakGjFIvNrHA6y3HCF1CInLy+QLq8dTJPQ1b+KDUqDFctkdRW44e1w==", "dev": true, - "optional": true, "requires": { "emoji-regex": "^7.0.1", "is-fullwidth-code-point": "^2.0.0", @@ -3296,7 +3292,6 @@ "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-5.2.0.tgz", "integrity": "sha512-DuRs1gKbBqsMKIZlrffwlug8MHkcnpjs5VPmL1PAh+mA30U0DTotfDZ0d2UUsXpPmPmMMJ6W773MaA3J+lbiWA==", "dev": true, - "optional": true, "requires": { "ansi-regex": "^4.1.0" } @@ -5847,8 +5842,7 @@ "ansi-regex": { "version": "2.1.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "aproba": { "version": "1.2.0", @@ -5869,14 +5863,12 @@ "balanced-match": { "version": "1.0.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "brace-expansion": { "version": "1.1.11", "bundled": true, "dev": true, - "optional": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -5891,20 +5883,17 @@ "code-point-at": { "version": "1.1.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "concat-map": { "version": "0.0.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "console-control-strings": { "version": "1.1.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "core-util-is": { "version": "1.0.2", @@ -6021,8 +6010,7 @@ "inherits": { "version": "2.0.4", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "ini": { "version": "1.3.5", @@ -6034,7 +6022,6 @@ "version": "1.0.0", "bundled": true, "dev": true, - "optional": true, "requires": { "number-is-nan": "^1.0.0" } @@ -6049,7 +6036,6 @@ "version": "3.0.4", "bundled": true, "dev": true, - "optional": true, "requires": { "brace-expansion": "^1.1.7" } @@ -6057,14 +6043,12 @@ "minimist": { "version": "0.0.8", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "minipass": { "version": "2.9.0", "bundled": true, "dev": true, - "optional": true, "requires": { "safe-buffer": "^5.1.2", "yallist": "^3.0.0" @@ -6083,7 +6067,6 @@ "version": "0.5.1", "bundled": true, "dev": true, - "optional": true, "requires": { "minimist": "0.0.8" } @@ -6173,8 +6156,7 @@ "number-is-nan": { "version": "1.0.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "object-assign": { "version": "4.1.1", @@ -6186,7 +6168,6 @@ "version": "1.4.0", "bundled": true, "dev": true, - "optional": true, "requires": { "wrappy": "1" } @@ -6272,8 +6253,7 @@ "safe-buffer": { "version": "5.1.2", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "safer-buffer": { "version": "2.1.2", @@ -6309,7 +6289,6 @@ "version": "1.0.2", "bundled": true, "dev": true, - "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", @@ -6329,7 +6308,6 @@ "version": "3.0.1", "bundled": true, "dev": true, - "optional": true, "requires": { "ansi-regex": "^2.0.0" } @@ -6373,14 +6351,12 @@ "wrappy": { "version": "1.0.2", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "yallist": { "version": "3.1.1", "bundled": true, - "dev": true, - "optional": true + "dev": true } } }, @@ -6527,8 +6503,7 @@ "version": "2.0.5", "resolved": "https://registry.npmjs.org/get-caller-file/-/get-caller-file-2.0.5.tgz", "integrity": "sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg==", - "dev": true, - "optional": true + "dev": true }, "get-stdin": { "version": "4.0.1", @@ -9366,8 +9341,7 @@ "version": "2.1.1", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.1.tgz", "integrity": "sha512-tgp+dl5cGk28utYktBsrFqA7HKgrhgPsg6Z/EfhWI4gl1Hwq8B/GmY/0oXZ6nF8hDVesS/FpnYaD/kOWhYQvyg==", - "dev": true, - "optional": true + "dev": true }, "strip-json-comments": { "version": "2.0.1", @@ -11785,8 +11759,7 @@ "version": "2.0.0", "resolved": "https://registry.npmjs.org/require-main-filename/-/require-main-filename-2.0.0.tgz", "integrity": "sha512-NKN5kMDylKuldxYLSUfrbo5Tuzh4hd+2E8NPPX02mZtn1VuREQToYe/ZdlJy+J3uCpfaiGF05e7B8W0iXbQHmg==", - "dev": true, - "optional": true + "dev": true }, "requires-port": { "version": "1.0.0", @@ -14732,7 +14705,6 @@ "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-5.1.0.tgz", "integrity": "sha512-QC1/iN/2/RPVJ5jYK8BGttj5z83LmSKmvbvrXPNCLZSEb32KKVDJDl/MOt2N01qU2H/FkzEa9PKto1BqDjtd7Q==", "dev": true, - "optional": true, "requires": { "ansi-styles": "^3.2.0", "string-width": "^3.0.0", @@ -14743,15 +14715,13 @@ "version": "4.1.0", "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-4.1.0.tgz", "integrity": "sha512-1apePfXM1UOSqw0o9IiFAovVz9M5S1Dg+4TrDwfMewQ6p/rmMueb7tWZjQ1rx4Loy1ArBggoqGpfqqdI4rondg==", - "dev": true, - "optional": true + "dev": true }, "ansi-styles": { "version": "3.2.1", "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-3.2.1.tgz", "integrity": "sha512-VT0ZI6kZRdTh8YyJw3SMbYm/u+NqfsAxEpWO0Pf9sq8/e94WxxOpPKx9FR1FlyCtOVDNOQ+8ntlqFxiRc+r5qA==", "dev": true, - "optional": true, "requires": { "color-convert": "^1.9.0" } @@ -14760,15 +14730,13 @@ "version": "7.0.3", "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-7.0.3.tgz", "integrity": "sha512-CwBLREIQ7LvYFB0WyRvwhq5N5qPhc6PMjD6bYggFlI5YyDgl+0vxq5VHbMOFqLg7hfWzmu8T5Z1QofhmTIhItA==", - "dev": true, - "optional": true + "dev": true }, "string-width": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/string-width/-/string-width-3.1.0.tgz", "integrity": "sha512-vafcv6KjVZKSgz06oM/H6GDBrAtz8vdhQakGjFIvNrHA6y3HCF1CInLy+QLq8dTJPQ1b+KDUqDFctkdRW44e1w==", "dev": true, - "optional": true, "requires": { "emoji-regex": "^7.0.1", "is-fullwidth-code-point": "^2.0.0", @@ -14780,7 +14748,6 @@ "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-5.2.0.tgz", "integrity": "sha512-DuRs1gKbBqsMKIZlrffwlug8MHkcnpjs5VPmL1PAh+mA30U0DTotfDZ0d2UUsXpPmPmMMJ6W773MaA3J+lbiWA==", "dev": true, - "optional": true, "requires": { "ansi-regex": "^4.1.0" } @@ -14887,7 +14854,6 @@ "resolved": "https://registry.npmjs.org/yargs/-/yargs-13.3.0.tgz", "integrity": "sha512-2eehun/8ALW8TLoIl7MVaRUrg+yCnenu8B4kBlRxj3GJGDKU1Og7sMXPNm1BYyM1DOJmTZ4YeN/Nwxv+8XJsUA==", "dev": true, - "optional": true, "requires": { "cliui": "^5.0.0", "find-up": "^3.0.0", @@ -14905,22 +14871,19 @@ "version": "4.1.0", "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-4.1.0.tgz", "integrity": "sha512-1apePfXM1UOSqw0o9IiFAovVz9M5S1Dg+4TrDwfMewQ6p/rmMueb7tWZjQ1rx4Loy1ArBggoqGpfqqdI4rondg==", - "dev": true, - "optional": true + "dev": true }, "emoji-regex": { "version": "7.0.3", "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-7.0.3.tgz", "integrity": "sha512-CwBLREIQ7LvYFB0WyRvwhq5N5qPhc6PMjD6bYggFlI5YyDgl+0vxq5VHbMOFqLg7hfWzmu8T5Z1QofhmTIhItA==", - "dev": true, - "optional": true + "dev": true }, "string-width": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/string-width/-/string-width-3.1.0.tgz", "integrity": "sha512-vafcv6KjVZKSgz06oM/H6GDBrAtz8vdhQakGjFIvNrHA6y3HCF1CInLy+QLq8dTJPQ1b+KDUqDFctkdRW44e1w==", "dev": true, - "optional": true, "requires": { "emoji-regex": "^7.0.1", "is-fullwidth-code-point": "^2.0.0", @@ -14932,7 +14895,6 @@ "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-5.2.0.tgz", "integrity": "sha512-DuRs1gKbBqsMKIZlrffwlug8MHkcnpjs5VPmL1PAh+mA30U0DTotfDZ0d2UUsXpPmPmMMJ6W773MaA3J+lbiWA==", "dev": true, - "optional": true, "requires": { "ansi-regex": "^4.1.0" } @@ -14944,7 +14906,6 @@ "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-13.1.1.tgz", "integrity": "sha512-oVAVsHz6uFrg3XQheFII8ESO2ssAf9luWuAd6Wexsu4F3OtIW0o8IribPXYrD4WC24LWtPrJlGy87y5udK+dxQ==", "dev": true, - "optional": true, "requires": { "camelcase": "^5.0.0", "decamelize": "^1.2.0" diff --git a/src/core/Chef.mjs b/src/core/Chef.mjs index 6a338060..5eb07517 100755 --- a/src/core/Chef.mjs +++ b/src/core/Chef.mjs @@ -40,9 +40,9 @@ class Chef { async bake(input, recipeConfig, options) { log.debug("Chef baking"); - const startTime = new Date().now(); + const startTime = Date.now(); const recipe = await Recipe.buildRecipe(recipeConfig); - const containsFc = recipe.containsFlowControl(); + const containsFc = recipe.state.containsFlowControl(); const notUTF8 = options && "treatAsUtf8" in options && !options.treatAsUtf8; let error = false; @@ -112,7 +112,7 @@ class Chef { async silentBake(recipeConfig) { log.debug("Running silent bake"); - const startTime = new Date().now(); + const startTime = Date.now(); const recipe = await Recipe.buildRecipe(recipeConfig); const dish = new Dish(); diff --git a/src/core/Recipe.mjs b/src/core/Recipe.mjs index 2f53f5e1..5314e6c4 100755 --- a/src/core/Recipe.mjs +++ b/src/core/Recipe.mjs @@ -30,9 +30,6 @@ class Recipe { } - - - /** * Returns the value of the Recipe as it should be displayed in a recipe config. * @@ -45,9 +42,6 @@ class Recipe { })); } - get opList() { - return this.state.opList; - } /** * Adds a new Operation to this Recipe. @@ -71,16 +65,6 @@ class Recipe { } - /** - * Returns true if there is a Flow Control Operation in this Recipe. - * - * @returns {boolean} - */ - containsFlowControl() { - return this.state.containsFlowControl() - } - - /** * Executes each operation in the recipe over the given Dish. * @@ -121,10 +105,7 @@ class Recipe { } if (op.flowControl) { - this.state = await op.run(this.state); - - this.state.progress++; } else { output = await op.run(input, op.ingValues); @@ -226,11 +207,16 @@ class Recipe { } /** - * - * @param recipeConfig + * Build a recipe using a recipeConfig + * + * Hydrate the recipeConfig before using the hydrated operations + * in the Recipe constructor. This decouples the hydration of + * the operations from the Recipe logic. + * + * @param {recipeConfig} recipeConfig */ static async buildRecipe(recipeConfig) { - let operations = []; + const operations = []; recipeConfig.forEach(c => { operations.push({ name: c.op, @@ -248,7 +234,6 @@ class Recipe { modules = modules.default; } - const hydratedOperations = operations.map(o => { if (o instanceof Operation) { return o; @@ -264,12 +249,19 @@ class Recipe { return new Recipe(hydratedOperations); } - } +/** + * Encapsulate the state of a Recipe + * + * Encapsulating the state makes it cleaner when passing the state + * between operations. + */ class RecipeState { - + /** + * initialise a RecipeState + */ constructor() { this.dish = null; this.opList = []; @@ -279,34 +271,51 @@ class RecipeState { this.numJumps = 0; } + /** + * get the next operation due to be run. + * @return {Operation} + */ get currentOp() { return this.opList[this.progress]; } + /** + * add an operation to the end of RecipeState's opList + * @param {Operation} operation + */ addOperation(operation) { this.opList.push(operation); } + /** + * @returns {boolean} whether there's a flowControl operation in + * the RecipeState's opList + */ containsFlowControl() { return this.opList.reduce((p, c) => { return p || c.flowControl; }, false); } + /** + * Update the RecipeState with state from a fork. Used at the end + * of the Fork operation. + * @param {Object} forkState + */ updateForkState(forkState) { - if (forkState.progress || forkState.progress == 0) { - this.progress = forkState.progress + if (forkState.progress || forkState.progress === 0) { + this.progress = forkState.progress; } - if (forkState.numRegisters || forkState.numRegisters == 0) { + if (forkState.numRegisters || forkState.numRegisters === 0) { this.numRegisters = forkState.numRegisters; } - if (forkState.numJumps || forkState.numJumps == 0) { + if (forkState.numJumps || forkState.numJumps === 0) { this.numJumps = forkState.numJumps; } - if (forkState.forkOffset || forkState.forkOffset == 0) { + if (forkState.forkOffset || forkState.forkOffset === 0) { this.forkOffset = forkState.forkOffset; } } diff --git a/src/core/lib/Magic.mjs b/src/core/lib/Magic.mjs index 94460ac6..f4595a2c 100644 --- a/src/core/lib/Magic.mjs +++ b/src/core/lib/Magic.mjs @@ -402,7 +402,7 @@ class Magic { try { await recipe.execute(dish); // Return an empty buffer if the recipe did not run to completion - if (recipe.lastRunOp === recipe.opList[recipe.opList.length - 1]) { + if (recipe.lastRunOp === recipe.state.opList[recipe.state.opList.length - 1]) { return await dish.get(Dish.ARRAY_BUFFER); } else { return new ArrayBuffer(); diff --git a/src/core/operations/Fork.mjs b/src/core/operations/Fork.mjs index 0dbd4b88..0d012410 100644 --- a/src/core/operations/Fork.mjs +++ b/src/core/operations/Fork.mjs @@ -89,7 +89,7 @@ class Fork extends Operation { // Run recipe over each tranche for (i = 0; i < inputs.length; i++) { // Baseline ing values for each tranche so that registers are reset - recipe.opList.forEach((op, i) => { + recipe.state.opList.forEach((op, i) => { op.ingValues = JSON.parse(JSON.stringify(ingValues[i])); }); diff --git a/src/core/operations/Subsection.mjs b/src/core/operations/Subsection.mjs index 2c2bd959..e668f172 100644 --- a/src/core/operations/Subsection.mjs +++ b/src/core/operations/Subsection.mjs @@ -115,7 +115,7 @@ class Subsection extends Operation { } // Baseline ing values for each tranche so that registers are reset - recipe.opList.forEach((op, i) => { + recipe.state.opList.forEach((op, i) => { op.ingValues = JSON.parse(JSON.stringify(ingValues[i])); });