UNPKG

@fastnear/wallet-adapter

Version:

Wallet adapter implementations for Meteor Wallet and Near Mobile

64 lines 2.54 kB
/* ⋈ 🏃🏻💨 FastNear Wallet Adapters - ESM (@fastnear/wallet-adapter version 1.2.0) */ /* https://www.npmjs.com/package/@fastnear/wallet-adapter/v/1.2.0 */ var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); import { TransportError } from "./errors.js"; const sleep = /* @__PURE__ */ __name((ms) => new Promise((resolve) => setTimeout(resolve, ms)), "sleep"); const waitUntilVisible = /* @__PURE__ */ __name(async (checkIntervalMs, timeoutMs) => { if (typeof document === "undefined") return; if (!document.hidden) return; const start = Date.now(); while (document.hidden) { if (Date.now() - start >= timeoutMs) { throw new TransportError("VISIBILITY_TIMEOUT", "Browser tab stayed hidden for too long while polling"); } await sleep(checkIntervalMs); } }, "waitUntilVisible"); const withTimeout = /* @__PURE__ */ __name(async (promise, timeoutMs, timeoutCode) => { let timeoutId; try { return await Promise.race([ promise, new Promise((_, reject) => { timeoutId = setTimeout(() => { reject(new TransportError(timeoutCode, `Operation timed out after ${timeoutMs}ms`)); }, timeoutMs); }) ]); } finally { if (timeoutId != null) clearTimeout(timeoutId); } }, "withTimeout"); const visibilityAwarePoll = /* @__PURE__ */ __name(async (fn, isPending, options) => { const start = Date.now(); let iteration = 0; let delay = options.delayMs; while (true) { if (Date.now() - start > options.requestTimeoutMs) { throw new TransportError("POLLING_TIMEOUT", "Polling timed out"); } await waitUntilVisible(options.backgroundVisibilityCheckIntervalMs, options.backgroundVisibilityCheckTimeoutMs); const value = await withTimeout(fn(), options.requestCallTimeoutMs, "POLLING_REQUEST_TIMEOUT"); if (!isPending(value)) return value; if (iteration >= options.maxIterations) { throw new TransportError("POLLING_MAX_ITERATIONS", "Polling reached the maximum number of iterations"); } await sleep(delay); delay = Math.min(5e3, Math.ceil(delay * 1.15)); iteration += 1; } }, "visibilityAwarePoll"); const defaultPollingOptions = { delayMs: 1e3, maxIterations: 1e3, requestTimeoutMs: 10 * 60 * 1e3, backgroundVisibilityCheckIntervalMs: 1e3, backgroundVisibilityCheckTimeoutMs: 10 * 60 * 1e3, requestCallTimeoutMs: 3e4 }; export { defaultPollingOptions, visibilityAwarePoll }; //# sourceMappingURL=polling.js.map