From 0ca6b76f64a7f085dee38a84b2c718b2e5f12251 Mon Sep 17 00:00:00 2001 From: AlexandrHoroshih Date: Mon, 8 Sep 2025 19:02:57 +0700 Subject: [PATCH 1/4] fix: do not spawn additional stores inside variant --- src/core/variant.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/core/variant.ts b/src/core/variant.ts index 9773970..8c26cad 100644 --- a/src/core/variant.ts +++ b/src/core/variant.ts @@ -38,8 +38,7 @@ export function variantFactory(context: Context) { // Shortcut for Store if ('if' in config) { - $case = config.if.map((value): Variant => (value ? 'then' : 'else') as Variant); - + $case = config.if as any; cases = { then: config.then, else: config.else, @@ -54,7 +53,10 @@ export function variantFactory(context: Context) { } function View(props: Props) { - const nameOfCase = context.useUnit($case, config.useUnitConfig); + const nameOfCaseRaw = context.useUnit($case, config.useUnitConfig); + const nameOfCase = ( + 'if' in config ? ifToVariant(nameOfCaseRaw as any) : nameOfCaseRaw + ) as Variant; const Component = cases[nameOfCase] ?? def; return React.createElement(Component as any, props as any); @@ -70,3 +72,7 @@ export function variantFactory(context: Context) { }) as unknown as (p: Props) => React.ReactNode; }; } + +function ifToVariant(value: boolean): 'then' | 'else' { + return value ? 'then' : 'else'; +} From 65a1815f5565acdd3e5c7f342a923dbb7865ac48 Mon Sep 17 00:00:00 2001 From: AlexandrHoroshih Date: Mon, 8 Sep 2025 19:07:37 +0700 Subject: [PATCH 2/4] less hacky typecasts --- src/core/variant.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/core/variant.ts b/src/core/variant.ts index 8c26cad..7ae45f7 100644 --- a/src/core/variant.ts +++ b/src/core/variant.ts @@ -53,9 +53,14 @@ export function variantFactory(context: Context) { } function View(props: Props) { - const nameOfCaseRaw = context.useUnit($case, config.useUnitConfig); + const nameOfCaseRaw = context.useUnit( + $case as Store, + config.useUnitConfig, + ); const nameOfCase = ( - 'if' in config ? ifToVariant(nameOfCaseRaw as any) : nameOfCaseRaw + typeof nameOfCaseRaw === 'string' + ? nameOfCaseRaw + : booleanToVariant(nameOfCaseRaw as boolean) ) as Variant; const Component = cases[nameOfCase] ?? def; @@ -73,6 +78,6 @@ export function variantFactory(context: Context) { }; } -function ifToVariant(value: boolean): 'then' | 'else' { +function booleanToVariant(value: boolean): 'then' | 'else' { return value ? 'then' : 'else'; } From 51d83efba6e13827252184e505c56729753fc431 Mon Sep 17 00:00:00 2001 From: AlexandrHoroshih Date: Mon, 8 Sep 2025 19:08:10 +0700 Subject: [PATCH 3/4] Remove useless typecast --- src/core/variant.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/variant.ts b/src/core/variant.ts index 7ae45f7..6782d47 100644 --- a/src/core/variant.ts +++ b/src/core/variant.ts @@ -60,7 +60,7 @@ export function variantFactory(context: Context) { const nameOfCase = ( typeof nameOfCaseRaw === 'string' ? nameOfCaseRaw - : booleanToVariant(nameOfCaseRaw as boolean) + : booleanToVariant(nameOfCaseRaw) ) as Variant; const Component = cases[nameOfCase] ?? def; From 17d2731f1dc3e3576f8698b4761346be329a16f5 Mon Sep 17 00:00:00 2001 From: AlexandrHoroshih Date: Mon, 8 Sep 2025 19:09:16 +0700 Subject: [PATCH 4/4] even less hacky typecast --- src/core/variant.ts | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/core/variant.ts b/src/core/variant.ts index 6782d47..c969f97 100644 --- a/src/core/variant.ts +++ b/src/core/variant.ts @@ -32,13 +32,13 @@ export function variantFactory(context: Context) { useUnitConfig?: UseUnitConifg; }, ): (p: Props) => React.ReactNode { - let $case: Store; + let $case: Store; let cases: Record>; let def: View; // Shortcut for Store if ('if' in config) { - $case = config.if as any; + $case = config.if; cases = { then: config.then, else: config.else, @@ -53,10 +53,7 @@ export function variantFactory(context: Context) { } function View(props: Props) { - const nameOfCaseRaw = context.useUnit( - $case as Store, - config.useUnitConfig, - ); + const nameOfCaseRaw = context.useUnit($case, config.useUnitConfig); const nameOfCase = ( typeof nameOfCaseRaw === 'string' ? nameOfCaseRaw