@threlte/rapier
Version:
Components and hooks to use the Rapier physics engine in Threlte
28 lines (27 loc) • 775 B
JavaScript
import { onDestroy } from 'svelte';
export const useCreateEvent = (oncreate) => {
const cleanupFunctions = [];
let ref;
const dispatchCreateEvent = () => {
// call every cleanup function
cleanupFunctions.forEach((cleanup) => cleanup());
// clear the cleanup functions array
cleanupFunctions.length = 0;
if (ref === undefined)
return;
const cleanup = oncreate?.(ref);
if (cleanup)
cleanupFunctions.push(cleanup);
};
const updateRef = (newRef) => {
ref = newRef;
dispatchCreateEvent();
};
onDestroy(() => {
// call every cleanup function
cleanupFunctions.forEach((cleanup) => cleanup());
});
return {
updateRef
};
};