Skip to content

Commit

Permalink
feat(api): async variants now mandated by interface
Browse files Browse the repository at this point in the history
Backends that naturally work in sync mode anyway don't need many changes, but this at least allows us to await async-only backends like AsyncStorage or custom database backends.
  • Loading branch information
Pkmmte committed Sep 25, 2022
1 parent 49f7477 commit 528e33e
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 9 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ yarn add @waveplay/stashy
import stashy from '@waveplay/stashy';

// Get a value from storage
const stuff = stashy.getString('stuff');
const displayName = stashy.getString('name');
console.log(`Your username is ${displayName}`);

// Update a value in storage
stashy.set('stuff', stuff + ' and more stuff');
stashy.set('name', 'Pkmmte Xeleon');
```

## Credits
Expand Down
3 changes: 3 additions & 0 deletions src/backend/_base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ export interface StashyBackendInitOptions {
export interface StashyBackend {
_init: (StashyBackendInitOptions) => void
clearAll: (options?: StashyOptions) => void
clearAllAsync: (options?: StashyOptions) => Promise<void>
delete: (key: string, options?: StashyOptions) => void
deleteAsync: (key: string, options?: StashyOptions) => Promise<void>
getBoolean: (key: string, options?: StashyOptions) => boolean
getBooleanAsync: (key: string, options?: StashyOptions) => Promise<boolean>
getNumber: (key: string, options?: StashyOptions) => number
getNumberAsync: (key: string, options?: StashyOptions) => Promise<number>
getString: (key: string, options?: StashyOptions) => string
getStringAsync: (key: string, options?: StashyOptions) => Promise<string>
set: (key: string, value: boolean | number | string, options?: StashyOptions) => void
setAsync: (key: string, value: boolean | number | string, options?: StashyOptions) => Promise<void>
}
12 changes: 12 additions & 0 deletions src/backend/async-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,18 @@ export default class AsyncStorageBackend implements StashyBackend {
AsyncStorage.clear();
}

public async clearAllAsync(): Promise<void> {
return AsyncStorage.clear();
}

public delete(key: string) {
AsyncStorage.removeItem(this._key(key));
}

public async deleteAsync(key: string): Promise<void> {
return AsyncStorage.removeItem(this._key(key));
}

public getBoolean(key: string): boolean {
const value = this.getString(key);
return value ? value === 'true' : null;
Expand Down Expand Up @@ -59,4 +67,8 @@ export default class AsyncStorageBackend implements StashyBackend {
public set(key: string, value: boolean | number | string) {
AsyncStorage.setItem(this._key(key), typeof value === 'string' ? value : String(value));
}

public async setAsync(key: string, value: boolean | number | string): Promise<void> {
return AsyncStorage.setItem(this._key(key), typeof value === 'string' ? value : String(value));
}
}
12 changes: 12 additions & 0 deletions src/backend/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,19 @@ export default class CookieBackend implements StashyBackend {
throw new Error(`clearAll() is not supported for CookieBackend`);
}

public async clearAllAsync(): Promise<void> {
throw new Error(`clearAllAsync() is not supported for CookieBackend`);
}

public delete(key: string, options?: StashyOptions) {
validateOptions(options);
destroyCookie(options?.context, this._key(key));
}

public async deleteAsync(key: string, options?: StashyOptions): Promise<void> {
this.delete(key, options);
}

public getBoolean(key: string, options?: StashyOptions): boolean {
const value = this.getString(key, options);
return value ? value === 'true' : null;
Expand Down Expand Up @@ -71,6 +79,10 @@ export default class CookieBackend implements StashyBackend {
secure: options?.secure
});
}

public async setAsync(key: string, value: boolean | number | string, options?: StashyOptions): Promise<void> {
this.set(key, value, options);
}
}

const validateOptions = (options?: StashyOptions) => {
Expand Down
12 changes: 12 additions & 0 deletions src/backend/local-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,18 @@ export default class LocalStorageBackend implements StashyBackend {
localStorage.clear();
}

public async clearAllAsync(): Promise<void> {
this.clearAll();
}

public delete(key: string) {
localStorage.removeItem(this._key(key));
}

public async deleteAsync(key: string): Promise<void> {
this.delete(key);
}

public getBoolean(key: string): boolean {
const value = this.getString(key);
return value ? value === 'true' : null;
Expand Down Expand Up @@ -56,4 +64,8 @@ export default class LocalStorageBackend implements StashyBackend {
public set(key: string, value: boolean | number | string) {
localStorage.setItem(this._key(key), typeof value === 'string' ? value : String(value));
}

public async setAsync(key: string, value: boolean | number | string): Promise<void> {
this.set(key, value);
}
}
12 changes: 12 additions & 0 deletions src/backend/mmkv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,18 @@ export default class MmkvBackend implements StashyBackend {
this._storage?.clearAll();
}

public async clearAllAsync(): Promise<void> {
this.clearAll();
}

public delete(key: string) {
this._storage.delete(key);
}

public async deleteAsync(key: string): Promise<void> {
this.delete(key);
}

public getBoolean(key: string): boolean {
return this._storage.getBoolean(key);
};
Expand Down Expand Up @@ -63,4 +71,8 @@ export default class MmkvBackend implements StashyBackend {
public set(key: string, value: boolean | number | string) {
this._storage.set(key, value);
}

public async setAsync(key: string, value: boolean | number | string): Promise<void> {
this.set(key, value);
}
}
43 changes: 36 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,28 +83,57 @@ export class Stashy {
this._pino.trace(`[${this._getId()}] get(${key}) with options:`, options);
const encodedValue = this._backend().getString(key, options);
const value = encodedValue ? JSON.parse(encodedValue) : null;
this._pino.debug(`[${this._getId()}] get(${key}) >>`, value);
this._pino.debug(`[${this._getId()}] get(${key}) value is:`, value);
return value;
};

public getBoolean(key: string, options?: StashyOptions) {
public async getAsync<T>(key: string, options?: StashyOptions): Promise<T> {
this._pino.trace(`[${this._getId()}] getAsync(${key}) with options:`, options);
const encodedValue = await this._backend().getStringAsync(key, options);
const value = encodedValue ? JSON.parse(encodedValue) : null;
this._pino.debug(`[${this._getId()}] getAsync(${key}) value is:`, value);
return value;
};

public getBoolean(key: string, options?: StashyOptions): boolean {
this._pino.trace(`[${this._getId()}] getBoolean(${key}) with options:`, options);
const value = this._backend().getBoolean(key, options);
this._pino.debug(`[${this._getId()}] getBoolean(${key}) >>`, value);
this._pino.debug(`[${this._getId()}] getBoolean(${key}) value is:`, value);
return value;
};

public async getBooleanAsync(key: string, options?: StashyOptions): Promise<boolean> {
this._pino.trace(`[${this._getId()}] getBooleanAsync(${key}) with options:`, options);
const value = await this._backend().getBooleanAsync(key, options);
this._pino.debug(`[${this._getId()}] getBooleanAsync(${key}) value is:`, value);
return value;
};

public getNumber(key: string, options?: StashyOptions) {
public getNumber(key: string, options?: StashyOptions): number {
this._pino.trace(`[${this._getId()}] getNumber(${key}) with options:`, options);
const value = this._backend().getNumber(key, options);
this._pino.debug(`[${this._getId()}] getNumber(${key}) >>`, value);
this._pino.debug(`[${this._getId()}] getNumber(${key}) value is:`, value);
return value;
};

public getString(key: string, options?: StashyOptions) {
public async getNumberAsync(key: string, options?: StashyOptions): Promise<number> {
this._pino.trace(`[${this._getId()}] getNumberAsync(${key}) with options:`, options);
const value = await this._backend().getNumberAsync(key, options);
this._pino.debug(`[${this._getId()}] getNumberAsync(${key}) value is:`, value);
return value;
};

public getString(key: string, options?: StashyOptions): string {
this._pino.trace(`[${this._getId()}] getString(${key}) with options:`, options);
const value = this._backend().getString(key, options);
this._pino.debug(`[${this._getId()}] getString(${key}) >>`, value);
this._pino.debug(`[${this._getId()}] getString(${key}) value is:`, value);
return value;
};

public async getStringAsync(key: string, options?: StashyOptions): Promise<string> {
this._pino.trace(`[${this._getId()}] getStringAsync(${key}) with options:`, options);
const value = await this._backend().getStringAsync(key, options);
this._pino.debug(`[${this._getId()}] getStringAsync(${key}) value is:`, value);
return value;
};

Expand Down

0 comments on commit 528e33e

Please sign in to comment.