Update PhantomWalletAdapter in serum (#71)
This commit is contained in:
parent
23f1032bbc
commit
7c5e888053
|
@ -20,21 +20,15 @@ interface PhantomProvider {
|
|||
disconnect: () => Promise<void>;
|
||||
on: (event: PhantomEvent, handler: (args: any) => void) => void;
|
||||
request: (method: PhantomRequestMethod, params: any) => Promise<any>;
|
||||
listeners: (event: PhantomEvent) => (() => void)[];
|
||||
}
|
||||
|
||||
const SUPPORTED_PHANTOM_EVENTS: PhantomEvent[] = ['connect', 'disconnect'];
|
||||
|
||||
export class PhantomWalletAdapter
|
||||
extends EventEmitter
|
||||
implements WalletAdapter {
|
||||
constructor() {
|
||||
super();
|
||||
this.connect = this.connect.bind(this);
|
||||
window.onload = () => {
|
||||
for (const event of SUPPORTED_PHANTOM_EVENTS) {
|
||||
this._provider?.on(event, (...args) => this.emit(event, ...args));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private get _provider(): PhantomProvider | undefined {
|
||||
|
@ -44,6 +38,14 @@ export class PhantomWalletAdapter
|
|||
return undefined;
|
||||
}
|
||||
|
||||
private _handleConnect = (...args) => {
|
||||
this.emit('connect', ...args);
|
||||
}
|
||||
|
||||
private _handleDisconnect = (...args) => {
|
||||
this.emit('disconnect', ...args);
|
||||
}
|
||||
|
||||
get connected() {
|
||||
return this._provider?.isConnected || false;
|
||||
}
|
||||
|
@ -83,7 +85,12 @@ export class PhantomWalletAdapter
|
|||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (!this._provider.listeners('connect').length) {
|
||||
this._provider?.on('connect', this._handleConnect);
|
||||
}
|
||||
if (!this._provider.listeners('disconnect').length) {
|
||||
this._provider?.on('disconnect', this._handleDisconnect);
|
||||
}
|
||||
return this._provider?.connect();
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue