Skip to content

Commit cdc1af3

Browse files
chore: export event subscription hook
1 parent 3192787 commit cdc1af3

File tree

7 files changed

+129
-2
lines changed

7 files changed

+129
-2
lines changed

.changeset/five-pumas-wash.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
'@reown/appkit-scaffold-react-native': patch
3+
'@reown/appkit-ethers5-react-native': patch
4+
'@reown/appkit-ethers-react-native': patch
5+
'@reown/appkit-wagmi-react-native': patch
6+
'@reown/appkit-core-react-native': patch
7+
'@reown/appkit-auth-ethers-react-native': patch
8+
'@reown/appkit-auth-wagmi-react-native': patch
9+
'@reown/appkit-coinbase-ethers-react-native': patch
10+
'@reown/appkit-coinbase-wagmi-react-native': patch
11+
'@reown/appkit-common-react-native': patch
12+
'@reown/appkit-scaffold-utils-react-native': patch
13+
'@reown/appkit-siwe-react-native': patch
14+
'@reown/appkit-ui-react-native': patch
15+
'@reown/appkit-wallet-react-native': patch
16+
---
17+
18+
chore: added event subscription hook

packages/core/src/controllers/EventsController.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ApiController } from './ApiController';
33
import { OptionsController } from './OptionsController';
44
import { CoreHelperUtil } from '../utils/CoreHelperUtil';
55
import { FetchUtil } from '../utils/FetchUtil';
6-
import type { Event } from '../utils/TypeUtil';
6+
import type { Event, EventName } from '../utils/TypeUtil';
77

88
// -- Helpers ------------------------------------------- //
99
const baseUrl = CoreHelperUtil.getAnalyticsUrl();
@@ -33,6 +33,14 @@ export const EventsController = {
3333
return sub(state, () => callback(state));
3434
},
3535

