diff --git a/example/src/App.tsx b/example/src/App.tsx index cd6bd031..092fddf5 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -6,21 +6,24 @@ import { Lattice } from './Lattice'; function App() { const [label, setLabel] = useState('No Device'); - const getStoredClient = () => + const getStoredClient = async () => window.localStorage.getItem('storedClient') || ''; - const setStoredClient = (storedClient: string | null) => { + const setStoredClient = async (storedClient: string | null) => { if (!storedClient) return; window.localStorage.setItem('storedClient', storedClient); - const client = getClient(); - setLabel(client?.getDeviceId() || 'No Device'); + getClient()?.then((client) => { + setLabel(client?.getDeviceId() || 'No Device'); + }); }; useEffect(() => { - if (getStoredClient()) { - setup({ getStoredClient, setStoredClient }); - } + getStoredClient().then((storedClient) => { + if (storedClient) { + setup({ getStoredClient, setStoredClient }); + } + }); }, []); const submitInit = (e: any) => { diff --git a/src/api/state.ts b/src/api/state.ts index 6d7e7f6e..9f053cd8 100644 --- a/src/api/state.ts +++ b/src/api/state.ts @@ -6,9 +6,9 @@ export const setSaveClient = (fn: (clientData: string | null) => void) => { saveClient = fn; }; -export let loadClient: () => Client | undefined; +export let loadClient: () => Promise; -export const setLoadClient = (fn: () => Client | undefined) => { +export const setLoadClient = (fn: () => Promise) => { loadClient = fn; }; diff --git a/src/api/utilities.ts b/src/api/utilities.ts index 13059da6..dddbe853 100644 --- a/src/api/utilities.ts +++ b/src/api/utilities.ts @@ -24,14 +24,14 @@ export const setup = async ({ deviceId?: string; password?: string; name?: string; - getStoredClient: () => string; - setStoredClient: (clientData: string | null) => void; + getStoredClient: () => Promise; + setStoredClient: (clientData: string | null) => Promise; }) => { if (!getStoredClient) throw new Error('Client data getter required'); setSaveClient(buildSaveClientFn(setStoredClient)); if (!setStoredClient) throw new Error('Client data setter required'); - setLoadClient(buildLoadClientFn(getStoredClient)); + setLoadClient(await buildLoadClientFn(getStoredClient)); if (deviceId && password && name) { const privKey = Utils.generateAppSecret(deviceId, password, name); @@ -41,7 +41,7 @@ export const setup = async ({ return isPaired; }); } else { - const client = loadClient(); + const client = await loadClient(); if (!client) throw new Error('Client not initialized'); const deviceId = client.getDeviceId(); if (!client.ephemeralPub && deviceId) { @@ -60,8 +60,8 @@ export const setup = async ({ * and two concurrent requests could result in the same key being used twice or the wrong key being * written to memory locally. */ -export const queue = (fn: (client: Client) => Promise) => { - const client = loadClient(); +export const queue = async (fn: (client: Client) => Promise) => { + const client = await loadClient(); if (!client) throw new Error('Client not initialized'); if (!getFunctionQueue()) { setFunctionQueue(Promise.resolve()); @@ -103,9 +103,9 @@ const buildSaveClientFn = ( }; }; -const buildLoadClientFn = (getStoredClient: () => string) => { - return () => { - const clientData = getStoredClient(); +const buildLoadClientFn = (getStoredClient: () => Promise) => { + return async () => { + const clientData = await getStoredClient(); if (!clientData) return undefined; const stateData = decodeClientData(clientData); if (!stateData) return undefined;