From b397929c6278ff90028a878b8effa547dd4ad4be Mon Sep 17 00:00:00 2001 From: thepaperpilot Date: Tue, 26 Jul 2022 18:17:08 -0500 Subject: [PATCH] Made sequential modifiers lazy --- src/game/modifiers.tsx | 56 ++++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 26 deletions(-) diff --git a/src/game/modifiers.tsx b/src/game/modifiers.tsx index b476027..54f9a95 100644 --- a/src/game/modifiers.tsx +++ b/src/game/modifiers.tsx @@ -206,7 +206,7 @@ export function createExponentialModifier< * Takes an array of modifiers and applies and reverses them in order. * Modifiers that are not enabled will not be applied nor reversed. * Also joins their descriptions together. - * @param modifiers The modifiers to perform sequentially. + * @param modifiersFunc The modifiers to perform sequentially. * @see {@link createModifierSection}. */ export function createSequentialModifier< @@ -214,31 +214,35 @@ export function createSequentialModifier< S = T extends WithRequired[] ? WithRequired : Omit, "revert"> ->(...modifiers: T): S { - return { - apply: (gain: DecimalSource) => - modifiers - .filter(m => unref(m.enabled) !== false) - .reduce((gain, modifier) => modifier.apply(gain), gain), - revert: modifiers.every(m => m.revert != null) - ? (gain: DecimalSource) => - modifiers - .filter(m => unref(m.enabled) !== false) - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - .reduceRight((gain, modifier) => modifier.revert!(gain), gain) - : undefined, - enabled: computed(() => modifiers.filter(m => unref(m.enabled) !== false).length > 0), - description: jsx(() => ( - <> - {( - modifiers - .filter(m => unref(m.enabled) !== false) - .map(m => unref(m.description)) - .filter(d => d) as CoercableComponent[] - ).map(renderJSX)} - - )) - } as unknown as S; +>(modifiersFunc: () => T): S { + return createLazyProxy(() => { + const modifiers = modifiersFunc(); + + return { + apply: (gain: DecimalSource) => + modifiers + .filter(m => unref(m.enabled) !== false) + .reduce((gain, modifier) => modifier.apply(gain), gain), + revert: modifiers.every(m => m.revert != null) + ? (gain: DecimalSource) => + modifiers + .filter(m => unref(m.enabled) !== false) + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + .reduceRight((gain, modifier) => modifier.revert!(gain), gain) + : undefined, + enabled: computed(() => modifiers.filter(m => unref(m.enabled) !== false).length > 0), + description: jsx(() => ( + <> + {( + modifiers + .filter(m => unref(m.enabled) !== false) + .map(m => unref(m.description)) + .filter(d => d) as CoercableComponent[] + ).map(renderJSX)} + + )) + }; + }) as unknown as S; } /**