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/).

191 lines (190 loc) 4.91 kB
"use strict"; const vue = require("vue"); function tryOnScopeDispose(fn) { if (vue.getCurrentScope()) { vue.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 = vue.toValue(ms); const maxDuration = vue.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 || vue.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) vue.onMounted(fn, target); else if (sync) fn(); else vue.nextTick(fn); } function useTimeoutFn(cb, interval, options = {}) { const { immediate = true, immediateCallback = false } = options; const isPending = vue.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); }, vue.toValue(interval)); } if (immediate) { isPending.value = true; if (isClient) start(); } tryOnScopeDispose(stop); return { isPending: vue.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 = vue.computed(() => !controls.isPending.value); if (exposeControls) { return { ready, ...controls }; } else { return ready; } } function watchImmediate(source, cb, options) { return vue.watch( source, cb, { ...options, immediate: true } ); } exports.createSingletonPromise = createSingletonPromise; exports.isClient = isClient; exports.isIOS = isIOS; exports.isObject = isObject; exports.noop = noop; exports.toArray = toArray; exports.tryOnMounted = tryOnMounted; exports.tryOnScopeDispose = tryOnScopeDispose; exports.useDebounceFn = useDebounceFn; exports.useTimeout = useTimeout; exports.useTimeoutFn = useTimeoutFn; exports.watchImmediate = watchImmediate; //# sourceMappingURL=index-ab705c2a.js.map