UNPKG

@grail-ui/svelte

Version:

[![NPM](https://img.shields.io/npm/v/@grail-ui/svelte)](https://www.npmjs.com/package/@grail-ui/svelte) [![minified](https://img.shields.io/bundlephobia/min/@grail-ui/svelte)](https://bundlephobia.com/package/@grail-ui/svelte) [![minified + zipped](https:

37 lines (36 loc) 762 B
import { onDestroy, onMount } from 'svelte'; import { get_current_component } from 'svelte/internal'; /** * Call onMount() if it's inside a component lifecycle, if not, just call the function * * @param fn * @param sync if set to false, it will run in the nextTick() of Vue */ export function tryOnMount(fn) { let insideComponent = false; try { if (get_current_component()) { insideComponent = true; } } catch { /* empty */ } if (insideComponent) { onMount(fn); } else { fn(); } } /** * Call `onDestroy` if inside a component, otherwise do nothing */ export function tryOnDestroy(fn) { try { onDestroy(fn); } catch { // Do nothing } }