webext-storage 1.2.1 → 1.2.2
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.
|
@@ -7,9 +7,11 @@ export declare class StorageItem<Base, Default = Base | undefined, Return = Defa
|
|
|
7
7
|
readonly key: string;
|
|
8
8
|
readonly area: chrome.storage.AreaName;
|
|
9
9
|
readonly defaultValue?: Default;
|
|
10
|
+
/** @deprecated Use `onChanged` instead */
|
|
11
|
+
onChange: (callback: (value: NonNullable<Default>) => void, signal?: AbortSignal) => void;
|
|
10
12
|
constructor(key: string, { area, defaultValue, }?: StorageItemOptions<NonNullable<Default>>);
|
|
11
13
|
get: () => Promise<Return>;
|
|
12
14
|
set: (value: NonNullable<Default>) => Promise<void>;
|
|
13
15
|
remove: () => Promise<void>;
|
|
14
|
-
|
|
16
|
+
onChanged(callback: (value: NonNullable<Default>) => void, signal?: AbortSignal): void;
|
|
15
17
|
}
|
|
@@ -3,6 +3,8 @@ export class StorageItem {
|
|
|
3
3
|
key;
|
|
4
4
|
area;
|
|
5
5
|
defaultValue;
|
|
6
|
+
/** @deprecated Use `onChanged` instead */
|
|
7
|
+
onChange = this.onChanged;
|
|
6
8
|
constructor(key, { area = 'local', defaultValue, } = {}) {
|
|
7
9
|
this.key = key;
|
|
8
10
|
this.area = area;
|
|
@@ -22,7 +24,7 @@ export class StorageItem {
|
|
|
22
24
|
remove = async () => {
|
|
23
25
|
await chromeP.storage[this.area].remove(this.key);
|
|
24
26
|
};
|
|
25
|
-
|
|
27
|
+
onChanged(callback, signal) {
|
|
26
28
|
const changeHandler = (changes, area) => {
|
|
27
29
|
const changedItem = changes[this.key];
|
|
28
30
|
if (area === this.area && changedItem) {
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -29,7 +29,7 @@ chrome.storage.onChanged.addListener((storageArea, change) => {
|
|
|
29
29
|
const options = new StorageItem<Record<string, string>>('user-options');
|
|
30
30
|
const value = await options.get(); // The type is `Record<string, string> | undefined`
|
|
31
31
|
await options.set({color: 'red'}) // Type-checked
|
|
32
|
-
options.
|
|
32
|
+
options.onChanged(newValue => {
|
|
33
33
|
console.log('New options', newValue)
|
|
34
34
|
});
|
|
35
35
|
```
|
|
@@ -40,7 +40,7 @@ Why this is better:
|
|
|
40
40
|
- `get` only is only `.get()` instead of the awkward post-get object access that
|
|
41
41
|
- Every `get` and `set` operation is type-safe
|
|
42
42
|
- If you provide a `defaultValue`, the return type will not be ` | undefined`
|
|
43
|
-
- The `
|
|
43
|
+
- The `onChanged` example speaks for itself
|
|
44
44
|
|
|
45
45
|
## Install
|
|
46
46
|
|
|
@@ -71,7 +71,7 @@ await username.remove();
|
|
|
71
71
|
await username.set({name: 'Ugo'});
|
|
72
72
|
// TypeScript Error: Argument of type '{ name: string; }' is not assignable to parameter of type 'string'.
|
|
73
73
|
|
|
74
|
-
username.
|
|
74
|
+
username.onChanged(newName => {
|
|
75
75
|
console.log('The user’s new name is', newName);
|
|
76
76
|
});
|
|
77
77
|
```
|