-
Notifications
You must be signed in to change notification settings - Fork 0
/
asyncStorageHelper.js
61 lines (55 loc) · 1.97 KB
/
asyncStorageHelper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { AsyncStorage } from 'react-native';
const AsyncStorageKey = "@KEY_ACCESS_TOKEN"; //must be unique for your app
class AccessAsyncStore {
constructor(){
this._asyncData = null
this.initialCall() // just to store initial data in this.__asyncData remove if your data is to big to store right after importing
}
initialCall = ()=>{
AsyncStorage.getItem(AsyncStorageKey)
.then((data)=>{
if(data){
this._asyncData = JSON.parse(data)
}
})
.catch((err)=> console('Error initializing asyncStore',err));
}
get(){
return new Promise((next, error)=>{
if(this._asyncData) return next(this._asyncData); // this will help to get token quickly and we dont call async again and again
AsyncStorage.getItem(AsyncStorageKey)
.then((data)=>{
if(data){
this._asyncData = JSON.parse(data)
next(this._asyncData)
}else{
next(null);
}
})
.catch((err)=> error(err));
})
}
set(data){
return new Promise((next, error)=>{
let dataHolder = data;
if( data ){
this._asyncData = data;
if( (typeof dataHolder !== "string") ){ // just to check if data is string or object
dataHolder = JSON.stringify(dataHolder);
}
AsyncStorage.setItem(AsyncStorageKey, dataHolder)
.then(()=>{
next(data);
})
.catch((err)=>error(err));
}else{
error('Error storing data to asyncStore.');
}
})
}
clear(){
this._asyncData = null;
return AsyncStorage.removeItem(AsyncStorageKey);
}
}
export default new AccessAsyncStore();