Skip to content

Commit

Permalink
event emitter for react-native wallet creation
Browse files Browse the repository at this point in the history
  • Loading branch information
maycon-mello committed Nov 1, 2023
1 parent 8bf6abe commit 5ea55d7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
19 changes: 19 additions & 0 deletions packages/react-native/lib/wallet.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {
walletEventEmitter,
initializeWallet,
getWallet,
WalletEvents,
} from './wallet';

describe('Wallet handler', () => {
it('expect to dispatchEvent to webview', async () => {
jest.spyOn(walletEventEmitter, 'on');

await initializeWallet();

expect(getWallet()).toBeDefined();
expect(walletEventEmitter.on).toBeCalledWith(
WalletEvents.walletInitialized,
);
});
});
19 changes: 17 additions & 2 deletions packages/react-native/lib/wallet.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
import {EventEmitter} from 'events';
import {createWallet, IWallet} from '@docknetwork/wallet-sdk-core/src/wallet';
import {dockDocumentNetworkResolver} from '@docknetwork/wallet-sdk-core/src/network-resolver';
import {DataStoreConfigs} from '@docknetwork/wallet-sdk-data-store/src/types';
import { createDIDProvider, IDIDProvider } from '@docknetwork/wallet-sdk-core/src/did-provider';
import { createMessageProvider, IMessageProvider } from '@docknetwork/wallet-sdk-core/src/message-provider';
import {
createDIDProvider,
IDIDProvider,
} from '@docknetwork/wallet-sdk-core/src/did-provider';
import {
createMessageProvider,
IMessageProvider,
} from '@docknetwork/wallet-sdk-core/src/message-provider';

let wallet: IWallet;
let didProvider: IDIDProvider;
let messageProvider: IMessageProvider;

export const WalletEvents = {
walletInitialized: 'walletInitialized',
};

export const walletEventEmitter = new EventEmitter();

export function getWallet() {
if (!wallet) {
throw new Error('Wallet not initialized');
Expand Down Expand Up @@ -76,5 +89,7 @@ export async function initializeWallet(params: DataStoreConfigs = {} as any) {

setMessageProvider(_messageProvider);

walletEventEmitter.emit(WalletEvents.walletInitialized);

return _wallet;
}

0 comments on commit 5ea55d7

Please sign in to comment.