36+
subscribeEvent(event: EventName, callback: (newEvent: EventsControllerState) => void) {
37+
return sub(state, () => {
38+
if (state.data.event === event) {
39+
callback(state);
40+
}
41+
});
42+
},
43+
3644
async _sendAnalyticsEvent(data: EventsControllerState['data'], timestamp: number) {
3745
if (excluded.includes(data.event)) {
3846
return;

packages/core/src/utils/TypeUtil.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,51 @@ export type CustomWallet = Pick<
343343
>;
344344

345345
// -- EventsController Types ----------------------------------------------------
346+
export type EventName =
347+
| 'MODAL_LOADED'
348+
| 'MODAL_OPEN'
349+
| 'MODAL_CLOSE'
350+
| 'CLICK_ALL_WALLETS'
351+
| 'CLICK_NETWORKS'
352+
| 'SWITCH_NETWORK'
353+
| 'SELECT_WALLET'
354+
| 'CONNECT_SUCCESS'
355+
| 'CONNECT_ERROR'
356+
| 'DISCONNECT_SUCCESS'
357+
| 'DISCONNECT_ERROR'
358+
| 'CLICK_WALLET_HELP'
359+
| 'CLICK_NETWORK_HELP'
360+
| 'CLICK_GET_WALLET'
361+
| 'EMAIL_LOGIN_SELECTED'
362+
| 'EMAIL_SUBMITTED'
363+
| 'DEVICE_REGISTERED_FOR_EMAIL'
364+
| 'EMAIL_VERIFICATION_CODE_SENT'
365+
| 'EMAIL_VERIFICATION_CODE_PASS'
366+
| 'EMAIL_VERIFICATION_CODE_FAIL'
367+
| 'EMAIL_EDIT'
368+
| 'EMAIL_EDIT_COMPLETE'
369+
| 'EMAIL_UPGRADE_FROM_MODAL'
370+
| 'CLICK_SIGN_SIWE_MESSAGE'
371+
| 'CLICK_CANCEL_SIWE'
372+
| 'SIWE_AUTH_SUCCESS'
373+
| 'SIWE_AUTH_ERROR'
374+
| 'CLICK_TRANSACTIONS'
375+
| 'ERROR_FETCH_TRANSACTIONS'
376+
| 'LOAD_MORE_TRANSACTIONS'
377+
| 'OPEN_SEND'
378+
| 'OPEN_SWAP'
379+
| 'INITIATE_SWAP'
380+
| 'SWAP_SUCCESS'
381+
| 'SWAP_ERROR'
382+
| 'SEND_INITIATED'
383+
| 'SEND_SUCCESS'
384+
| 'SEND_ERROR'
385+
| 'SOCIAL_LOGIN_STARTED'
386+
| 'SOCIAL_LOGIN_SUCCESS'
387+
| 'SOCIAL_LOGIN_REQUEST_USER_DATA'
388+
| 'SOCIAL_LOGIN_CANCELED'
389+
| 'SOCIAL_LOGIN_ERROR'
390+
| 'SET_PREFERRED_ACCOUNT_TYPE';
346391

347392
export type Event =
348393
| {

packages/ethers/src/index.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export {
1313
NetworkButton,
1414
AppKit
1515
} from '@reown/appkit-scaffold-react-native';
16+
import type { EventName } from '@reown/appkit-scaffold-react-native';
1617

1718
export { defaultConfig } from './utils/defaultConfig';
1819

@@ -148,3 +149,19 @@ export function useAppKitEvents() {
148149

149150
return event;
150151
}
152+
153+
export function useAppKitEventSubscription(event: EventName, callback: () => void) {
154+
if (!modal) {
155+
throw new Error('Please call "createAppKit" before using "useAppKitEventSubscription" hook');
156+
}
157+
158+
useEffect(() => {
159+
const unsubscribe = modal?.subscribeEvent(event, callback);
160+
161+
return () => {
162+
unsubscribe?.();
163+
};
164+
}, [callback, event]);
165+
166+
return event;
167+
}

packages/ethers5/src/index.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export {
66
NetworkButton,
77
AppKit
88
} from '@reown/appkit-scaffold-react-native';
9+
import type { EventName } from '@reown/appkit-scaffold-react-native';
910
import {
1011
ConstantsUtil,
1112
EthersStoreUtil,
@@ -145,3 +146,19 @@ export function useAppKitEvents() {
145146

146147
return event;
147148
}
149+
150+
export function useAppKitEventSubscription(event: EventName, callback: () => void) {
151+
if (!modal) {
152+
throw new Error('Please call "createAppKit" before using "useAppKitEventSubscription" hook');
153+
}
154+
155+
useEffect(() => {
156+
const unsubscribe = modal?.subscribeEvent(event, callback);
157+
158+
return () => {
159+
unsubscribe?.();
160+
};
161+
}, [callback, event]);
162+
163+
return event;
164+
}

packages/scaffold/src/client.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import type {
1212
ThemeControllerState,
1313
Connector,
1414
ConnectedWalletInfo,
15-
Features
15+
Features,
16+
EventName
1617
} from '@reown/appkit-core-react-native';
1718
import { SIWEController, type SIWEControllerClient } from '@reown/appkit-siwe-react-native';
1819
import {
@@ -145,6 +146,10 @@ export class AppKitScaffold {
145146
return EventsController.subscribe(callback);
146147
}
147148

149+
public subscribeEvent(event: EventName, callback: (newEvent: EventsControllerState) => void) {
150+
return EventsController.subscribeEvent(event, callback);
151+
}
152+
148153
public resolveReownName = async (name: string) => {
149154
const wcNameAddress = await EnsController.resolveName(name);
150155
const networkNameAddresses = wcNameAddress?.addresses

packages/wagmi/src/index.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export {
66
NetworkButton,
77
AppKit
88
} from '@reown/appkit-scaffold-react-native';
9+
import type { EventName } from '@reown/appkit-scaffold-react-native';
910
import { ConstantsUtil } from '@reown/appkit-scaffold-utils-react-native';
1011
export { defaultWagmiConfig } from './utils/defaultWagmiConfig';
1112
import { useEffect, useState, useSyncExternalStore } from 'react';
@@ -101,3 +102,19 @@ export function useAppKitEvents() {
101102

102103
return event;
103104
}
105+
106+
export function useAppKitEventSubscription(event: EventName, callback: () => void) {
107+
if (!modal) {
108+
throw new Error('Please call "createAppKit" before using "useAppKitEventSubscription" hook');
109+
}
110+
111+
useEffect(() => {
112+
const unsubscribe = modal?.subscribeEvent(event, callback);
113+
114+
return () => {
115+
unsubscribe?.();
116+
};
117+
}, [callback, event]);
118+
119+
return event;
120+
}

0 commit comments

Comments
 (0)