Skip to content

Commit

Permalink
use store subscription rather than poll; add timeout (defaults to 1 s…
Browse files Browse the repository at this point in the history
…econd)
  • Loading branch information
technoplato committed Dec 19, 2024
1 parent e6dc449 commit 2ebf4f0
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions data/store/appStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,20 @@ export const useAppStore = create<AppStoreType>()(
* This can be used, for example, to ensure that the xmtp clients have been instantiated and the
* conversation list is fetched before navigating to a conversation.
*
* As of December 19, 2024, when we say Hydration, we mean that the following are true:
* @param timeout - The maximum time to wait for hydration in milliseconds. Defaults to 1000ms (1 second).
*
* As of December 19, 2024, when we say XMTP client hydration, we mean that the following are true:
* 1) XMTP client for all accounts added to device have been instantiated and cached
* 2) Conversation list for all accounts added to device have been fetched from the network and cached
* 3) Inbox ID for all accounts added to device have been fetched from the network and cached
*
* You can observe that logic in the HydrationStateHandler, and that will likely be moved once
* we refactor accounts to be InboxID based in upcoming refactors.
*/
export const waitForXmtpClientHydration = (): Promise<void> => {
return new Promise((resolve) => {
export const waitForXmtpClientHydration = (
timeout: number = 1000
): Promise<void> => {
const hydrationPromise = new Promise<void>((resolve) => {
const { hydrationDone } = useAppStore.getState();
if (hydrationDone) {
resolve();
Expand All @@ -115,4 +119,12 @@ export const waitForXmtpClientHydration = (): Promise<void> => {
}
});
});

const timeoutPromise = new Promise<void>((_, reject) => {
setTimeout(() => {
reject(new Error("Xmtp client hydration timed out"));
}, timeout);
});

return Promise.race([hydrationPromise, timeoutPromise]);
};

0 comments on commit 2ebf4f0

Please sign in to comment.