@threlte/core
Version:
A 3D framework for the web, built on top of Svelte and Three.js
29 lines (28 loc) • 915 B
JavaScript
/**
* Typeguard to check if a value is extending THREE.EventDispatcher
* @param value
* @returns
*/
const isEventDispatcher = (value) => {
return (value !== null &&
typeof value === 'object' &&
'addEventListener' in value &&
'removeEventListener' in value);
};
export const useEvents = (getRef, propKeys, props) => {
const ref = $derived(getRef());
for (const key of propKeys) {
const prop = $derived(props[key]);
// Don't create the effect unless the prop begins with "on"
if (key.startsWith('on')) {
$effect.pre(() => {
if (typeof prop !== 'function' || !isEventDispatcher(ref)) {
return;
}
const name = key.slice(2);
ref.addEventListener(name, prop);
return () => ref.removeEventListener(name, prop);
});
}
}
};