varstor 0.6.63 → 0.6.65
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 +55 -18
- package/dist/varstor-webextension.js +114 -78
- package/dist/varstor-webextension.min.js +1 -1
- package/dist/varstor.js +105 -74
- package/dist/varstor.min.js +1 -1
- package/package.json +1 -1
- package/src/actions.ts +3 -9
- package/src/helpers.ts +8 -0
- package/src/index.ts +87 -20
- package/src/listeners.ts +21 -15
- package/src/storage.ts +7 -0
- package/src/types.d.ts +12 -4
- package/src/webextension-storage.ts +7 -0
- package/src/webextension.ts +3 -3
- package/src/validation.ts +0 -31
package/src/listeners.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { STATE,
|
|
1
|
+
import { STATE, createStore } from ".";
|
|
2
2
|
import { NAMESPACE_DELIMITER } from "./constants";
|
|
3
|
-
import { map, isFunction } from "./helpers";
|
|
3
|
+
import { map, isFunction, isAsyncFunction } from "./helpers";
|
|
4
4
|
|
|
5
5
|
export const LISTENERS = {} as IStateListeners;
|
|
6
6
|
|
|
@@ -8,28 +8,34 @@ export async function runStateChangeListeners(realChanges: IStateChanges) {
|
|
|
8
8
|
if (Object.keys(realChanges).length) {
|
|
9
9
|
const { namespace } =
|
|
10
10
|
STATE[Object.keys(realChanges)[0] as keyof typeof STATE];
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
for (let [changeKey, change] of Object.entries(realChanges)) {
|
|
11
|
+
for (const [changeKey] of Object.entries(realChanges)) {
|
|
14
12
|
const { listeners, key } = STATE[changeKey];
|
|
15
13
|
for (const cb of listeners!) {
|
|
16
|
-
|
|
14
|
+
const fn = cb.bind(null, [key], createStore(namespace()));
|
|
15
|
+
if (isAsyncFunction(cb)) {
|
|
16
|
+
await fn();
|
|
17
|
+
} else {
|
|
18
|
+
fn();
|
|
19
|
+
}
|
|
17
20
|
}
|
|
18
21
|
}
|
|
19
22
|
|
|
23
|
+
const namespaceLength = `${namespace()}${NAMESPACE_DELIMITER}`.length;
|
|
20
24
|
const readableKeys = map(realChanges, (k) =>
|
|
21
|
-
k.slice(
|
|
25
|
+
k.slice(namespaceLength),
|
|
22
26
|
) as string[];
|
|
23
27
|
|
|
24
28
|
for (const cb of LISTENERS[namespace()]) {
|
|
25
|
-
|
|
29
|
+
const fn = cb.bind(
|
|
30
|
+
null,
|
|
26
31
|
readableKeys,
|
|
27
|
-
|
|
28
|
-
map(realChanges, (k, v) => [
|
|
29
|
-
k.slice(NAMESPACE_DELIMITER.length),
|
|
30
|
-
v,
|
|
31
|
-
]) as IStateChanges,
|
|
32
|
+
createStore(namespace())
|
|
32
33
|
);
|
|
34
|
+
if (isAsyncFunction(cb)) {
|
|
35
|
+
await fn();
|
|
36
|
+
} else {
|
|
37
|
+
fn();
|
|
38
|
+
}
|
|
33
39
|
}
|
|
34
40
|
}
|
|
35
41
|
}
|
|
@@ -39,7 +45,7 @@ export function addStateListener(
|
|
|
39
45
|
observables: string[] | IStateListener,
|
|
40
46
|
cb: () => {},
|
|
41
47
|
) {
|
|
42
|
-
if (isFunction(observables)) {
|
|
48
|
+
if (isFunction(observables) || isAsyncFunction(observables)) {
|
|
43
49
|
LISTENERS[namespace()].push(observables as IStateListener);
|
|
44
50
|
return createStore(namespace());
|
|
45
51
|
}
|
|
@@ -56,7 +62,7 @@ export function removeStateListener(
|
|
|
56
62
|
observables: string[] | IStateListener,
|
|
57
63
|
removeCb: () => {},
|
|
58
64
|
) {
|
|
59
|
-
if (isFunction(observables)) {
|
|
65
|
+
if (isFunction(observables) || isAsyncFunction(observables)) {
|
|
60
66
|
const removeIdx = LISTENERS[namespace()].findIndex(
|
|
61
67
|
(cb) => cb === observables,
|
|
62
68
|
);
|
package/src/storage.ts
CHANGED
|
@@ -29,9 +29,16 @@ async function setStorageValue(
|
|
|
29
29
|
return false;
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
+
function removeStorageKey (type: string, key: string) {
|
|
33
|
+
if (type) {
|
|
34
|
+
localStorage.removeItem(key);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
32
38
|
export default {
|
|
33
39
|
GET_TYPE: getStorageType,
|
|
34
40
|
IS_AVAILABE: isStorageAvailable,
|
|
35
41
|
UPDATE_STATE: updateFromLocalStorage,
|
|
36
42
|
SET_VALUES: setStorageValue,
|
|
43
|
+
REMOVE_KEY: removeStorageKey,
|
|
37
44
|
};
|
package/src/types.d.ts
CHANGED
|
@@ -29,16 +29,24 @@ type IStorageFunctions = {
|
|
|
29
29
|
IS_AVAILABE: (type: string) => boolean;
|
|
30
30
|
UPDATE_STATE: (state: IStateDefault, type: string) => IStateDefault;
|
|
31
31
|
SET_VALUES: (type: string, changes: Record<string, any>) => Promise<boolean>;
|
|
32
|
+
REMOVE_KEY: (type: string, key: string) => void;
|
|
32
33
|
};
|
|
33
34
|
|
|
34
35
|
type IStateListener = (
|
|
35
36
|
changes: string[],
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
) => {};
|
|
37
|
+
store: Record<string, any>,
|
|
38
|
+
) => any;
|
|
39
39
|
|
|
40
40
|
type IStateListeners = Record<string, IStateListener[]>;
|
|
41
41
|
|
|
42
42
|
type IStateAction = (mutator: IStateMutator, args: any[]) => void;
|
|
43
43
|
|
|
44
|
-
type IStateActions = Record<string, IStateAction>;
|
|
44
|
+
type IStateActions = Record<string, IStateAction>;
|
|
45
|
+
|
|
46
|
+
type IStateUpdatePieces = Record<string, {
|
|
47
|
+
storageTypes: Record<string, boolean>;
|
|
48
|
+
readyStorageTypes: Record<string, boolean>;
|
|
49
|
+
changes: IStateChanges;
|
|
50
|
+
}>;
|
|
51
|
+
|
|
52
|
+
type IStatePendingChanges = Record<string, Record<string, any>>;
|
|
@@ -5,6 +5,7 @@ export default {
|
|
|
5
5
|
IS_AVAILABE: isStorageAvailable,
|
|
6
6
|
UPDATE_STATE: updateStateFromStorage,
|
|
7
7
|
SET_VALUES: setStorageValue,
|
|
8
|
+
REMOVE_KEY: removeStorageKey,
|
|
8
9
|
};
|
|
9
10
|
|
|
10
11
|
function getStorageType(isPersistent: boolean) {
|
|
@@ -27,6 +28,12 @@ async function setStorageValue(type: string, changes: Record<string, any>) {
|
|
|
27
28
|
return true;
|
|
28
29
|
}
|
|
29
30
|
|
|
31
|
+
async function removeStorageKey(type: string, key: string) {
|
|
32
|
+
if (type) {
|
|
33
|
+
await browser.storage[type].remove(key);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
30
37
|
export function isSessionStorageSupport() {
|
|
31
38
|
return !!browser.storage.session;
|
|
32
39
|
}
|
package/src/webextension.ts
CHANGED
|
@@ -6,12 +6,12 @@ import webextensionStorageUtils, {
|
|
|
6
6
|
isSessionStorageSupport,
|
|
7
7
|
} from "./webextension-storage";
|
|
8
8
|
|
|
9
|
-
const {
|
|
9
|
+
const { joinStateChanges, setStorageUtils } = main;
|
|
10
10
|
|
|
11
11
|
if (isBackgroundScript()) {
|
|
12
|
-
browser.storage.local.onChanged.addListener(
|
|
12
|
+
browser.storage.local.onChanged.addListener(joinStateChanges);
|
|
13
13
|
isSessionStorageSupport() &&
|
|
14
|
-
browser.storage.session.onChanged.addListener(
|
|
14
|
+
browser.storage.session.onChanged.addListener(joinStateChanges);
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
setStorageUtils(webextensionStorageUtils);
|
package/src/validation.ts
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { STATE } from ".";
|
|
2
|
-
import { NAMESPACE_DELIMITER } from "./constants";
|
|
3
|
-
|
|
4
|
-
function splitFullKey(fullKey: string) {
|
|
5
|
-
const segments = fullKey.split(NAMESPACE_DELIMITER);
|
|
6
|
-
return [segments.slice(-1)[0], segments.join(NAMESPACE_DELIMITER)];
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export const isValid = {
|
|
10
|
-
Setting: (fullKey: string) => {
|
|
11
|
-
if (!STATE[fullKey]) {
|
|
12
|
-
const [key, namespace] = splitFullKey(fullKey);
|
|
13
|
-
throw new Error(
|
|
14
|
-
`Setting "${key}" key in "${namespace}" namespace. DOES NOT EXIST`,
|
|
15
|
-
);
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
return true;
|
|
19
|
-
},
|
|
20
|
-
|
|
21
|
-
Defining: (fullKey: string) => {
|
|
22
|
-
if (STATE[fullKey]) {
|
|
23
|
-
const [key, namespace] = splitFullKey(fullKey);
|
|
24
|
-
throw new Error(
|
|
25
|
-
`Redefining "${key}" key in "${namespace}" namespace. ALREADY DEFINED`,
|
|
26
|
-
);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
return true;
|
|
30
|
-
},
|
|
31
|
-
};
|