From 5de64e14bc188d9f90aedda35e85147ba38259ed Mon Sep 17 00:00:00 2001 From: David Wheatley Date: Sun, 31 Oct 2021 00:28:30 +0200 Subject: [PATCH] chore: rewrite SubtreeRetainer into Typescript (#3137) * chore: rewrite SubtreeRetainer in Typescript * chore: mark attributes as protected --- ...{SubtreeRetainer.js => SubtreeRetainer.ts} | 24 ++++++++----------- 1 file changed, 10 insertions(+), 14 deletions(-) rename framework/core/js/src/common/utils/{SubtreeRetainer.js => SubtreeRetainer.ts} (82%) diff --git a/framework/core/js/src/common/utils/SubtreeRetainer.js b/framework/core/js/src/common/utils/SubtreeRetainer.ts similarity index 82% rename from framework/core/js/src/common/utils/SubtreeRetainer.js rename to framework/core/js/src/common/utils/SubtreeRetainer.ts index 5b042ccf8..d00f2250a 100644 --- a/framework/core/js/src/common/utils/SubtreeRetainer.js +++ b/framework/core/js/src/common/utils/SubtreeRetainer.ts @@ -22,12 +22,16 @@ * @see https://mithril.js.org/lifecycle-methods.html#onbeforeupdate */ export default class SubtreeRetainer { + protected callbacks: (() => any)[]; + protected data: Record; + /** - * @param {...callbacks} callbacks Functions returning data to keep track of. + * @param callbacks Functions returning data to keep track of. */ - constructor(...callbacks) { + constructor(...callbacks: (() => any)[]) { this.callbacks = callbacks; this.data = {}; + // Build the initial data, so it is available when calling // needsRebuild from the onbeforeupdate hook. this.needsRebuild(); @@ -36,11 +40,8 @@ export default class SubtreeRetainer { /** * Return whether any data has changed since the last check. * If so, Mithril needs to re-diff the vnode and its children. - * - * @return {boolean} - * @public */ - needsRebuild() { + needsRebuild(): boolean { let needsRebuild = false; this.callbacks.forEach((callback, i) => { @@ -57,22 +58,17 @@ export default class SubtreeRetainer { /** * Add another callback to be checked. - * - * @param {...Function} callbacks - * @public */ - check(...callbacks) { + check(...callbacks: (() => any)[]): void { this.callbacks = this.callbacks.concat(callbacks); // Update the data cache when new checks are added. this.needsRebuild(); } /** - * Invalidate the subtree, forcing it to be rerendered. - * - * @public + * Invalidate the subtree, forcing it to be redrawn. */ - invalidate() { + invalidate(): void { this.data = {}; } }