-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(backend): New AsyncStorage backend (default)
This is the new default for native apps. It's simply an easier starting point compared to MMKV which is not compatible with Expo Manage projects unless you create a development build first.
- Loading branch information
Showing
2 changed files
with
64 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* © 2022 WavePlay <[email protected]> | ||
*/ | ||
import { StashyBackend, StashyBackendInitOptions } from './_base'; | ||
import { AsyncStorage } from 'react-native'; | ||
|
||
/** | ||
* Basic AsyncStorage implementation. | ||
* Good start for native platforms, but we recommend using MMKV instead for better performance. | ||
*/ | ||
export default class AsyncStorageBackend implements StashyBackend { | ||
private _id: string; | ||
|
||
public _init(options: StashyBackendInitOptions) { | ||
this._id = options.id || ''; | ||
} | ||
|
||
private _key(key: string): string { | ||
return this._id ? this._id + '_' + key : key; | ||
} | ||
|
||
public clearAll() { | ||
AsyncStorage.clear(); | ||
} | ||
|
||
public delete(key: string) { | ||
AsyncStorage.removeItem(this._key(key)); | ||
} | ||
|
||
public getBoolean(key: string): boolean { | ||
const value = this.getString(key); | ||
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 { | ||
// @ts-ignore | ||
return AsyncStorage.getItem(this._key(key)); | ||
}; | ||
|
||
public async getStringAsync(key: string): Promise<string> { | ||
return AsyncStorage.getItem(this._key(key)); | ||
}; | ||
|
||
public set(key: string, value: boolean | number | string) { | ||
AsyncStorage.setItem(this._key(key), typeof value === 'string' ? value : String(value)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters