Skip to content

Commit 919b34e

Browse files
Merge pull request #308 from reown-com/chore/event-hook
chore: event subscription hook
2 parents e9ec408 + c93188e commit 919b34e

File tree

7 files changed

+141
-8
lines changed

7 files changed

+141
-8
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: 21 additions & 2 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, EventsControllerState } from '@reown/appkit-scaffold-react-native';
1617

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

@@ -129,7 +130,7 @@ export function useAppKitError() {
129130
};
130131
}
131132

132-
export function useAppKitEvents() {
133+
export function useAppKitEvents(callback?: (newEvent: EventsControllerState) => void) {
133134
if (!modal) {
134135
throw new Error('Please call "createAppKit" before using "useAppKitEvents" hook');
135136
}
@@ -139,12 +140,30 @@ export function useAppKitEvents() {
139140
useEffect(() => {
140141
const unsubscribe = modal?.subscribeEvents(newEvent => {
141142
setEvents({ ...newEvent });
143+
callback?.(newEvent);
142144
});
143145

144146
return () => {
145147
unsubscribe?.();
146148
};
147-
}, []);
149+
}, [callback]);
148150

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

packages/ethers5/src/index.tsx

Lines changed: 21 additions & 2 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, EventsControllerState } from '@reown/appkit-scaffold-react-native';
910
import {
1011
ConstantsUtil,
1112
EthersStoreUtil,
@@ -126,7 +127,7 @@ export function useAppKitError() {
126127
};
127128
}
128129

129-
export function useAppKitEvents() {
130+
export function useAppKitEvents(callback?: (newEvent: EventsControllerState) => void) {
130131
if (!modal) {
131132
throw new Error('Please call "createAppKit" before using "useAppKitEvents" hook');
132133
}
@@ -136,12 +137,30 @@ export function useAppKitEvents() {
136137
useEffect(() => {
137138
const unsubscribe = modal?.subscribeEvents(newEvent => {
138139
setEvents({ ...newEvent });
140+
callback?.(newEvent);
139141
});
140142

141143
return () => {
142144
unsubscribe?.();
143145
};
144-
}, []);
146+
}, [callback]);
145147

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

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: 21 additions & 2 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, EventsControllerState } 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';
@@ -82,7 +83,7 @@ export function useWalletInfo() {
8283
return { walletInfo };
8384
}
8485

85-
export function useAppKitEvents() {
86+
export function useAppKitEvents(callback?: (newEvent: EventsControllerState) => void) {
8687
if (!modal) {
8788
throw new Error('Please call "createAppKit" before using "useAppKitEvents" hook');
8889
}
@@ -92,12 +93,30 @@ export function useAppKitEvents() {
9293
useEffect(() => {
9394
const unsubscribe = modal?.subscribeEvents(newEvent => {
9495
setEvents({ ...newEvent });
96+
callback?.(newEvent);
9597
});
9698

9799
return () => {
98100
unsubscribe?.();
99101
};
100-
}, []);
102+
}, [callback]);
101103

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

0 commit comments

Comments
 (0)