@webext-core/storage 1.0.1 → 1.1.1
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 +3 -30
- package/lib/index.cjs +75 -4
- package/lib/index.d.ts +12 -0
- package/lib/index.global.js +1130 -0
- package/lib/index.js +72 -0
- package/package.json +4 -3
package/README.md
CHANGED
|
@@ -6,39 +6,12 @@ A type-safe wrapper around the `Browser.storage` APIs, based off local storage.
|
|
|
6
6
|
pnpm i @webext-core/storage
|
|
7
7
|
```
|
|
8
8
|
|
|
9
|
-
## Usage
|
|
10
|
-
|
|
11
|
-
If you don't use TypeScript, or don't want to type your store, you can use any of the named exports from the package.
|
|
12
|
-
|
|
13
9
|
```ts
|
|
14
|
-
import { localExtStorage
|
|
10
|
+
import { localExtStorage } from '@webext-core/storage';
|
|
15
11
|
|
|
16
12
|
const value = await localExtStorage.getItem('some-key');
|
|
17
13
|
```
|
|
18
14
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
```ts
|
|
22
|
-
// storage.ts
|
|
23
|
-
import Browser from 'webextension-polyfill';
|
|
24
|
-
import { defineExtensionStorage } from '@webext-core/storage';
|
|
25
|
-
|
|
26
|
-
export interface LocalExtStorageSchema {
|
|
27
|
-
someKey: boolean;
|
|
28
|
-
someOtherKey?: string;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export const localExtStorage = defineExtensionStorage<LocalExtStorageSchema>(Browser.storage.local);
|
|
32
|
-
```
|
|
33
|
-
|
|
34
|
-
Then use `localExtStorage` from your own module:
|
|
35
|
-
|
|
36
|
-
```ts
|
|
37
|
-
import { localExtStorage } from './storage';
|
|
38
|
-
|
|
39
|
-
// This works
|
|
40
|
-
const value: boolean | null = await localExtStorage.getItem('someKey');
|
|
15
|
+
## Get Started
|
|
41
16
|
|
|
42
|
-
|
|
43
|
-
localExtStorage.getItem('xyz');
|
|
44
|
-
```
|
|
17
|
+
See [documentation](https://webext-core.aklinker1.io/storage) to get started!
|
package/lib/index.cjs
CHANGED
|
@@ -22,6 +22,26 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
22
22
|
mod
|
|
23
23
|
));
|
|
24
24
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
25
|
+
var __async = (__this, __arguments, generator) => {
|
|
26
|
+
return new Promise((resolve, reject) => {
|
|
27
|
+
var fulfilled = (value) => {
|
|
28
|
+
try {
|
|
29
|
+
step(generator.next(value));
|
|
30
|
+
} catch (e) {
|
|
31
|
+
reject(e);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
var rejected = (value) => {
|
|
35
|
+
try {
|
|
36
|
+
step(generator.throw(value));
|
|
37
|
+
} catch (e) {
|
|
38
|
+
reject(e);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
42
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
43
|
+
});
|
|
44
|
+
};
|
|
25
45
|
|
|
26
46
|
// src/index.ts
|
|
27
47
|
var src_exports = {};
|
|
@@ -34,7 +54,37 @@ __export(src_exports, {
|
|
|
34
54
|
module.exports = __toCommonJS(src_exports);
|
|
35
55
|
|
|
36
56
|
// src/defineExtensionStorage.ts
|
|
57
|
+
var import_webextension_polyfill = __toESM(require("webextension-polyfill"), 1);
|
|
37
58
|
function defineExtensionStorage(storage) {
|
|
59
|
+
const area = getStorageName(storage);
|
|
60
|
+
const onStorageChanged = (changes, changedArea) => __async(this, null, function* () {
|
|
61
|
+
if (area !== changedArea)
|
|
62
|
+
return;
|
|
63
|
+
const work = listeners.map(({ key, cb }) => {
|
|
64
|
+
if (!(key in changes))
|
|
65
|
+
return;
|
|
66
|
+
const { newValue, oldValue } = changes[key];
|
|
67
|
+
if (newValue === oldValue)
|
|
68
|
+
return;
|
|
69
|
+
return cb(newValue, oldValue);
|
|
70
|
+
});
|
|
71
|
+
yield Promise.all(work);
|
|
72
|
+
});
|
|
73
|
+
let listeners = [];
|
|
74
|
+
function addListener(listener) {
|
|
75
|
+
if (listeners.length === 0) {
|
|
76
|
+
import_webextension_polyfill.default.storage.onChanged.addListener(onStorageChanged);
|
|
77
|
+
}
|
|
78
|
+
listeners.push(listener);
|
|
79
|
+
}
|
|
80
|
+
function removeListener(listener) {
|
|
81
|
+
const i = listeners.indexOf(listener);
|
|
82
|
+
if (i >= 0)
|
|
83
|
+
listeners.splice(i, 1);
|
|
84
|
+
if (listeners.length === 0) {
|
|
85
|
+
import_webextension_polyfill.default.storage.onChanged.removeListener(onStorageChanged);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
38
88
|
return {
|
|
39
89
|
clear: storage.clear,
|
|
40
90
|
getItem(key) {
|
|
@@ -48,15 +98,36 @@ function defineExtensionStorage(storage) {
|
|
|
48
98
|
},
|
|
49
99
|
removeItem(key) {
|
|
50
100
|
return storage.remove(key);
|
|
101
|
+
},
|
|
102
|
+
onChange(key, cb) {
|
|
103
|
+
const listener = {
|
|
104
|
+
key,
|
|
105
|
+
cb
|
|
106
|
+
};
|
|
107
|
+
addListener(listener);
|
|
108
|
+
return () => removeListener(listener);
|
|
51
109
|
}
|
|
52
110
|
};
|
|
53
111
|
}
|
|
112
|
+
function getStorageName(storage) {
|
|
113
|
+
switch (storage) {
|
|
114
|
+
case import_webextension_polyfill.default.storage.local:
|
|
115
|
+
return "local";
|
|
116
|
+
case import_webextension_polyfill.default.storage.sync:
|
|
117
|
+
return "sync";
|
|
118
|
+
case import_webextension_polyfill.default.storage.managed:
|
|
119
|
+
return "managed";
|
|
120
|
+
}
|
|
121
|
+
throw Error(
|
|
122
|
+
"Unsupported storage area. local, sync, and managed are the only supporte storage areas."
|
|
123
|
+
);
|
|
124
|
+
}
|
|
54
125
|
|
|
55
126
|
// src/index.ts
|
|
56
|
-
var
|
|
57
|
-
var localExtStorage = defineExtensionStorage(
|
|
58
|
-
var syncExtStorage = defineExtensionStorage(
|
|
59
|
-
var managedExtStorage = defineExtensionStorage(
|
|
127
|
+
var import_webextension_polyfill2 = __toESM(require("webextension-polyfill"), 1);
|
|
128
|
+
var localExtStorage = defineExtensionStorage(import_webextension_polyfill2.default.storage.local);
|
|
129
|
+
var syncExtStorage = defineExtensionStorage(import_webextension_polyfill2.default.storage.sync);
|
|
130
|
+
var managedExtStorage = defineExtensionStorage(import_webextension_polyfill2.default.storage.managed);
|
|
60
131
|
// Annotate the CommonJS export names for ESM import in node:
|
|
61
132
|
0 && (module.exports = {
|
|
62
133
|
defineExtensionStorage,
|
package/lib/index.d.ts
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import { Storage } from 'webextension-polyfill';
|
|
2
2
|
|
|
3
3
|
declare type AnySchema = Record<string, any>;
|
|
4
|
+
/**
|
|
5
|
+
* Call this method to remove the listener that was added.
|
|
6
|
+
*/
|
|
7
|
+
declare type RemoveListenerCallback = () => void;
|
|
8
|
+
declare type OnChangeCallback<TSchema extends AnySchema, TKey extends keyof TSchema = keyof TSchema> = (newValue: TSchema[TKey], oldValue: TSchema[TKey] | null) => void;
|
|
4
9
|
interface ExtensionStorage<TSchema extends AnySchema> {
|
|
5
10
|
/**
|
|
6
11
|
* Clear all values from storage.
|
|
@@ -19,6 +24,13 @@ interface ExtensionStorage<TSchema extends AnySchema> {
|
|
|
19
24
|
* Remove the value from storage at a key.
|
|
20
25
|
*/
|
|
21
26
|
removeItem<TKey extends keyof TSchema>(key: TKey): Promise<void>;
|
|
27
|
+
/**
|
|
28
|
+
* Add a callback that is executed when a key is changed in the storage. Listeners are executed in
|
|
29
|
+
* parallel, but the first listeners added are always started first.
|
|
30
|
+
*
|
|
31
|
+
* Returns a method that, when called, removes the listener that was added.
|
|
32
|
+
*/
|
|
33
|
+
onChange<TKey extends keyof TSchema>(key: TKey, cb: OnChangeCallback<TSchema, TKey>): RemoveListenerCallback;
|
|
22
34
|
}
|
|
23
35
|
|
|
24
36
|
/**
|