UNPKG

@clerk/shared

Version:

Internal package utils used by the Clerk SDKs

1 lines 9.14 kB
{"version":3,"file":"esm-vhKiNaSe.mjs","names":[],"sources":["../../../../node_modules/.pnpm/@wallet-standard+app@1.1.0/node_modules/@wallet-standard/app/lib/esm/wallets.js"],"sourcesContent":["var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {\n if (kind === \"a\" && !f) throw new TypeError(\"Private accessor was defined without a getter\");\n if (typeof state === \"function\" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError(\"Cannot read private member from an object whose class did not declare it\");\n return kind === \"m\" ? f : kind === \"a\" ? f.call(receiver) : f ? f.value : state.get(receiver);\n};\nvar __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {\n if (kind === \"m\") throw new TypeError(\"Private method is not writable\");\n if (kind === \"a\" && !f) throw new TypeError(\"Private accessor was defined without a setter\");\n if (typeof state === \"function\" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError(\"Cannot write private member to an object whose class did not declare it\");\n return (kind === \"a\" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;\n};\nvar _AppReadyEvent_detail;\nlet wallets = undefined;\nconst registeredWalletsSet = new Set();\nfunction addRegisteredWallet(wallet) {\n cachedWalletsArray = undefined;\n registeredWalletsSet.add(wallet);\n}\nfunction removeRegisteredWallet(wallet) {\n cachedWalletsArray = undefined;\n registeredWalletsSet.delete(wallet);\n}\nconst listeners = {};\n/**\n * Get an API for {@link Wallets.get | getting}, {@link Wallets.on | listening for}, and\n * {@link Wallets.register | registering} {@link \"@wallet-standard/base\".Wallet | Wallets}.\n *\n * When called for the first time --\n *\n * This dispatches a {@link \"@wallet-standard/base\".WindowAppReadyEvent} to notify each Wallet that the app is ready\n * to register it.\n *\n * This also adds a listener for {@link \"@wallet-standard/base\".WindowRegisterWalletEvent} to listen for a notification\n * from each Wallet that the Wallet is ready to be registered by the app.\n *\n * This combination of event dispatch and listener guarantees that each Wallet will be registered synchronously as soon\n * as the app is ready whether the app loads before or after each Wallet.\n *\n * @return API for getting, listening for, and registering Wallets.\n *\n * @group App\n */\nexport function getWallets() {\n if (wallets)\n return wallets;\n wallets = Object.freeze({ register, get, on });\n if (typeof window === 'undefined')\n return wallets;\n const api = Object.freeze({ register });\n try {\n window.addEventListener('wallet-standard:register-wallet', ({ detail: callback }) => callback(api));\n }\n catch (error) {\n console.error('wallet-standard:register-wallet event listener could not be added\\n', error);\n }\n try {\n window.dispatchEvent(new AppReadyEvent(api));\n }\n catch (error) {\n console.error('wallet-standard:app-ready event could not be dispatched\\n', error);\n }\n return wallets;\n}\nfunction register(...wallets) {\n // Filter out wallets that have already been registered.\n // This prevents the same wallet from being registered twice, but it also prevents wallets from being\n // unregistered by reusing a reference to the wallet to obtain the unregister function for it.\n wallets = wallets.filter((wallet) => !registeredWalletsSet.has(wallet));\n // If there are no new wallets to register, just return a no-op unregister function.\n // eslint-disable-next-line @typescript-eslint/no-empty-function\n if (!wallets.length)\n return () => { };\n wallets.forEach((wallet) => addRegisteredWallet(wallet));\n listeners['register']?.forEach((listener) => guard(() => listener(...wallets)));\n // Return a function that unregisters the registered wallets.\n return function unregister() {\n wallets.forEach((wallet) => removeRegisteredWallet(wallet));\n listeners['unregister']?.forEach((listener) => guard(() => listener(...wallets)));\n };\n}\nlet cachedWalletsArray;\nfunction get() {\n if (!cachedWalletsArray) {\n cachedWalletsArray = [...registeredWalletsSet];\n }\n return cachedWalletsArray;\n}\nfunction on(event, listener) {\n listeners[event]?.push(listener) || (listeners[event] = [listener]);\n // Return a function that removes the event listener.\n return function off() {\n listeners[event] = listeners[event]?.filter((existingListener) => listener !== existingListener);\n };\n}\nfunction guard(callback) {\n try {\n callback();\n }\n catch (error) {\n console.error(error);\n }\n}\nclass AppReadyEvent extends Event {\n get detail() {\n return __classPrivateFieldGet(this, _AppReadyEvent_detail, \"f\");\n }\n get type() {\n return 'wallet-standard:app-ready';\n }\n constructor(api) {\n super('wallet-standard:app-ready', {\n bubbles: false,\n cancelable: false,\n composed: false,\n });\n _AppReadyEvent_detail.set(this, void 0);\n __classPrivateFieldSet(this, _AppReadyEvent_detail, api, \"f\");\n }\n /** @deprecated */\n preventDefault() {\n throw new Error('preventDefault cannot be called');\n }\n /** @deprecated */\n stopImmediatePropagation() {\n throw new Error('stopImmediatePropagation cannot be called');\n }\n /** @deprecated */\n stopPropagation() {\n throw new Error('stopPropagation cannot be called');\n }\n}\n_AppReadyEvent_detail = new WeakMap();\n/**\n * @deprecated Use {@link getWallets} instead.\n *\n * @group Deprecated\n */\nexport function DEPRECATED_getWallets() {\n if (wallets)\n return wallets;\n wallets = getWallets();\n if (typeof window === 'undefined')\n return wallets;\n const callbacks = window.navigator.wallets || [];\n if (!Array.isArray(callbacks)) {\n console.error('window.navigator.wallets is not an array');\n return wallets;\n }\n const { register } = wallets;\n const push = (...callbacks) => callbacks.forEach((callback) => guard(() => callback({ register })));\n try {\n Object.defineProperty(window.navigator, 'wallets', {\n value: Object.freeze({ push }),\n });\n }\n catch (error) {\n console.error('window.navigator.wallets could not be set');\n return wallets;\n }\n push(...callbacks);\n return wallets;\n}\n//# sourceMappingURL=wallets.js.map"],"x_google_ignoreList":[0],"mappings":";AAAA,IAAI,4CAAuC,0BAA2B,SAAU,UAAU,OAAO,MAAM,GAAG;CACtG,IAAI,SAAS,OAAO,CAAC,GAAG,MAAM,IAAI,UAAU,+CAA+C;CAC3F,IAAI,OAAO,UAAU,aAAa,aAAa,SAAS,CAAC,IAAI,CAAC,MAAM,IAAI,QAAQ,GAAG,MAAM,IAAI,UAAU,0EAA0E;CACjL,OAAO,SAAS,MAAM,IAAI,SAAS,MAAM,EAAE,KAAK,QAAQ,IAAI,IAAI,EAAE,QAAQ,MAAM,IAAI,QAAQ;AAChG;AACA,IAAI,4CAAuC,0BAA2B,SAAU,UAAU,OAAO,OAAO,MAAM,GAAG;CAC7G,IAAI,SAAS,KAAK,MAAM,IAAI,UAAU,gCAAgC;CACtE,IA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