@vueuse/integrations 9.3.1 → 9.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/index.cjs +134 -71
- package/index.d.ts +30 -2
- package/index.iife.js +135 -73
- package/index.iife.min.js +1 -1
- package/index.mjs +134 -72
- package/package.json +13 -3
- package/useIDBKeyval.cjs +68 -0
- package/useIDBKeyval.d.ts +31 -0
- package/useIDBKeyval.iife.js +181 -0
- package/useIDBKeyval.iife.min.js +1 -0
- package/useIDBKeyval.mjs +66 -0
package/useIDBKeyval.mjs
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { resolveUnref } from '@vueuse/shared';
|
|
2
|
+
import { shallowRef, ref, watch } from 'vue-demi';
|
|
3
|
+
import { get, set, del, update } from 'idb-keyval';
|
|
4
|
+
|
|
5
|
+
var __defProp = Object.defineProperty;
|
|
6
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
9
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
10
|
+
var __spreadValues = (a, b) => {
|
|
11
|
+
for (var prop in b || (b = {}))
|
|
12
|
+
if (__hasOwnProp.call(b, prop))
|
|
13
|
+
__defNormalProp(a, prop, b[prop]);
|
|
14
|
+
if (__getOwnPropSymbols)
|
|
15
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
16
|
+
if (__propIsEnum.call(b, prop))
|
|
17
|
+
__defNormalProp(a, prop, b[prop]);
|
|
18
|
+
}
|
|
19
|
+
return a;
|
|
20
|
+
};
|
|
21
|
+
function useIDBKeyval(key, initialValue, options = {}) {
|
|
22
|
+
const {
|
|
23
|
+
flush = "pre",
|
|
24
|
+
deep = true,
|
|
25
|
+
shallow,
|
|
26
|
+
onError = (e) => {
|
|
27
|
+
console.error(e);
|
|
28
|
+
}
|
|
29
|
+
} = options;
|
|
30
|
+
const data = (shallow ? shallowRef : ref)(initialValue);
|
|
31
|
+
const rawInit = resolveUnref(initialValue);
|
|
32
|
+
async function read() {
|
|
33
|
+
try {
|
|
34
|
+
const rawValue = await get(key);
|
|
35
|
+
if (rawValue === void 0) {
|
|
36
|
+
if (rawInit)
|
|
37
|
+
set(key, rawInit);
|
|
38
|
+
} else {
|
|
39
|
+
data.value = rawValue;
|
|
40
|
+
}
|
|
41
|
+
} catch (e) {
|
|
42
|
+
onError(e);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
read();
|
|
46
|
+
async function write() {
|
|
47
|
+
try {
|
|
48
|
+
if (data.value == null) {
|
|
49
|
+
await del(key);
|
|
50
|
+
} else {
|
|
51
|
+
if (Array.isArray(data.value))
|
|
52
|
+
await update(key, () => JSON.parse(JSON.stringify(data.value)));
|
|
53
|
+
else if (typeof data.value === "object")
|
|
54
|
+
await update(key, () => __spreadValues({}, data.value));
|
|
55
|
+
else
|
|
56
|
+
await update(key, () => data.value);
|
|
57
|
+
}
|
|
58
|
+
} catch (e) {
|
|
59
|
+
onError(e);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
watch(data, () => write(), { flush, deep });
|
|
63
|
+
return data;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export { useIDBKeyval };
|