atomaric 0.0.2 → 0.0.4
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 +48 -35
- package/build/atomaric.umd.cjs +1 -1
- package/package.json +1 -1
- package/types/model.d.ts +16 -3
package/build/atomaric.js
CHANGED
|
@@ -1,46 +1,59 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
const u = {};
|
|
2
|
+
window.addEventListener("storage", (e) => {
|
|
3
|
+
e.key === null || u[e.key] === void 0 || u[e.key](e);
|
|
4
|
+
});
|
|
5
|
+
class g {
|
|
6
|
+
constructor(s, i) {
|
|
3
7
|
if (this._defaultValue = s, this.subscribers = /* @__PURE__ */ new Set(), this.save = () => {
|
|
4
|
-
}, this.invokeSubscriber = (
|
|
5
|
-
this.subscribers.delete(
|
|
6
|
-
}), this.set = (
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
},
|
|
10
|
-
const e = `atom/${o}`;
|
|
11
|
-
this.value = e in localStorage ? JSON.parse(localStorage[e]) : s, this.save = (r) => {
|
|
12
|
-
if (r === s) {
|
|
13
|
-
this.reset();
|
|
14
|
-
return;
|
|
15
|
-
}
|
|
16
|
-
localStorage[e] = JSON.stringify(r);
|
|
17
|
-
}, this.reset = () => {
|
|
18
|
-
delete localStorage[e], this.set(s, !0);
|
|
19
|
-
};
|
|
20
|
-
} else
|
|
21
|
-
this.value = s, this.reset = () => {
|
|
22
|
-
this.set(s, !0), this.subscribers.forEach(this.invokeSubscriber, this);
|
|
23
|
-
};
|
|
24
|
-
typeof s != "boolean" && (this.toggle = () => {
|
|
8
|
+
}, this.invokeSubscriber = (t) => t(this.value), this.get = () => this.value, this.toggle = () => this.set(!this.value), this.inkrement = (t) => this.set(this.value + t), this.subscribe = (t) => (this.subscribers.add(t), () => {
|
|
9
|
+
this.subscribers.delete(t);
|
|
10
|
+
}), this.set = (t, l) => {
|
|
11
|
+
const o = typeof t == "function" ? t(this.value) : t;
|
|
12
|
+
o === this.value || o === void 0 || typeof o == "number" && isNaN(o) || (this.value = o, this.subscribers.forEach(this.invokeSubscriber, this), l !== !0 && this.save(o));
|
|
13
|
+
}, this.value = s, typeof s != "boolean" && (this.toggle = () => {
|
|
25
14
|
}), typeof s != "number" && (this.inkrement = () => {
|
|
15
|
+
}), this.reset = () => {
|
|
16
|
+
this.set(s, !0), this.subscribers.forEach(this.invokeSubscriber, this);
|
|
17
|
+
}, i == null) return;
|
|
18
|
+
let n = null, a = !0, h = !0;
|
|
19
|
+
if (typeof i == "string" ? n = i : i.storeKey !== void 0 && (a = i.warnOnDuplicateStoreKey ?? !0, h = i.listenStorageChanges ?? !0, n = i.storeKey), n === null) return;
|
|
20
|
+
const r = `atom/${n}`;
|
|
21
|
+
this.value = r in localStorage ? JSON.parse(localStorage[r]) : s, this.save = (t) => {
|
|
22
|
+
if (t === s) {
|
|
23
|
+
this.reset();
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
localStorage[r] = JSON.stringify(t);
|
|
27
|
+
}, this.reset = () => {
|
|
28
|
+
delete localStorage[r], this.set(s, !0);
|
|
29
|
+
}, a && u[r] !== void 0 && console.warn("Duplicate Atom key", i), h && (u[r] = (t) => {
|
|
30
|
+
if (t.newValue === null) {
|
|
31
|
+
this.reset();
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
try {
|
|
35
|
+
this.set(JSON.parse(t.newValue));
|
|
36
|
+
} catch {
|
|
37
|
+
console.warn("Invalid json value", t.newValue);
|
|
38
|
+
}
|
|
26
39
|
});
|
|
27
40
|
}
|
|
28
41
|
get defaultValue() {
|
|
29
42
|
return this._defaultValue;
|
|
30
43
|
}
|
|
31
44
|
}
|
|
32
|
-
let
|
|
33
|
-
throw "you meed pass react useSyncExternalStore hook func in
|
|
45
|
+
let c = () => {
|
|
46
|
+
throw "you meed pass react useSyncExternalStore hook func in registerReactHooks() before all actions";
|
|
34
47
|
};
|
|
35
|
-
const
|
|
48
|
+
const f = (e) => c = e.useSyncExternalStore, b = (e) => c(e.subscribe, e.get), S = (e) => e.set, k = (e) => e.get, m = (e) => e.toggle, v = (e) => e.inkrement, y = (e) => [b(e), S(e)], w = (e, s) => new g(e, s);
|
|
36
49
|
export {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
50
|
+
g as Atom,
|
|
51
|
+
w as atom,
|
|
52
|
+
f as registerReactHooks,
|
|
53
|
+
y as useAtom,
|
|
54
|
+
k as useAtomGet,
|
|
55
|
+
v as useAtomInkrement,
|
|
56
|
+
S as useAtomSet,
|
|
57
|
+
m as useAtomToggle,
|
|
58
|
+
b as useAtomValue
|
|
46
59
|
};
|
package/build/atomaric.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(s,
|
|
1
|
+
(function(s,n){typeof exports=="object"&&typeof module<"u"?n(exports):typeof define=="function"&&define.amd?define(["exports"],n):(s=typeof globalThis<"u"?globalThis:s||self,n(s.atomaric={}))})(this,function(s){"use strict";const n={};window.addEventListener("storage",e=>{e.key===null||n[e.key]===void 0||n[e.key](e)});class a{constructor(i,o){if(this._defaultValue=i,this.subscribers=new Set,this.save=()=>{},this.invokeSubscriber=t=>t(this.value),this.get=()=>this.value,this.toggle=()=>this.set(!this.value),this.inkrement=t=>this.set(this.value+t),this.subscribe=t=>(this.subscribers.add(t),()=>{this.subscribers.delete(t)}),this.set=(t,b)=>{const u=typeof t=="function"?t(this.value):t;u===this.value||u===void 0||typeof u=="number"&&isNaN(u)||(this.value=u,this.subscribers.forEach(this.invokeSubscriber,this),b!==!0&&this.save(u))},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)},o==null)return;let l=null,g=!0,m=!0;if(typeof o=="string"?l=o:o.storeKey!==void 0&&(g=o.warnOnDuplicateStoreKey??!0,m=o.listenStorageChanges??!0,l=o.storeKey),l===null)return;const r=`atom/${l}`;this.value=r in localStorage?JSON.parse(localStorage[r]):i,this.save=t=>{if(t===i){this.reset();return}localStorage[r]=JSON.stringify(t)},this.reset=()=>{delete localStorage[r],this.set(i,!0)},g&&n[r]!==void 0&&console.warn("Duplicate Atom key",o),m&&(n[r]=t=>{if(t.newValue===null){this.reset();return}try{this.set(JSON.parse(t.newValue))}catch{console.warn("Invalid json value",t.newValue)}})}get defaultValue(){return this._defaultValue}}let c=()=>{throw"you meed pass react useSyncExternalStore hook func in registerReactHooks() before all actions"};const S=e=>c=e.useSyncExternalStore,h=e=>c(e.subscribe,e.get),f=e=>e.set,d=e=>e.get,y=e=>e.toggle,k=e=>e.inkrement,v=e=>[h(e),f(e)],A=(e,i)=>new a(e,i);s.Atom=a,s.atom=A,s.registerReactHooks=S,s.useAtom=v,s.useAtomGet=d,s.useAtomInkrement=k,s.useAtomSet=f,s.useAtomToggle=y,s.useAtomValue=h,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED
package/types/model.d.ts
CHANGED
|
@@ -1,7 +1,17 @@
|
|
|
1
1
|
type Sunscriber<Value> = (value: Value) => void;
|
|
2
2
|
|
|
3
|
+
export type AtomStoreKey = `${string}${string}:${string}${string}`;
|
|
4
|
+
|
|
5
|
+
export type AtomOptions = {
|
|
6
|
+
storeKey?: AtomStoreKey;
|
|
7
|
+
/** **default: true** */
|
|
8
|
+
warnOnDuplicateStoreKey?: boolean;
|
|
9
|
+
/** **default: true** */
|
|
10
|
+
listenStorageChanges?: boolean;
|
|
11
|
+
};
|
|
12
|
+
|
|
3
13
|
export class Atom<Value> {
|
|
4
|
-
constructor(defaultValue: Value,
|
|
14
|
+
constructor(defaultValue: Value, storeKeyOrOptions: AtomStoreKey | undefined | AtomOptions);
|
|
5
15
|
|
|
6
16
|
readonly defaultValue: Value;
|
|
7
17
|
readonly get: () => Value;
|
|
@@ -20,6 +30,9 @@ export function useAtomInkrement(atom: Atom<number>): (typeof atom)['inkrement']
|
|
|
20
30
|
|
|
21
31
|
export function useAtom<Value>(atom: Atom<Value>): [Value, (typeof atom)['set']];
|
|
22
32
|
|
|
23
|
-
export function atom<Value>(
|
|
33
|
+
export function atom<Value>(
|
|
34
|
+
value: Value,
|
|
35
|
+
storeKeyOrOptions?: `${string}${string}:${string}${string}` | AtomOptions,
|
|
36
|
+
): Atom<Value>;
|
|
24
37
|
|
|
25
|
-
export function
|
|
38
|
+
export function registerReactHooks(hooks: { useSyncExternalStore: typeof useSyncExternalStore }): void;
|