@choksheak/ts-utils 0.3.3 → 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 +6 -6
- 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 +6 -6
- package/capLength.cjs +80 -0
- package/capLength.d.mts +3 -0
- package/capLength.d.ts +3 -0
- package/capLength.min.cjs +6 -0
- package/capLength.min.cjs.map +1 -0
- package/capLength.min.mjs +6 -0
- package/capLength.min.mjs.map +1 -0
- package/capLength.mjs +53 -0
- package/{iterators.cjs → concatIterators.cjs} +4 -4
- package/{iterators.min.cjs → concatIterators.min.cjs} +1 -1
- package/concatIterators.min.cjs.map +1 -0
- package/{iterators.min.mjs → concatIterators.min.mjs} +1 -1
- package/concatIterators.min.mjs.map +1 -0
- package/{iterators.mjs → concatIterators.mjs} +1 -1
- package/duration.min.cjs +1 -1
- package/duration.min.mjs +1 -1
- 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 +6 -6
- 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 +6 -6
- package/median.cjs +7 -7
- 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 +7 -7
- package/package.json +67 -82
- package/round.cjs +2 -10
- package/round.d.mts +2 -8
- package/round.d.ts +2 -8
- package/round.min.cjs +1 -1
- package/round.min.cjs.map +1 -1
- package/round.min.mjs +1 -1
- package/round.min.mjs.map +1 -1
- package/round.mjs +1 -8
- package/{safeParseInt.cjs → roundToString.cjs} +19 -9
- package/roundToString.d.mts +8 -0
- package/roundToString.d.ts +8 -0
- package/roundToString.min.cjs +2 -0
- package/roundToString.min.cjs.map +1 -0
- package/roundToString.min.mjs +2 -0
- package/roundToString.min.mjs.map +1 -0
- package/roundToString.mjs +16 -0
- 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 +6 -6
- 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 +6 -6
- 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/toReadableString.cjs +69 -0
- package/toReadableString.d.mts +10 -0
- package/toReadableString.d.ts +10 -0
- package/toReadableString.min.cjs +6 -0
- package/toReadableString.min.cjs.map +1 -0
- package/toReadableString.min.mjs +6 -0
- package/toReadableString.min.mjs.map +1 -0
- package/toReadableString.mjs +44 -0
- package/average.cjs +0 -55
- package/average.d.mts +0 -7
- package/average.d.ts +0 -7
- package/average.min.cjs +0 -2
- package/average.min.cjs.map +0 -1
- package/average.min.mjs +0 -2
- package/average.min.mjs.map +0 -1
- package/average.mjs +0 -28
- package/iterators.min.cjs.map +0 -1
- package/iterators.min.mjs.map +0 -1
- package/logging.cjs +0 -47
- package/logging.d.mts +0 -4
- package/logging.d.ts +0 -4
- package/logging.min.cjs +0 -2
- package/logging.min.cjs.map +0 -1
- package/logging.min.mjs +0 -2
- package/logging.min.mjs.map +0 -1
- package/logging.mjs +0 -21
- 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.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/{iterators.d.mts → concatIterators.d.mts} +0 -0
- /package/{iterators.d.ts → concatIterators.d.ts} +0 -0
package/localStore.cjs
CHANGED
|
@@ -20,10 +20,9 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/localStore.ts
|
|
21
21
|
var localStore_exports = {};
|
|
22
22
|
__export(localStore_exports, {
|
|
23
|
-
LocalStore: () => LocalStore,
|
|
24
23
|
LocalStoreConfig: () => LocalStoreConfig,
|
|
25
|
-
LocalStoreItem: () => LocalStoreItem,
|
|
26
24
|
configureLocalStore: () => configureLocalStore,
|
|
25
|
+
createLocalStore: () => createLocalStore,
|
|
27
26
|
localStore: () => localStore,
|
|
28
27
|
localStoreItem: () => localStoreItem
|
|
29
28
|
});
|
|
@@ -66,202 +65,204 @@ function validateStoredObject(obj) {
|
|
|
66
65
|
}
|
|
67
66
|
return obj;
|
|
68
67
|
}
|
|
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
|
-
|
|
68
|
+
function createLocalStore(storeName, options) {
|
|
69
|
+
const keyPrefix = storeName + ":";
|
|
70
|
+
const defaultExpiryMs = options?.defaultExpiryMs ? durationOrMsToMs(options.defaultExpiryMs) : LocalStoreConfig.expiryMs;
|
|
71
|
+
const gcIntervalMs = options?.gcIntervalMs ? durationOrMsToMs(options.gcIntervalMs) : LocalStoreConfig.gcIntervalMs;
|
|
72
|
+
const gcMsStorageKey = `__localStore:lastGcMs:${storeName}`;
|
|
73
|
+
const obj = {
|
|
74
|
+
/** Input name for the store. */
|
|
75
|
+
storeName,
|
|
76
|
+
/**
|
|
77
|
+
* The prefix string for the local storage key which identifies items
|
|
78
|
+
* belonging to this namespace.
|
|
79
|
+
*/
|
|
80
|
+
keyPrefix,
|
|
81
|
+
/** Default expiry to use if not specified in set(). */
|
|
82
|
+
defaultExpiryMs,
|
|
83
|
+
/** Time interval for when GC's occur. */
|
|
84
|
+
gcIntervalMs,
|
|
85
|
+
/** Local storage key name for the last GC completed timestamp. */
|
|
86
|
+
gcMsStorageKey,
|
|
87
|
+
/** Set a value in the store. */
|
|
88
|
+
set(key, value, expiryDeltaMs) {
|
|
89
|
+
const nowMs = Date.now();
|
|
90
|
+
const stored = {
|
|
91
|
+
value,
|
|
92
|
+
storedMs: nowMs,
|
|
93
|
+
expiryMs: nowMs + durationOrMsToMs(expiryDeltaMs ?? defaultExpiryMs)
|
|
94
|
+
};
|
|
95
|
+
localStorage.setItem(keyPrefix + key, JSON.stringify(stored));
|
|
96
|
+
obj.gc();
|
|
97
|
+
return value;
|
|
98
|
+
},
|
|
99
|
+
/** Delete one or multiple keys. */
|
|
100
|
+
delete(key) {
|
|
101
|
+
if (typeof key === "string") {
|
|
102
|
+
localStorage.removeItem(keyPrefix + key);
|
|
103
|
+
} else {
|
|
104
|
+
for (const k of key) {
|
|
105
|
+
localStorage.removeItem(keyPrefix + k);
|
|
106
|
+
}
|
|
105
107
|
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
if (!stored) {
|
|
113
|
-
return void 0;
|
|
114
|
-
}
|
|
115
|
-
try {
|
|
116
|
-
const parsed = JSON.parse(stored);
|
|
117
|
-
const obj = validateStoredObject(parsed);
|
|
118
|
-
if (!obj) {
|
|
119
|
-
this.delete(k);
|
|
120
|
-
this.gc();
|
|
108
|
+
},
|
|
109
|
+
/** Mainly used to get the expiration timestamp of an object. */
|
|
110
|
+
getStoredObject(key) {
|
|
111
|
+
const k = keyPrefix + key;
|
|
112
|
+
const stored = localStorage.getItem(k);
|
|
113
|
+
if (!stored) {
|
|
121
114
|
return void 0;
|
|
122
115
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
forEach(callback) {
|
|
138
|
-
for (const k of Object.keys(localStorage)) {
|
|
139
|
-
if (!k.startsWith(this.keyPrefix)) continue;
|
|
140
|
-
const key = k.slice(this.keyPrefix.length);
|
|
141
|
-
const obj = this.getStoredObject(key);
|
|
142
|
-
if (!obj) continue;
|
|
143
|
-
callback(key, obj.value, obj.expiryMs, obj.storedMs);
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
/**
|
|
147
|
-
* Returns the number of items in the store. Note that getting the size
|
|
148
|
-
* requires iterating through the entire store because the items could expire
|
|
149
|
-
* at any time, and hence the size is a dynamic number.
|
|
150
|
-
*/
|
|
151
|
-
size() {
|
|
152
|
-
let count = 0;
|
|
153
|
-
this.forEach(() => {
|
|
154
|
-
count++;
|
|
155
|
-
});
|
|
156
|
-
return count;
|
|
157
|
-
}
|
|
158
|
-
/** Remove all items from the store. */
|
|
159
|
-
clear() {
|
|
160
|
-
for (const key of Object.keys(localStorage)) {
|
|
161
|
-
if (key.startsWith(this.keyPrefix)) {
|
|
162
|
-
localStorage.removeItem(key);
|
|
116
|
+
try {
|
|
117
|
+
const parsed = JSON.parse(stored);
|
|
118
|
+
const valid = validateStoredObject(parsed);
|
|
119
|
+
if (!valid) {
|
|
120
|
+
obj.delete(k);
|
|
121
|
+
obj.gc();
|
|
122
|
+
return void 0;
|
|
123
|
+
}
|
|
124
|
+
return valid;
|
|
125
|
+
} catch (e) {
|
|
126
|
+
console.error(`Invalid local value: ${k}=${stored}:`, e);
|
|
127
|
+
obj.delete(k);
|
|
128
|
+
obj.gc();
|
|
129
|
+
return void 0;
|
|
163
130
|
}
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
}
|
|
189
|
-
/** Perform garbage-collection if due, else do nothing. */
|
|
190
|
-
gc() {
|
|
191
|
-
const lastGcMs = this.lastGcMs;
|
|
192
|
-
if (!lastGcMs) {
|
|
193
|
-
this.lastGcMs = Date.now();
|
|
194
|
-
return;
|
|
195
|
-
}
|
|
196
|
-
if (Date.now() < lastGcMs + this.gcIntervalMs) {
|
|
197
|
-
return;
|
|
198
|
-
}
|
|
199
|
-
this.gcNow();
|
|
200
|
-
}
|
|
201
|
-
/**
|
|
202
|
-
* Perform garbage collection immediately without checking whether we are
|
|
203
|
-
* due for the next GC or not.
|
|
204
|
-
*/
|
|
205
|
-
gcNow() {
|
|
206
|
-
console.log(`Starting localStore GC on ${this.storeName}`);
|
|
207
|
-
this.lastGcMs = Date.now();
|
|
208
|
-
let count = 0;
|
|
209
|
-
this.forEach((key, value, expiryMs) => {
|
|
210
|
-
if (Date.now() >= expiryMs) {
|
|
211
|
-
this.delete(key);
|
|
131
|
+
},
|
|
132
|
+
/** Get a value by key, or undefined if it does not exist. */
|
|
133
|
+
get(key) {
|
|
134
|
+
const stored = obj.getStoredObject(key);
|
|
135
|
+
return stored?.value;
|
|
136
|
+
},
|
|
137
|
+
/** Generic way to iterate through all entries. */
|
|
138
|
+
forEach(callback) {
|
|
139
|
+
for (const k of Object.keys(localStorage)) {
|
|
140
|
+
if (!k.startsWith(keyPrefix)) continue;
|
|
141
|
+
const key = k.slice(keyPrefix.length);
|
|
142
|
+
const stored = obj.getStoredObject(key);
|
|
143
|
+
if (!stored) continue;
|
|
144
|
+
callback(key, stored.value, stored.expiryMs, stored.storedMs);
|
|
145
|
+
}
|
|
146
|
+
},
|
|
147
|
+
/**
|
|
148
|
+
* Returns the number of items in the store. Note that getting the size
|
|
149
|
+
* requires iterating through the entire store because the items could expire
|
|
150
|
+
* at any time, and hence the size is a dynamic number.
|
|
151
|
+
*/
|
|
152
|
+
size() {
|
|
153
|
+
let count = 0;
|
|
154
|
+
obj.forEach(() => {
|
|
212
155
|
count++;
|
|
156
|
+
});
|
|
157
|
+
return count;
|
|
158
|
+
},
|
|
159
|
+
/** Remove all items from the store. */
|
|
160
|
+
clear() {
|
|
161
|
+
for (const key of Object.keys(localStorage)) {
|
|
162
|
+
if (key.startsWith(keyPrefix)) {
|
|
163
|
+
localStorage.removeItem(key);
|
|
164
|
+
}
|
|
213
165
|
}
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
};
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
166
|
+
},
|
|
167
|
+
/**
|
|
168
|
+
* Returns all items as map of key to value, mainly used for debugging dumps.
|
|
169
|
+
* The type T is applied to all values, even though they might not be of type
|
|
170
|
+
* T (in the case when you store different data types in the same store).
|
|
171
|
+
*/
|
|
172
|
+
asMap() {
|
|
173
|
+
const map = /* @__PURE__ */ new Map();
|
|
174
|
+
obj.forEach(
|
|
175
|
+
(key, value, expiryMs, storedMs) => {
|
|
176
|
+
map.set(key, { value, expiryMs, storedMs });
|
|
177
|
+
}
|
|
178
|
+
);
|
|
179
|
+
return map;
|
|
180
|
+
},
|
|
181
|
+
/** Returns the ms timestamp for the last GC (garbage collection). */
|
|
182
|
+
getLastGcMs() {
|
|
183
|
+
const lastGcMsStr = localStorage.getItem(gcMsStorageKey);
|
|
184
|
+
if (!lastGcMsStr) return 0;
|
|
185
|
+
const ms = Number(lastGcMsStr);
|
|
186
|
+
return isNaN(ms) ? 0 : ms;
|
|
187
|
+
},
|
|
188
|
+
/** Set the ms timestamp for the last GC (garbage collection). */
|
|
189
|
+
setLastGcMs(ms) {
|
|
190
|
+
localStorage.setItem(gcMsStorageKey, String(ms));
|
|
191
|
+
},
|
|
192
|
+
/** Perform garbage-collection if due, else do nothing. */
|
|
193
|
+
gc() {
|
|
194
|
+
const lastGcMs = obj.getLastGcMs();
|
|
195
|
+
if (!lastGcMs) {
|
|
196
|
+
obj.setLastGcMs(Date.now());
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
if (Date.now() < lastGcMs + gcIntervalMs) {
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
obj.gcNow();
|
|
203
|
+
},
|
|
204
|
+
/**
|
|
205
|
+
* Perform garbage collection immediately without checking whether we are
|
|
206
|
+
* due for the next GC or not.
|
|
207
|
+
*/
|
|
208
|
+
gcNow() {
|
|
209
|
+
console.log(`Starting localStore GC on ${storeName}`);
|
|
210
|
+
obj.setLastGcMs(Date.now());
|
|
211
|
+
let count = 0;
|
|
212
|
+
obj.forEach((key, value, expiryMs) => {
|
|
213
|
+
if (Date.now() >= expiryMs) {
|
|
214
|
+
obj.delete(key);
|
|
215
|
+
count++;
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
console.log(
|
|
219
|
+
`Finished localStore GC on ${storeName} - deleted ${count} keys`
|
|
220
|
+
);
|
|
221
|
+
obj.setLastGcMs(Date.now());
|
|
222
|
+
},
|
|
223
|
+
/** Returns `this` casted into a StorageAdapter<T>. */
|
|
224
|
+
asStorageAdapter() {
|
|
225
|
+
return obj;
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
return obj;
|
|
229
|
+
}
|
|
230
|
+
var localStore = createLocalStore(LocalStoreConfig.storeName);
|
|
255
231
|
function localStoreItem(key, expiryMs, store = localStore) {
|
|
256
|
-
|
|
257
|
-
|
|
232
|
+
const defaultExpiryMs = expiryMs && durationOrMsToMs(expiryMs);
|
|
233
|
+
const obj = {
|
|
234
|
+
key,
|
|
235
|
+
defaultExpiryMs,
|
|
236
|
+
store,
|
|
237
|
+
/** Set a value in the store. */
|
|
238
|
+
set(value, expiryDeltaMs) {
|
|
239
|
+
store.set(key, value, expiryDeltaMs ?? defaultExpiryMs);
|
|
240
|
+
},
|
|
241
|
+
/**
|
|
242
|
+
* Example usage:
|
|
243
|
+
*
|
|
244
|
+
* const { value, storedMs, expiryMs, storedMs } =
|
|
245
|
+
* await myLocalItem.getStoredObject();
|
|
246
|
+
*/
|
|
247
|
+
getStoredObject() {
|
|
248
|
+
return store.getStoredObject(key);
|
|
249
|
+
},
|
|
250
|
+
/** Get a value by key, or undefined if it does not exist. */
|
|
251
|
+
get() {
|
|
252
|
+
return store.get(key);
|
|
253
|
+
},
|
|
254
|
+
/** Delete this key from the store. */
|
|
255
|
+
delete() {
|
|
256
|
+
store.delete(key);
|
|
257
|
+
}
|
|
258
|
+
};
|
|
259
|
+
return obj;
|
|
258
260
|
}
|
|
259
261
|
// Annotate the CommonJS export names for ESM import in node:
|
|
260
262
|
0 && (module.exports = {
|
|
261
|
-
LocalStore,
|
|
262
263
|
LocalStoreConfig,
|
|
263
|
-
LocalStoreItem,
|
|
264
264
|
configureLocalStore,
|
|
265
|
+
createLocalStore,
|
|
265
266
|
localStore,
|
|
266
267
|
localStoreItem
|
|
267
268
|
});
|
package/localStore.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Duration } from './duration.mjs';
|
|
2
|
-
import {
|
|
2
|
+
import { StoredObject, StorageAdapter } from './storageAdapter.mjs';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Local storage key-value store with support for auto-expirations.
|
|
@@ -33,72 +33,174 @@ declare function configureLocalStore(config: Partial<LocalStoreConfig>): void;
|
|
|
33
33
|
* You can create multiple LocalStores if you want, but most likely you will only
|
|
34
34
|
* need to use the default `localStore` instance.
|
|
35
35
|
*/
|
|
36
|
-
declare
|
|
36
|
+
declare function createLocalStore(storeName: string, options?: {
|
|
37
|
+
defaultExpiryMs?: number | Duration;
|
|
38
|
+
gcIntervalMs?: number | Duration;
|
|
39
|
+
}): {
|
|
40
|
+
/** Input name for the store. */
|
|
37
41
|
readonly storeName: string;
|
|
38
42
|
/**
|
|
39
43
|
* The prefix string for the local storage key which identifies items
|
|
40
44
|
* belonging to this namespace.
|
|
41
45
|
*/
|
|
42
46
|
readonly keyPrefix: string;
|
|
43
|
-
/**
|
|
44
|
-
readonly gcMsStorageKey: string;
|
|
47
|
+
/** Default expiry to use if not specified in set(). */
|
|
45
48
|
readonly defaultExpiryMs: number;
|
|
49
|
+
/** Time interval for when GC's occur. */
|
|
46
50
|
readonly gcIntervalMs: number;
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
gcIntervalMs?: number | Duration;
|
|
50
|
-
});
|
|
51
|
+
/** Local storage key name for the last GC completed timestamp. */
|
|
52
|
+
readonly gcMsStorageKey: string;
|
|
51
53
|
/** Set a value in the store. */
|
|
52
|
-
set<T>(key: string, value: T, expiryDeltaMs?: number | Duration)
|
|
54
|
+
readonly set: <T>(key: string, value: T, expiryDeltaMs?: number | Duration) => T;
|
|
53
55
|
/** Delete one or multiple keys. */
|
|
54
|
-
delete(key: string | string[])
|
|
56
|
+
readonly delete: (key: string | string[]) => void;
|
|
55
57
|
/** Mainly used to get the expiration timestamp of an object. */
|
|
56
|
-
getStoredObject<T>(key: string)
|
|
58
|
+
readonly getStoredObject: <T>(key: string) => StoredObject<T> | undefined;
|
|
57
59
|
/** Get a value by key, or undefined if it does not exist. */
|
|
58
|
-
get<T>(key: string)
|
|
60
|
+
readonly get: <T>(key: string) => T | undefined;
|
|
59
61
|
/** Generic way to iterate through all entries. */
|
|
60
|
-
forEach<T>(callback: (key: string, value: T, expiryMs: number, storedMs: number) => void)
|
|
62
|
+
readonly forEach: <T>(callback: (key: string, value: T, expiryMs: number, storedMs: number) => void) => void;
|
|
61
63
|
/**
|
|
62
64
|
* Returns the number of items in the store. Note that getting the size
|
|
63
65
|
* requires iterating through the entire store because the items could expire
|
|
64
66
|
* at any time, and hence the size is a dynamic number.
|
|
65
67
|
*/
|
|
66
|
-
size()
|
|
68
|
+
readonly size: () => number;
|
|
67
69
|
/** Remove all items from the store. */
|
|
68
|
-
clear()
|
|
70
|
+
readonly clear: () => void;
|
|
69
71
|
/**
|
|
70
72
|
* Returns all items as map of key to value, mainly used for debugging dumps.
|
|
71
73
|
* The type T is applied to all values, even though they might not be of type
|
|
72
74
|
* T (in the case when you store different data types in the same store).
|
|
73
75
|
*/
|
|
74
|
-
asMap<T>()
|
|
76
|
+
readonly asMap: <T>() => Map<string, StoredObject<T>>;
|
|
75
77
|
/** Returns the ms timestamp for the last GC (garbage collection). */
|
|
76
|
-
|
|
78
|
+
readonly getLastGcMs: () => number;
|
|
77
79
|
/** Set the ms timestamp for the last GC (garbage collection). */
|
|
78
|
-
|
|
80
|
+
readonly setLastGcMs: (ms: number) => void;
|
|
79
81
|
/** Perform garbage-collection if due, else do nothing. */
|
|
80
|
-
gc()
|
|
82
|
+
readonly gc: () => void;
|
|
81
83
|
/**
|
|
82
84
|
* Perform garbage collection immediately without checking whether we are
|
|
83
85
|
* due for the next GC or not.
|
|
84
86
|
*/
|
|
85
|
-
gcNow()
|
|
87
|
+
readonly gcNow: () => void;
|
|
86
88
|
/** Returns `this` casted into a StorageAdapter<T>. */
|
|
87
|
-
asStorageAdapter<T>()
|
|
88
|
-
}
|
|
89
|
+
readonly asStorageAdapter: <T>() => StorageAdapter<T>;
|
|
90
|
+
};
|
|
91
|
+
type LocalStore = ReturnType<typeof createLocalStore>;
|
|
89
92
|
/**
|
|
90
93
|
* Default local store ready for immediate use. You can create new instances if
|
|
91
94
|
* you want, but most likely you will only need one store instance.
|
|
92
95
|
*/
|
|
93
|
-
declare const localStore:
|
|
94
|
-
/**
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
96
|
+
declare const localStore: {
|
|
97
|
+
/** Input name for the store. */
|
|
98
|
+
readonly storeName: string;
|
|
99
|
+
/**
|
|
100
|
+
* The prefix string for the local storage key which identifies items
|
|
101
|
+
* belonging to this namespace.
|
|
102
|
+
*/
|
|
103
|
+
readonly keyPrefix: string;
|
|
104
|
+
/** Default expiry to use if not specified in set(). */
|
|
100
105
|
readonly defaultExpiryMs: number;
|
|
101
|
-
|
|
106
|
+
/** Time interval for when GC's occur. */
|
|
107
|
+
readonly gcIntervalMs: number;
|
|
108
|
+
/** Local storage key name for the last GC completed timestamp. */
|
|
109
|
+
readonly gcMsStorageKey: string;
|
|
110
|
+
/** Set a value in the store. */
|
|
111
|
+
readonly set: <T>(key: string, value: T, expiryDeltaMs?: number | Duration) => T;
|
|
112
|
+
/** Delete one or multiple keys. */
|
|
113
|
+
readonly delete: (key: string | string[]) => void;
|
|
114
|
+
/** Mainly used to get the expiration timestamp of an object. */
|
|
115
|
+
readonly getStoredObject: <T>(key: string) => StoredObject<T> | undefined;
|
|
116
|
+
/** Get a value by key, or undefined if it does not exist. */
|
|
117
|
+
readonly get: <T>(key: string) => T | undefined;
|
|
118
|
+
/** Generic way to iterate through all entries. */
|
|
119
|
+
readonly forEach: <T>(callback: (key: string, value: T, expiryMs: number, storedMs: number) => void) => void;
|
|
120
|
+
/**
|
|
121
|
+
* Returns the number of items in the store. Note that getting the size
|
|
122
|
+
* requires iterating through the entire store because the items could expire
|
|
123
|
+
* at any time, and hence the size is a dynamic number.
|
|
124
|
+
*/
|
|
125
|
+
readonly size: () => number;
|
|
126
|
+
/** Remove all items from the store. */
|
|
127
|
+
readonly clear: () => void;
|
|
128
|
+
/**
|
|
129
|
+
* Returns all items as map of key to value, mainly used for debugging dumps.
|
|
130
|
+
* The type T is applied to all values, even though they might not be of type
|
|
131
|
+
* T (in the case when you store different data types in the same store).
|
|
132
|
+
*/
|
|
133
|
+
readonly asMap: <T>() => Map<string, StoredObject<T>>;
|
|
134
|
+
/** Returns the ms timestamp for the last GC (garbage collection). */
|
|
135
|
+
readonly getLastGcMs: () => number;
|
|
136
|
+
/** Set the ms timestamp for the last GC (garbage collection). */
|
|
137
|
+
readonly setLastGcMs: (ms: number) => void;
|
|
138
|
+
/** Perform garbage-collection if due, else do nothing. */
|
|
139
|
+
readonly gc: () => void;
|
|
140
|
+
/**
|
|
141
|
+
* Perform garbage collection immediately without checking whether we are
|
|
142
|
+
* due for the next GC or not.
|
|
143
|
+
*/
|
|
144
|
+
readonly gcNow: () => void;
|
|
145
|
+
/** Returns `this` casted into a StorageAdapter<T>. */
|
|
146
|
+
readonly asStorageAdapter: <T>() => StorageAdapter<T>;
|
|
147
|
+
};
|
|
148
|
+
/** Create a local store item with a key and a default expiration. */
|
|
149
|
+
declare function localStoreItem<T>(key: string, expiryMs?: number | Duration, store?: LocalStore): {
|
|
150
|
+
key: string;
|
|
151
|
+
defaultExpiryMs: number | undefined;
|
|
152
|
+
store: {
|
|
153
|
+
/** Input name for the store. */
|
|
154
|
+
readonly storeName: string;
|
|
155
|
+
/**
|
|
156
|
+
* The prefix string for the local storage key which identifies items
|
|
157
|
+
* belonging to this namespace.
|
|
158
|
+
*/
|
|
159
|
+
readonly keyPrefix: string;
|
|
160
|
+
/** Default expiry to use if not specified in set(). */
|
|
161
|
+
readonly defaultExpiryMs: number;
|
|
162
|
+
/** Time interval for when GC's occur. */
|
|
163
|
+
readonly gcIntervalMs: number;
|
|
164
|
+
/** Local storage key name for the last GC completed timestamp. */
|
|
165
|
+
readonly gcMsStorageKey: string;
|
|
166
|
+
/** Set a value in the store. */
|
|
167
|
+
readonly set: <T_1>(key: string, value: T_1, expiryDeltaMs?: number | Duration) => T_1;
|
|
168
|
+
/** Delete one or multiple keys. */
|
|
169
|
+
readonly delete: (key: string | string[]) => void;
|
|
170
|
+
/** Mainly used to get the expiration timestamp of an object. */
|
|
171
|
+
readonly getStoredObject: <T_1>(key: string) => StoredObject<T_1> | undefined;
|
|
172
|
+
/** Get a value by key, or undefined if it does not exist. */
|
|
173
|
+
readonly get: <T_1>(key: string) => T_1 | undefined;
|
|
174
|
+
/** Generic way to iterate through all entries. */
|
|
175
|
+
readonly forEach: <T_1>(callback: (key: string, value: T_1, expiryMs: number, storedMs: number) => void) => void;
|
|
176
|
+
/**
|
|
177
|
+
* Returns the number of items in the store. Note that getting the size
|
|
178
|
+
* requires iterating through the entire store because the items could expire
|
|
179
|
+
* at any time, and hence the size is a dynamic number.
|
|
180
|
+
*/
|
|
181
|
+
readonly size: () => number;
|
|
182
|
+
/** Remove all items from the store. */
|
|
183
|
+
readonly clear: () => void;
|
|
184
|
+
/**
|
|
185
|
+
* Returns all items as map of key to value, mainly used for debugging dumps.
|
|
186
|
+
* The type T is applied to all values, even though they might not be of type
|
|
187
|
+
* T (in the case when you store different data types in the same store).
|
|
188
|
+
*/
|
|
189
|
+
readonly asMap: <T_1>() => Map<string, StoredObject<T_1>>;
|
|
190
|
+
/** Returns the ms timestamp for the last GC (garbage collection). */
|
|
191
|
+
readonly getLastGcMs: () => number;
|
|
192
|
+
/** Set the ms timestamp for the last GC (garbage collection). */
|
|
193
|
+
readonly setLastGcMs: (ms: number) => void;
|
|
194
|
+
/** Perform garbage-collection if due, else do nothing. */
|
|
195
|
+
readonly gc: () => void;
|
|
196
|
+
/**
|
|
197
|
+
* Perform garbage collection immediately without checking whether we are
|
|
198
|
+
* due for the next GC or not.
|
|
199
|
+
*/
|
|
200
|
+
readonly gcNow: () => void;
|
|
201
|
+
/** Returns `this` casted into a StorageAdapter<T>. */
|
|
202
|
+
readonly asStorageAdapter: <T_1>() => StorageAdapter<T_1>;
|
|
203
|
+
};
|
|
102
204
|
/** Set a value in the store. */
|
|
103
205
|
set(value: T, expiryDeltaMs?: number | undefined): void;
|
|
104
206
|
/**
|
|
@@ -112,8 +214,7 @@ declare class LocalStoreItem<T> {
|
|
|
112
214
|
get(): T | undefined;
|
|
113
215
|
/** Delete this key from the store. */
|
|
114
216
|
delete(): void;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
declare function localStoreItem<T>(key: string, expiryMs?: number | Duration, store?: LocalStore): LocalStoreItem<T>;
|
|
217
|
+
};
|
|
218
|
+
type LocalStoreItem<T> = ReturnType<typeof localStoreItem<T>>;
|
|
118
219
|
|
|
119
|
-
export { LocalStore, LocalStoreConfig, LocalStoreItem, configureLocalStore, localStore, localStoreItem };
|
|
220
|
+
export { type LocalStore, LocalStoreConfig, type LocalStoreItem, configureLocalStore, createLocalStore, localStore, localStoreItem };
|