@wxt-dev/storage 1.2.9 → 1.3.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/dist/index.d.mts +4 -0
- package/dist/index.mjs +7 -1
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -128,6 +128,8 @@ interface WxtStorage {
|
|
|
128
128
|
restoreSnapshot(base: StorageArea, data: any): Promise<void>;
|
|
129
129
|
/** Watch for changes to a specific key in storage. */
|
|
130
130
|
watch<T>(key: StorageItemKey, cb: WatchCallback<T | null>): Unwatch;
|
|
131
|
+
/** Watch for changes to a specific key's metadata in storage. */
|
|
132
|
+
watchMeta<T extends Record<string, unknown>>(key: StorageItemKey, cb: WatchCallback<T>): Unwatch;
|
|
131
133
|
/** Remove all watch listeners. */
|
|
132
134
|
unwatch(): void;
|
|
133
135
|
/**
|
|
@@ -168,6 +170,8 @@ interface WxtStorageItem<TValue, TMetadata extends Record<string, unknown>> {
|
|
|
168
170
|
removeMeta(properties?: string[]): Promise<void>;
|
|
169
171
|
/** Listen for changes to the value in storage. */
|
|
170
172
|
watch(cb: WatchCallback<TValue>): Unwatch;
|
|
173
|
+
/** Listen for changes to the metadata in storage. */
|
|
174
|
+
watchMeta(cb: WatchCallback<NullablePartial<TMetadata>>): Unwatch;
|
|
171
175
|
/**
|
|
172
176
|
* If there are migrations defined on the storage item, migrate to the latest
|
|
173
177
|
* version.
|
package/dist/index.mjs
CHANGED
|
@@ -70,7 +70,7 @@ function createStorage() {
|
|
|
70
70
|
return newFields;
|
|
71
71
|
};
|
|
72
72
|
const getValueOrFallback = (value, fallback) => value ?? fallback ?? null;
|
|
73
|
-
const getMetaValue = (properties) => typeof properties === "object" && !Array.isArray(properties) ? properties : {};
|
|
73
|
+
const getMetaValue = (properties) => properties != null && typeof properties === "object" && !Array.isArray(properties) ? properties : {};
|
|
74
74
|
const getItem = async (driver, driverKey, opts) => {
|
|
75
75
|
return getValueOrFallback(await driver.getItem(driverKey), opts?.fallback ?? opts?.defaultValue);
|
|
76
76
|
};
|
|
@@ -103,6 +103,7 @@ function createStorage() {
|
|
|
103
103
|
}
|
|
104
104
|
};
|
|
105
105
|
const watch = (driver, driverKey, cb) => driver.watch(driverKey, cb);
|
|
106
|
+
const watchMeta = (driver, driverKey, cb) => watch(driver, getMetaKey(driverKey), (newValue, oldValue) => cb(getMetaValue(newValue), getMetaValue(oldValue)));
|
|
106
107
|
return {
|
|
107
108
|
getItem: async (key, opts) => {
|
|
108
109
|
const { driver, driverKey } = resolveKey(key);
|
|
@@ -271,6 +272,10 @@ function createStorage() {
|
|
|
271
272
|
const { driver, driverKey } = resolveKey(key);
|
|
272
273
|
return watch(driver, driverKey, cb);
|
|
273
274
|
},
|
|
275
|
+
watchMeta: (key, cb) => {
|
|
276
|
+
const { driver, driverKey } = resolveKey(key);
|
|
277
|
+
return watchMeta(driver, driverKey, cb);
|
|
278
|
+
},
|
|
274
279
|
unwatch() {
|
|
275
280
|
Object.values(drivers).forEach((driver) => {
|
|
276
281
|
driver.unwatch();
|
|
@@ -362,6 +367,7 @@ function createStorage() {
|
|
|
362
367
|
return await removeMeta(driver, driverKey, properties);
|
|
363
368
|
},
|
|
364
369
|
watch: (cb) => watch(driver, driverKey, (newValue, oldValue) => cb(newValue ?? getFallback(), oldValue ?? getFallback())),
|
|
370
|
+
watchMeta: (cb) => watchMeta(driver, driverKey, cb),
|
|
365
371
|
migrate
|
|
366
372
|
};
|
|
367
373
|
}
|