Skip to content

Commit 6fb9294

Browse files
committed
fix(core): assign intermediate ref to the parent store for non-three instance during applyProps
For default objects like the default `MeshBasicMaterial` on a `ngt-mesh`, we want to still have access to the color space information on the renderer which is only available on `NgtStore`. In order to do this, we assign an intermediate reference to the store from the `parent` during `applyProps` so the instance (`MeshBasicMaterial` in this example) has access to the Store. <ngt-mesh> <ngt-value [rawValue]="texture()" attach="material.map" /> </ngt-mesh> things like this. Consumers probably do not run into this because they would use `ngt-mesh-basic-material#map` instead.
1 parent 709d259 commit 6fb9294

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

libs/core/src/lib/utils/apply-props.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,18 @@ function diffProps(instance: NgtAnyRecord, props: NgtAnyRecord) {
2525
return changes;
2626
}
2727

28+
// NOTE: this is a workaround to give the instance a change to have the store from the parent.
29+
// we clear this property after the applyProps is done
30+
export const NGT_APPLY_PROPS = '__ngt_apply_props__';
31+
2832
// This function applies a set of changes to the instance
2933
export function applyProps(instance: NgtInstanceNode, props: NgtAnyRecord) {
3034
// if props is empty
3135
if (!Object.keys(props).length) return instance;
3236

3337
// filter equals, and reserved props
3438
const localState = getLocalState(instance);
35-
const rootState = localState?.store?.snapshot ?? ({} as NgtState);
39+
const rootState = localState?.store?.snapshot ?? instance[NGT_APPLY_PROPS]?.snapshot ?? ({} as NgtState);
3640
const changes = diffProps(instance, props);
3741

3842
for (let i = 0; i < changes.length; i++) {
@@ -125,5 +129,10 @@ export function applyProps(instance: NgtInstanceNode, props: NgtAnyRecord) {
125129
localState.onUpdate(instance);
126130
}
127131

132+
// clearing the intermediate store from the instance
133+
if (instance[NGT_APPLY_PROPS]) {
134+
delete instance[NGT_APPLY_PROPS];
135+
}
136+
128137
return instance;
129138
}

libs/core/src/lib/utils/attach.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { getLocalState } from '../instance';
22
import { NgtAnyRecord, NgtAttachFunction, NgtState } from '../types';
3-
import { applyProps } from './apply-props';
3+
import { applyProps, NGT_APPLY_PROPS } from './apply-props';
44
import { NgtSignalStore } from './signal-store';
55

66
export function attach(object: NgtAnyRecord, value: unknown, paths: string[] = [], useApplyProps = false): void {
@@ -11,7 +11,7 @@ export function attach(object: NgtAnyRecord, value: unknown, paths: string[] = [
1111
if (useApplyProps) applyProps(object, { [base]: value });
1212
else object[base] = value;
1313
} else {
14-
assignEmpty(object, base);
14+
assignEmpty(object, base, useApplyProps);
1515
attach(object[base], value, remaining, useApplyProps);
1616
}
1717
}
@@ -24,10 +24,22 @@ export function detach(parent: NgtAnyRecord, child: NgtAnyRecord, attachProp: st
2424
}
2525
}
2626

27-
function assignEmpty(obj: NgtAnyRecord, base: string) {
27+
function assignEmpty(obj: NgtAnyRecord, base: string, shouldAssignStoreForApplyProps = false) {
2828
if ((!Object.hasOwn(obj, base) && Reflect && !!Reflect.has && !Reflect.has(obj, base)) || obj[base] === undefined) {
2929
obj[base] = {};
3030
}
31+
32+
if (shouldAssignStoreForApplyProps) {
33+
const localState = getLocalState(obj[base]);
34+
// if we already have local state, bail out
35+
if (localState) return;
36+
37+
const parentLocalState = getLocalState(obj);
38+
// if parent doesn't have local state, bail out
39+
if (!parentLocalState) return;
40+
41+
Object.assign(obj[base], { [NGT_APPLY_PROPS]: parentLocalState.store });
42+
}
3143
}
3244

3345
export function createAttachFunction<TChild = any, TParent = any>(

0 commit comments

Comments
 (0)