-
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.
- Loading branch information
Showing
8 changed files
with
15,788 additions
and
11 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 |
---|---|---|
@@ -1 +1,2 @@ | ||
import 'react-native-localstorage-polyfill'; | ||
export { default } from './src/App'; |
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
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
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
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,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; |
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 |
---|---|---|
@@ -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; |
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,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; |
Oops, something went wrong.