-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
66 lines (62 loc) · 1.76 KB
/
utils.ts
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
let getSerializerPromiseCache: Promise<LocalForageSerializer>;
export function getSerializerPromise(localForageInstance: LocalForage) {
if (getSerializerPromiseCache) {
return getSerializerPromiseCache;
}
if (
!localForageInstance ||
typeof localForageInstance.getSerializer !== 'function'
) {
return Promise.reject(
new Error(
'localforage.getSerializer() was not available! ' +
'localforage v1.4+ is required!',
),
);
}
getSerializerPromiseCache = localForageInstance.getSerializer();
return getSerializerPromiseCache;
}
let getDriverPromiseCache: {
[driverName: string]: Promise<LocalForageDriver>;
};
export function getDriverPromise(
localForageInstance: LocalForage,
driverName: string,
) {
getDriverPromiseCache = getDriverPromiseCache || {};
if (getDriverPromiseCache[driverName]) {
return getDriverPromiseCache[driverName];
}
if (
!localForageInstance ||
typeof localForageInstance.getDriver !== 'function'
) {
return Promise.reject(
new Error(
`localforage.getDriver() was not available!
localforage v1.4+ is required!`,
),
);
}
getDriverPromiseCache[driverName] = localForageInstance.getDriver(
driverName,
);
return getDriverPromiseCache[driverName];
}
export function executeCallback<T>(
promise: Promise<T>,
callback?: (error: any, result?: T) => void,
) {
if (callback) {
promise.then(
function(result) {
callback(null, result);
},
function(error) {
callback(error);
},
);
}
return promise;
}