Made sequential modifiers lazy

This commit is contained in:
thepaperpilot 2022-07-26 18:17:08 -05:00
parent 2af202a7f1
commit b397929c62

View file

@ -206,7 +206,7 @@ export function createExponentialModifier<
* Takes an array of modifiers and applies and reverses them in order. * Takes an array of modifiers and applies and reverses them in order.
* Modifiers that are not enabled will not be applied nor reversed. * Modifiers that are not enabled will not be applied nor reversed.
* Also joins their descriptions together. * Also joins their descriptions together.
* @param modifiers The modifiers to perform sequentially. * @param modifiersFunc The modifiers to perform sequentially.
* @see {@link createModifierSection}. * @see {@link createModifierSection}.
*/ */
export function createSequentialModifier< export function createSequentialModifier<
@ -214,31 +214,35 @@ export function createSequentialModifier<
S = T extends WithRequired<Modifier, "revert">[] S = T extends WithRequired<Modifier, "revert">[]
? WithRequired<Modifier, "description" | "revert"> ? WithRequired<Modifier, "description" | "revert">
: Omit<WithRequired<Modifier, "description">, "revert"> : Omit<WithRequired<Modifier, "description">, "revert">
>(...modifiers: T): S { >(modifiersFunc: () => T): S {
return { return createLazyProxy(() => {
apply: (gain: DecimalSource) => const modifiers = modifiersFunc();
modifiers
.filter(m => unref(m.enabled) !== false) return {
.reduce((gain, modifier) => modifier.apply(gain), gain), apply: (gain: DecimalSource) =>
revert: modifiers.every(m => m.revert != null) modifiers
? (gain: DecimalSource) => .filter(m => unref(m.enabled) !== false)
modifiers .reduce((gain, modifier) => modifier.apply(gain), gain),
.filter(m => unref(m.enabled) !== false) revert: modifiers.every(m => m.revert != null)
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion ? (gain: DecimalSource) =>
.reduceRight((gain, modifier) => modifier.revert!(gain), gain) modifiers
: undefined, .filter(m => unref(m.enabled) !== false)
enabled: computed(() => modifiers.filter(m => unref(m.enabled) !== false).length > 0), // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
description: jsx(() => ( .reduceRight((gain, modifier) => modifier.revert!(gain), gain)
<> : undefined,
{( enabled: computed(() => modifiers.filter(m => unref(m.enabled) !== false).length > 0),
modifiers description: jsx(() => (
.filter(m => unref(m.enabled) !== false) <>
.map(m => unref(m.description)) {(
.filter(d => d) as CoercableComponent[] modifiers
).map(renderJSX)} .filter(m => unref(m.enabled) !== false)
</> .map(m => unref(m.description))
)) .filter(d => d) as CoercableComponent[]
} as unknown as S; ).map(renderJSX)}
</>
))
};
}) as unknown as S;
} }
/** /**