Skip to content

Commit

Permalink
chore: storage return types
Browse files Browse the repository at this point in the history
Signed-off-by: Case Wylie <[email protected]>
  • Loading branch information
cmwylie19 committed Dec 4, 2024
1 parent b74496c commit 3f83b2b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
9 changes: 9 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@
"project": ["tsconfig.json"],
"ecmaVersion": 2022
},
"overrides": [
{
"files": ["*.ts"],
"excludedFiles": "*.test.ts",
"rules": {
"@typescript-eslint/explicit-function-return-type": "warn"
}
}
],
"rules": {
"@typescript-eslint/no-floating-promises": "warn",
"class-methods-use-this": "warn",
Expand Down
34 changes: 17 additions & 17 deletions src/lib/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ export type Unsubscribe = () => void;
const MAX_WAIT_TIME = 15000;
const STORE_VERSION_PREFIX = "v2";

export function v2StoreKey(key: string) {
export function v2StoreKey(key: string): string {
return `${STORE_VERSION_PREFIX}-${pointer.escape(key)}`;
}

export function v2UnescapedStoreKey(key: string) {
export function v2UnescapedStoreKey(key: string): string {
return `${STORE_VERSION_PREFIX}-${key}`;
}

export function stripV2Prefix(key: string) {
export function stripV2Prefix(key: string): string {
return key.replace(/^v2-/, "");
}
export interface PeprStore {
Expand Down Expand Up @@ -80,11 +80,11 @@ export class Storage implements PeprStore {
#subscriberId = 0;
#readyHandlers: DataReceiver[] = [];

registerSender = (send: DataSender) => {
registerSender = (send: DataSender): void => {
this.#send = send;
};

receive = (data: DataStore) => {
receive = (data: DataStore): void => {
this.#store = data || {};

this.#onReady();
Expand All @@ -96,27 +96,27 @@ export class Storage implements PeprStore {
}
};

getItem = (key: string) => {
getItem = (key: string): string | null => {
const result = this.#store[v2UnescapedStoreKey(key)] || null;
if (result !== null && typeof result !== "function" && typeof result !== "object") {
return result;
}
return null;
};

clear = () => {
clear = (): void => {
Object.keys(this.#store).length > 0 &&
this.#dispatchUpdate(
"remove",
Object.keys(this.#store).map(key => pointer.escape(key)),
);
};

removeItem = (key: string) => {
removeItem = (key: string): void => {
this.#dispatchUpdate("remove", [v2StoreKey(key)]);
};

setItem = (key: string, value: string) => {
setItem = (key: string, value: string): void => {
this.#dispatchUpdate("add", [v2StoreKey(key)], value);
};

Expand All @@ -128,7 +128,7 @@ export class Storage implements PeprStore {
* @param value - The value of the key
* @returns
*/
setItemAndWait = (key: string, value: string) => {
setItemAndWait = (key: string, value: string): Promise<void> => {
this.#dispatchUpdate("add", [v2StoreKey(key)], value);

return new Promise<void>((resolve, reject) => {
Expand All @@ -154,7 +154,7 @@ export class Storage implements PeprStore {
* @param key - The key to add into the store
* @returns
*/
removeItemAndWait = (key: string) => {
removeItemAndWait = (key: string): Promise<void> => {
this.#dispatchUpdate("remove", [v2StoreKey(key)]);
return new Promise<void>((resolve, reject) => {
const unsubscribe = this.subscribe(data => {
Expand All @@ -172,32 +172,32 @@ export class Storage implements PeprStore {
});
};

subscribe = (subscriber: DataReceiver) => {
subscribe = (subscriber: DataReceiver): (() => void) => {
const idx = this.#subscriberId++;
this.#subscribers[idx] = subscriber;
return () => this.unsubscribe(idx);
};

onReady = (callback: DataReceiver) => {
onReady = (callback: DataReceiver): void => {
this.#readyHandlers.push(callback);
};

/**
* Remove a subscriber from the list of subscribers.
* @param idx - The index of the subscriber to remove.
*/
unsubscribe = (idx: number) => {
unsubscribe = (idx: number): void => {
delete this.#subscribers[idx];
};

#onReady = () => {
#onReady = (): void => {
// Notify all ready handlers with a clone of the store
for (const handler of this.#readyHandlers) {
handler(clone(this.#store));
}

// Make this a noop so that it can't be called again
this.#onReady = () => {};
this.#onReady = (): void => {};
};

/**
Expand All @@ -206,7 +206,7 @@ export class Storage implements PeprStore {
* @param keys - The keys to update.
* @param [value] - The new value.
*/
#dispatchUpdate = (op: DataOp, keys: string[], value?: string) => {
#dispatchUpdate = (op: DataOp, keys: string[], value?: string): void => {
this.#send(op, keys, value);
};
}

0 comments on commit 3f83b2b

Please sign in to comment.