Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

chore: event subscription hook #308

Merged
merged 2 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .changeset/five-pumas-wash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
'@reown/appkit-scaffold-react-native': patch
'@reown/appkit-ethers5-react-native': patch
'@reown/appkit-ethers-react-native': patch
'@reown/appkit-wagmi-react-native': patch
'@reown/appkit-core-react-native': patch
'@reown/appkit-auth-ethers-react-native': patch
'@reown/appkit-auth-wagmi-react-native': patch
'@reown/appkit-coinbase-ethers-react-native': patch
'@reown/appkit-coinbase-wagmi-react-native': patch
'@reown/appkit-common-react-native': patch
'@reown/appkit-scaffold-utils-react-native': patch
'@reown/appkit-siwe-react-native': patch
'@reown/appkit-ui-react-native': patch
'@reown/appkit-wallet-react-native': patch
---

chore: added event subscription hook
10 changes: 9 additions & 1 deletion packages/core/src/controllers/EventsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ApiController } from './ApiController';
import { OptionsController } from './OptionsController';
import { CoreHelperUtil } from '../utils/CoreHelperUtil';
import { FetchUtil } from '../utils/FetchUtil';
import type { Event } from '../utils/TypeUtil';
import type { Event, EventName } from '../utils/TypeUtil';

// -- Helpers ------------------------------------------- //
const baseUrl = CoreHelperUtil.getAnalyticsUrl();
Expand Down Expand Up @@ -33,6 +33,14 @@ export const EventsController = {
return sub(state, () => callback(state));
},

subscribeEvent(event: EventName, callback: (newEvent: EventsControllerState) => void) {
return sub(state, () => {
if (state.data.event === event) {
callback(state);
}
});
},

