@neovici/cosmoz-utils 6.21.0 → 6.22.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/dist/hooks/use-persist.d.ts +17 -0
- package/dist/hooks/use-persist.js +62 -0
- package/dist/promise.d.ts +1 -0
- package/dist/promise.js +2 -0
- package/package.json +7 -17
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
type UsePersistResult = {
|
|
2
|
+
value: string;
|
|
3
|
+
setValue: (value: string) => void;
|
|
4
|
+
revalidate: () => string;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* Hook to persist a string property to localStorage with cross-tab sync.
|
|
8
|
+
* Uses useProperty internally to make the value reactive to parent prop changes.
|
|
9
|
+
* When key is undefined, acts like a regular useProperty.
|
|
10
|
+
*
|
|
11
|
+
* @param property - property name on the host element
|
|
12
|
+
* @param key - localStorage key (if undefined, persistence is disabled)
|
|
13
|
+
* @param defaultValue - fallback value when no stored value exists
|
|
14
|
+
* @returns { value, setValue, revalidate }
|
|
15
|
+
*/
|
|
16
|
+
export declare const usePersist: (property: string, key: string | undefined, defaultValue: string) => UsePersistResult;
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { useCallback, useEffect, useProperty } from '@pionjs/pion';
|
|
2
|
+
/**
|
|
3
|
+
* Hook to persist a string property to localStorage with cross-tab sync.
|
|
4
|
+
* Uses useProperty internally to make the value reactive to parent prop changes.
|
|
5
|
+
* When key is undefined, acts like a regular useProperty.
|
|
6
|
+
*
|
|
7
|
+
* @param property - property name on the host element
|
|
8
|
+
* @param key - localStorage key (if undefined, persistence is disabled)
|
|
9
|
+
* @param defaultValue - fallback value when no stored value exists
|
|
10
|
+
* @returns { value, setValue, revalidate }
|
|
11
|
+
*/
|
|
12
|
+
export const usePersist = (property, key, defaultValue) => {
|
|
13
|
+
const [value, setValue] = useProperty(property, () => {
|
|
14
|
+
if (!key)
|
|
15
|
+
return defaultValue;
|
|
16
|
+
try {
|
|
17
|
+
return localStorage.getItem(key) ?? defaultValue;
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
return defaultValue;
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
// Cross-tab sync via storage events (different browser windows)
|
|
24
|
+
useEffect(() => {
|
|
25
|
+
if (!key)
|
|
26
|
+
return;
|
|
27
|
+
const handleStorage = (e) => {
|
|
28
|
+
if (e.key === key && e.newValue) {
|
|
29
|
+
setValue(e.newValue);
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
window.addEventListener('storage', handleStorage);
|
|
33
|
+
return () => window.removeEventListener('storage', handleStorage);
|
|
34
|
+
}, [key]);
|
|
35
|
+
const setPersistedValue = useCallback((newValue) => {
|
|
36
|
+
setValue(newValue);
|
|
37
|
+
if (key) {
|
|
38
|
+
try {
|
|
39
|
+
localStorage.setItem(key, newValue);
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
// Silently fail
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}, [key]);
|
|
46
|
+
const revalidate = useCallback(() => {
|
|
47
|
+
if (!key)
|
|
48
|
+
return value;
|
|
49
|
+
try {
|
|
50
|
+
const stored = localStorage.getItem(key);
|
|
51
|
+
if (stored != null && stored !== value) {
|
|
52
|
+
setValue(stored);
|
|
53
|
+
return stored;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
// Ignore
|
|
58
|
+
}
|
|
59
|
+
return value;
|
|
60
|
+
}, [key, value]);
|
|
61
|
+
return { value, setValue: setPersistedValue, revalidate };
|
|
62
|
+
};
|
package/dist/promise.d.ts
CHANGED
|
@@ -10,4 +10,5 @@ export declare const limit$: <T extends unknown[], P>(fn: (...args: T) => Promis
|
|
|
10
10
|
export declare const debounce$: <T extends unknown[], P>(fn: (...args: T) => P | PromiseLike<P>, ms?: number) => (...args: T) => Promise<P>;
|
|
11
11
|
export declare const log$: <T extends unknown[], P>(fn: (...args: T) => PromiseLike<P>) => (...args: T) => PromiseLike<P>;
|
|
12
12
|
export declare const retry$: <T extends unknown[], P>(fn: (...args: T) => P | PromiseLike<P>, n: number) => (...args: T) => Promise<P>;
|
|
13
|
+
export declare const invoke$: <T, A extends unknown[]>(fn: T | ((...args: A) => T | PromiseLike<T>), ...args: A) => Promise<Awaited<T> | Awaited<T>>;
|
|
13
14
|
export {};
|
package/dist/promise.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { invoke } from './function';
|
|
1
2
|
export class ManagedPromise extends Promise {
|
|
2
3
|
constructor(callback) {
|
|
3
4
|
const handles = {};
|
|
@@ -84,3 +85,4 @@ export const retry$ = (fn, n) => async (...args) => {
|
|
|
84
85
|
}
|
|
85
86
|
throw error;
|
|
86
87
|
};
|
|
88
|
+
export const invoke$ = (fn, ...args) => Promise.resolve(invoke(fn, ...args));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@neovici/cosmoz-utils",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.22.1",
|
|
4
4
|
"description": "Date, money and template management functions commonly needed in Cosmoz views.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"polymer",
|
|
@@ -30,18 +30,8 @@
|
|
|
30
30
|
"start": "wds",
|
|
31
31
|
"test": "wtr --coverage",
|
|
32
32
|
"test:watch": "wtr --watch",
|
|
33
|
-
"prepare": "husky install"
|
|
34
|
-
|
|
35
|
-
"release": {
|
|
36
|
-
"plugins": [
|
|
37
|
-
"@semantic-release/commit-analyzer",
|
|
38
|
-
"@semantic-release/release-notes-generator",
|
|
39
|
-
"@semantic-release/changelog",
|
|
40
|
-
"@semantic-release/github",
|
|
41
|
-
"@semantic-release/npm",
|
|
42
|
-
"@semantic-release/git"
|
|
43
|
-
],
|
|
44
|
-
"branch": "master"
|
|
33
|
+
"prepare": "husky install",
|
|
34
|
+
"changeset": "changeset"
|
|
45
35
|
},
|
|
46
36
|
"publishConfig": {
|
|
47
37
|
"access": "public"
|
|
@@ -73,18 +63,18 @@
|
|
|
73
63
|
"@pionjs/pion": "^2.7.1"
|
|
74
64
|
},
|
|
75
65
|
"devDependencies": {
|
|
66
|
+
"@changesets/cli": "^2.27.0",
|
|
76
67
|
"@commitlint/cli": "^19.0.0",
|
|
77
68
|
"@commitlint/config-conventional": "^19.0.0",
|
|
78
|
-
"@neovici/cfg": "^2.
|
|
69
|
+
"@neovici/cfg": "^2.13.0",
|
|
79
70
|
"@open-wc/testing": "^4.0.0",
|
|
71
|
+
"@playwright/test": "1.60.0",
|
|
80
72
|
"@polymer/polymer": "^3.3.1",
|
|
81
|
-
"@semantic-release/changelog": "^6.0.0",
|
|
82
|
-
"@semantic-release/git": "^10.0.0",
|
|
83
73
|
"@types/mocha": "^10.0.3",
|
|
74
|
+
"@web/test-runner-playwright": "^0.11.1",
|
|
84
75
|
"husky": "^8.0.0",
|
|
85
76
|
"lint-staged": "^16.2.7",
|
|
86
77
|
"lit-html": "^2.0.0 || ^3.0.0",
|
|
87
|
-
"semantic-release": "^25.0.2",
|
|
88
78
|
"sinon": "^19.0.0"
|
|
89
79
|
}
|
|
90
80
|
}
|