@blac/react 0.0.58 → 0.1.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/dist/blac-react.mjs +104 -0
- package/dist/blac-react.umd.js +1 -0
- package/package.json +19 -44
- package/README.md +0 -16
- package/externalBlocStore.d.ts +0 -9
- package/index.cjs.js +0 -2
- package/index.cjs.js.map +0 -1
- package/index.d.ts +0 -4
- package/index.es.js +0 -80
- package/index.es.js.map +0 -1
- package/useBloc.d.ts +0 -16
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { Blac as v } from "@blac/core";
|
|
2
|
+
import { useId as K, useRef as a, useState as D, useMemo as C, useSyncExternalStore as M, useLayoutEffect as O, useEffect as k } from "react";
|
|
3
|
+
const A = (n, s, c) => ({
|
|
4
|
+
subscribe: (d) => {
|
|
5
|
+
const f = n._observer.subscribe({
|
|
6
|
+
fn: () => {
|
|
7
|
+
try {
|
|
8
|
+
d(Object.freeze(n.state));
|
|
9
|
+
} catch (y) {
|
|
10
|
+
console.error({
|
|
11
|
+
e: y,
|
|
12
|
+
bloc: n,
|
|
13
|
+
dependencyArray: s
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
dependencyArray: s,
|
|
18
|
+
id: c
|
|
19
|
+
});
|
|
20
|
+
return () => {
|
|
21
|
+
f();
|
|
22
|
+
};
|
|
23
|
+
},
|
|
24
|
+
getSnapshot: () => Object.freeze(n.state),
|
|
25
|
+
getServerSnapshot: () => Object.freeze(n.state)
|
|
26
|
+
}), L = (n) => [[n]];
|
|
27
|
+
class T {
|
|
28
|
+
static useBloc(s, c) {
|
|
29
|
+
let { dependencySelector: d, id: f, props: y } = c ?? {};
|
|
30
|
+
const o = K(), S = a(/* @__PURE__ */ new Set()), p = a(/* @__PURE__ */ new Set()), h = a(!1), B = a(/* @__PURE__ */ new Set()), g = a(/* @__PURE__ */ new Set()), m = a(!1), _ = /* @__PURE__ */ new Set();
|
|
31
|
+
s.isolated && (f = o);
|
|
32
|
+
const [e, w] = D(
|
|
33
|
+
v.getInstance().getBloc(s, {
|
|
34
|
+
id: f,
|
|
35
|
+
props: y,
|
|
36
|
+
instanceRef: o
|
|
37
|
+
})
|
|
38
|
+
);
|
|
39
|
+
if (!e)
|
|
40
|
+
throw new Error(`useBloc: could not resolve: ${s.name || s}`);
|
|
41
|
+
o === e._instanceRef && (c != null && c.props) && (e.props = c.props);
|
|
42
|
+
const j = (t, r) => {
|
|
43
|
+
if (d)
|
|
44
|
+
return d(t, r);
|
|
45
|
+
if (e.defaultDependencySelector)
|
|
46
|
+
return e.defaultDependencySelector(t, r);
|
|
47
|
+
if (typeof t != "object")
|
|
48
|
+
return L(t);
|
|
49
|
+
const u = [];
|
|
50
|
+
for (const i of S.current)
|
|
51
|
+
i in t && u.push(t[i]);
|
|
52
|
+
const I = [];
|
|
53
|
+
for (const i of B.current)
|
|
54
|
+
if (i in e) {
|
|
55
|
+
const b = e[i];
|
|
56
|
+
if (typeof b == "function")
|
|
57
|
+
continue;
|
|
58
|
+
I.push(b);
|
|
59
|
+
}
|
|
60
|
+
return [u, I];
|
|
61
|
+
}, { subscribe: x, getSnapshot: E, getServerSnapshot: P } = C(
|
|
62
|
+
() => A(e, j, o),
|
|
63
|
+
[e._createdAt]
|
|
64
|
+
), l = M(
|
|
65
|
+
x,
|
|
66
|
+
E,
|
|
67
|
+
P
|
|
68
|
+
), R = C(() => {
|
|
69
|
+
try {
|
|
70
|
+
if (typeof l == "object")
|
|
71
|
+
return new Proxy(l, {
|
|
72
|
+
get(t, r) {
|
|
73
|
+
const u = l[r];
|
|
74
|
+
return S.current.add(r), p.current.add(r), h.current = !0, u;
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
} catch (t) {
|
|
78
|
+
v.instance.log("useBloc Error", t);
|
|
79
|
+
}
|
|
80
|
+
return l;
|
|
81
|
+
}, [l, S, p]), z = C(() => new Proxy(e, {
|
|
82
|
+
get(t, r) {
|
|
83
|
+
const u = e[r];
|
|
84
|
+
return typeof u != "function" && (B.current.add(r), g.current.add(r), m.current = !0), u;
|
|
85
|
+
}
|
|
86
|
+
}), [e, B, g]);
|
|
87
|
+
return O(() => (S.current = new Set(p.current), B.current = new Set(g.current), () => {
|
|
88
|
+
setTimeout(() => {
|
|
89
|
+
m.current && (g.current.clear(), m.current = !1), h.current && (p.current.clear(), h.current = !1);
|
|
90
|
+
});
|
|
91
|
+
}), [_]), k(() => (e._addConsumer(o), e._consumers.size !== 0 && w(
|
|
92
|
+
v.getInstance().getBloc(s, {
|
|
93
|
+
id: f,
|
|
94
|
+
props: y,
|
|
95
|
+
instanceRef: o
|
|
96
|
+
})
|
|
97
|
+
), () => e._removeConsumer(o)), []), [R, z];
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
const H = T.useBloc;
|
|
101
|
+
export {
|
|
102
|
+
A as externalBlocStore,
|
|
103
|
+
H as useBloc
|
|
104
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(function(r,o){typeof exports=="object"&&typeof module<"u"?o(exports,require("@blac/core"),require("react")):typeof define=="function"&&define.amd?define(["exports","@blac/core","react"],o):(r=typeof globalThis<"u"?globalThis:r||self,o(r.BlacReact={},r.BlacCore,r.react))})(this,function(r,o,t){"use strict";const C=(c,u,f)=>({subscribe:S=>{const l=c._observer.subscribe({fn:()=>{try{S(Object.freeze(c.state))}catch(B){console.error({e:B,bloc:c,dependencyArray:u})}},dependencyArray:u,id:f});return()=>{l()}},getSnapshot:()=>Object.freeze(c.state),getServerSnapshot:()=>Object.freeze(c.state)}),_=c=>[[c]];class j{static useBloc(u,f){let{dependencySelector:S,id:l,props:B}=f??{};const a=t.useId(),p=t.useRef(new Set),h=t.useRef(new Set),g=t.useRef(!1),m=t.useRef(new Set),b=t.useRef(new Set),v=t.useRef(!1),x=new Set;u.isolated&&(l=a);const[e,M]=t.useState(o.Blac.getInstance().getBloc(u,{id:l,props:B,instanceRef:a}));if(!e)throw new Error(`useBloc: could not resolve: ${u.name||u}`);a===e._instanceRef&&(f!=null&&f.props)&&(e.props=f.props);const P=(n,s)=>{if(S)return S(n,s);if(e.defaultDependencySelector)return e.defaultDependencySelector(n,s);if(typeof n!="object")return _(n);const i=[];for(const y of p.current)y in n&&i.push(n[y]);const R=[];for(const y of m.current)if(y in e){const I=e[y];if(typeof I=="function")continue;R.push(I)}return[i,R]},{subscribe:E,getSnapshot:z,getServerSnapshot:K}=t.useMemo(()=>C(e,P,a),[e._createdAt]),d=t.useSyncExternalStore(E,z,K),O=t.useMemo(()=>{try{if(typeof d=="object")return new Proxy(d,{get(n,s){const i=d[s];return p.current.add(s),h.current.add(s),g.current=!0,i}})}catch(n){o.Blac.instance.log("useBloc Error",n)}return d},[d,p,h]),T=t.useMemo(()=>new Proxy(e,{get(n,s){const i=e[s];return typeof i!="function"&&(m.current.add(s),b.current.add(s),v.current=!0),i}}),[e,m,b]);return t.useLayoutEffect(()=>(p.current=new Set(h.current),m.current=new Set(b.current),()=>{setTimeout(()=>{v.current&&(b.current.clear(),v.current=!1),g.current&&(h.current.clear(),g.current=!1)})}),[x]),t.useEffect(()=>(e._addConsumer(a),e._consumers.size!==0&&M(o.Blac.getInstance().getBloc(u,{id:l,props:B,instanceRef:a})),()=>e._removeConsumer(a)),[]),[O,T]}}const w=j.useBloc;r.externalBlocStore=C,r.useBloc=w,Object.defineProperty(r,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED
|
@@ -1,51 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@blac/react",
|
|
3
|
-
"version": "0.0
|
|
4
|
-
"
|
|
5
|
-
|
|
6
|
-
"module": "index.es.js",
|
|
7
|
-
"typings": "index.d.ts",
|
|
8
|
-
"types": "index.d.ts",
|
|
9
|
-
"keywords": [
|
|
10
|
-
"react",
|
|
11
|
-
"typescript",
|
|
12
|
-
"rxjs",
|
|
13
|
-
"state-management",
|
|
14
|
-
"reactjs",
|
|
15
|
-
"observer-pattern",
|
|
16
|
-
"bloc",
|
|
17
|
-
"bloc-pattern"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"files": [
|
|
5
|
+
"dist"
|
|
18
6
|
],
|
|
7
|
+
"main": "./dist/blac-react.umd.js",
|
|
8
|
+
"module": "./dist/blac-react.mjs",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": "./dist/blac-react.mjs",
|
|
12
|
+
"require": "./dist/blac-react.umd.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
19
15
|
"scripts": {
|
|
20
|
-
"build": "vite build"
|
|
21
|
-
"pub:patch": "npm run build && bun publish.ts && cd dist && npm publish",
|
|
22
|
-
"pub:patch:next": "npm run build && bun publish.ts && cd dist && npm publish --tag next",
|
|
23
|
-
"dev": "bun dev.ts",
|
|
24
|
-
"prettier": "prettier --write ./src",
|
|
25
|
-
"test": "vitest --config vitest.config.ts",
|
|
26
|
-
"test:watch": "vitest --watch --config vitest.config.ts",
|
|
27
|
-
"pub": "npm run build && npm publish"
|
|
16
|
+
"build": "vite build"
|
|
28
17
|
},
|
|
18
|
+
"keywords": [],
|
|
19
|
+
"author": "",
|
|
20
|
+
"license": "ISC",
|
|
21
|
+
"description": "",
|
|
29
22
|
"dependencies": {
|
|
30
|
-
"blac": "
|
|
31
|
-
"
|
|
32
|
-
},
|
|
33
|
-
"devDependencies": {
|
|
34
|
-
"@testing-library/dom": "^10.1.0",
|
|
35
|
-
"@testing-library/jest-dom": "^6.4.6",
|
|
36
|
-
"@testing-library/react": "^16.0.0",
|
|
37
|
-
"@testing-library/user-event": "^14.5.2",
|
|
38
|
-
"@typescript-eslint/eslint-plugin": "^7.13.1",
|
|
39
|
-
"@typescript-eslint/parser": "^7.13.1",
|
|
40
|
-
"@vitejs/plugin-react-refresh": "^1.3.6",
|
|
41
|
-
"@vitest/browser": "^1.6.0",
|
|
42
|
-
"@vitest/coverage-v8": "^1.6.0",
|
|
43
|
-
"eslint": "^9.5.0",
|
|
44
|
-
"jsdom": "^24.1.0",
|
|
45
|
-
"prettier": "^3.3.2",
|
|
46
|
-
"react-dom": "^18.3.1",
|
|
47
|
-
"typescript": "^5.4.5",
|
|
48
|
-
"vite-plugin-dts": "^3.9.1",
|
|
49
|
-
"vitest": "^1.6.0"
|
|
23
|
+
"@blac/core": "workspace:*",
|
|
24
|
+
"vite": "^6.0.5"
|
|
50
25
|
}
|
|
51
|
-
}
|
|
26
|
+
}
|
package/README.md
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
# Blac for React
|
|
2
|
-
|
|
3
|
-
Demo [https://blac-react-demo.vercel.app/](https://blac-react-demo.vercel.app/)
|
|
4
|
-
|
|
5
|
-
## Installation
|
|
6
|
-
|
|
7
|
-
```bash
|
|
8
|
-
npm install blac-react
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
## Examples
|
|
12
|
-
|
|
13
|
-
- [Counter with Bloc](../../apps/react-demo/ui/examples/CounterWithBloc.tsx)
|
|
14
|
-
- [Counter with Cubit](../../apps/react-demo/ui/examples/CounterWithCubit.tsx)
|
|
15
|
-
- [Counter with Global State](../../apps/react-demo/ui/examples/CounterWithCubitGlobal.tsx)
|
|
16
|
-
- [Counters with Local State](../../apps/react-demo/ui/examples/CounterMultiInstance.tsx)
|
package/externalBlocStore.d.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { BlocGeneric, BlocHookDependencyArrayFn, BlocState } from 'blac';
|
|
2
|
-
|
|
3
|
-
export interface ExternalStore<B extends BlocGeneric<any, any>, S extends BlocState<B>> {
|
|
4
|
-
subscribe: (onStoreChange: () => void) => () => void;
|
|
5
|
-
getSnapshot: () => S;
|
|
6
|
-
getServerSnapshot?: () => S;
|
|
7
|
-
}
|
|
8
|
-
declare const externalBlocStore: <B extends BlocGeneric<any, any>, S extends BlocState<B>>(bloc: B, dependencyArray: BlocHookDependencyArrayFn<InstanceType<any>>, rid: string) => ExternalStore<B, S>;
|
|
9
|
-
export default externalBlocStore;
|
package/index.cjs.js
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const g=require("blac"),r=require("react"),h=(e,s,d)=>({subscribe:o=>{const u=e._observer.subscribe({fn:()=>{try{o(Object.freeze(e.state))}catch(f){console.error({e:f,bloc:e,dependencyArray:s})}},dependencyArray:s,id:d});return()=>{u()}},getSnapshot:()=>Object.freeze(e.state),getServerSnapshot:()=>Object.freeze(e.state)}),E=e=>[e];class O{static useBloc(s,d){let{dependencySelector:o,id:u,props:f}=d??{};const y=r.useId(),a=r.useRef(new Set),l=r.useRef(new Set),p=new Set,S=r.useRef(!1);s.isolated&&(u=y);const i=g.Blac.getInstance().getBloc(s,{id:u,props:f,reconnect:!1});if(!i)throw new Error(`useBloc: could not resolve: ${s.name||s}`);const j=(t,c)=>{if(o)return o(t,c);if(typeof t!="object")return E(t);const B=[];for(const b of a.current)b in t&&B.push(t[b]);return B},{subscribe:v,getSnapshot:I,getServerSnapshot:m}=r.useMemo(()=>h(i,j,y),[i._createdAt]),n=r.useSyncExternalStore(v,I,m),x=r.useMemo(()=>{try{if(typeof n=="object")return new Proxy(n,{get(t,c){return a.current.add(c),l.current.add(c),S.current=!0,n[c]}})}catch(t){g.Blac.instance.log("useBloc Error",t)}return n},[n,a,l]);return r.useLayoutEffect(()=>(a.current=new Set(l.current),()=>{setTimeout(()=>{S.current&&(l.current.clear(),S.current=!1)})}),[p]),[x,i]}}const w=O.useBloc;exports.externalBlocStore=h;exports.useBloc=w;
|
|
2
|
-
//# sourceMappingURL=index.cjs.js.map
|
package/index.cjs.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs.js","sources":["../src/externalBlocStore.ts","../src/useBloc.tsx"],"sourcesContent":["import { BlocGeneric, BlocHookDependencyArrayFn, BlocState } from 'blac';\n\nexport interface ExternalStore<\n B extends BlocGeneric<any, any>,\n S extends BlocState<B>,\n> {\n subscribe: (onStoreChange: () => void) => () => void;\n getSnapshot: () => S;\n getServerSnapshot?: () => S;\n}\n\nconst externalBlocStore = <\n B extends BlocGeneric<any, any>,\n S extends BlocState<B>,\n>(\n bloc: B,\n dependencyArray: BlocHookDependencyArrayFn<InstanceType<any>>,\n rid: string,\n): ExternalStore<B, S> => {\n return {\n subscribe: (listener: (state: S) => void) => {\n const unSub = bloc._observer.subscribe({\n fn: () => {\n try {\n listener(Object.freeze(bloc.state));\n } catch (e) {\n console.error({\n e,\n bloc,\n dependencyArray,\n });\n }\n },\n dependencyArray,\n id: rid,\n });\n\n return () => {\n unSub();\n };\n },\n getSnapshot: (): S => Object.freeze(bloc.state),\n getServerSnapshot: (): S => Object.freeze(bloc.state),\n };\n};\n\nexport default externalBlocStore;\n","import {\n Blac,\n BlocBase,\n BlocBaseAbstract,\n BlocConstructor,\n BlocGeneric,\n BlocHookDependencyArrayFn,\n BlocState,\n InferPropsFromGeneric,\n} from 'blac';\nimport {\n useEffect,\n useId,\n useLayoutEffect,\n useMemo,\n useRef,\n useSyncExternalStore,\n} from 'react';\nimport externalBlocStore from './externalBlocStore';\n\ntype HookTypes<B extends BlocConstructor<BlocGeneric>> = [\n BlocState<InstanceType<B>>,\n InstanceType<B>,\n];\n\nexport interface BlocHookOptions<B extends BlocGeneric<any, any>> {\n id?: string;\n dependencySelector?: BlocHookDependencyArrayFn<B>;\n props?: InferPropsFromGeneric<B>;\n}\n\nconst defaultDependencySelector: BlocHookDependencyArrayFn<any> = (s) => [s];\n\nexport class UseBlocClass {\n static useBloc<\n B extends BlocConstructor<BlocGeneric>,\n O extends BlocHookOptions<InstanceType<B>>,\n >(bloc: B, options?: O): HookTypes<B> {\n let { dependencySelector, id: blocId, props } = options ?? {};\n const rid = useId();\n const usedKeys = useRef<Set<string>>(new Set());\n const instanceKeys = useRef<Set<string>>(new Set());\n const renderInstance = new Set();\n const shouldClear = useRef(false);\n\n const base = bloc as unknown as BlocBaseAbstract;\n const isIsolated = base.isolated;\n if (isIsolated) {\n blocId = rid;\n }\n\n const resolvedBloc = Blac.getInstance().getBloc(bloc, {\n id: blocId,\n props: props as any,\n reconnect: false,\n }) as InstanceType<B>;\n\n if (!resolvedBloc) {\n throw new Error(`useBloc: could not resolve: ${bloc.name || bloc}`);\n }\n\n // default options\n const dependencyArray: BlocHookDependencyArrayFn<InstanceType<B>> = (\n newState,\n oldState,\n ) => {\n if (dependencySelector) {\n return dependencySelector(newState, oldState);\n }\n if (typeof newState !== 'object') {\n return defaultDependencySelector(newState, oldState);\n }\n\n const used: string[] = [];\n for (const key of usedKeys.current) {\n if (key in newState) {\n used.push(newState[key as keyof typeof newState]);\n }\n }\n\n return used;\n };\n\n const { subscribe, getSnapshot, getServerSnapshot } = useMemo(\n () => externalBlocStore(resolvedBloc, dependencyArray, rid),\n [resolvedBloc._createdAt],\n );\n\n const state = useSyncExternalStore<BlocState<InstanceType<B>>>(\n subscribe,\n getSnapshot,\n getServerSnapshot,\n );\n\n const returnState: BlocState<InstanceType<B>> = useMemo(() => {\n try {\n if (typeof state === 'object') {\n return new Proxy(state as any, {\n get(_, prop) {\n usedKeys.current.add(prop as string);\n instanceKeys.current.add(prop as string);\n shouldClear.current = true;\n return state[prop as keyof typeof state];\n },\n });\n }\n } catch (error) {\n Blac.instance.log('useBloc Error', error);\n }\n return state;\n }, [state, usedKeys, instanceKeys]);\n\n useLayoutEffect(() => {\n usedKeys.current = new Set(instanceKeys.current);\n\n return () => {\n setTimeout(() => {\n if (shouldClear.current) {\n instanceKeys.current.clear();\n shouldClear.current = false;\n }\n });\n };\n }, [renderInstance]);\n\n return [returnState, resolvedBloc];\n }\n}\n\nconst useBloc = UseBlocClass.useBloc;\n\nexport default useBloc;\n"],"names":["externalBlocStore","bloc","dependencyArray","rid","listener","unSub","e","defaultDependencySelector","s","UseBlocClass","options","dependencySelector","blocId","props","useId","usedKeys","useRef","instanceKeys","renderInstance","shouldClear","resolvedBloc","Blac","newState","oldState","used","key","subscribe","getSnapshot","getServerSnapshot","useMemo","state","useSyncExternalStore","returnState","_","prop","error","useLayoutEffect","useBloc"],"mappings":"2HAWMA,EAAoB,CAIxBC,EACAC,EACAC,KAEO,CACL,UAAYC,GAAiC,CACrC,MAAAC,EAAQJ,EAAK,UAAU,UAAU,CACrC,GAAI,IAAM,CACJ,GAAA,CACFG,EAAS,OAAO,OAAOH,EAAK,KAAK,CAAC,QAC3BK,EAAG,CACV,QAAQ,MAAM,CACZ,EAAAA,EACA,KAAAL,EACA,gBAAAC,CAAA,CACD,CACH,CACF,EACA,gBAAAA,EACA,GAAIC,CAAA,CACL,EAED,MAAO,IAAM,CACLE,GAAA,CAEV,EACA,YAAa,IAAS,OAAO,OAAOJ,EAAK,KAAK,EAC9C,kBAAmB,IAAS,OAAO,OAAOA,EAAK,KAAK,CAAA,GCXlDM,EAA6DC,GAAM,CAACA,CAAC,EAEpE,MAAMC,CAAa,CACxB,OAAO,QAGLR,EAASS,EAA2B,CACpC,GAAI,CAAE,mBAAAC,EAAoB,GAAIC,EAAQ,MAAAC,CAAM,EAAIH,GAAW,GAC3D,MAAMP,EAAMW,EAAAA,QACNC,EAAWC,EAAAA,OAAwB,IAAA,GAAK,EACxCC,EAAeD,EAAAA,OAAwB,IAAA,GAAK,EAC5CE,MAAqB,IACrBC,EAAcH,SAAO,EAAK,EAEnBf,EACW,WAEbW,EAAAT,GAGX,MAAMiB,EAAeC,EAAA,KAAK,YAAY,EAAE,QAAQpB,EAAM,CACpD,GAAIW,EACJ,MAAAC,EACA,UAAW,EAAA,CACZ,EAED,GAAI,CAACO,EACH,MAAM,IAAI,MAAM,+BAA+BnB,EAAK,MAAQA,CAAI,EAAE,EAI9D,MAAAC,EAA8D,CAClEoB,EACAC,IACG,CACH,GAAIZ,EACK,OAAAA,EAAmBW,EAAUC,CAAQ,EAE1C,GAAA,OAAOD,GAAa,SACf,OAAAf,EAA0Be,CAAkB,EAGrD,MAAME,EAAiB,CAAA,EACZ,UAAAC,KAAOV,EAAS,QACrBU,KAAOH,GACJE,EAAA,KAAKF,EAASG,CAA4B,CAAC,EAI7C,OAAAD,CAAA,EAGH,CAAE,UAAAE,EAAW,YAAAC,EAAa,kBAAAC,CAAsB,EAAAC,EAAA,QACpD,IAAM7B,EAAkBoB,EAAclB,EAAiBC,CAAG,EAC1D,CAACiB,EAAa,UAAU,CAAA,EAGpBU,EAAQC,EAAA,qBACZL,EACAC,EACAC,CAAA,EAGII,EAA0CH,EAAAA,QAAQ,IAAM,CACxD,GAAA,CACE,GAAA,OAAOC,GAAU,SACZ,OAAA,IAAI,MAAMA,EAAc,CAC7B,IAAIG,EAAGC,EAAM,CACF,OAAAnB,EAAA,QAAQ,IAAImB,CAAc,EACtBjB,EAAA,QAAQ,IAAIiB,CAAc,EACvCf,EAAY,QAAU,GACfW,EAAMI,CAA0B,CACzC,CAAA,CACD,QAEIC,EAAO,CACTd,EAAAA,KAAA,SAAS,IAAI,gBAAiBc,CAAK,CAC1C,CACO,OAAAL,CACN,EAAA,CAACA,EAAOf,EAAUE,CAAY,CAAC,EAElCmB,OAAAA,EAAAA,gBAAgB,KACdrB,EAAS,QAAU,IAAI,IAAIE,EAAa,OAAO,EAExC,IAAM,CACX,WAAW,IAAM,CACXE,EAAY,UACdF,EAAa,QAAQ,QACrBE,EAAY,QAAU,GACxB,CACD,CAAA,GAEF,CAACD,CAAc,CAAC,EAEZ,CAACc,EAAaZ,CAAY,CACnC,CACF,CAEA,MAAMiB,EAAU5B,EAAa"}
|
package/index.d.ts
DELETED
package/index.es.js
DELETED
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
import { Blac as b } from "blac";
|
|
2
|
-
import { useId as E, useRef as S, useMemo as h, useSyncExternalStore as w, useLayoutEffect as z } from "react";
|
|
3
|
-
const O = (e, r, d) => ({
|
|
4
|
-
subscribe: (c) => {
|
|
5
|
-
const o = e._observer.subscribe({
|
|
6
|
-
fn: () => {
|
|
7
|
-
try {
|
|
8
|
-
c(Object.freeze(e.state));
|
|
9
|
-
} catch (f) {
|
|
10
|
-
console.error({
|
|
11
|
-
e: f,
|
|
12
|
-
bloc: e,
|
|
13
|
-
dependencyArray: r
|
|
14
|
-
});
|
|
15
|
-
}
|
|
16
|
-
},
|
|
17
|
-
dependencyArray: r,
|
|
18
|
-
id: d
|
|
19
|
-
});
|
|
20
|
-
return () => {
|
|
21
|
-
o();
|
|
22
|
-
};
|
|
23
|
-
},
|
|
24
|
-
getSnapshot: () => Object.freeze(e.state),
|
|
25
|
-
getServerSnapshot: () => Object.freeze(e.state)
|
|
26
|
-
}), _ = (e) => [e];
|
|
27
|
-
class C {
|
|
28
|
-
static useBloc(r, d) {
|
|
29
|
-
let { dependencySelector: c, id: o, props: f } = d ?? {};
|
|
30
|
-
const y = E(), u = S(/* @__PURE__ */ new Set()), a = S(/* @__PURE__ */ new Set()), g = /* @__PURE__ */ new Set(), i = S(!1);
|
|
31
|
-
r.isolated && (o = y);
|
|
32
|
-
const l = b.getInstance().getBloc(r, {
|
|
33
|
-
id: o,
|
|
34
|
-
props: f,
|
|
35
|
-
reconnect: !1
|
|
36
|
-
});
|
|
37
|
-
if (!l)
|
|
38
|
-
throw new Error(`useBloc: could not resolve: ${r.name || r}`);
|
|
39
|
-
const m = (t, n) => {
|
|
40
|
-
if (c)
|
|
41
|
-
return c(t, n);
|
|
42
|
-
if (typeof t != "object")
|
|
43
|
-
return _(t);
|
|
44
|
-
const p = [];
|
|
45
|
-
for (const B of u.current)
|
|
46
|
-
B in t && p.push(t[B]);
|
|
47
|
-
return p;
|
|
48
|
-
}, { subscribe: I, getSnapshot: j, getServerSnapshot: v } = h(
|
|
49
|
-
() => O(l, m, y),
|
|
50
|
-
[l._createdAt]
|
|
51
|
-
), s = w(
|
|
52
|
-
I,
|
|
53
|
-
j,
|
|
54
|
-
v
|
|
55
|
-
), x = h(() => {
|
|
56
|
-
try {
|
|
57
|
-
if (typeof s == "object")
|
|
58
|
-
return new Proxy(s, {
|
|
59
|
-
get(t, n) {
|
|
60
|
-
return u.current.add(n), a.current.add(n), i.current = !0, s[n];
|
|
61
|
-
}
|
|
62
|
-
});
|
|
63
|
-
} catch (t) {
|
|
64
|
-
b.instance.log("useBloc Error", t);
|
|
65
|
-
}
|
|
66
|
-
return s;
|
|
67
|
-
}, [s, u, a]);
|
|
68
|
-
return z(() => (u.current = new Set(a.current), () => {
|
|
69
|
-
setTimeout(() => {
|
|
70
|
-
i.current && (a.current.clear(), i.current = !1);
|
|
71
|
-
});
|
|
72
|
-
}), [g]), [x, l];
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
const L = C.useBloc;
|
|
76
|
-
export {
|
|
77
|
-
O as externalBlocStore,
|
|
78
|
-
L as useBloc
|
|
79
|
-
};
|
|
80
|
-
//# sourceMappingURL=index.es.js.map
|
package/index.es.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.es.js","sources":["../src/externalBlocStore.ts","../src/useBloc.tsx"],"sourcesContent":["import { BlocGeneric, BlocHookDependencyArrayFn, BlocState } from 'blac';\n\nexport interface ExternalStore<\n B extends BlocGeneric<any, any>,\n S extends BlocState<B>,\n> {\n subscribe: (onStoreChange: () => void) => () => void;\n getSnapshot: () => S;\n getServerSnapshot?: () => S;\n}\n\nconst externalBlocStore = <\n B extends BlocGeneric<any, any>,\n S extends BlocState<B>,\n>(\n bloc: B,\n dependencyArray: BlocHookDependencyArrayFn<InstanceType<any>>,\n rid: string,\n): ExternalStore<B, S> => {\n return {\n subscribe: (listener: (state: S) => void) => {\n const unSub = bloc._observer.subscribe({\n fn: () => {\n try {\n listener(Object.freeze(bloc.state));\n } catch (e) {\n console.error({\n e,\n bloc,\n dependencyArray,\n });\n }\n },\n dependencyArray,\n id: rid,\n });\n\n return () => {\n unSub();\n };\n },\n getSnapshot: (): S => Object.freeze(bloc.state),\n getServerSnapshot: (): S => Object.freeze(bloc.state),\n };\n};\n\nexport default externalBlocStore;\n","import {\n Blac,\n BlocBase,\n BlocBaseAbstract,\n BlocConstructor,\n BlocGeneric,\n BlocHookDependencyArrayFn,\n BlocState,\n InferPropsFromGeneric,\n} from 'blac';\nimport {\n useEffect,\n useId,\n useLayoutEffect,\n useMemo,\n useRef,\n useSyncExternalStore,\n} from 'react';\nimport externalBlocStore from './externalBlocStore';\n\ntype HookTypes<B extends BlocConstructor<BlocGeneric>> = [\n BlocState<InstanceType<B>>,\n InstanceType<B>,\n];\n\nexport interface BlocHookOptions<B extends BlocGeneric<any, any>> {\n id?: string;\n dependencySelector?: BlocHookDependencyArrayFn<B>;\n props?: InferPropsFromGeneric<B>;\n}\n\nconst defaultDependencySelector: BlocHookDependencyArrayFn<any> = (s) => [s];\n\nexport class UseBlocClass {\n static useBloc<\n B extends BlocConstructor<BlocGeneric>,\n O extends BlocHookOptions<InstanceType<B>>,\n >(bloc: B, options?: O): HookTypes<B> {\n let { dependencySelector, id: blocId, props } = options ?? {};\n const rid = useId();\n const usedKeys = useRef<Set<string>>(new Set());\n const instanceKeys = useRef<Set<string>>(new Set());\n const renderInstance = new Set();\n const shouldClear = useRef(false);\n\n const base = bloc as unknown as BlocBaseAbstract;\n const isIsolated = base.isolated;\n if (isIsolated) {\n blocId = rid;\n }\n\n const resolvedBloc = Blac.getInstance().getBloc(bloc, {\n id: blocId,\n props: props as any,\n reconnect: false,\n }) as InstanceType<B>;\n\n if (!resolvedBloc) {\n throw new Error(`useBloc: could not resolve: ${bloc.name || bloc}`);\n }\n\n // default options\n const dependencyArray: BlocHookDependencyArrayFn<InstanceType<B>> = (\n newState,\n oldState,\n ) => {\n if (dependencySelector) {\n return dependencySelector(newState, oldState);\n }\n if (typeof newState !== 'object') {\n return defaultDependencySelector(newState, oldState);\n }\n\n const used: string[] = [];\n for (const key of usedKeys.current) {\n if (key in newState) {\n used.push(newState[key as keyof typeof newState]);\n }\n }\n\n return used;\n };\n\n const { subscribe, getSnapshot, getServerSnapshot } = useMemo(\n () => externalBlocStore(resolvedBloc, dependencyArray, rid),\n [resolvedBloc._createdAt],\n );\n\n const state = useSyncExternalStore<BlocState<InstanceType<B>>>(\n subscribe,\n getSnapshot,\n getServerSnapshot,\n );\n\n const returnState: BlocState<InstanceType<B>> = useMemo(() => {\n try {\n if (typeof state === 'object') {\n return new Proxy(state as any, {\n get(_, prop) {\n usedKeys.current.add(prop as string);\n instanceKeys.current.add(prop as string);\n shouldClear.current = true;\n return state[prop as keyof typeof state];\n },\n });\n }\n } catch (error) {\n Blac.instance.log('useBloc Error', error);\n }\n return state;\n }, [state, usedKeys, instanceKeys]);\n\n useLayoutEffect(() => {\n usedKeys.current = new Set(instanceKeys.current);\n\n return () => {\n setTimeout(() => {\n if (shouldClear.current) {\n instanceKeys.current.clear();\n shouldClear.current = false;\n }\n });\n };\n }, [renderInstance]);\n\n return [returnState, resolvedBloc];\n }\n}\n\nconst useBloc = UseBlocClass.useBloc;\n\nexport default useBloc;\n"],"names":["externalBlocStore","bloc","dependencyArray","rid","listener","unSub","e","defaultDependencySelector","s","UseBlocClass","options","dependencySelector","blocId","props","useId","usedKeys","useRef","instanceKeys","renderInstance","shouldClear","resolvedBloc","Blac","newState","oldState","used","key","subscribe","getSnapshot","getServerSnapshot","useMemo","state","useSyncExternalStore","returnState","_","prop","error","useLayoutEffect","useBloc"],"mappings":";;AAWA,MAAMA,IAAoB,CAIxBC,GACAC,GACAC,OAEO;AAAA,EACL,WAAW,CAACC,MAAiC;AACrC,UAAAC,IAAQJ,EAAK,UAAU,UAAU;AAAA,MACrC,IAAI,MAAM;AACJ,YAAA;AACF,UAAAG,EAAS,OAAO,OAAOH,EAAK,KAAK,CAAC;AAAA,iBAC3BK,GAAG;AACV,kBAAQ,MAAM;AAAA,YACZ,GAAAA;AAAA,YACA,MAAAL;AAAA,YACA,iBAAAC;AAAA,UAAA,CACD;AAAA,QACH;AAAA,MACF;AAAA,MACA,iBAAAA;AAAA,MACA,IAAIC;AAAA,IAAA,CACL;AAED,WAAO,MAAM;AACL,MAAAE;IAAA;AAAA,EAEV;AAAA,EACA,aAAa,MAAS,OAAO,OAAOJ,EAAK,KAAK;AAAA,EAC9C,mBAAmB,MAAS,OAAO,OAAOA,EAAK,KAAK;AAAA,ICXlDM,IAA4D,CAACC,MAAM,CAACA,CAAC;AAEpE,MAAMC,EAAa;AAAA,EACxB,OAAO,QAGLR,GAASS,GAA2B;AACpC,QAAI,EAAE,oBAAAC,GAAoB,IAAIC,GAAQ,OAAAC,EAAM,IAAIH,KAAW;AAC3D,UAAMP,IAAMW,KACNC,IAAWC,EAAwB,oBAAA,IAAK,CAAA,GACxCC,IAAeD,EAAwB,oBAAA,IAAK,CAAA,GAC5CE,wBAAqB,OACrBC,IAAcH,EAAO,EAAK;AAIhC,IAFaf,EACW,aAEbW,IAAAT;AAGX,UAAMiB,IAAeC,EAAK,YAAY,EAAE,QAAQpB,GAAM;AAAA,MACpD,IAAIW;AAAA,MACJ,OAAAC;AAAA,MACA,WAAW;AAAA,IAAA,CACZ;AAED,QAAI,CAACO;AACH,YAAM,IAAI,MAAM,+BAA+BnB,EAAK,QAAQA,CAAI,EAAE;AAI9D,UAAAC,IAA8D,CAClEoB,GACAC,MACG;AACH,UAAIZ;AACK,eAAAA,EAAmBW,GAAUC,CAAQ;AAE1C,UAAA,OAAOD,KAAa;AACf,eAAAf,EAA0Be,CAAkB;AAGrD,YAAME,IAAiB,CAAA;AACZ,iBAAAC,KAAOV,EAAS;AACzB,QAAIU,KAAOH,KACJE,EAAA,KAAKF,EAASG,CAA4B,CAAC;AAI7C,aAAAD;AAAA,IAAA,GAGH,EAAE,WAAAE,GAAW,aAAAC,GAAa,mBAAAC,EAAsB,IAAAC;AAAA,MACpD,MAAM7B,EAAkBoB,GAAclB,GAAiBC,CAAG;AAAA,MAC1D,CAACiB,EAAa,UAAU;AAAA,IAAA,GAGpBU,IAAQC;AAAA,MACZL;AAAA,MACAC;AAAA,MACAC;AAAA,IAAA,GAGII,IAA0CH,EAAQ,MAAM;AACxD,UAAA;AACE,YAAA,OAAOC,KAAU;AACZ,iBAAA,IAAI,MAAMA,GAAc;AAAA,YAC7B,IAAIG,GAAGC,GAAM;AACF,qBAAAnB,EAAA,QAAQ,IAAImB,CAAc,GACtBjB,EAAA,QAAQ,IAAIiB,CAAc,GACvCf,EAAY,UAAU,IACfW,EAAMI,CAA0B;AAAA,YACzC;AAAA,UAAA,CACD;AAAA,eAEIC,GAAO;AACT,QAAAd,EAAA,SAAS,IAAI,iBAAiBc,CAAK;AAAA,MAC1C;AACO,aAAAL;AAAA,IACN,GAAA,CAACA,GAAOf,GAAUE,CAAY,CAAC;AAElC,WAAAmB,EAAgB,OACdrB,EAAS,UAAU,IAAI,IAAIE,EAAa,OAAO,GAExC,MAAM;AACX,iBAAW,MAAM;AACf,QAAIE,EAAY,YACdF,EAAa,QAAQ,SACrBE,EAAY,UAAU;AAAA,MACxB,CACD;AAAA,IAAA,IAEF,CAACD,CAAc,CAAC,GAEZ,CAACc,GAAaZ,CAAY;AAAA,EACnC;AACF;AAEA,MAAMiB,IAAU5B,EAAa;"}
|
package/useBloc.d.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { BlocConstructor, BlocGeneric, BlocHookDependencyArrayFn, BlocState, InferPropsFromGeneric } from 'blac';
|
|
2
|
-
|
|
3
|
-
type HookTypes<B extends BlocConstructor<BlocGeneric>> = [
|
|
4
|
-
BlocState<InstanceType<B>>,
|
|
5
|
-
InstanceType<B>
|
|
6
|
-
];
|
|
7
|
-
export interface BlocHookOptions<B extends BlocGeneric<any, any>> {
|
|
8
|
-
id?: string;
|
|
9
|
-
dependencySelector?: BlocHookDependencyArrayFn<B>;
|
|
10
|
-
props?: InferPropsFromGeneric<B>;
|
|
11
|
-
}
|
|
12
|
-
export declare class UseBlocClass {
|
|
13
|
-
static useBloc<B extends BlocConstructor<BlocGeneric>, O extends BlocHookOptions<InstanceType<B>>>(bloc: B, options?: O): HookTypes<B>;
|
|
14
|
-
}
|
|
15
|
-
declare const useBloc: typeof UseBlocClass.useBloc;
|
|
16
|
-
export default useBloc;
|