mirror of
https://github.com/windmill-labs/windmill.git
synced 2026-09-06 00:02:13 +00:00
* dispatchIfMounted * fixed all warnings * typo * fix another infinite loop * buggy date time input on runs page when changing search params
20 lines
665 B
TypeScript
20 lines
665 B
TypeScript
import { onMount, type EventDispatcher } from 'svelte'
|
|
|
|
/**
|
|
* This wrapper emulates the behavior of an event dispatcher in Svelte 4
|
|
*
|
|
* A lot of svelte 4 code depended on events not being picked up when they
|
|
* were dispatched before being mounted. In svelte 5, these events are
|
|
* being reacted to which cause infinite loops.
|
|
*/
|
|
export function createDispatcherIfMounted<EventMap extends Record<string, any> = any>(
|
|
dispatch: EventDispatcher<EventMap>
|
|
): EventDispatcher<EventMap> {
|
|
let mounted = false
|
|
onMount(() => (mounted = true))
|
|
return ((...args: Parameters<typeof dispatch>) => {
|
|
if (!mounted) return false
|
|
return dispatch(...args)
|
|
}) as any
|
|
}
|