Skip to content

Commit

Permalink
feat(api): New async function variants
Browse files Browse the repository at this point in the history
This makes it possible to use backends that *must* be used asynchronously. Like React Native's AsyncStorage, for example. Or a custom Firebase backend!
  • Loading branch information
Pkmmte committed Sep 24, 2022
1 parent 787e128 commit 2f8eae3
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/backend/_base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ export interface StashyBackend {
clearAll: (options?: StashyOptions) => void
delete: (key: string, options?: StashyOptions) => 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
}
15 changes: 15 additions & 0 deletions src/backend/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,31 @@ export default class CookieBackend implements StashyBackend {
return value ? value === 'true' : null;
};

public async getBooleanAsync(key: string, options?: StashyOptions): Promise<boolean> {
const value = this.getString(key, options);
return value ? value === 'true' : null;
};

public getNumber(key: string, options?: StashyOptions): number {
const value = this.getString(key, options);
return value ? parseFloat(value) : null;
};

public async getNumberAsync(key: string, options?: StashyOptions): Promise<number> {
const value = this.getString(key, options);
return value ? parseFloat(value) : null;
};

public getString(key: string, options?: StashyOptions): string {
validateOptions(options);
return parseCookies(options?.context)?.[this._key(key)];
};

public async getStringAsync(key: string, options?: StashyOptions): Promise<string> {
validateOptions(options);
return parseCookies(options?.context)?.[this._key(key)];
};

public set(key: string, value: boolean | number | string, options?: StashyOptions) {
validateOptions(options);
const encodedValue = typeof value === 'string' ? value : String(value);
Expand Down
14 changes: 14 additions & 0 deletions src/backend/local-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,29 @@ export default class LocalStorageBackend implements StashyBackend {
return value ? value === 'true' : null;
};

public async getBooleanAsync(key: string): Promise<boolean> {
const value = this.getString(key);
return value ? value === 'true' : null;
};

public getNumber(key: string): number {
const value = this.getString(key);
return value ? parseFloat(value) : null;
};

public async getNumberAsync(key: string): Promise<number> {
const value = this.getString(key);
return value ? parseFloat(value) : null;
};

public getString(key: string): string {
return localStorage.getItem(this._key(key));
};

public async getStringAsync(key: string): Promise<string> {
return localStorage.getItem(this._key(key));
};

public set(key: string, value: boolean | number | string) {
localStorage.setItem(this._key(key), typeof value === 'string' ? value : String(value));
}
Expand Down
12 changes: 12 additions & 0 deletions src/backend/mmkv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,26 @@ export default class MmkvBackend implements StashyBackend {
return this._storage.getBoolean(key);
};

public async getBooleanAsync(key: string): Promise<boolean> {
return this._storage.getBoolean(key);
};

public getNumber(key: string): number {
return this._storage.getNumber(key);
};

public async getNumberAsync(key: string): Promise<number> {
return this._storage.getNumber(key);
};

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

public async getStringAsync(key: string): Promise<string> {
return this._storage.getString(key);
};

public set(key: string, value: boolean | number | string) {
this._storage.set(key, value);
}
Expand Down

0 comments on commit 2f8eae3

Please sign in to comment.