@choksheak/ts-utils 0.3.4 → 0.3.5
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 +9 -3
- package/asNumber.cjs +3 -3
- package/asNumber.d.mts +1 -1
- package/asNumber.d.ts +1 -1
- package/asNumber.min.cjs +1 -1
- package/asNumber.min.cjs.map +1 -1
- package/asNumber.min.mjs +1 -1
- package/asNumber.min.mjs.map +1 -1
- package/asNumber.mjs +3 -3
- package/kvStore.cjs +230 -239
- package/kvStore.d.mts +142 -44
- package/kvStore.d.ts +142 -44
- package/kvStore.min.cjs +1 -1
- package/kvStore.min.cjs.map +1 -1
- package/kvStore.min.mjs +1 -1
- package/kvStore.min.mjs.map +1 -1
- package/kvStore.mjs +229 -237
- package/localStore.cjs +187 -186
- package/localStore.d.mts +135 -34
- package/localStore.d.ts +135 -34
- package/localStore.min.cjs +1 -1
- package/localStore.min.cjs.map +1 -1
- package/localStore.min.mjs +1 -1
- package/localStore.min.mjs.map +1 -1
- package/localStore.mjs +186 -184
- package/mean.cjs +3 -3
- package/mean.min.cjs +1 -1
- package/mean.min.cjs.map +1 -1
- package/mean.min.mjs +1 -1
- package/mean.min.mjs.map +1 -1
- package/mean.mjs +3 -3
- package/median.cjs +4 -4
- package/median.min.cjs +1 -1
- package/median.min.cjs.map +1 -1
- package/median.min.mjs +1 -1
- package/median.min.mjs.map +1 -1
- package/median.mjs +4 -4
- package/package.json +2 -34
- package/safeBtoa.d.mts +7 -0
- package/safeBtoa.d.ts +7 -0
- package/safeBtoa.min.cjs.map +1 -1
- package/safeBtoa.min.mjs.map +1 -1
- package/sum.cjs +3 -3
- package/sum.min.cjs +1 -1
- package/sum.min.cjs.map +1 -1
- package/sum.min.mjs +1 -1
- package/sum.min.mjs.map +1 -1
- package/sum.mjs +3 -3
- package/timer.cjs +19 -24
- package/timer.d.mts +9 -6
- package/timer.d.ts +9 -6
- package/timer.min.cjs +1 -1
- package/timer.min.cjs.map +1 -1
- package/timer.min.mjs +1 -1
- package/timer.min.mjs.map +1 -1
- package/timer.mjs +19 -23
- package/safeParseFloat.cjs +0 -33
- package/safeParseFloat.d.mts +0 -6
- package/safeParseFloat.d.ts +0 -6
- package/safeParseFloat.min.cjs +0 -2
- package/safeParseFloat.min.cjs.map +0 -1
- package/safeParseFloat.min.mjs +0 -2
- package/safeParseFloat.min.mjs.map +0 -1
- package/safeParseFloat.mjs +0 -8
- package/safeParseInt.cjs +0 -33
- package/safeParseInt.d.mts +0 -6
- package/safeParseInt.d.ts +0 -6
- package/safeParseInt.min.cjs +0 -2
- package/safeParseInt.min.cjs.map +0 -1
- package/safeParseInt.min.mjs +0 -2
- package/safeParseInt.min.mjs.map +0 -1
- package/safeParseInt.mjs +0 -8
package/kvStore.mjs
CHANGED
|
@@ -54,33 +54,20 @@ function withOnError(request, reject) {
|
|
|
54
54
|
};
|
|
55
55
|
return request;
|
|
56
56
|
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
/** Local storage key name for the last GC completed timestamp. */
|
|
69
|
-
gcMsStorageKey;
|
|
70
|
-
dbVersion;
|
|
71
|
-
storeName;
|
|
72
|
-
defaultExpiryMs;
|
|
73
|
-
gcIntervalMs;
|
|
74
|
-
async getOrCreateDb() {
|
|
75
|
-
if (!this.db) {
|
|
76
|
-
this.db = await new Promise((resolve, reject) => {
|
|
77
|
-
const request = withOnError(
|
|
78
|
-
globalThis.indexedDB.open(this.dbName, this.dbVersion),
|
|
79
|
-
reject
|
|
80
|
-
);
|
|
57
|
+
function createKvStore(dbName, options) {
|
|
58
|
+
let db;
|
|
59
|
+
const dbVersion = options?.dbVersion ?? KvStoreConfig.dbVersion;
|
|
60
|
+
const storeName = options?.storeName ?? KvStoreConfig.storeName;
|
|
61
|
+
const defaultExpiryMs = options?.defaultExpiryMs ? durationOrMsToMs(options.defaultExpiryMs) : KvStoreConfig.expiryMs;
|
|
62
|
+
const gcIntervalMs = options?.gcIntervalMs ? durationOrMsToMs(options.gcIntervalMs) : KvStoreConfig.gcIntervalMs;
|
|
63
|
+
const gcMsStorageKey = `__kvStore:lastGcMs:${dbName}:v${dbVersion}:${storeName}`;
|
|
64
|
+
async function getOrCreateDb() {
|
|
65
|
+
if (!db) {
|
|
66
|
+
db = await new Promise((resolve, reject) => {
|
|
67
|
+
const request = withOnError(indexedDB.open(dbName, dbVersion), reject);
|
|
81
68
|
request.onupgradeneeded = (event) => {
|
|
82
|
-
const
|
|
83
|
-
const objectStore =
|
|
69
|
+
const db2 = event.target.result;
|
|
70
|
+
const objectStore = db2.createObjectStore(storeName, {
|
|
84
71
|
keyPath: "key"
|
|
85
72
|
});
|
|
86
73
|
objectStore.createIndex("key", "key", {
|
|
@@ -88,247 +75,252 @@ var KvStore = class {
|
|
|
88
75
|
});
|
|
89
76
|
};
|
|
90
77
|
request.onsuccess = (event) => {
|
|
91
|
-
const
|
|
92
|
-
resolve(
|
|
78
|
+
const db2 = event.target.result;
|
|
79
|
+
resolve(db2);
|
|
93
80
|
};
|
|
94
81
|
});
|
|
95
82
|
}
|
|
96
|
-
return
|
|
83
|
+
return db;
|
|
97
84
|
}
|
|
98
|
-
async transact(mode, callback) {
|
|
99
|
-
const
|
|
85
|
+
async function transact(mode, callback) {
|
|
86
|
+
const db2 = await getOrCreateDb();
|
|
100
87
|
return await new Promise((resolve, reject) => {
|
|
101
|
-
const transaction = withOnError(
|
|
102
|
-
db.transaction(this.storeName, mode),
|
|
103
|
-
reject
|
|
104
|
-
);
|
|
88
|
+
const transaction = withOnError(db2.transaction(storeName, mode), reject);
|
|
105
89
|
transaction.onabort = (event) => {
|
|
106
90
|
reject(event);
|
|
107
91
|
};
|
|
108
|
-
const objectStore = transaction.objectStore(
|
|
92
|
+
const objectStore = transaction.objectStore(storeName);
|
|
109
93
|
callback(objectStore, resolve, reject);
|
|
110
94
|
});
|
|
111
95
|
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
96
|
+
const obj = {
|
|
97
|
+
/** Input name for the DB. */
|
|
98
|
+
dbName,
|
|
99
|
+
/** Input version for the DB. */
|
|
100
|
+
dbVersion,
|
|
101
|
+
/** Input name for the DB store. */
|
|
102
|
+
storeName,
|
|
103
|
+
/** Default expiry to use if not specified in set(). */
|
|
104
|
+
defaultExpiryMs,
|
|
105
|
+
/** Time interval for when GC's occur. */
|
|
106
|
+
gcIntervalMs,
|
|
107
|
+
/** Local storage key name for the last GC completed timestamp. */
|
|
108
|
+
gcMsStorageKey,
|
|
109
|
+
/** Set a value in the store. */
|
|
110
|
+
async set(key, value, expiryDeltaMs) {
|
|
111
|
+
const nowMs = Date.now();
|
|
112
|
+
const stored = {
|
|
113
|
+
key,
|
|
114
|
+
value,
|
|
115
|
+
storedMs: nowMs,
|
|
116
|
+
expiryMs: nowMs + durationOrMsToMs(expiryDeltaMs ?? defaultExpiryMs)
|
|
117
|
+
};
|
|
118
|
+
return await transact("readwrite", (objectStore, resolve, reject) => {
|
|
119
|
+
const request = withOnError(objectStore.put(stored), reject);
|
|
125
120
|
request.onsuccess = () => {
|
|
126
121
|
resolve(value);
|
|
127
|
-
|
|
128
|
-
};
|
|
129
|
-
}
|
|
130
|
-
);
|
|
131
|
-
}
|
|
132
|
-
/** Delete one or multiple keys. */
|
|
133
|
-
async delete(key) {
|
|
134
|
-
return await this.transact(
|
|
135
|
-
"readwrite",
|
|
136
|
-
(objectStore, resolve, reject) => {
|
|
137
|
-
objectStore.transaction.oncomplete = () => {
|
|
138
|
-
resolve();
|
|
122
|
+
obj.gc();
|
|
139
123
|
};
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
124
|
+
});
|
|
125
|
+
},
|
|
126
|
+
/** Delete one or multiple keys. */
|
|
127
|
+
async delete(key) {
|
|
128
|
+
return await transact(
|
|
129
|
+
"readwrite",
|
|
130
|
+
(objectStore, resolve, reject) => {
|
|
131
|
+
objectStore.transaction.oncomplete = () => {
|
|
132
|
+
resolve();
|
|
133
|
+
};
|
|
134
|
+
if (typeof key === "string") {
|
|
135
|
+
withOnError(objectStore.delete(key), reject);
|
|
136
|
+
} else {
|
|
137
|
+
for (const k of key) {
|
|
138
|
+
withOnError(objectStore.delete(k), reject);
|
|
139
|
+
}
|
|
145
140
|
}
|
|
146
141
|
}
|
|
142
|
+
);
|
|
143
|
+
},
|
|
144
|
+
/** Mainly used to get the expiration timestamp of an object. */
|
|
145
|
+
async getStoredObject(key) {
|
|
146
|
+
const stored = await transact(
|
|
147
|
+
"readonly",
|
|
148
|
+
(objectStore, resolve, reject) => {
|
|
149
|
+
const request = withOnError(objectStore.get(key), reject);
|
|
150
|
+
request.onsuccess = () => {
|
|
151
|
+
resolve(request.result);
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
);
|
|
155
|
+
if (!stored) {
|
|
156
|
+
return void 0;
|
|
147
157
|
}
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
);
|
|
161
|
-
if (!stored) {
|
|
162
|
-
return void 0;
|
|
163
|
-
}
|
|
164
|
-
try {
|
|
165
|
-
const obj = validateStoredObject(stored);
|
|
166
|
-
if (!obj) {
|
|
167
|
-
await this.delete(key);
|
|
168
|
-
this.gc();
|
|
158
|
+
try {
|
|
159
|
+
const valid = validateStoredObject(stored);
|
|
160
|
+
if (!valid) {
|
|
161
|
+
await obj.delete(key);
|
|
162
|
+
obj.gc();
|
|
163
|
+
return void 0;
|
|
164
|
+
}
|
|
165
|
+
return valid;
|
|
166
|
+
} catch (e) {
|
|
167
|
+
console.error(`Invalid kv value: ${key}=${JSON.stringify(stored)}:`, e);
|
|
168
|
+
await obj.delete(key);
|
|
169
|
+
obj.gc();
|
|
169
170
|
return void 0;
|
|
170
171
|
}
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
await
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
await callback(
|
|
195
|
-
String(cursor.key),
|
|
196
|
-
obj.value,
|
|
197
|
-
obj.expiryMs,
|
|
198
|
-
obj.storedMs
|
|
199
|
-
);
|
|
172
|
+
},
|
|
173
|
+
/** Get a value by key, or undefined if it does not exist. */
|
|
174
|
+
async get(key) {
|
|
175
|
+
const stored = await obj.getStoredObject(key);
|
|
176
|
+
return stored?.value;
|
|
177
|
+
},
|
|
178
|
+
/** Generic way to iterate through all entries. */
|
|
179
|
+
async forEach(callback) {
|
|
180
|
+
await transact("readonly", (objectStore, resolve, reject) => {
|
|
181
|
+
const request = withOnError(objectStore.openCursor(), reject);
|
|
182
|
+
request.onsuccess = async (event) => {
|
|
183
|
+
const cursor = event.target.result;
|
|
184
|
+
if (cursor) {
|
|
185
|
+
if (cursor.key) {
|
|
186
|
+
const valid = validateStoredObject(cursor.value);
|
|
187
|
+
if (valid !== void 0) {
|
|
188
|
+
await callback(
|
|
189
|
+
String(cursor.key),
|
|
190
|
+
valid.value,
|
|
191
|
+
valid.expiryMs,
|
|
192
|
+
valid.storedMs
|
|
193
|
+
);
|
|
194
|
+
}
|
|
200
195
|
}
|
|
196
|
+
cursor.continue();
|
|
197
|
+
} else {
|
|
198
|
+
resolve();
|
|
201
199
|
}
|
|
202
|
-
|
|
203
|
-
|
|
200
|
+
};
|
|
201
|
+
});
|
|
202
|
+
},
|
|
203
|
+
/**
|
|
204
|
+
* Returns the number of items in the store. Note that getting the size
|
|
205
|
+
* requires iterating through the entire store because the items could expire
|
|
206
|
+
* at any time, and hence the size is a dynamic number.
|
|
207
|
+
*/
|
|
208
|
+
async size() {
|
|
209
|
+
let count = 0;
|
|
210
|
+
await obj.forEach(() => {
|
|
211
|
+
count++;
|
|
212
|
+
});
|
|
213
|
+
return count;
|
|
214
|
+
},
|
|
215
|
+
/** Remove all items from the store. */
|
|
216
|
+
async clear() {
|
|
217
|
+
await transact("readwrite", (objectStore, resolve, reject) => {
|
|
218
|
+
const request = withOnError(objectStore.clear(), reject);
|
|
219
|
+
request.onsuccess = () => {
|
|
204
220
|
resolve();
|
|
221
|
+
};
|
|
222
|
+
});
|
|
223
|
+
},
|
|
224
|
+
/**
|
|
225
|
+
* Returns all items as map of key to value, mainly used for debugging dumps.
|
|
226
|
+
* The type T is applied to all values, even though they might not be of type
|
|
227
|
+
* T (in the case when you store different data types in the same store).
|
|
228
|
+
*/
|
|
229
|
+
async asMap() {
|
|
230
|
+
const map = /* @__PURE__ */ new Map();
|
|
231
|
+
await obj.forEach((key, value, expiryMs, storedMs) => {
|
|
232
|
+
map.set(key, { value, expiryMs, storedMs });
|
|
233
|
+
});
|
|
234
|
+
return map;
|
|
235
|
+
},
|
|
236
|
+
/** Returns the ms timestamp for the last GC (garbage collection). */
|
|
237
|
+
getLastGcMs() {
|
|
238
|
+
const lastGcMsStr = localStorage.getItem(gcMsStorageKey);
|
|
239
|
+
if (!lastGcMsStr) return 0;
|
|
240
|
+
const ms = Number(lastGcMsStr);
|
|
241
|
+
return isNaN(ms) ? 0 : ms;
|
|
242
|
+
},
|
|
243
|
+
/** Set the ms timestamp for the last GC (garbage collection). */
|
|
244
|
+
setLastGcMs(ms) {
|
|
245
|
+
localStorage.setItem(gcMsStorageKey, String(ms));
|
|
246
|
+
},
|
|
247
|
+
/** Perform garbage-collection if due, else do nothing. */
|
|
248
|
+
async gc() {
|
|
249
|
+
const lastGcMs = obj.getLastGcMs();
|
|
250
|
+
if (!lastGcMs) {
|
|
251
|
+
obj.setLastGcMs(Date.now());
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
if (Date.now() < lastGcMs + gcIntervalMs) {
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
await obj.gcNow();
|
|
258
|
+
},
|
|
259
|
+
/**
|
|
260
|
+
* Perform garbage collection immediately without checking whether we are
|
|
261
|
+
* due for the next GC or not.
|
|
262
|
+
*/
|
|
263
|
+
async gcNow() {
|
|
264
|
+
console.log(`Starting kvStore GC on ${dbName} v${dbVersion}...`);
|
|
265
|
+
obj.setLastGcMs(Date.now());
|
|
266
|
+
const keysToDelete = [];
|
|
267
|
+
await obj.forEach(
|
|
268
|
+
async (key, value, expiryMs) => {
|
|
269
|
+
if (value === void 0 || Date.now() >= expiryMs) {
|
|
270
|
+
keysToDelete.push(key);
|
|
271
|
+
}
|
|
205
272
|
}
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
/**
|
|
210
|
-
* Returns the number of items in the store. Note that getting the size
|
|
211
|
-
* requires iterating through the entire store because the items could expire
|
|
212
|
-
* at any time, and hence the size is a dynamic number.
|
|
213
|
-
*/
|
|
214
|
-
async size() {
|
|
215
|
-
let count = 0;
|
|
216
|
-
await this.forEach(() => {
|
|
217
|
-
count++;
|
|
218
|
-
});
|
|
219
|
-
return count;
|
|
220
|
-
}
|
|
221
|
-
/** Remove all items from the store. */
|
|
222
|
-
async clear() {
|
|
223
|
-
await this.transact("readwrite", (objectStore, resolve, reject) => {
|
|
224
|
-
const request = withOnError(objectStore.clear(), reject);
|
|
225
|
-
request.onsuccess = () => {
|
|
226
|
-
resolve();
|
|
227
|
-
};
|
|
228
|
-
});
|
|
229
|
-
}
|
|
230
|
-
/**
|
|
231
|
-
* Returns all items as map of key to value, mainly used for debugging dumps.
|
|
232
|
-
* The type T is applied to all values, even though they might not be of type
|
|
233
|
-
* T (in the case when you store different data types in the same store).
|
|
234
|
-
*/
|
|
235
|
-
async asMap() {
|
|
236
|
-
const map = /* @__PURE__ */ new Map();
|
|
237
|
-
await this.forEach((key, value, expiryMs, storedMs) => {
|
|
238
|
-
map.set(key, { value, expiryMs, storedMs });
|
|
239
|
-
});
|
|
240
|
-
return map;
|
|
241
|
-
}
|
|
242
|
-
/** Returns the ms timestamp for the last GC (garbage collection). */
|
|
243
|
-
get lastGcMs() {
|
|
244
|
-
const lastGcMsStr = globalThis.localStorage.getItem(this.gcMsStorageKey);
|
|
245
|
-
if (!lastGcMsStr) return 0;
|
|
246
|
-
const ms = Number(lastGcMsStr);
|
|
247
|
-
return isNaN(ms) ? 0 : ms;
|
|
248
|
-
}
|
|
249
|
-
/** Set the ms timestamp for the last GC (garbage collection). */
|
|
250
|
-
set lastGcMs(ms) {
|
|
251
|
-
globalThis.localStorage.setItem(this.gcMsStorageKey, String(ms));
|
|
252
|
-
}
|
|
253
|
-
/** Perform garbage-collection if due, else do nothing. */
|
|
254
|
-
async gc() {
|
|
255
|
-
const lastGcMs = this.lastGcMs;
|
|
256
|
-
if (!lastGcMs) {
|
|
257
|
-
this.lastGcMs = Date.now();
|
|
258
|
-
return;
|
|
259
|
-
}
|
|
260
|
-
if (Date.now() < lastGcMs + this.gcIntervalMs) {
|
|
261
|
-
return;
|
|
262
|
-
}
|
|
263
|
-
await this.gcNow();
|
|
264
|
-
}
|
|
265
|
-
/**
|
|
266
|
-
* Perform garbage collection immediately without checking whether we are
|
|
267
|
-
* due for the next GC or not.
|
|
268
|
-
*/
|
|
269
|
-
async gcNow() {
|
|
270
|
-
console.log(`Starting kvStore GC on ${this.dbName} v${this.dbVersion}...`);
|
|
271
|
-
this.lastGcMs = Date.now();
|
|
272
|
-
const keysToDelete = [];
|
|
273
|
-
await this.forEach(
|
|
274
|
-
async (key, value, expiryMs) => {
|
|
275
|
-
if (value === void 0 || Date.now() >= expiryMs) {
|
|
276
|
-
keysToDelete.push(key);
|
|
277
|
-
}
|
|
273
|
+
);
|
|
274
|
+
if (keysToDelete.length) {
|
|
275
|
+
await obj.delete(keysToDelete);
|
|
278
276
|
}
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
277
|
+
console.log(
|
|
278
|
+
`Finished kvStore GC on ${dbName} v${dbVersion} - deleted ${keysToDelete.length} keys`
|
|
279
|
+
);
|
|
280
|
+
obj.setLastGcMs(Date.now());
|
|
281
|
+
},
|
|
282
|
+
/** Returns `this` casted into a StorageAdapter<T>. */
|
|
283
|
+
asStorageAdapter() {
|
|
284
|
+
return obj;
|
|
282
285
|
}
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
}
|
|
288
|
-
/** Returns `this` casted into a StorageAdapter<T>. */
|
|
289
|
-
asStorageAdapter() {
|
|
290
|
-
return this;
|
|
291
|
-
}
|
|
292
|
-
};
|
|
293
|
-
var kvStore = new KvStore(KvStoreConfig.dbName);
|
|
294
|
-
var KvStoreItem = class {
|
|
295
|
-
constructor(key, defaultExpiryMs = KvStoreConfig.expiryMs, store = kvStore) {
|
|
296
|
-
this.key = key;
|
|
297
|
-
this.store = store;
|
|
298
|
-
this.defaultExpiryMs = defaultExpiryMs && durationOrMsToMs(defaultExpiryMs);
|
|
299
|
-
}
|
|
300
|
-
defaultExpiryMs;
|
|
301
|
-
/** Set a value in the store. */
|
|
302
|
-
async set(value, expiryDeltaMs = this.defaultExpiryMs) {
|
|
303
|
-
await this.store.set(this.key, value, expiryDeltaMs);
|
|
304
|
-
}
|
|
305
|
-
/**
|
|
306
|
-
* Example usage:
|
|
307
|
-
*
|
|
308
|
-
* const { value, storedMs, expiryMs, storedMs } =
|
|
309
|
-
* await myKvItem.getStoredObject();
|
|
310
|
-
*/
|
|
311
|
-
async getStoredObject() {
|
|
312
|
-
return await this.store.getStoredObject(this.key);
|
|
313
|
-
}
|
|
314
|
-
/** Get a value by key, or undefined if it does not exist. */
|
|
315
|
-
async get() {
|
|
316
|
-
return await this.store.get(this.key);
|
|
317
|
-
}
|
|
318
|
-
/** Delete this key from the store. */
|
|
319
|
-
async delete() {
|
|
320
|
-
await this.store.delete(this.key);
|
|
321
|
-
}
|
|
322
|
-
};
|
|
286
|
+
};
|
|
287
|
+
return obj;
|
|
288
|
+
}
|
|
289
|
+
var kvStore = createKvStore(KvStoreConfig.dbName);
|
|
323
290
|
function kvStoreItem(key, expiryMs, store = kvStore) {
|
|
324
|
-
|
|
325
|
-
|
|
291
|
+
const defaultExpiryMs = expiryMs && durationOrMsToMs(expiryMs);
|
|
292
|
+
const obj = {
|
|
293
|
+
key,
|
|
294
|
+
defaultExpiryMs,
|
|
295
|
+
store,
|
|
296
|
+
/** Set a value in the store. */
|
|
297
|
+
async set(value, expiryDeltaMs) {
|
|
298
|
+
await store.set(key, value, expiryDeltaMs ?? defaultExpiryMs);
|
|
299
|
+
},
|
|
300
|
+
/**
|
|
301
|
+
* Example usage:
|
|
302
|
+
*
|
|
303
|
+
* const { value, storedMs, expiryMs, storedMs } =
|
|
304
|
+
* await myKvItem.getStoredObject();
|
|
305
|
+
*/
|
|
306
|
+
async getStoredObject() {
|
|
307
|
+
return await store.getStoredObject(key);
|
|
308
|
+
},
|
|
309
|
+
/** Get a value by key, or undefined if it does not exist. */
|
|
310
|
+
async get() {
|
|
311
|
+
return await store.get(key);
|
|
312
|
+
},
|
|
313
|
+
/** Delete this key from the store. */
|
|
314
|
+
async delete() {
|
|
315
|
+
await store.delete(key);
|
|
316
|
+
}
|
|
317
|
+
};
|
|
318
|
+
return obj;
|
|
326
319
|
}
|
|
327
320
|
export {
|
|
328
|
-
KvStore,
|
|
329
321
|
KvStoreConfig,
|
|
330
|
-
KvStoreItem,
|
|
331
322
|
configureKvStore,
|
|
323
|
+
createKvStore,
|
|
332
324
|
kvStore,
|
|
333
325
|
kvStoreItem
|
|
334
326
|
};
|