forked from wallet-standard/wallet-standard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindow.ts
More file actions
38 lines (28 loc) · 1.55 KB
/
window.ts
File metadata and controls
38 lines (28 loc) · 1.55 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
import { getWallets, registerWallet } from '@wallet-standard/core';
import { EthereumWallet } from './ethereumWallet.js';
import { SolanaWallet } from './solanaWallet.js';
(function () {
// TODO: update comments
// The dapp hasn't loaded yet, so the first wallet to load registers itself on the window
registerWallet(new SolanaWallet());
// ... time passes, the dapp loads ...
// The dapp replaces the queue with an object API, and runs any queued commands
const { get, on } = getWallets();
// The dapp gets all the wallets that have been registered so far, receiving all the registered wallets, which it
// can add to its own state context
let wallets = get();
// The dapp adds an event listener for new wallets that get registered after this point, receiving an unsubscribe
// function, which it can later use to remove the listener
const removeRegisterListener = on('register', function () {
// The dapp can add new wallets to its own state context as they are registered
wallets = get();
});
const removeUnregisterListener = on('unregister', function () {
wallets = get();
});
// ... time passes, other wallets load ...
// The second wallet to load registers itself on the window
registerWallet(new EthereumWallet());
// The dapp has an event listener now, so it sees new wallets immediately and doesn't need to poll or list them again
// This also works if the dapp loads before any wallets (it will initialize, see no wallets, then see wallets as they load)
})();