UNPKG

@shopware-ag/meteor-component-library

Version:

The meteor component library is a Vue component library developed by Shopware. It is based on the [Meteor Design System](https://shopware.design/).

192 lines (191 loc) 4.8 kB
import { computed, onMounted, nextTick, shallowRef, shallowReadonly, getCurrentScope, onScopeDispose, toValue, getCurrentInstance, watch } from "vue"; function tryOnScopeDispose(fn) { if (getCurrentScope()) { onScopeDispose(fn); return true; } return false; } const isClient = typeof window !== "undefined" && typeof document !== "undefined"; typeof WorkerGlobalScope !== "undefined" && globalThis instanceof WorkerGlobalScope; const toString = Object.prototype.toString; const isObject = (val) => toString.call(val) === "[object Object]"; const noop = () => { }; const isIOS = /* @__PURE__ */ getIsIOS(); function getIsIOS() { var _a, _b; return isClient && ((_a = window == null ? void 0 : window.navigator) == null ? void 0 : _a.userAgent) && (/iP(?:ad|hone|od)/.test(window.navigator.userAgent) || ((_b = window == null ? void 0 : window.navigator) == null ? void 0 : _b.maxTouchPoints) > 2 && /iPad|Macintosh/.test(window == null ? void 0 : window.navigator.userAgent)); } function createFilterWrapper(filter, fn) { function wrapper(...args) { return new Promise((resolve, reject) => { Promise.resolve(filter(() => fn.apply(this, args), { fn, thisArg: this, args })).then(resolve).catch(reject); }); } return wrapper; } function debounceFilter(ms, options = {}) { let timer; let maxTimer; let lastRejector = noop; const _clearTimeout = (timer2) => { clearTimeout(timer2); lastRejector(); lastRejector = noop; }; let lastInvoker; const filter = (invoke) => { const duration = toValue(ms); const maxDuration = toValue(options.maxWait); if (timer) _clearTimeout(timer); if (duration <= 0 || maxDuration !== void 0 && maxDuration <= 0) { if (maxTimer) { _clearTimeout(maxTimer); maxTimer = null; } return Promise.resolve(invoke()); } return new Promise((resolve, reject) => { lastRejector = options.rejectOnCancel ? reject : resolve; lastInvoker = invoke; if (maxDuration && !maxTimer) { maxTimer = setTimeout(() => { if (timer) _clearTimeout(timer); maxTimer = null; resolve(lastInvoker()); }, maxDuration); } timer = setTimeout(() => { if (maxTimer) _clearTimeout(maxTimer); maxTimer = null; resolve(invoke()); }, duration); }); }; return filter; } function createSingletonPromise(fn) { let _promise; function wrapper() { if (!_promise) _promise = fn(); return _promise; } wrapper.reset = async () => { const _prev = _promise; _promise = void 0; if (_prev) await _prev; }; return wrapper; } function toArray(value) { return Array.isArray(value) ? value : [value]; } function getLifeCycleTarget(target) { return target || getCurrentInstance(); } function useDebounceFn(fn, ms = 200, options = {}) { return createFilterWrapper( debounceFilter(ms, options), fn ); } function tryOnMounted(fn, sync = true, target) { const instance = getLifeCycleTarget(target); if (instance) onMounted(fn, target); else if (sync) fn(); else nextTick(fn); } function useTimeoutFn(cb, interval, options = {}) { const { immediate = true, immediateCallback = false } = options; const isPending = shallowRef(false); let timer = null; function clear() { if (timer) { clearTimeout(timer); timer = null; } } function stop() { isPending.value = false; clear(); } function start(...args) { if (immediateCallback) cb(); clear(); isPending.value = true; timer = setTimeout(() => { isPending.value = false; timer = null; cb(...args); }, toValue(interval)); } if (immediate) { isPending.value = true; if (isClient) start(); } tryOnScopeDispose(stop); return { isPending: shallowReadonly(isPending), start, stop }; } function useTimeout(interval = 1e3, options = {}) { const { controls: exposeControls = false, callback } = options; const controls = useTimeoutFn( callback != null ? callback : noop, interval, options ); const ready = computed(() => !controls.isPending.value); if (exposeControls) { return { ready, ...controls }; } else { return ready; } } function watchImmediate(source, cb, options) { return watch( source, cb, { ...options, immediate: true } ); } export { useTimeout as a, useTimeoutFn as b, isClient as c, toArray as d, tryOnScopeDispose as e, isObject as f, createSingletonPromise as g, isIOS as i, noop as n, tryOnMounted as t, useDebounceFn as u, watchImmediate as w }; //# sourceMappingURL=index-221bad05.mjs.map