Skip to content

Commit 734d4ac

Browse files
refactor(core): Revise optimizations and remove specs
This commit incorporates feedback on previous optimizations: 1. **`libs/core/src/lib/instance.ts` (`prepare` function):** * Reverted the definition of `setPointerEvent`, `addInteraction`, and `removeInteraction` methods to use `Object.defineProperty` and `Object.defineProperties` respectively. This restores their original definition style, ensuring properties like non-enumerability are maintained as intended. 2. **`libs/core/src/lib/events.ts` (`intersect` function):** * The sorting mechanism for `raycastResults` has been updated to use a `WeakMap`. * Before sorting, a `WeakMap` is populated where keys are the `THREE.Intersection` objects and values are their calculated `priority` and `stateExists` status. * The sort comparator then uses this `WeakMap` to access these pre-calculated values. This approach avoids intermediate array mapping, potentially improving memory efficiency while still preventing redundant `getInstanceState` calls within the sort. 3. **Spec Files Removed:** * Deleted `libs/core/src/lib/events.spec.ts`. * Deleted `libs/core/src/lib/instance.spec.ts`. * Deleted `libs/core/src/lib/loader.spec.ts`. Unit tests will be handled separately as per feedback. The optimization for `libs/core/src/lib/loader.ts` (`_injectLoader` effect) from the previous set of changes remains in place.
1 parent 188f3a1 commit 734d4ac

File tree

5 files changed

+78
-664
lines changed

5 files changed

+78
-664
lines changed

libs/core/src/lib/events.spec.ts

Lines changed: 0 additions & 273 deletions
This file was deleted.

libs/core/src/lib/events.ts

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -123,37 +123,40 @@ export function createEvents(store: SignalState<NgtState>) {
123123
}
124124

125125
// Sort by event priority and distance
126-
// Map to temporary array with pre-fetched values for sorting
127-
const sortableRaycastResults = raycastResults.map((item) => {
126+
// Use a WeakMap to cache sort data associated with each intersection object.
127+
const sortDataCache = new WeakMap<
128+
THREE.Intersection<THREE.Object3D>,
129+
{ priority?: number; stateExists: boolean }
130+
>();
131+
132+
for (const item of raycastResults) {
128133
const state = getInstanceState(item.object)?.store?.snapshot;
129-
return {
130-
originalItem: item,
134+
sortDataCache.set(item, {
131135
priority: state?.events.priority,
132-
distance: item.distance,
133136
stateExists: !!state,
134-
};
135-
});
137+
});
138+
}
136139

137-
// Sort the temporary array
138-
sortableRaycastResults.sort((a, b) => {
139-
if (a.stateExists && b.stateExists) {
140+
raycastResults.sort((a, b) => {
141+
const aData = sortDataCache.get(a);
142+
const bData = sortDataCache.get(b);
143+
144+
// Should always find data, but defensively handle if not.
145+
if (!aData || !bData) return a.distance - b.distance;
146+
147+
if (aData.stateExists && bData.stateExists) {
140148
// Both items have state, sort by priority then distance
141-
// Use a default for priority (e.g., 0) if it might be undefined,
142-
// though stateExists should imply it's a number as per original logic.
143-
return (b.priority ?? 0) - (a.priority ?? 0) || a.distance - b.distance;
149+
return (bData.priority ?? 0) - (aData.priority ?? 0) || a.distance - b.distance;
144150
}
145151
// One or both states don't exist, sort by distance only
146152
return a.distance - b.distance;
147153
});
148154

149-
// Map back to the original raycastResults structure
150-
const sortedRaycastResults = sortableRaycastResults.map((item) => item.originalItem);
151-
152155
// Filter out duplicates - more efficient than chaining
153-
// Use sortedRaycastResults instead of raycastResults for filtering
156+
// raycastResults is now sorted in-place.
154157
let hits: THREE.Intersection<THREE.Object3D>[] = [];
155-
for (let i = 0; i < sortedRaycastResults.length; i++) {
156-
const item = sortedRaycastResults[i];
158+
for (let i = 0; i < raycastResults.length; i++) {
159+
const item = raycastResults[i];
157160
const id = makeId(item as NgtIntersection);
158161
if (duplicates.has(id)) continue;
159162
duplicates.add(id);

0 commit comments

Comments
 (0)