Skip to content

Commit af71414

Browse files
committed
refactor(core): merge setPointerEvent to defineProperties in instance
1 parent 4a746ce commit af71414

File tree

1 file changed

+31
-32
lines changed

1 file changed

+31
-32
lines changed

libs/core/src/lib/instance.ts

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -112,40 +112,39 @@ export function prepare<TInstance extends NgtAnyRecord = NgtAnyRecord>(
112112
};
113113
}
114114

115-
Object.defineProperty(instance.__ngt__, 'setPointerEvent', {
116-
value: <TEvent extends keyof NgtEventHandlers>(
117-
eventName: TEvent,
118-
callback: NonNullable<NgtEventHandlers[TEvent]>,
119-
) => {
120-
const iS = getInstanceState(instance) as NgtInstanceState;
121-
if (!iS.handlers) iS.handlers = {};
122-
123-
// try to get the previous handler. compound might have one, the THREE object might also have one with the same name
124-
const previousHandler = iS.handlers[eventName];
125-
// readjust the callback
126-
const updatedCallback: typeof callback = (event: any) => {
127-
if (previousHandler) previousHandler(event);
128-
callback(event);
129-
};
130-
131-
Object.assign(iS.handlers, { [eventName]: updatedCallback });
132-
133-
// increment the count everytime
134-
iS.eventCount += 1;
135-
136-
// clean up the event listener by removing the target from the interaction array
137-
return () => {
115+
Object.defineProperties(instance.__ngt__, {
116+
setPointerEvent: {
117+
value: <TEvent extends keyof NgtEventHandlers>(
118+
eventName: TEvent,
119+
callback: NonNullable<NgtEventHandlers[TEvent]>,
120+
) => {
138121
const iS = getInstanceState(instance) as NgtInstanceState;
139-
if (iS) {
140-
iS.handlers && delete iS.handlers[eventName];
141-
iS.eventCount -= 1;
142-
}
143-
};
122+
if (!iS.handlers) iS.handlers = {};
123+
124+
// try to get the previous handler. compound might have one, the THREE object might also have one with the same name
125+
const previousHandler = iS.handlers[eventName];
126+
// readjust the callback
127+
const updatedCallback: typeof callback = (event: any) => {
128+
if (previousHandler) previousHandler(event);
129+
callback(event);
130+
};
131+
132+
Object.assign(iS.handlers, { [eventName]: updatedCallback });
133+
134+
// increment the count everytime
135+
iS.eventCount += 1;
136+
137+
// clean up the event listener by removing the target from the interaction array
138+
return () => {
139+
const iS = getInstanceState(instance) as NgtInstanceState;
140+
if (iS) {
141+
iS.handlers && delete iS.handlers[eventName];
142+
iS.eventCount -= 1;
143+
}
144+
};
145+
},
146+
configurable: true,
144147
},
145-
configurable: true,
146-
});
147-
148-
Object.defineProperties(instance.__ngt__, {
149148
addInteraction: {
150149
value: (store?: SignalState<NgtState>) => {
151150
if (!store) return;

0 commit comments

Comments
 (0)