atomaric 0.0.6 → 0.0.8
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/build/atomaric.js +55 -38
- package/build/atomaric.umd.cjs +1 -1
- package/package.json +6 -3
- package/types/model.d.ts +7 -6
package/build/atomaric.js
CHANGED
|
@@ -1,44 +1,61 @@
|
|
|
1
|
-
const
|
|
2
|
-
window.addEventListener("storage", (
|
|
3
|
-
|
|
1
|
+
const a = {};
|
|
2
|
+
window.addEventListener("storage", (t) => {
|
|
3
|
+
t.key === null || a[t.key] === void 0 || a[t.key](t);
|
|
4
4
|
});
|
|
5
|
-
class
|
|
6
|
-
constructor(s,
|
|
5
|
+
class m {
|
|
6
|
+
constructor(s, i) {
|
|
7
7
|
if (this._defaultValue = s, this.subscribers = /* @__PURE__ */ new Set(), this.save = () => {
|
|
8
|
-
}, this.invokeSubscriber = (
|
|
9
|
-
this.subscribers.delete(
|
|
10
|
-
}), this.set = (
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
}, this.setDeferred = (
|
|
14
|
-
|
|
15
|
-
this.set(
|
|
16
|
-
},
|
|
8
|
+
}, this.invokeSubscriber = (e) => e(this.value), this.get = () => this.value, this.toggle = () => this.set(!this.value), this.inkrement = (e) => this.set(this.value + e), this.subscribe = (e) => (this.subscribers.add(e), () => {
|
|
9
|
+
this.subscribers.delete(e);
|
|
10
|
+
}), this.set = (e, l) => {
|
|
11
|
+
const r = typeof e == "function" ? e(this.value) : e;
|
|
12
|
+
r === this.value || r === void 0 || typeof r == "number" && isNaN(r) || (this.value = r, this.subscribers.forEach(this.invokeSubscriber, this), l !== !0 && this.save(r));
|
|
13
|
+
}, this.setDeferred = (e, l = 500, r, S = !0) => {
|
|
14
|
+
S && this.debounceTimeout === void 0 && this.set(e, r), clearTimeout(this.debounceTimeout), this.debounceTimeout = setTimeout(() => {
|
|
15
|
+
this.set(e, r), delete this.debounceTimeout;
|
|
16
|
+
}, l);
|
|
17
17
|
}, this.value = s, typeof s != "boolean" && (this.toggle = () => {
|
|
18
18
|
}), typeof s != "number" && (this.inkrement = () => {
|
|
19
19
|
}), this.reset = () => {
|
|
20
20
|
this.set(s, !0), this.subscribers.forEach(this.invokeSubscriber, this);
|
|
21
|
-
},
|
|
22
|
-
let
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
21
|
+
}, i == null) return;
|
|
22
|
+
let o = null, h = !0, f = !0, u = s instanceof Set ? (e) => new Set(JSON.parse(e)) : (e) => JSON.parse(e), c = s instanceof Set ? (e) => {
|
|
23
|
+
if (e instanceof Set) return JSON.stringify(Array.from(e));
|
|
24
|
+
throw console.error(e), "The value is not Set instance";
|
|
25
|
+
} : (e) => JSON.stringify(e);
|
|
26
|
+
if (typeof i == "string" ? o = i : i.storeKey !== void 0 && (h = i.warnOnDuplicateStoreKey ?? !0, f = i.listenStorageChanges ?? !0, o = i.storeKey, u = i.parseValue ?? u, c = i.stringifyValue ?? c), o === null) return;
|
|
27
|
+
const n = `atom/${o}`;
|
|
28
|
+
let g = !0;
|
|
29
|
+
this.get = () => {
|
|
30
|
+
if (this.get = () => this.value, g) {
|
|
31
|
+
g = !1;
|
|
32
|
+
try {
|
|
33
|
+
this.value = n in localStorage ? u(localStorage[n]) : s;
|
|
34
|
+
} catch {
|
|
35
|
+
console.warn("Invalid json value", localStorage[n]);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return this.value;
|
|
39
|
+
}, this.save = (e) => {
|
|
40
|
+
if (e === s) {
|
|
27
41
|
this.reset();
|
|
28
42
|
return;
|
|
29
43
|
}
|
|
30
|
-
localStorage[
|
|
44
|
+
localStorage[n] = c(e);
|
|
31
45
|
}, this.reset = () => {
|
|
32
|
-
delete localStorage[
|
|
33
|
-
},
|
|
34
|
-
|
|
46
|
+
delete localStorage[n], this.set(s, !0);
|
|
47
|
+
}, h && a[n] !== void 0 && console.warn(
|
|
48
|
+
"Duplicate Atom key",
|
|
49
|
+
typeof i == "string" ? i : i.storeKey
|
|
50
|
+
), f && (a[n] = (e) => {
|
|
51
|
+
if (e.newValue === null) {
|
|
35
52
|
this.reset();
|
|
36
53
|
return;
|
|
37
54
|
}
|
|
38
55
|
try {
|
|
39
|
-
this.set(
|
|
56
|
+
this.set(u(e.newValue));
|
|
40
57
|
} catch {
|
|
41
|
-
console.warn("Invalid json value",
|
|
58
|
+
console.warn("Invalid json value", e.newValue);
|
|
42
59
|
}
|
|
43
60
|
});
|
|
44
61
|
}
|
|
@@ -46,19 +63,19 @@ class f {
|
|
|
46
63
|
return this._defaultValue;
|
|
47
64
|
}
|
|
48
65
|
}
|
|
49
|
-
let
|
|
66
|
+
let b = () => {
|
|
50
67
|
throw "call configureAtomaric() before all!";
|
|
51
68
|
};
|
|
52
|
-
const
|
|
69
|
+
const w = (t) => b = t.useSyncExternalStore, d = (t) => b(t.subscribe, t.get), v = (t) => t.set, y = (t) => t.setDeferred, k = (t) => t.get, A = (t) => t.toggle, T = (t) => t.inkrement, D = (t) => [d(t), v(t)], N = (t, s) => new m(t, s);
|
|
53
70
|
export {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
71
|
+
m as Atom,
|
|
72
|
+
N as atom,
|
|
73
|
+
w as configureAtomaric,
|
|
74
|
+
D as useAtom,
|
|
75
|
+
k as useAtomGet,
|
|
76
|
+
T as useAtomInkrement,
|
|
77
|
+
v as useAtomSet,
|
|
78
|
+
y as useAtomSetDeferred,
|
|
79
|
+
A as useAtomToggle,
|
|
80
|
+
d as useAtomValue
|
|
64
81
|
};
|
package/build/atomaric.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(s,
|
|
1
|
+
(function(s,o){typeof exports=="object"&&typeof module<"u"?o(exports):typeof define=="function"&&define.amd?define(["exports"],o):(s=typeof globalThis<"u"?globalThis:s||self,o(s.atomaric={}))})(this,function(s){"use strict";const o={};window.addEventListener("storage",t=>{t.key===null||o[t.key]===void 0||o[t.key](t)});class f{constructor(i,n){if(this._defaultValue=i,this.subscribers=new Set,this.save=()=>{},this.invokeSubscriber=e=>e(this.value),this.get=()=>this.value,this.toggle=()=>this.set(!this.value),this.inkrement=e=>this.set(this.value+e),this.subscribe=e=>(this.subscribers.add(e),()=>{this.subscribers.delete(e)}),this.set=(e,a)=>{const r=typeof e=="function"?e(this.value):e;r===this.value||r===void 0||typeof r=="number"&&isNaN(r)||(this.value=r,this.subscribers.forEach(this.invokeSubscriber,this),a!==!0&&this.save(r))},this.setDeferred=(e,a=500,r,E=!0)=>{E&&this.debounceTimeout===void 0&&this.set(e,r),clearTimeout(this.debounceTimeout),this.debounceTimeout=setTimeout(()=>{this.set(e,r),delete this.debounceTimeout},a)},this.value=i,typeof i!="boolean"&&(this.toggle=()=>{}),typeof i!="number"&&(this.inkrement=()=>{}),this.reset=()=>{this.set(i,!0),this.subscribers.forEach(this.invokeSubscriber,this)},n==null)return;let l=null,b=!0,d=!0,c=i instanceof Set?e=>new Set(JSON.parse(e)):e=>JSON.parse(e),h=i instanceof Set?e=>{if(e instanceof Set)return JSON.stringify(Array.from(e));throw console.error(e),"The value is not Set instance"}:e=>JSON.stringify(e);if(typeof n=="string"?l=n:n.storeKey!==void 0&&(b=n.warnOnDuplicateStoreKey??!0,d=n.listenStorageChanges??!0,l=n.storeKey,c=n.parseValue??c,h=n.stringifyValue??h),l===null)return;const u=`atom/${l}`;let A=!0;this.get=()=>{if(this.get=()=>this.value,A){A=!1;try{this.value=u in localStorage?c(localStorage[u]):i}catch{console.warn("Invalid json value",localStorage[u])}}return this.value},this.save=e=>{if(e===i){this.reset();return}localStorage[u]=h(e)},this.reset=()=>{delete localStorage[u],this.set(i,!0)},b&&o[u]!==void 0&&console.warn("Duplicate Atom key",typeof n=="string"?n:n.storeKey),d&&(o[u]=e=>{if(e.newValue===null){this.reset();return}try{this.set(c(e.newValue))}catch{console.warn("Invalid json value",e.newValue)}})}get defaultValue(){return this._defaultValue}}let m=()=>{throw"call configureAtomaric() before all!"};const y=t=>m=t.useSyncExternalStore,g=t=>m(t.subscribe,t.get),S=t=>t.set,v=t=>t.setDeferred,w=t=>t.get,k=t=>t.toggle,T=t=>t.inkrement,D=t=>[g(t),S(t)],N=(t,i)=>new f(t,i);s.Atom=f,s.atom=N,s.configureAtomaric=y,s.useAtom=D,s.useAtomGet=w,s.useAtomInkrement=T,s.useAtomSet=S,s.useAtomSetDeferred=v,s.useAtomToggle=k,s.useAtomValue=g,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "atomaric",
|
|
3
3
|
"description": "Manage your project state",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.8",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./build/atomaric.umd.cjs",
|
|
7
7
|
"module": "./build/atomaric.js",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"manager"
|
|
24
24
|
],
|
|
25
25
|
"scripts": {
|
|
26
|
-
"
|
|
26
|
+
"start": "vite --port 8378 --host",
|
|
27
27
|
"build": "tsc && vite build",
|
|
28
28
|
"preview": "vite preview"
|
|
29
29
|
},
|
|
@@ -31,8 +31,11 @@
|
|
|
31
31
|
"@types/md5": "^2.3.5",
|
|
32
32
|
"@types/node": "^22.15.1",
|
|
33
33
|
"@types/react": "^19.1.2",
|
|
34
|
-
"
|
|
34
|
+
"@types/react-dom": "^19.1.8",
|
|
35
|
+
"@vitejs/plugin-react": "^4.7.0",
|
|
35
36
|
"react": "^19.1.0",
|
|
37
|
+
"react-dom": "^19.1.1",
|
|
38
|
+
"vite": "^6.3.1",
|
|
36
39
|
"vite-plugin-eslint": "^1.8.1"
|
|
37
40
|
},
|
|
38
41
|
"dependencies": {
|
package/types/model.d.ts
CHANGED
|
@@ -2,12 +2,14 @@ type Sunscriber<Value> = (value: Value) => void;
|
|
|
2
2
|
|
|
3
3
|
export type AtomStoreKey = `${string}${string}:${string}${string}`;
|
|
4
4
|
|
|
5
|
-
export type AtomOptions = {
|
|
5
|
+
export type AtomOptions<Value> = {
|
|
6
6
|
storeKey?: AtomStoreKey;
|
|
7
7
|
/** **default: true** */
|
|
8
8
|
warnOnDuplicateStoreKey?: boolean;
|
|
9
9
|
/** **default: true** */
|
|
10
10
|
listenStorageChanges?: boolean;
|
|
11
|
+
parseValue?: (stringifiedValue: string) => Value;
|
|
12
|
+
stringifyValue?: (value: Value) => string;
|
|
11
13
|
};
|
|
12
14
|
|
|
13
15
|
export type AtomSetMethod<Value> = (value: Value | ((prev: Value) => Value), isPreventSave?: boolean) => void;
|
|
@@ -21,7 +23,7 @@ export type AtomSetDeferredMethod<Value> = (
|
|
|
21
23
|
export type AtomSubscribeMethod<Value> = (subscriber: Sunscriber<Value>) => () => void;
|
|
22
24
|
|
|
23
25
|
export class Atom<Value> {
|
|
24
|
-
constructor(defaultValue: Value, storeKeyOrOptions: AtomStoreKey | undefined | AtomOptions);
|
|
26
|
+
constructor(defaultValue: Value, storeKeyOrOptions: AtomStoreKey | undefined | AtomOptions<Value>);
|
|
25
27
|
|
|
26
28
|
readonly defaultValue: Value;
|
|
27
29
|
readonly get: () => Value;
|
|
@@ -42,9 +44,8 @@ export function useAtomInkrement(atom: Atom<number>): (typeof atom)['inkrement']
|
|
|
42
44
|
|
|
43
45
|
export function useAtom<Value>(atom: Atom<Value>): [Value, (typeof atom)['set']];
|
|
44
46
|
|
|
45
|
-
export
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
): Atom<Value>;
|
|
47
|
+
export type StoreKeyOrOptions<Value> = `${string}${string}:${string}${string}` | AtomOptions<Value>;
|
|
48
|
+
|
|
49
|
+
export function atom<Value>(value: Value, storeKeyOrOptions?: StoreKeyOrOptions<Value>): Atom<Value>;
|
|
49
50
|
|
|
50
51
|
export function configureAtomaric(hooks: { useSyncExternalStore: typeof useSyncExternalStore }): void;
|