atomaric 0.0.1 → 0.0.3

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 CHANGED
@@ -1,43 +1,59 @@
1
- class c {
2
- constructor(s, o) {
3
- if (this.subscribers = /* @__PURE__ */ new Set(), this.save = () => {
4
- }, 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), () => {
5
- this.subscribers.delete(e);
6
- }), this.set = (e, r) => {
7
- const i = typeof e == "function" ? e(this.value) : e;
8
- i === this.value || i === void 0 || typeof i == "number" && isNaN(i) || (this.value = i, this.subscribers.forEach(this.invokeSubscriber, this), r !== !0 && this.save(i));
9
- }, o !== void 0) {
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 = () => {
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) {
7
+ if (this._defaultValue = s, this.subscribers = /* @__PURE__ */ new Set(), this.save = () => {
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
  }
41
+ get defaultValue() {
42
+ return this._defaultValue;
43
+ }
28
44
  }
29
- let n = () => {
30
- throw "you meed pass react useSyncExternalStore hook func in registerReactUseSyncExternalStoreHookFunc() before all ";
45
+ let c = () => {
46
+ throw "you meed pass react useSyncExternalStore hook func in registerReactHooks() before all actions";
31
47
  };
32
- const b = (t) => n = t, h = (t) => n(t.subscribe, t.get), u = (t) => t.set, a = (t) => t.get, l = (t) => t.toggle, S = (t) => t.inkrement, g = (t) => [h(t), u(t)], v = (t, s) => new c(t, s);
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);
33
49
  export {
34
- c as Atom,
35
- v as atom,
36
- b as registerReactUseSyncExternalStoreHookFunc,
37
- g as useAtom,
38
- a as useAtomGet,
39
- S as useAtomInkrement,
40
- u as useAtomSet,
41
- l as useAtomToggle,
42
- h as useAtomValue
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
43
59
  };
@@ -1 +1 @@
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";class o{constructor(i,a){if(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,r)=>{const n=typeof t=="function"?t(this.value):t;n===this.value||n===void 0||typeof n=="number"&&isNaN(n)||(this.value=n,this.subscribers.forEach(this.invokeSubscriber,this),r!==!0&&this.save(n))},a!==void 0){const t=`atom/${a}`;this.value=t in localStorage?JSON.parse(localStorage[t]):i,this.save=r=>{if(r===i){this.reset();return}localStorage[t]=JSON.stringify(r)},this.reset=()=>{delete localStorage[t],this.set(i,!0)}}else this.value=i,this.reset=()=>{this.set(i,!0),this.subscribers.forEach(this.invokeSubscriber,this)};typeof i!="boolean"&&(this.toggle=()=>{}),typeof i!="number"&&(this.inkrement=()=>{})}}let c=()=>{throw"you meed pass react useSyncExternalStore hook func in registerReactUseSyncExternalStoreHookFunc() before all "};const l=e=>c=e,u=e=>c(e.subscribe,e.get),h=e=>e.set,m=e=>e.get,b=e=>e.toggle,S=e=>e.inkrement,g=e=>[u(e),h(e)],f=(e,i)=>new o(e,i);s.Atom=o,s.atom=f,s.registerReactUseSyncExternalStoreHookFunc=l,s.useAtom=g,s.useAtomGet=m,s.useAtomInkrement=S,s.useAtomSet=h,s.useAtomToggle=b,s.useAtomValue=u,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});
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
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "atomaric",
3
3
  "description": "Manage your project state",
4
- "version": "0.0.1",
4
+ "version": "0.0.3",
5
5
  "type": "module",
6
6
  "main": "./build/atomaric.umd.cjs",
7
7
  "module": "./build/atomaric.js",
package/types/model.d.ts CHANGED
@@ -1,8 +1,19 @@
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, storeKey: `${string}${string}:${string}${string}` | undefined);
14
+ constructor(defaultValue: Value, storeKey: AtomStoreKey | undefined | AtomOptions);
5
15
 
16
+ readonly defaultValue: Value;
6
17
  readonly get: () => Value;
7
18
  readonly reset: () => void;
8
19
  readonly toggle: () => void;
@@ -21,4 +32,4 @@ export function useAtom<Value>(atom: Atom<Value>): [Value, (typeof atom)['set']]
21
32
 
22
33
  export function atom<Value>(value: Value, storeKey?: `${string}${string}:${string}${string}`): Atom<Value>;
23
34
 
24
- export function registerReactUseSyncExternalStoreHookFunc(hook: typeof useSyncExternalStore): void;
35
+ export function registerReactHooks(hooks: { useSyncExternalStore: typeof useSyncExternalStore }): void;