Skip to content

Commit

Permalink
adds async getter support
Browse files Browse the repository at this point in the history
  • Loading branch information
netbonus committed Feb 26, 2024
1 parent 32b5389 commit a14aa34
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
17 changes: 10 additions & 7 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
4 changes: 2 additions & 2 deletions src/api/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ export const setSaveClient = (fn: (clientData: string | null) => void) => {
saveClient = fn;
};

export let loadClient: () => Client | undefined;
export let loadClient: () => Promise<Client | undefined>;

export const setLoadClient = (fn: () => Client | undefined) => {
export const setLoadClient = (fn: () => Promise<Client | undefined>) => {
loadClient = fn;
};

Expand Down
18 changes: 9 additions & 9 deletions src/api/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ export const setup = async ({
deviceId?: string;
password?: string;
name?: string;
getStoredClient: () => string;
setStoredClient: (clientData: string | null) => void;
getStoredClient: () => Promise<string>;
setStoredClient: (clientData: string | null) => Promise<void>;
}) => {
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);
Expand All @@ -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) {
Expand All @@ -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<any>) => {
const client = loadClient();
export const queue = async (fn: (client: Client) => Promise<any>) => {
const client = await loadClient();
if (!client) throw new Error('Client not initialized');
if (!getFunctionQueue()) {
setFunctionQueue(Promise.resolve());
Expand Down Expand Up @@ -103,9 +103,9 @@ const buildSaveClientFn = (
};
};

const buildLoadClientFn = (getStoredClient: () => string) => {
return () => {
const clientData = getStoredClient();
const buildLoadClientFn = (getStoredClient: () => Promise<string>) => {
return async () => {
const clientData = await getStoredClient();
if (!clientData) return undefined;
const stateData = decodeClientData(clientData);
if (!stateData) return undefined;
Expand Down

0 comments on commit a14aa34

Please sign in to comment.