Skip to content

Commit

Permalink
feat(backend): New AsyncStorage backend (default)
Browse files Browse the repository at this point in the history
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
Pkmmte committed Sep 24, 2022
1 parent 2f8eae3 commit 49f7477
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
62 changes: 62 additions & 0 deletions src/backend/async-storage.ts
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));
}
}
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import { StashyBackend, StashyOptions } from './backend/_base';
import CookieBackend from './backend/cookie';
import LocalStorageBackend from './backend/local-storage';
import MmkvBackend from './backend/mmkv';
import pino from 'pino';
import { Platform } from 'react-native';
import AsyncStorageBackend from './backend/async-storage';

interface StashyConstructor {
backend?: StashyBackend | {
Expand Down Expand Up @@ -121,7 +121,7 @@ export default stashy;

function getDefaultBackend(platform: 'native' | 'ssr' | 'web'): StashyBackend {
if (isNative() && platform === 'native') {
return new MmkvBackend();
return new AsyncStorageBackend();
} else if (isSSR() && platform === 'ssr') {
return new CookieBackend();
} else if (!isNative() && !isSSR() && platform === 'web') {
Expand Down

0 comments on commit 49f7477

Please sign in to comment.