Skip to content

Commit

Permalink
feat: v1 localstorage polyfill
Browse files Browse the repository at this point in the history
  • Loading branch information
rayan1810 committed Oct 9, 2024
1 parent 2991481 commit 438b8da
Show file tree
Hide file tree
Showing 8 changed files with 15,788 additions and 11 deletions.
1 change: 1 addition & 0 deletions example/App.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
import 'react-native-localstorage-polyfill';
export { default } from './src/App';
1 change: 1 addition & 0 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
},
"dependencies": {
"@expo/metro-runtime": "~3.2.3",
"@react-native-async-storage/async-storage": "1.23.1",
"expo": "~51.0.28",
"expo-status-bar": "~1.12.1",
"react": "18.2.0",
Expand Down
10 changes: 2 additions & 8 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import { useState, useEffect } from 'react';
import { StyleSheet, View, Text } from 'react-native';
import { multiply } from 'react-native-localstorage-polyfill';

export default function App() {
const [result, setResult] = useState<number | undefined>();

useEffect(() => {
multiply(3, 7).then(setResult);
}, []);
localStorage.setItem('key', '1');

return (
<View style={styles.container}>
<Text>Result: {result}</Text>
<Text>Key Stored in Local Storage: {localStorage.getItem('key')}</Text>
</View>
);
}
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -181,5 +181,8 @@
"create-react-native-library": {
"type": "library",
"version": "0.41.2"
},
"dependencies": {
"@react-native-async-storage/async-storage": "^2.0.0"
}
}
64 changes: 64 additions & 0 deletions src/SyncStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import AsyncStorage from '@react-native-async-storage/async-storage';

type KeyType = string;

class SyncStorage {
data: Map<any, any> = new Map();

loading: boolean = true;

init(): Promise<Array<any>> {
return AsyncStorage.getAllKeys().then((keys: any) =>
AsyncStorage.multiGet(keys).then((data: any): Array<any> => {
data.forEach(this.saveItem.bind(this));

return [...this.data];
})
);
}

getItem(key: KeyType): any {
return this.data.get(key);
}

setItem(key: KeyType, value: any): Promise<any> {
if (!key) return Promise.resolve('error a key is not provided');

this.data.set(key, value);
return AsyncStorage.setItem(key, JSON.stringify(value));
}

removeItem(key: KeyType): Promise<any> {
if (!key) return Promise.resolve('error a key is not provided');

this.data.delete(key);
return AsyncStorage.removeItem(key);
}

saveItem(item: Array<KeyType>) {
let value;

try {
value = JSON.parse(item[1] ?? '{}');
} catch (e) {
console.log(e);
[, value] = item;
}

this.data.set(item[0], value);
this.loading = false;
}

getAllKeys(): Array<any> {
return Array.from(this.data.keys());
}

clear(): Promise<void> {
this.data.clear();
return AsyncStorage.clear();
}
}

const syncStorage = new SyncStorage();

export default syncStorage;
7 changes: 4 additions & 3 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export function multiply(a: number, b: number): Promise<number> {
return Promise.resolve(a * b);
}
import localStorage from './localstorage';

// @ts-ignore
global.localStorage = localStorage;
54 changes: 54 additions & 0 deletions src/localstorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// localStoragePolyfill.js
import syncStorage from './SyncStorage';
const localStorage = {
async init() {
try {
const value = await syncStorage.init();
return value;
} catch (error) {
console.error('Error Initializing syncStorage', error);
return null;
}
},
getItem(key: string) {
try {
const value = syncStorage.getItem(key);
return value;
} catch (error) {
console.error('Error getting item from localStorage', error);
return null;
}
},
async setItem(key: string, value: any) {
try {
await syncStorage.setItem(key, value);
} catch (error) {
console.error('Error setting item to localStorage', error);
}
},
async removeItem(key: string) {
try {
await syncStorage.removeItem(key);
} catch (error) {
console.error('Error removing item from localStorage', error);
}
},
async clear() {
try {
await syncStorage.clear();
} catch (error) {
console.error('Error clearing localStorage', error);
}
},
getAllKeys() {
try {
const keys = syncStorage.getAllKeys();
return keys;
} catch (error) {
console.error('Error getting all keys from localStorage', error);
return [];
}
},
};

export default localStorage;
Loading

0 comments on commit 438b8da

Please sign in to comment.