Skip to content

Commit

Permalink
feat: add network sync callbacks
Browse files Browse the repository at this point in the history
mostly to be used for analytics
  • Loading branch information
Prithpal-Sooriya committed Oct 24, 2024
1 parent 49ef491 commit b18119c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,30 @@ type ControllerConfig = {
*/
onAccountNameUpdated?: (profileId: string) => void;
};

networkSyncing?: {
/**
* Callback that fires when network sync adds a network
* This is used for analytics.
* @param profileId - ID for a given User (shared cross devices once authenticated)
* @param chainId - Chain ID for the network added (in hex)
*/
onNetworkAdded?: (profileId: string, chainId: string) => void;
/**
* Callback that fires when network sync updates a network
* This is used for analytics.
* @param profileId - ID for a given User (shared cross devices once authenticated)
* @param chainId - Chain ID for the network added (in hex)
*/
onNetworkUpdated?: (profileId: string, chainId: string) => void;
/**
* Callback that fires when network sync deletes a network
* This is used for analytics.
* @param profileId - ID for a given User (shared cross devices once authenticated)
* @param chainId - Chain ID for the network added (in hex)
*/
onNetworkRemoved?: (profileId: string, chainId: string) => void;
};
};

// Messenger Actions
Expand Down Expand Up @@ -1015,9 +1039,17 @@ export default class UserStorageController extends BaseController<
return;
}

const profileId = await this.#auth.getProfileId();

await performMainNetworkSync({
messenger: this.messagingSystem,
getStorageConfig: this.#getStorageOptions,
onNetworkAdded: (cId) =>
this.#config?.networkSyncing?.onNetworkAdded?.(profileId, cId),
onNetworkUpdated: (cId) =>
this.#config?.networkSyncing?.onNetworkUpdated?.(profileId, cId),
onNetworkRemoved: (cId) =>
this.#config?.networkSyncing?.onNetworkRemoved?.(profileId, cId),
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@ import { getAllRemoteNetworks } from './services';
import { findNetworksToUpdate } from './sync-all';
import { batchUpdateNetworks, deleteNetwork } from './sync-mutations';

type NetworkSyncingProps = {
type StartNetworkSyncingProps = {
messenger: UserStorageControllerMessenger;
getStorageConfig: () => Promise<UserStorageBaseOptions | null>;
};

type PerformMainNetworkSyncProps = {
messenger: UserStorageControllerMessenger;
getStorageConfig: () => Promise<UserStorageBaseOptions | null>;
onNetworkAdded?: (chainId: string) => void;
onNetworkUpdated?: (chainId: string) => void;
onNetworkRemoved?: (chainId: string) => void;
};

/**
* Global in-mem cache to signify that the network syncing is in progress
* Ensures that listeners do not fire during main sync (prevent double requests)
Expand All @@ -27,7 +35,7 @@ let isMainNetworkSyncInProgress = false;
*
* @param props - parameters used for initializing and enabling network syncing
*/
export function startNetworkSyncing(props: NetworkSyncingProps) {
export function startNetworkSyncing(props: StartNetworkSyncingProps) {
const { messenger, getStorageConfig } = props;
try {
messenger.subscribe(
Expand Down Expand Up @@ -61,7 +69,9 @@ export function startNetworkSyncing(props: NetworkSyncingProps) {
* It will fetch local networks and remote networks, then determines which networks (local and remote) to add/update
* @param props - parameters used for this main sync
*/
export async function performMainNetworkSync(props: NetworkSyncingProps) {
export async function performMainNetworkSync(
props: PerformMainNetworkSyncProps,
) {
const { messenger, getStorageConfig } = props;
isMainNetworkSyncInProgress = true;
try {
Expand Down Expand Up @@ -92,6 +102,7 @@ export async function performMainNetworkSync(props: NetworkSyncingProps) {
networksToUpdate.missingLocalNetworks.forEach((n) => {
try {
messenger.call('NetworkController:addNetwork', n);
props.onNetworkAdded?.(n.chainId);
} catch {
// Silently fail, we can try this again on next main sync
}
Expand All @@ -103,6 +114,7 @@ export async function performMainNetworkSync(props: NetworkSyncingProps) {
const promises = networksToUpdate.localNetworksToUpdate.map(async (n) => {
try {
await messenger.call('NetworkController:updateNetwork', n.chainId, n);
props.onNetworkUpdated?.(n.chainId);
} catch {
// Silently fail, we can try this again on next main sync
}
Expand All @@ -115,6 +127,7 @@ export async function performMainNetworkSync(props: NetworkSyncingProps) {
networksToUpdate.localNetworksToRemove.forEach((n) => {
try {
messenger.call('NetworkController:removeNetwork', n.chainId);
props.onNetworkRemoved?.(n.chainId);
} catch {
// Silently fail, we can try this again on next main sync
}
Expand Down

0 comments on commit b18119c

Please sign in to comment.