async _sendAnalyticsEvent(data: EventsControllerState['data'], timestamp: number) {
if (excluded.includes(data.event)) {
return;
Expand Down
45 changes: 45 additions & 0 deletions packages/core/src/utils/TypeUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,51 @@ export type CustomWallet = Pick<
>;

// -- EventsController Types ----------------------------------------------------
export type EventName =
| 'MODAL_LOADED'
| 'MODAL_OPEN'
| 'MODAL_CLOSE'
| 'CLICK_ALL_WALLETS'
| 'CLICK_NETWORKS'
| 'SWITCH_NETWORK'
| 'SELECT_WALLET'
| 'CONNECT_SUCCESS'
| 'CONNECT_ERROR'
| 'DISCONNECT_SUCCESS'
| 'DISCONNECT_ERROR'
| 'CLICK_WALLET_HELP'
| 'CLICK_NETWORK_HELP'
| 'CLICK_GET_WALLET'
| 'EMAIL_LOGIN_SELECTED'
| 'EMAIL_SUBMITTED'
| 'DEVICE_REGISTERED_FOR_EMAIL'
| 'EMAIL_VERIFICATION_CODE_SENT'
| 'EMAIL_VERIFICATION_CODE_PASS'
| 'EMAIL_VERIFICATION_CODE_FAIL'
| 'EMAIL_EDIT'
| 'EMAIL_EDIT_COMPLETE'
| 'EMAIL_UPGRADE_FROM_MODAL'
| 'CLICK_SIGN_SIWE_MESSAGE'
| 'CLICK_CANCEL_SIWE'
| 'SIWE_AUTH_SUCCESS'
| 'SIWE_AUTH_ERROR'
| 'CLICK_TRANSACTIONS'
| 'ERROR_FETCH_TRANSACTIONS'
| 'LOAD_MORE_TRANSACTIONS'
| 'OPEN_SEND'
| 'OPEN_SWAP'
| 'INITIATE_SWAP'
| 'SWAP_SUCCESS'
| 'SWAP_ERROR'
| 'SEND_INITIATED'
| 'SEND_SUCCESS'
| 'SEND_ERROR'
| 'SOCIAL_LOGIN_STARTED'
| 'SOCIAL_LOGIN_SUCCESS'
| 'SOCIAL_LOGIN_REQUEST_USER_DATA'
| 'SOCIAL_LOGIN_CANCELED'
| 'SOCIAL_LOGIN_ERROR'
| 'SET_PREFERRED_ACCOUNT_TYPE';

export type Event =
| {
Expand Down
23 changes: 21 additions & 2 deletions packages/ethers/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export {
NetworkButton,
AppKit
} from '@reown/appkit-scaffold-react-native';
import type { EventName, EventsControllerState } from '@reown/appkit-scaffold-react-native';

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

Expand Down Expand Up @@ -129,7 +130,7 @@ export function useAppKitError() {
};
}

export function useAppKitEvents() {
export function useAppKitEvents(callback?: (newEvent: EventsControllerState) => void) {
if (!modal) {
throw new Error('Please call "createAppKit" before using "useAppKitEvents" hook');
}
Expand All @@ -139,12 +140,30 @@ export function useAppKitEvents() {
useEffect(() => {
const unsubscribe = modal?.subscribeEvents(newEvent => {
setEvents({ ...newEvent });
callback?.(newEvent);
});

return () => {
unsubscribe?.();
};
}, []);
}, [callback]);

return event;
}

export function useAppKitEventSubscription(
event: EventName,
callback: (newEvent: EventsControllerState) => void
) {
if (!modal) {
throw new Error('Please call "createAppKit" before using "useAppKitEventSubscription" hook');
}

useEffect(() => {
const unsubscribe = modal?.subscribeEvent(event, callback);

return () => {
unsubscribe?.();
};
}, [callback, event]);
}
23 changes: 21 additions & 2 deletions packages/ethers5/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export {
NetworkButton,
AppKit
} from '@reown/appkit-scaffold-react-native';
import type { EventName, EventsControllerState } from '@reown/appkit-scaffold-react-native';
import {
ConstantsUtil,
EthersStoreUtil,
Expand Down Expand Up @@ -126,7 +127,7 @@ export function useAppKitError() {
};
}

export function useAppKitEvents() {
export function useAppKitEvents(callback?: (newEvent: EventsControllerState) => void) {
if (!modal) {
throw new Error('Please call "createAppKit" before using "useAppKitEvents" hook');
}
Expand All @@ -136,12 +137,30 @@ export function useAppKitEvents() {
useEffect(() => {
const unsubscribe = modal?.subscribeEvents(newEvent => {
setEvents({ ...newEvent });
callback?.(newEvent);
});

return () => {
unsubscribe?.();
};
}, []);
}, [callback]);

return event;
}

export function useAppKitEventSubscription(
event: EventName,
callback: (newEvent: EventsControllerState) => void
) {
if (!modal) {
throw new Error('Please call "createAppKit" before using "useAppKitEventSubscription" hook');
}

useEffect(() => {
const unsubscribe = modal?.subscribeEvent(event, callback);

return () => {
unsubscribe?.();
};
}, [callback, event]);
}
7 changes: 6 additions & 1 deletion packages/scaffold/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import type {
ThemeControllerState,
Connector,
ConnectedWalletInfo,
Features
Features,
EventName
} from '@reown/appkit-core-react-native';
import { SIWEController, type SIWEControllerClient } from '@reown/appkit-siwe-react-native';
import {
Expand Down Expand Up @@ -145,6 +146,10 @@ export class AppKitScaffold {
return EventsController.subscribe(callback);
}

public subscribeEvent(event: EventName, callback: (newEvent: EventsControllerState) => void) {
return EventsController.subscribeEvent(event, callback);
}

public resolveReownName = async (name: string) => {
const wcNameAddress = await EnsController.resolveName(name);
const networkNameAddresses = wcNameAddress?.addresses
Expand Down
23 changes: 21 additions & 2 deletions packages/wagmi/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export {
NetworkButton,
AppKit
} from '@reown/appkit-scaffold-react-native';
import type { EventName, EventsControllerState } from '@reown/appkit-scaffold-react-native';
import { ConstantsUtil } from '@reown/appkit-scaffold-utils-react-native';
export { defaultWagmiConfig } from './utils/defaultWagmiConfig';
import { useEffect, useState, useSyncExternalStore } from 'react';
Expand Down Expand Up @@ -82,7 +83,7 @@ export function useWalletInfo() {
return { walletInfo };
}

export function useAppKitEvents() {
export function useAppKitEvents(callback?: (newEvent: EventsControllerState) => void) {
if (!modal) {
throw new Error('Please call "createAppKit" before using "useAppKitEvents" hook');
}
Expand All @@ -92,12 +93,30 @@ export function useAppKitEvents() {
useEffect(() => {
const unsubscribe = modal?.subscribeEvents(newEvent => {
setEvents({ ...newEvent });
callback?.(newEvent);
});

return () => {
unsubscribe?.();
};
}, []);
}, [callback]);

return event;
}

export function useAppKitEventSubscription(
event: EventName,
callback: (newEvent: EventsControllerState) => void
) {
if (!modal) {
throw new Error('Please call "createAppKit" before using "useAppKitEventSubscription" hook');
}

useEffect(() => {
const unsubscribe = modal?.subscribeEvent(event, callback);

return () => {
unsubscribe?.();
};
}, [callback, event]);
}
Loading