forked from manuelhuber/AsyncStorage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·50 lines (44 loc) · 1.4 KB
/
index.js
File metadata and controls
executable file
·50 lines (44 loc) · 1.4 KB
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
import AsyncStorageInterface from './interface/AsyncStorage';
export class AsyncStorage extends AsyncStorageInterface {
static setItem(key, value, callback) {
return new Promise(function (resolve) {
localStorage[`@AsyncStorage:${key}`] = value;
if (callback) callback();
resolve();
});
}
static getItem(key, callback) {
return new Promise(function (resolve) {
let value = localStorage[`@AsyncStorage:${key}`];
let error;
if (callback) callback(error, value);
resolve(value);
});
}
static removeItem(key, callback) {
return new Promise(function (resolve) {
localStorage.removeItem(`@AsyncStorage:${key}`);
let error;
if (callback) callback(error);
resolve();
});
}
static getAllKeys(callback) {
return new Promise(function (resolve) {
let keys = [];
Object.keys(localStorage).forEach((key) =>
key.indexOf("@AsyncStorage:") >= 0 ? keys.push(key) : 0
);
let error;
if (callback) callback(error, keys);
resolve(keys);
});
}
static clear(callback) {
return new Promise(function (resolve) {
localStorage.clear();
if (callback) callback();
resolve();
});
}
}