atomaric 0.0.0
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 +19 -0
- package/build/atomaric.js +43 -0
- package/build/atomaric.umd.cjs +1 -0
- package/package.json +41 -0
- package/types/model.d.ts +24 -0
package/README.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
## 🕹️Installation and usage
|
|
2
|
+
|
|
3
|
+
```sh
|
|
4
|
+
npm install atomaric
|
|
5
|
+
```
|
|
6
|
+
|
|
7
|
+
### usage
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { microb, useMicrobValue } from 'atomaric';
|
|
11
|
+
|
|
12
|
+
const nameMicrob = microb('World');
|
|
13
|
+
|
|
14
|
+
function App() {
|
|
15
|
+
const [name, setIsOpen] = useMicrobValue(nameMicrob);
|
|
16
|
+
|
|
17
|
+
return <div onClick={() => setIsOpen(isOpen => (name === 'World' ? 'Man' : 'World'))}>Hello, {name}</div>;
|
|
18
|
+
}
|
|
19
|
+
```
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
class n {
|
|
2
|
+
constructor(t, o) {
|
|
3
|
+
if (this.subscribers = /* @__PURE__ */ new Set(), this.save = () => {
|
|
4
|
+
}, this.invokeSubscriber = (s) => s(this.value), this.get = () => this.value, this.toggle = () => this.set(!this.value), this.inkrement = (s) => this.set(this.value + s), this.subscribe = (s) => (this.subscribers.add(s), () => {
|
|
5
|
+
this.subscribers.delete(s);
|
|
6
|
+
}), this.set = (s, r) => {
|
|
7
|
+
const i = typeof s == "function" ? s(this.value) : s;
|
|
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 s = `microb/${o}`;
|
|
11
|
+
this.value = s in localStorage ? JSON.parse(localStorage[s]) : t, this.save = (r) => {
|
|
12
|
+
if (r === t) {
|
|
13
|
+
this.reset();
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
localStorage[s] = JSON.stringify(r);
|
|
17
|
+
}, this.reset = () => {
|
|
18
|
+
delete localStorage[s], this.set(t, !0);
|
|
19
|
+
};
|
|
20
|
+
} else
|
|
21
|
+
this.value = t, this.reset = () => {
|
|
22
|
+
this.set(t, !0), this.subscribers.forEach(this.invokeSubscriber, this);
|
|
23
|
+
};
|
|
24
|
+
typeof t != "boolean" && (this.toggle = () => {
|
|
25
|
+
}), typeof t != "number" && (this.inkrement = () => {
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
let c = () => {
|
|
30
|
+
throw "you meed pass react useSyncExternalStore hook func in registerReactUseSyncExternalStoreHookFunc() before all ";
|
|
31
|
+
};
|
|
32
|
+
const u = (e) => c = e, h = (e) => c(e.subscribe, e.get), b = (e) => e.set, a = (e) => e.get, l = (e) => e.toggle, S = (e) => e.inkrement, g = (e) => [h(e), b(e)], v = (e, t) => new n(e, t);
|
|
33
|
+
export {
|
|
34
|
+
n as Microb,
|
|
35
|
+
v as microb,
|
|
36
|
+
u as registerReactUseSyncExternalStoreHookFunc,
|
|
37
|
+
g as useMicrob,
|
|
38
|
+
a as useMicrobGet,
|
|
39
|
+
S as useMicrobInkrement,
|
|
40
|
+
b as useMicrobSet,
|
|
41
|
+
l as useMicrobToggle,
|
|
42
|
+
h as useMicrobValue
|
|
43
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(function(s,r){typeof exports=="object"&&typeof module<"u"?r(exports):typeof define=="function"&&define.amd?define(["exports"],r):(s=typeof globalThis<"u"?globalThis:s||self,r(s.atomaric={}))})(this,function(s){"use strict";class r{constructor(i,b){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,n)=>{const o=typeof t=="function"?t(this.value):t;o===this.value||o===void 0||typeof o=="number"&&isNaN(o)||(this.value=o,this.subscribers.forEach(this.invokeSubscriber,this),n!==!0&&this.save(o))},b!==void 0){const t=`microb/${b}`;this.value=t in localStorage?JSON.parse(localStorage[t]):i,this.save=n=>{if(n===i){this.reset();return}localStorage[t]=JSON.stringify(n)},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 a=e=>c=e,u=e=>c(e.subscribe,e.get),h=e=>e.set,l=e=>e.get,S=e=>e.toggle,g=e=>e.inkrement,f=e=>[u(e),h(e)],v=(e,i)=>new r(e,i);s.Microb=r,s.microb=v,s.registerReactUseSyncExternalStoreHookFunc=a,s.useMicrob=f,s.useMicrobGet=l,s.useMicrobInkrement=g,s.useMicrobSet=h,s.useMicrobToggle=S,s.useMicrobValue=u,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "atomaric",
|
|
3
|
+
"description": "Manage your project state",
|
|
4
|
+
"version": "0.0.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./build/atomaric.umd.cjs",
|
|
7
|
+
"module": "./build/atomaric.js",
|
|
8
|
+
"types": "./types/model.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./types/model.d.ts",
|
|
12
|
+
"require": "./build/atomaric.umd.cjs",
|
|
13
|
+
"import": "./build/atomaric.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"build",
|
|
18
|
+
"types"
|
|
19
|
+
],
|
|
20
|
+
"keywords": [
|
|
21
|
+
"react",
|
|
22
|
+
"state",
|
|
23
|
+
"manager"
|
|
24
|
+
],
|
|
25
|
+
"scripts": {
|
|
26
|
+
"dev": "vite",
|
|
27
|
+
"build": "tsc && vite build",
|
|
28
|
+
"preview": "vite preview"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@types/md5": "^2.3.5",
|
|
32
|
+
"@types/node": "^22.15.1",
|
|
33
|
+
"@types/react": "^19.1.2",
|
|
34
|
+
"vite": "^6.3.1",
|
|
35
|
+
"react": "^19.1.0",
|
|
36
|
+
"vite-plugin-eslint": "^1.8.1"
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"typescript": "~5.7.2"
|
|
40
|
+
}
|
|
41
|
+
}
|
package/types/model.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
type Sunscriber<Value> = (value: Value) => void;
|
|
2
|
+
|
|
3
|
+
export class Microb<Value> {
|
|
4
|
+
constructor(defaultValue: Value, storeKey: `${string}${string}:${string}${string}` | undefined);
|
|
5
|
+
|
|
6
|
+
readonly get: () => Value;
|
|
7
|
+
readonly reset: () => void;
|
|
8
|
+
readonly toggle: () => void;
|
|
9
|
+
readonly inkrement: (delta: number) => void;
|
|
10
|
+
readonly subscribe: (sub: Sunscriber<Value>) => () => void;
|
|
11
|
+
readonly set: (value: Value | ((prev: Value) => Value), isPreventSave?: boolean) => void;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function useMicrobValue<Value>(microb: Microb<Value>): Value;
|
|
15
|
+
export function useMicrobSet<Value>(microb: Microb<Value>): (typeof microb)['set'];
|
|
16
|
+
export function useMicrobGet<Value>(microb: Microb<Value>): (typeof microb)['get'];
|
|
17
|
+
export function useMicrobToggle(microb: Microb<boolean>): (typeof microb)['toggle'];
|
|
18
|
+
export function useMicrobInkrement(microb: Microb<number>): (typeof microb)['inkrement'];
|
|
19
|
+
|
|
20
|
+
export function useMicrob<Value>(microb: Microb<Value>): [Value, (typeof microb)['set']];
|
|
21
|
+
|
|
22
|
+
export function microb<Value>(value: Value, storeKey?: `${string}${string}:${string}${string}`): Microb<Value>;
|
|
23
|
+
|
|
24
|
+
export function registerReactUseSyncExternalStoreHookFunc(hook: typeof useSyncExternalStore): void;
|