forked from robwalkerco/redux-persist-filesystem-storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
149 lines (127 loc) · 3.87 KB
/
index.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
* @flow
*/
import RNFetchBlob from "rn-fetch-blob";
const createStoragePathIfNeeded = path =>
RNFetchBlob.fs
.exists(path)
.then(exists =>
exists
? new Promise(resolve => resolve(true))
: RNFetchBlob.fs.mkdir(path)
);
const onStorageReadyFactory = (storagePath: string) => (func: Function) => {
const storage = createStoragePathIfNeeded(storagePath);
return (...args: Array<any>) => storage.then(() => func(...args));
};
const defaultStoragePath = `${RNFetchBlob.fs.dirs.DocumentDir}/persistStore`;
let onStorageReady = onStorageReadyFactory(defaultStoragePath);
let options = {
storagePath: defaultStoragePath,
encoding: "utf8",
toFileName: (name: string) => name.split(":").join("-"),
fromFileName: (name: string) => name.split("-").join(":")
};
const pathForKey = (key: string) =>
`${options.storagePath}/${options.toFileName(key)}`;
const FilesystemStorage = {
config: (customOptions: Object) => {
options = {
...options,
...customOptions
};
onStorageReady = onStorageReadyFactory(options.storagePath);
},
setItem: (key: string, value: string, callback?: (error: ?Error) => void) =>
RNFetchBlob.fs
.writeFile(pathForKey(key), value, options.encoding)
.then(() => callback && callback())
.catch(error => callback && callback(error)),
getItem: onStorageReady(
(key: string, callback?: (error: ?Error, result: ?(Array<number> | string)) => void) => {
const filePath = pathForKey(options.toFileName(key));
return RNFetchBlob.fs
.readFile(filePath, options.encoding)
.then(data => {
callback && callback(null, data);
if (!callback) {
return data;
}
})
.catch(error => {
callback && callback(error);
if (!callback) {
throw error;
}
});
}
),
removeItem: (key: string, callback?: (error: ?Error) => void) =>
RNFetchBlob.fs
.unlink(pathForKey(options.toFileName(key)))
.then(() => callback && callback())
.catch(error => {
callback && callback(error);
if (!callback) {
throw error;
}
}),
getAllKeys: (callback?: (error: ?Error, keys: ?Array<string>) => any) => {
return RNFetchBlob.fs
.exists(options.storagePath)
.then(async exists => {
if (exists) {
return true;
}
try {
await RNFetchBlob.fs.mkdir(options.storagePath);
} catch(e) {
return true;
}
return true;
})
.then(() => {
return RNFetchBlob.fs
.ls(options.storagePath)
.then(files => files.map<string>(file => options.fromFileName(file)))
.then(files => {
callback && callback(null, files);
if (!callback) {
return files;
}
});
})
.catch(error => {
callback && callback(error);
if (!callback) {
throw error;
}
})},
clear: undefined // Workaround for Flow error coming from `clear` not being part of object literal
};
FilesystemStorage.clear = (callback?: (error: ?Error, allKeysCleared: boolean | void) => void) =>
FilesystemStorage.getAllKeys((error, keys) => {
if (error) throw error;
if (Array.isArray(keys) && keys.length) {
const removedKeys = [];
keys.forEach(key => {
FilesystemStorage.removeItem(key, (error: ?Error) => {
removedKeys.push(key);
if (error && callback) {
callback(error, false);
}
if (removedKeys.length === keys.length && callback)
callback(null, true);
});
});
return true;
}
callback && callback(null, false);
return false;
}).catch(error => {
callback && callback(error);
if (!callback) {
throw error;
}
});
export default FilesystemStorage;