|
| 1 | +import { EventEmitter, WritableSignal, signal, untracked } from '@angular/core'; |
| 2 | +import { type NgtEventHandlers } from './events'; |
| 3 | +import { type NgtState } from './store'; |
| 4 | +import type { NgtAnyRecord } from './types'; |
| 5 | +import { checkUpdate, signalStore, type NgtSignalStore } from './utils'; |
| 6 | + |
| 7 | +export type NgtAttachFunction<TChild = any, TParent = any> = ( |
| 8 | + parent: TParent, |
| 9 | + child: TChild, |
| 10 | + store: NgtSignalStore<NgtState> |
| 11 | +) => void | (() => void); |
| 12 | + |
| 13 | +export type NgtAfterAttach< |
| 14 | + TParent extends NgtInstanceNode = NgtInstanceNode, |
| 15 | + TChild extends NgtInstanceNode = NgtInstanceNode |
| 16 | +> = { parent: TParent; node: TChild }; |
| 17 | + |
| 18 | +export type NgtInstanceLocalState = { |
| 19 | + /** the state getter of the canvas that the instance is being rendered to */ |
| 20 | + store: NgtSignalStore<NgtState>; |
| 21 | + // objects and parent are used when children are added with `attach` instead of being added to the Object3D scene graph |
| 22 | + nonObjects: WritableSignal<NgtInstanceNode[]>; |
| 23 | + // objects that are Object3D |
| 24 | + objects: WritableSignal<NgtInstanceNode[]>; |
| 25 | + // shortcut to add/remove object to list |
| 26 | + add: (instance: NgtInstanceNode, type: 'objects' | 'nonObjects') => void; |
| 27 | + remove: (instance: NgtInstanceNode, type: 'objects' | 'nonObjects') => void; |
| 28 | + // native props signal |
| 29 | + nativeProps: NgtSignalStore<NgtAnyRecord>; |
| 30 | + // parent based on attach three instance |
| 31 | + parent: WritableSignal<NgtInstanceNode | null>; |
| 32 | + // if this THREE instance is a ngt-primitive |
| 33 | + primitive?: boolean; |
| 34 | + // if this THREE object has any events bound to it |
| 35 | + eventCount: number; |
| 36 | + // list of handlers to handle the events |
| 37 | + handlers: Partial<NgtEventHandlers>; |
| 38 | + // previous args |
| 39 | + args?: unknown[]; |
| 40 | + // attach information so that we can detach as well as reset |
| 41 | + attach?: string[] | NgtAttachFunction; |
| 42 | + // previously attach information so we can reset as well as clean up |
| 43 | + previousAttach?: unknown | (() => void); |
| 44 | + // is raw value |
| 45 | + isRaw?: boolean; |
| 46 | + // priority for before render |
| 47 | + priority?: number; |
| 48 | + // emitter after props update |
| 49 | + afterUpdate?: EventEmitter<NgtInstanceNode>; |
| 50 | + // emitter after attaching to parent |
| 51 | + afterAttach?: EventEmitter<NgtAfterAttach>; |
| 52 | +}; |
| 53 | + |
| 54 | +export type NgtInstanceNode<TNode = any> = { |
| 55 | + __ngt__: NgtInstanceLocalState; |
| 56 | +} & NgtAnyRecord & |
| 57 | + TNode; |
| 58 | + |
| 59 | +export function getLocalState<TInstance extends object = NgtAnyRecord>( |
| 60 | + obj: TInstance | undefined |
| 61 | +): NgtInstanceLocalState { |
| 62 | + if (!obj) return {} as unknown as NgtInstanceLocalState; |
| 63 | + return (obj as NgtAnyRecord)['__ngt__'] || ({} as NgtInstanceLocalState); |
| 64 | +} |
| 65 | + |
| 66 | +export function invalidateInstance<TInstance extends object>(instance: TInstance) { |
| 67 | + const state = getLocalState(instance).store?.get(); |
| 68 | + if (state && state.internal.frames === 0) state.invalidate(); |
| 69 | + checkUpdate(instance); |
| 70 | +} |
| 71 | + |
| 72 | +export function prepare<TInstance extends object = NgtAnyRecord>( |
| 73 | + object: TInstance, |
| 74 | + localState?: Partial<NgtInstanceLocalState> |
| 75 | +): NgtInstanceNode<TInstance> { |
| 76 | + const instance = object as unknown as NgtInstanceNode<TInstance>; |
| 77 | + |
| 78 | + if (localState?.primitive || !instance.__ngt__) { |
| 79 | + const { |
| 80 | + objects = signal<NgtInstanceNode[]>([]), |
| 81 | + nonObjects = signal<NgtInstanceNode[]>([]), |
| 82 | + ...rest |
| 83 | + } = localState || {}; |
| 84 | + |
| 85 | + instance.__ngt__ = { |
| 86 | + previousAttach: null, |
| 87 | + store: null, |
| 88 | + parent: signal(null), |
| 89 | + memoized: {}, |
| 90 | + eventCount: 0, |
| 91 | + handlers: {}, |
| 92 | + objects, |
| 93 | + nonObjects, |
| 94 | + nativeProps: signalStore<NgtAnyRecord>(), |
| 95 | + add: (object, type) => { |
| 96 | + untracked(() => { |
| 97 | + const current = untracked(instance.__ngt__[type]); |
| 98 | + const foundIndex = current.indexOf((obj: NgtInstanceNode) => obj === object); |
| 99 | + if (foundIndex > -1) { |
| 100 | + // if we add an object with the same reference, then we switch it out |
| 101 | + // and update the BehaviorSubject |
| 102 | + current.splice(foundIndex, 1, object); |
| 103 | + instance.__ngt__[type].set(current); |
| 104 | + } else { |
| 105 | + instance.__ngt__[type].update((prev) => [...prev, object]); |
| 106 | + } |
| 107 | + notifyAncestors(untracked(instance.__ngt__.parent)); |
| 108 | + }); |
| 109 | + }, |
| 110 | + remove: (object, type) => { |
| 111 | + untracked(() => { |
| 112 | + instance.__ngt__[type].update((prev) => prev.filter((o) => o !== object)); |
| 113 | + notifyAncestors(untracked(instance.__ngt__.parent)); |
| 114 | + }); |
| 115 | + }, |
| 116 | + ...rest, |
| 117 | + } as NgtInstanceLocalState; |
| 118 | + } |
| 119 | + |
| 120 | + return instance; |
| 121 | +} |
| 122 | + |
| 123 | +function notifyAncestors(instance: NgtInstanceNode | null) { |
| 124 | + if (!instance) return; |
| 125 | + const localState = getLocalState(instance); |
| 126 | + if (localState.objects) localState.objects.update((prev) => prev); |
| 127 | + if (localState.nonObjects) localState.nonObjects.update((prev) => prev); |
| 128 | + notifyAncestors(untracked(localState.parent)); |
| 129 | +} |
0 commit comments