@blac/react 0.0.51 → 0.0.52
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/index.cjs.js +1 -1
- package/index.cjs.js.map +1 -1
- package/index.es.js +49 -49
- package/index.es.js.map +1 -1
- package/package.json +1 -1
- package/useBloc.d.ts +1 -0
package/index.cjs.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var m=Object.defineProperty;var x=(s,e,t)=>e in s?m(s,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):s[e]=t;var S=(s,e,t)=>x(s,typeof e!="symbol"?e+"":e,t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const h=require("blac"),d=require("react"),B=(s,e)=>({subscribe:t=>{const n=s.addSubscriber({fn:()=>{try{t(s.state)}catch(u){console.error({e:u,bloc:s,dependencyArray:e})}},dependencyArray:e});return()=>{n()}},getSnapshot:()=>s.state,getServerSnapshot:()=>s.state}),c=class c{static useBloc(e,t){const n=d.useId(),u=c.getUsedKeys(n);let y=t!=null&&t.inferStateUsage?r=>{if(typeof r!="object")return[r];const a=[];for(const b of u)b in r&&a.push(r[b]);return a}:r=>[r],f,g;typeof t=="object"&&(y=t.dependencySelector??y,f=t.id,g=t.props??g),e.isolated&&(f=n);const o=h.Blac.getInstance().getBloc(e,{id:f,props:g,reconnect:!1});if(!o)throw new Error(`useBloc: could not resolve: ${e.name||e}`);const{subscribe:U,getSnapshot:v,getServerSnapshot:I}=d.useMemo(()=>B(o,y),[o.createdAt]),l=d.useSyncExternalStore(U,v,I),i=new Set,j=d.useMemo(()=>{try{if(t!=null&&t.inferStateUsage&&typeof l=="object")return new Proxy(l,{get(r,a){return u.add(a),i.add(a),Reflect.get(r,a)}})}catch(r){h.Blac.instance.log("useBloc Error",r)}return l},[l]);return d.useLayoutEffect(()=>{c.getUsedKeys(n).clear();for(const r of i)c.getUsedKeys(n).add(r)},[i,n]),[j,o]}};S(c,"ridUsedKeys",new Map),S(c,"getUsedKeys",e=>(c.ridUsedKeys.has(e)||c.ridUsedKeys.set(e,new Set),c.ridUsedKeys.get(e)));let K=c;const E=K.useBloc;exports.externalBlocStore=B;exports.useBloc=E;
|
|
2
2
|
//# sourceMappingURL=index.cjs.js.map
|
package/index.cjs.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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): ExternalStore<B, S> => {\n return {\n subscribe: (listener: (state: S) => void) => {\n const unSub = bloc.addSubscriber({\n fn: () => {\n try {\n listener(bloc.state);\n } catch (e) {\n console.error({\n e,\n bloc,\n dependencyArray,\n });\n }\n },\n dependencyArray,\n });\n\n return () => {\n unSub();\n };\n },\n getSnapshot: (): S => bloc.state,\n getServerSnapshot: (): S => bloc.state,\n };\n};\n\nexport default externalBlocStore;\n","import {\n Blac,\n BlocBase,\n BlocConstructor,\n BlocGeneric,\n BlocHookDependencyArrayFn,\n BlocInstanceId,\n BlocState,\n InferPropsFromGeneric,\n} from 'blac';\nimport { useId, useLayoutEffect, useMemo, useSyncExternalStore } from 'react';\nimport externalBlocStore from './externalBlocStore';\n\n// export type BlocHookData<B extends BlocBase<S>, S> = [\n// value: ValueType<B>,\n// instance: B,\n// ];\n\ntype HookTypes<B extends BlocConstructor<BlocGeneric>> = [\n BlocState<InstanceType<B>>,\n InstanceType<B>,\n];\n\n// type BlocHookData<B extends BlocBase<S>, S> = [S, InstanceType<B>]; // B is the bloc class, S is the state of the bloc\n// type UseBlocClassResult<B, S> = BlocHookData<B, S>;\n\nexport interface BlocHookOptions<B extends BlocGeneric<any, any>> {\n id?: string;\n dependencySelector?: BlocHookDependencyArrayFn<B>;\n props?: InferPropsFromGeneric<B>;\n}\n\nexport class UseBlocClass {\n static ridUsedKeys: Map<string, Set<string>> = new Map();\n static getUsedKeys = (rid: string): Set<string> => {\n if (!UseBlocClass.ridUsedKeys.has(rid)) {\n UseBlocClass.ridUsedKeys.set(rid, new Set());\n }\n return UseBlocClass.ridUsedKeys.get(rid) as Set<string>;\n };\n\n static useBloc<\n B extends BlocConstructor<BlocGeneric>,\n O extends BlocHookOptions<InstanceType<B>>,\n >(bloc: B, options?: O): HookTypes<B> {\n const rid = useId();\n const usedKeys = UseBlocClass.getUsedKeys(rid);\n\n // default options\n let dependencyArray: BlocHookDependencyArrayFn<InstanceType<B>>
|
|
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): ExternalStore<B, S> => {\n return {\n subscribe: (listener: (state: S) => void) => {\n const unSub = bloc.addSubscriber({\n fn: () => {\n try {\n listener(bloc.state);\n } catch (e) {\n console.error({\n e,\n bloc,\n dependencyArray,\n });\n }\n },\n dependencyArray,\n });\n\n return () => {\n unSub();\n };\n },\n getSnapshot: (): S => bloc.state,\n getServerSnapshot: (): S => bloc.state,\n };\n};\n\nexport default externalBlocStore;\n","import {\n Blac,\n BlocBase,\n BlocConstructor,\n BlocGeneric,\n BlocHookDependencyArrayFn,\n BlocInstanceId,\n BlocState,\n InferPropsFromGeneric,\n} from 'blac';\nimport { useId, useLayoutEffect, useMemo, useSyncExternalStore } from 'react';\nimport externalBlocStore from './externalBlocStore';\n\n// export type BlocHookData<B extends BlocBase<S>, S> = [\n// value: ValueType<B>,\n// instance: B,\n// ];\n\ntype HookTypes<B extends BlocConstructor<BlocGeneric>> = [\n BlocState<InstanceType<B>>,\n InstanceType<B>,\n];\n\n// type BlocHookData<B extends BlocBase<S>, S> = [S, InstanceType<B>]; // B is the bloc class, S is the state of the bloc\n// type UseBlocClassResult<B, S> = BlocHookData<B, S>;\n\nexport interface BlocHookOptions<B extends BlocGeneric<any, any>> {\n id?: string;\n dependencySelector?: BlocHookDependencyArrayFn<B>;\n props?: InferPropsFromGeneric<B>;\n inferStateUsage?: boolean;\n}\n\nexport class UseBlocClass {\n static ridUsedKeys: Map<string, Set<string>> = new Map();\n static getUsedKeys = (rid: string): Set<string> => {\n if (!UseBlocClass.ridUsedKeys.has(rid)) {\n UseBlocClass.ridUsedKeys.set(rid, new Set());\n }\n return UseBlocClass.ridUsedKeys.get(rid) as Set<string>;\n };\n\n static useBloc<\n B extends BlocConstructor<BlocGeneric>,\n O extends BlocHookOptions<InstanceType<B>>,\n >(bloc: B, options?: O): HookTypes<B> {\n const rid = useId();\n const usedKeys = UseBlocClass.getUsedKeys(rid);\n\n // default options\n let dependencyArray: BlocHookDependencyArrayFn<InstanceType<B>> =\n options?.inferStateUsage\n ? (newState) => {\n if (typeof newState !== 'object') {\n return [newState] as unknown[];\n }\n\n const used: string[] = [];\n for (const key of usedKeys) {\n if (key in newState) {\n used.push(newState[key as keyof typeof newState]);\n }\n }\n return used;\n }\n : (s) => [s];\n let blocId: BlocInstanceId | undefined;\n let props: ConstructorParameters<BlocConstructor<B>> | undefined;\n\n if (typeof options === 'object') {\n dependencyArray = options.dependencySelector ?? dependencyArray;\n blocId = options.id;\n props = options.props ?? props;\n }\n\n const base = bloc as unknown as BlocBase<B, O['props']>;\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 const { subscribe, getSnapshot, getServerSnapshot } = useMemo(\n () => externalBlocStore(resolvedBloc, dependencyArray),\n [resolvedBloc.createdAt],\n );\n\n const state = useSyncExternalStore<BlocState<InstanceType<B>>>(\n subscribe,\n getSnapshot,\n getServerSnapshot,\n );\n\n const currentRenderUsedKeys = new Set<string>();\n const returnState: BlocState<InstanceType<B>> = useMemo(() => {\n try {\n if (options?.inferStateUsage && typeof state === 'object') {\n return new Proxy(state as any, {\n get(target, prop) {\n usedKeys.add(prop as string);\n currentRenderUsedKeys.add(prop as string);\n return Reflect.get(target, prop);\n },\n });\n }\n } catch (error) {\n Blac.instance.log('useBloc Error', error);\n }\n return state;\n }, [state]);\n\n useLayoutEffect(() => {\n UseBlocClass.getUsedKeys(rid).clear();\n for (const key of currentRenderUsedKeys) {\n UseBlocClass.getUsedKeys(rid).add(key);\n }\n }, [currentRenderUsedKeys, rid]);\n\n return [returnState, resolvedBloc];\n }\n}\n\nconst useBloc = UseBlocClass.useBloc;\n\nexport default useBloc;\n"],"names":["externalBlocStore","bloc","dependencyArray","listener","unSub","e","_UseBlocClass","options","rid","useId","usedKeys","newState","used","key","s","blocId","props","resolvedBloc","Blac","subscribe","getSnapshot","getServerSnapshot","useMemo","state","useSyncExternalStore","currentRenderUsedKeys","returnState","target","prop","error","useLayoutEffect","__publicField","UseBlocClass","useBloc"],"mappings":"+RAWMA,EAAoB,CAIxBC,EACAC,KAEO,CACL,UAAYC,GAAiC,CACrC,MAAAC,EAAQH,EAAK,cAAc,CAC/B,GAAI,IAAM,CACJ,GAAA,CACFE,EAASF,EAAK,KAAK,QACZI,EAAG,CACV,QAA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QC,EAAM,CAChB,OAAAlB,EAAS,IAAIkB,CAAc,EAC3BH,EAAsB,IAAIG,CAAc,EACjC,QAAQ,IAAID,EAAQC,CAAI,CACjC,CAAA,CACD,QAEIC,EAAO,CACTX,EAAAA,KAAA,SAAS,IAAI,gBAAiBW,CAAK,CAC1C,CACO,OAAAN,CAAA,EACN,CAACA,CAAK,CAAC,EAEVO,OAAAA,EAAAA,gBAAgB,IAAM,CACPxB,EAAA,YAAYE,CAAG,EAAE,MAAM,EACpC,UAAWK,KAAOY,EAChBnB,EAAa,YAAYE,CAAG,EAAE,IAAIK,CAAG,CACvC,EACC,CAACY,EAAuBjB,CAAG,CAAC,EAExB,CAACkB,EAAaT,CAAY,CACnC,CACF,EA/FEc,EADWzB,EACJ,cAAwC,IAAI,KACnDyB,EAFWzB,EAEJ,cAAeE,IACfF,EAAa,YAAY,IAAIE,CAAG,GACnCF,EAAa,YAAY,IAAIE,EAAK,IAAI,GAAK,EAEtCF,EAAa,YAAY,IAAIE,CAAG,IANpC,IAAMwB,EAAN1B,EAkGP,MAAM2B,EAAUD,EAAa"}
|
package/index.es.js
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
import { Blac as
|
|
5
|
-
import { useId as
|
|
6
|
-
const
|
|
7
|
-
subscribe: (
|
|
8
|
-
const n =
|
|
1
|
+
var v = Object.defineProperty;
|
|
2
|
+
var x = (s, e, t) => e in s ? v(s, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : s[e] = t;
|
|
3
|
+
var i = (s, e, t) => x(s, typeof e != "symbol" ? e + "" : e, t);
|
|
4
|
+
import { Blac as h } from "blac";
|
|
5
|
+
import { useId as E, useMemo as U, useSyncExternalStore as j, useLayoutEffect as w } from "react";
|
|
6
|
+
const k = (s, e) => ({
|
|
7
|
+
subscribe: (t) => {
|
|
8
|
+
const n = s.addSubscriber({
|
|
9
9
|
fn: () => {
|
|
10
10
|
try {
|
|
11
|
-
s
|
|
12
|
-
} catch (
|
|
11
|
+
t(s.state);
|
|
12
|
+
} catch (a) {
|
|
13
13
|
console.error({
|
|
14
|
-
e:
|
|
15
|
-
bloc:
|
|
14
|
+
e: a,
|
|
15
|
+
bloc: s,
|
|
16
16
|
dependencyArray: e
|
|
17
17
|
});
|
|
18
18
|
}
|
|
@@ -23,59 +23,59 @@ const w = (t, e) => ({
|
|
|
23
23
|
n();
|
|
24
24
|
};
|
|
25
25
|
},
|
|
26
|
-
getSnapshot: () =>
|
|
27
|
-
getServerSnapshot: () =>
|
|
28
|
-
}),
|
|
29
|
-
static useBloc(e,
|
|
30
|
-
const n =
|
|
31
|
-
let
|
|
26
|
+
getSnapshot: () => s.state,
|
|
27
|
+
getServerSnapshot: () => s.state
|
|
28
|
+
}), c = class c {
|
|
29
|
+
static useBloc(e, t) {
|
|
30
|
+
const n = E(), a = c.getUsedKeys(n);
|
|
31
|
+
let f = t != null && t.inferStateUsage ? (r) => {
|
|
32
32
|
if (typeof r != "object")
|
|
33
33
|
return [r];
|
|
34
|
-
const
|
|
35
|
-
for (const
|
|
36
|
-
|
|
37
|
-
return
|
|
38
|
-
},
|
|
39
|
-
typeof
|
|
40
|
-
const
|
|
41
|
-
id:
|
|
42
|
-
props:
|
|
34
|
+
const d = [];
|
|
35
|
+
for (const K of a)
|
|
36
|
+
K in r && d.push(r[K]);
|
|
37
|
+
return d;
|
|
38
|
+
} : (r) => [r], y, g;
|
|
39
|
+
typeof t == "object" && (f = t.dependencySelector ?? f, y = t.id, g = t.props ?? g), e.isolated && (y = n);
|
|
40
|
+
const u = h.getInstance().getBloc(e, {
|
|
41
|
+
id: y,
|
|
42
|
+
props: g,
|
|
43
43
|
reconnect: !1
|
|
44
44
|
});
|
|
45
|
-
if (!
|
|
45
|
+
if (!u)
|
|
46
46
|
throw new Error(`useBloc: could not resolve: ${e.name || e}`);
|
|
47
|
-
const { subscribe: b, getSnapshot: m, getServerSnapshot: B } =
|
|
48
|
-
() =>
|
|
49
|
-
[
|
|
50
|
-
),
|
|
47
|
+
const { subscribe: b, getSnapshot: m, getServerSnapshot: B } = U(
|
|
48
|
+
() => k(u, f),
|
|
49
|
+
[u.createdAt]
|
|
50
|
+
), o = j(
|
|
51
51
|
b,
|
|
52
52
|
m,
|
|
53
53
|
B
|
|
54
|
-
), l = /* @__PURE__ */ new Set(),
|
|
54
|
+
), l = /* @__PURE__ */ new Set(), I = U(() => {
|
|
55
55
|
try {
|
|
56
|
-
if (typeof
|
|
57
|
-
return new Proxy(
|
|
58
|
-
get(r,
|
|
59
|
-
return
|
|
56
|
+
if (t != null && t.inferStateUsage && typeof o == "object")
|
|
57
|
+
return new Proxy(o, {
|
|
58
|
+
get(r, d) {
|
|
59
|
+
return a.add(d), l.add(d), Reflect.get(r, d);
|
|
60
60
|
}
|
|
61
61
|
});
|
|
62
62
|
} catch (r) {
|
|
63
|
-
|
|
63
|
+
h.instance.log("useBloc Error", r);
|
|
64
64
|
}
|
|
65
|
-
return
|
|
66
|
-
}, [
|
|
67
|
-
return
|
|
68
|
-
|
|
65
|
+
return o;
|
|
66
|
+
}, [o]);
|
|
67
|
+
return w(() => {
|
|
68
|
+
c.getUsedKeys(n).clear();
|
|
69
69
|
for (const r of l)
|
|
70
|
-
|
|
71
|
-
}, [l, n]), [
|
|
70
|
+
c.getUsedKeys(n).add(r);
|
|
71
|
+
}, [l, n]), [I, u];
|
|
72
72
|
}
|
|
73
73
|
};
|
|
74
|
-
|
|
75
|
-
let S =
|
|
76
|
-
const
|
|
74
|
+
i(c, "ridUsedKeys", /* @__PURE__ */ new Map()), i(c, "getUsedKeys", (e) => (c.ridUsedKeys.has(e) || c.ridUsedKeys.set(e, /* @__PURE__ */ new Set()), c.ridUsedKeys.get(e)));
|
|
75
|
+
let S = c;
|
|
76
|
+
const $ = S.useBloc;
|
|
77
77
|
export {
|
|
78
|
-
|
|
79
|
-
|
|
78
|
+
k as externalBlocStore,
|
|
79
|
+
$ as useBloc
|
|
80
80
|
};
|
|
81
81
|
//# sourceMappingURL=index.es.js.map
|
package/index.es.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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): ExternalStore<B, S> => {\n return {\n subscribe: (listener: (state: S) => void) => {\n const unSub = bloc.addSubscriber({\n fn: () => {\n try {\n listener(bloc.state);\n } catch (e) {\n console.error({\n e,\n bloc,\n dependencyArray,\n });\n }\n },\n dependencyArray,\n });\n\n return () => {\n unSub();\n };\n },\n getSnapshot: (): S => bloc.state,\n getServerSnapshot: (): S => bloc.state,\n };\n};\n\nexport default externalBlocStore;\n","import {\n Blac,\n BlocBase,\n BlocConstructor,\n BlocGeneric,\n BlocHookDependencyArrayFn,\n BlocInstanceId,\n BlocState,\n InferPropsFromGeneric,\n} from 'blac';\nimport { useId, useLayoutEffect, useMemo, useSyncExternalStore } from 'react';\nimport externalBlocStore from './externalBlocStore';\n\n// export type BlocHookData<B extends BlocBase<S>, S> = [\n// value: ValueType<B>,\n// instance: B,\n// ];\n\ntype HookTypes<B extends BlocConstructor<BlocGeneric>> = [\n BlocState<InstanceType<B>>,\n InstanceType<B>,\n];\n\n// type BlocHookData<B extends BlocBase<S>, S> = [S, InstanceType<B>]; // B is the bloc class, S is the state of the bloc\n// type UseBlocClassResult<B, S> = BlocHookData<B, S>;\n\nexport interface BlocHookOptions<B extends BlocGeneric<any, any>> {\n id?: string;\n dependencySelector?: BlocHookDependencyArrayFn<B>;\n props?: InferPropsFromGeneric<B>;\n}\n\nexport class UseBlocClass {\n static ridUsedKeys: Map<string, Set<string>> = new Map();\n static getUsedKeys = (rid: string): Set<string> => {\n if (!UseBlocClass.ridUsedKeys.has(rid)) {\n UseBlocClass.ridUsedKeys.set(rid, new Set());\n }\n return UseBlocClass.ridUsedKeys.get(rid) as Set<string>;\n };\n\n static useBloc<\n B extends BlocConstructor<BlocGeneric>,\n O extends BlocHookOptions<InstanceType<B>>,\n >(bloc: B, options?: O): HookTypes<B> {\n const rid = useId();\n const usedKeys = UseBlocClass.getUsedKeys(rid);\n\n // default options\n let dependencyArray: BlocHookDependencyArrayFn<InstanceType<B>>
|
|
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): ExternalStore<B, S> => {\n return {\n subscribe: (listener: (state: S) => void) => {\n const unSub = bloc.addSubscriber({\n fn: () => {\n try {\n listener(bloc.state);\n } catch (e) {\n console.error({\n e,\n bloc,\n dependencyArray,\n });\n }\n },\n dependencyArray,\n });\n\n return () => {\n unSub();\n };\n },\n getSnapshot: (): S => bloc.state,\n getServerSnapshot: (): S => bloc.state,\n };\n};\n\nexport default externalBlocStore;\n","import {\n Blac,\n BlocBase,\n BlocConstructor,\n BlocGeneric,\n BlocHookDependencyArrayFn,\n BlocInstanceId,\n BlocState,\n InferPropsFromGeneric,\n} from 'blac';\nimport { useId, useLayoutEffect, useMemo, useSyncExternalStore } from 'react';\nimport externalBlocStore from './externalBlocStore';\n\n// export type BlocHookData<B extends BlocBase<S>, S> = [\n// value: ValueType<B>,\n// instance: B,\n// ];\n\ntype HookTypes<B extends BlocConstructor<BlocGeneric>> = [\n BlocState<InstanceType<B>>,\n InstanceType<B>,\n];\n\n// type BlocHookData<B extends BlocBase<S>, S> = [S, InstanceType<B>]; // B is the bloc class, S is the state of the bloc\n// type UseBlocClassResult<B, S> = BlocHookData<B, S>;\n\nexport interface BlocHookOptions<B extends BlocGeneric<any, any>> {\n id?: string;\n dependencySelector?: BlocHookDependencyArrayFn<B>;\n props?: InferPropsFromGeneric<B>;\n inferStateUsage?: boolean;\n}\n\nexport class UseBlocClass {\n static ridUsedKeys: Map<string, Set<string>> = new Map();\n static getUsedKeys = (rid: string): Set<string> => {\n if (!UseBlocClass.ridUsedKeys.has(rid)) {\n UseBlocClass.ridUsedKeys.set(rid, new Set());\n }\n return UseBlocClass.ridUsedKeys.get(rid) as Set<string>;\n };\n\n static useBloc<\n B extends BlocConstructor<BlocGeneric>,\n O extends BlocHookOptions<InstanceType<B>>,\n >(bloc: B, options?: O): HookTypes<B> {\n const rid = useId();\n const usedKeys = UseBlocClass.getUsedKeys(rid);\n\n // default options\n let dependencyArray: BlocHookDependencyArrayFn<InstanceType<B>> =\n options?.inferStateUsage\n ? (newState) => {\n if (typeof newState !== 'object') {\n return [newState] as unknown[];\n }\n\n const used: string[] = [];\n for (const key of usedKeys) {\n if (key in newState) {\n used.push(newState[key as keyof typeof newState]);\n }\n }\n return used;\n }\n : (s) => [s];\n let blocId: BlocInstanceId | undefined;\n let props: ConstructorParameters<BlocConstructor<B>> | undefined;\n\n if (typeof options === 'object') {\n dependencyArray = options.dependencySelector ?? dependencyArray;\n blocId = options.id;\n props = options.props ?? props;\n }\n\n const base = bloc as unknown as BlocBase<B, O['props']>;\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 const { subscribe, getSnapshot, getServerSnapshot } = useMemo(\n () => externalBlocStore(resolvedBloc, dependencyArray),\n [resolvedBloc.createdAt],\n );\n\n const state = useSyncExternalStore<BlocState<InstanceType<B>>>(\n subscribe,\n getSnapshot,\n getServerSnapshot,\n );\n\n const currentRenderUsedKeys = new Set<string>();\n const returnState: BlocState<InstanceType<B>> = useMemo(() => {\n try {\n if (options?.inferStateUsage && typeof state === 'object') {\n return new Proxy(state as any, {\n get(target, prop) {\n usedKeys.add(prop as string);\n currentRenderUsedKeys.add(prop as string);\n return Reflect.get(target, prop);\n },\n });\n }\n } catch (error) {\n Blac.instance.log('useBloc Error', error);\n }\n return state;\n }, [state]);\n\n useLayoutEffect(() => {\n UseBlocClass.getUsedKeys(rid).clear();\n for (const key of currentRenderUsedKeys) {\n UseBlocClass.getUsedKeys(rid).add(key);\n }\n }, [currentRenderUsedKeys, rid]);\n\n return [returnState, resolvedBloc];\n }\n}\n\nconst useBloc = UseBlocClass.useBloc;\n\nexport default useBloc;\n"],"names":["externalBlocStore","bloc","dependencyArray","listener","unSub","e","_UseBlocClass","options","rid","useId","usedKeys","newState","used","key","s","blocId","props","resolvedBloc","Blac","subscribe","getSnapshot","getServerSnapshot","useMemo","state","useSyncExternalStore","currentRenderUsedKeys","returnState","target","prop","error","useLayoutEffect","__publicField","UseBlocClass","useBloc"],"mappings":";;;;;AAWM,MAAAA,IAAoB,CAIxBC,GACAC,OAEO;AAAA,EACL,WAAW,CAACC,MAAiC;AACrC,UAAAC,IAAQH,EAAK,cAAc;AAAA,MAC/B,IAAI,MAAM;AACJ,YAAA;AACF,UAAAE,EAASF,EAAK,KAAK;AAAA,iBACZI,GAAG;AACV,kBAAQ,MAAM;AAAA,YACZ,GAAAA;AAAA,YACA,MAAAJ;AAAA,YACA,iBAAAC;AAAA,UAAA,CACD;AAAA,QACH;AAAA,MACF;AAAA,MACA,iBAAAA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c;AAAA,YAC7B,IAAII,GAAQC,GAAM;AAChB,qBAAAlB,EAAS,IAAIkB,CAAc,GAC3BH,EAAsB,IAAIG,CAAc,GACjC,QAAQ,IAAID,GAAQC,CAAI;AAAA,YACjC;AAAA,UAAA,CACD;AAAA,eAEIC,GAAO;AACT,QAAAX,EAAA,SAAS,IAAI,iBAAiBW,CAAK;AAAA,MAC1C;AACO,aAAAN;AAAA,IAAA,GACN,CAACA,CAAK,CAAC;AAEV,WAAAO,EAAgB,MAAM;AACP,MAAAxB,EAAA,YAAYE,CAAG,EAAE,MAAM;AACpC,iBAAWK,KAAOY;AAChB,QAAAnB,EAAa,YAAYE,CAAG,EAAE,IAAIK,CAAG;AAAA,IACvC,GACC,CAACY,GAAuBjB,CAAG,CAAC,GAExB,CAACkB,GAAaT,CAAY;AAAA,EACnC;AACF;AA/FEc,EADWzB,GACJ,eAAwC,oBAAI,QACnDyB,EAFWzB,GAEJ,eAAc,CAACE,OACfF,EAAa,YAAY,IAAIE,CAAG,KACnCF,EAAa,YAAY,IAAIE,GAAK,oBAAI,IAAK,CAAA,GAEtCF,EAAa,YAAY,IAAIE,CAAG;AANpC,IAAMwB,IAAN1B;AAkGP,MAAM2B,IAAUD,EAAa;"}
|
package/package.json
CHANGED
package/useBloc.d.ts
CHANGED
|
@@ -8,6 +8,7 @@ export interface BlocHookOptions<B extends BlocGeneric<any, any>> {
|
|
|
8
8
|
id?: string;
|
|
9
9
|
dependencySelector?: BlocHookDependencyArrayFn<B>;
|
|
10
10
|
props?: InferPropsFromGeneric<B>;
|
|
11
|
+
inferStateUsage?: boolean;
|
|
11
12
|
}
|
|
12
13
|
export declare class UseBlocClass {
|
|
13
14
|
static ridUsedKeys: Map<string, Set<string>>;
|