Skip to content

Commit

Permalink
Feat/async client state (#576)
Browse files Browse the repository at this point in the history
* feat(client): adjust function calls to async/await

* feat(repo): add nightly builds to pkg.pr.new

* fix merge conflicts

* remove nightly

---------

Co-authored-by: netbonus <[email protected]>
  • Loading branch information
mrcnk and netbonus authored Oct 16, 2024
1 parent ca6c981 commit 790c3a6
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 28 deletions.
6 changes: 3 additions & 3 deletions src/__test__/utils/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ expect.extend({
},
});

export const setStoredClient = (data: string) => {
export const setStoredClient = async (data: string) => {
try {
fs.writeFileSync('./client.temp', data);
} catch (err) {
return '';
return;
}
};

export const getStoredClient = () => {
export const getStoredClient = async () => {
try {
return fs.readFileSync('./client.temp', 'utf8');
} catch (err) {
Expand Down
18 changes: 9 additions & 9 deletions src/api/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { setSaveClient, setLoadClient, saveClient, loadClient } from './state';
import { buildLoadClientFn, buildSaveClientFn, queue } from './utilities';

/**
* @interface {Object} SetupParameters - paramaters for the setup function
* @interface {Object} SetupParameters - parameters for the setup function
* @prop {string} SetupParameters.deviceId - the device id of the client
* @prop {string} SetupParameters.password - the password of the client
* @prop {string} SetupParameters.name - the name of the client
Expand All @@ -18,12 +18,12 @@ type SetupParameters =
password: string;
name: string;
appSecret?: string;
getStoredClient: () => string;
setStoredClient: (clientData: string | null) => void;
getStoredClient: () => Promise<string>;
setStoredClient: (clientData: string | null) => Promise<void>;
}
| {
getStoredClient: () => string;
setStoredClient: (clientData: string | null) => void;
getStoredClient: () => Promise<string>;
setStoredClient: (clientData: string | null) => Promise<void>;
};

/**
Expand Down Expand Up @@ -57,18 +57,18 @@ export const setup = async (params: SetupParameters): Promise<boolean> => {
privKey,
name: params.name,
});
return client.connect(params.deviceId).then((isPaired) => {
saveClient(client.getStateData());
return client.connect(params.deviceId).then(async (isPaired) => {
await saveClient(client.getStateData());
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) {
return connect(deviceId);
} else {
saveClient(client.getStateData());
await saveClient(client.getStateData());
return Promise.resolve(true);
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/api/state.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Client } from '../client';

export let saveClient: (clientData: string | null) => void;
export let saveClient: (clientData: string | null) => Promise<void>;

export const setSaveClient = (fn: (clientData: string | null) => void) => {
export const setSaveClient = (fn: (clientData: string | null) => Promise<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
20 changes: 10 additions & 10 deletions src/api/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ import { EXTERNAL, HARDENED_OFFSET } from '../constants';
*
* @internal
*/
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());
}
setFunctionQueue(
getFunctionQueue().then(() =>
fn(client)
getFunctionQueue().then(async () =>
await fn(client)
.catch((err) => {
// Empty the queue if any function call fails
setFunctionQueue(Promise.resolve());
Expand All @@ -50,18 +50,18 @@ const decodeClientData = (clientData: string) => {
};

export const buildSaveClientFn = (
setStoredClient: (clientData: string | null) => void,
setStoredClient: (clientData: string | null) => Promise<void>,
) => {
return (clientData: string | null) => {
return async (clientData: string | null) => {
if (!clientData) return;
const encodedData = encodeClientData(clientData);
setStoredClient(encodedData);
await setStoredClient(encodedData);
};
};

export const buildLoadClientFn = (getStoredClient: () => string) => {
return () => {
const clientData = getStoredClient();
export 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
4 changes: 2 additions & 2 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class Client {
/** A wrapper function for handling retries and injecting the {@link Client} class */
private retryWrapper: (fn: any, params?: any) => Promise<any>;
/** Function to set the stored client data */
private setStoredClient: (clientData: string | null) => void;
private setStoredClient: (clientData: string | null) => Promise<void>;

/**
* @param params - Parameters are passed as an object.
Expand Down Expand Up @@ -91,7 +91,7 @@ export class Client {
/** The ID of the connected Lattice */
deviceId?: string;
/** Function to set the stored client data */
setStoredClient?: (clientData: string | null) => void;
setStoredClient?: (clientData: string | null) => Promise<void>;
}) {
this.name = name || 'Unknown';
this.baseUrl = baseUrl || BASE_URL;
Expand Down

0 comments on commit 790c3a6

Please sign in to comment.