forked from wallet-standard/wallet-standard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregister.ts
More file actions
89 lines (80 loc) · 2.77 KB
/
register.ts
File metadata and controls
89 lines (80 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import type {
DEPRECATED_WalletsWindow,
Wallet,
WalletEventsWindow,
WindowRegisterWalletEvent,
WindowRegisterWalletEventCallback,
} from '@wallet-standard/base';
/**
* Register a {@link "@wallet-standard/base".Wallet} as a Standard Wallet with the app.
*
* This dispatches a {@link "@wallet-standard/base".WindowRegisterWalletEvent} to notify the app that the Wallet is
* ready to be registered.
*
* This also adds a listener for {@link "@wallet-standard/base".WindowAppReadyEvent} to listen for a notification from
* the app that the app is ready to register the Wallet.
*
* This combination of event dispatch and listener guarantees that the Wallet will be registered synchronously as soon
* as the app is ready whether the Wallet loads before or after the app.
*
* @param wallet Wallet to register.
*
* @group Wallet
*/
export function registerWallet(wallet: Wallet): void {
const callback: WindowRegisterWalletEventCallback = ({ register }) => register(wallet);
try {
(window as WalletEventsWindow).dispatchEvent(new RegisterWalletEvent(callback));
} catch (error) {
console.error('wallet-standard:register-wallet event could not be dispatched\n', error);
}
try {
(window as WalletEventsWindow).addEventListener('wallet-standard:app-ready', ({ detail: api }) =>
callback(api)
);
} catch (error) {
console.error('wallet-standard:app-ready event listener could not be added\n', error);
}
}
class RegisterWalletEvent extends Event implements WindowRegisterWalletEvent {
readonly #detail: WindowRegisterWalletEventCallback;
get detail() {
return this.#detail;
}
get type() {
return 'wallet-standard:register-wallet' as const;
}
constructor(callback: WindowRegisterWalletEventCallback) {
super('wallet-standard:register-wallet', {
bubbles: false,
cancelable: false,
composed: false,
});
this.#detail = callback;
}
/** @deprecated */
preventDefault(): never {
throw new Error('preventDefault cannot be called');
}
/** @deprecated */
stopImmediatePropagation(): never {
throw new Error('stopImmediatePropagation cannot be called');
}
/** @deprecated */
stopPropagation(): never {
throw new Error('stopPropagation cannot be called');
}
}
/**
* @deprecated Use {@link registerWallet} instead.
*
* @group Deprecated
*/
export function DEPRECATED_registerWallet(wallet: Wallet): void {
registerWallet(wallet);
try {
((window as DEPRECATED_WalletsWindow).navigator.wallets ||= []).push(({ register }) => register(wallet));
} catch (error) {
console.error('window.navigator.wallets could not be pushed\n', error);
}
}