@blac/react 2.0.0 → 2.0.1
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 +207 -129
- package/dist/index.cjs +260 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +165 -0
- package/dist/index.d.ts +143 -77
- package/dist/index.js +257 -0
- package/dist/index.js.map +1 -0
- package/package.json +73 -28
- package/dist/index.cjs.js +0 -2
- package/dist/index.cjs.js.map +0 -1
- package/dist/index.es.js +0 -116
- package/dist/index.es.js.map +0 -1
- package/src/externalBlocStore.ts +0 -93
- package/src/index.ts +0 -4
- package/src/useBloc.tsx +0 -261
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["globalConfig","instance","instanceKey","adapterState","ExternalDepsManager","instanceKey"],"sources":["../src/utils/instance-keys.ts","../src/config.ts","../src/useBloc.ts","../src/useBlocActions.ts"],"sourcesContent":["/**\n * Instance key generation utilities for React integration\n */\n\nimport { generateIsolatedKey } from '@blac/core';\nimport type { ComponentRef } from '../types';\n\n/**\n * Generate an instance key for a bloc\n *\n * Logic:\n * - If user provides instanceId, use it (convert number to string)\n * - If isolated, generate or reuse a unique key for this component\n * - Otherwise, return undefined (use default key)\n *\n * @param componentRef - React component reference (persists across remounts)\n * @param isIsolated - Whether the bloc is isolated\n * @param providedId - User-provided instance ID (from options)\n * @returns Instance key string or undefined for default\n */\nexport function generateInstanceKey(\n componentRef: ComponentRef,\n isIsolated: boolean,\n providedId?: string | number,\n): string | undefined {\n // User explicitly provided an ID - use it\n if (providedId !== undefined) {\n return typeof providedId === 'number' ? String(providedId) : providedId;\n }\n\n // Isolated bloc - generate unique key per component\n if (isIsolated) {\n if (!componentRef.__blocInstanceId) {\n componentRef.__blocInstanceId = generateIsolatedKey();\n }\n return componentRef.__blocInstanceId;\n }\n\n // Shared bloc - use default key (undefined)\n return undefined;\n}\n","/**\n * Global configuration for @blac/react\n */\n\nexport interface BlacReactConfig {\n /** Enable automatic property tracking via Proxy (default: true) */\n autoTrack: boolean;\n}\n\nconst defaultConfig: BlacReactConfig = {\n autoTrack: true,\n};\n\nlet globalConfig: BlacReactConfig = { ...defaultConfig };\n\n/**\n * Configure global defaults for @blac/react hooks.\n *\n * @example\n * ```ts\n * import { configureBlacReact } from '@blac/react';\n *\n * // Disable auto-tracking globally\n * configureBlacReact({\n * autoTrack: false\n * });\n * ```\n *\n * @param config - Partial configuration to merge with defaults\n */\nexport function configureBlacReact(config: Partial<BlacReactConfig>): void {\n globalConfig = { ...globalConfig, ...config };\n}\n\n/**\n * Get the current global configuration.\n * @internal\n */\nexport function getBlacReactConfig(): BlacReactConfig {\n return globalConfig;\n}\n\n/**\n * Reset configuration to defaults (useful for testing).\n * @internal\n */\nexport function resetBlacReactConfig(): void {\n globalConfig = { ...defaultConfig };\n}\n","import {\n useMemo,\n useSyncExternalStore,\n useEffect,\n useRef,\n useReducer,\n} from 'react';\nimport {\n type ExtractState,\n type AdapterState,\n type IsStatelessContainer,\n ExternalDepsManager,\n autoTrackSubscribe,\n manualDepsSubscribe,\n noTrackSubscribe,\n autoTrackSnapshot,\n manualDepsSnapshot,\n noTrackSnapshot,\n autoTrackInit,\n manualDepsInit,\n noTrackInit,\n disableGetterTracking,\n isIsolatedClass,\n isStatelessClass,\n StateContainerConstructor,\n InstanceState,\n acquire,\n release,\n} from '@blac/core';\nimport type { UseBlocOptions, UseBlocReturn, ComponentRef } from './types';\nimport { generateInstanceKey } from './utils/instance-keys';\nimport { getBlacReactConfig } from './config';\n\ninterface TrackingMode {\n useManualDeps: boolean;\n autoTrackEnabled: boolean;\n}\n\nfunction determineTrackingMode<TBloc extends StateContainerConstructor>(\n options?: UseBlocOptions<TBloc>,\n): TrackingMode {\n const globalConfig = getBlacReactConfig();\n const autoTrackEnabled =\n options?.autoTrack !== undefined\n ? options.autoTrack\n : globalConfig.autoTrack;\n\n return {\n useManualDeps: options?.dependencies !== undefined,\n autoTrackEnabled,\n };\n}\n\n/**\n * Type that produces a TypeScript error when a stateless container is used.\n * Evaluates to `never` for stateless containers, causing a type mismatch.\n */\ntype StatefulContainer<T extends StateContainerConstructor> =\n IsStatelessContainer<T> extends true ? never : T;\n\n/**\n * React hook that connects a component to a state container with automatic re-render on state changes.\n *\n * Supports three tracking modes:\n * - **Auto-tracking** (default): Automatically detects accessed state properties via Proxy\n * - **Manual dependencies**: Explicit dependency array like useEffect\n * - **No tracking**: Returns full state without optimization\n *\n * **Note:** This hook does NOT support stateless containers (StatelessCubit, StatelessVertex).\n * Use `useBlocActions()` instead for stateless containers.\n *\n * @template T - The state container constructor type (inferred from BlocClass)\n * @param BlocClass - The state container class to connect to (must not be stateless)\n * @param options - Configuration options for tracking mode and instance management\n * @returns Tuple with [state, bloc instance, ref]\n *\n * @example Basic usage\n * ```ts\n * const [state, myBloc, ref] = useBloc(MyBloc);\n * ```\n *\n * @example With manual dependencies\n * ```ts\n * const [state, myBloc] = useBloc(MyBloc, {\n * dependencies: (state) => [state.count]\n * });\n * ```\n *\n * @example With isolated instance\n * ```ts\n * const [state, myBloc] = useBloc(MyBloc, {\n * instanceId: 'unique-id'\n * });\n * ```\n */\nexport function useBloc<\n T extends StateContainerConstructor = StateContainerConstructor,\n>(\n BlocClass: StatefulContainer<T>,\n options?: UseBlocOptions<T>,\n): UseBlocReturn<T, ExtractState<T>> {\n type TBloc = InstanceState<T>;\n\n const componentRef = useRef<ComponentRef>({});\n const initialPropsRef = useRef(options?.props);\n const isStateless = isStatelessClass(BlocClass);\n const isIsolated = isIsolatedClass(BlocClass);\n\n const [bloc, subscribe, getSnapshot, instanceKey, adapterState, rawInstance] =\n useMemo<\n readonly [\n TBloc,\n (callback: () => void) => () => void,\n () => ExtractState<T>,\n string | undefined,\n AdapterState<T> | null,\n TBloc,\n ]\n >(() => {\n // Runtime check for stateless containers - log error but don't crash\n if (isStateless) {\n console.error(\n `[BlaC] useBloc() does not support stateless containers. ` +\n `\"${BlocClass.name}\" is a StatelessCubit or StatelessVertex. ` +\n `Use useBlocActions() instead for stateless containers.`,\n );\n const instance = acquire(BlocClass, 'default') as TBloc;\n // Return minimal working functions that won't subscribe to anything\n return [\n instance,\n () => () => {},\n () => ({}) as ExtractState<T>,\n undefined,\n null,\n instance,\n ];\n }\n\n const instanceKey = generateInstanceKey(\n componentRef.current,\n isIsolated,\n options?.instanceId,\n );\n\n const instance = acquire(BlocClass, instanceKey, {\n props: initialPropsRef.current,\n }) as TBloc;\n\n if (initialPropsRef.current !== undefined) {\n instance.updateProps(initialPropsRef.current);\n }\n\n const { useManualDeps, autoTrackEnabled } =\n determineTrackingMode(options);\n\n let subscribeFn: (callback: () => void) => () => void;\n let getSnapshotFn: () => ExtractState<T>;\n let adapterState: AdapterState<T>;\n\n if (useManualDeps && options?.dependencies) {\n adapterState = manualDepsInit(instance);\n subscribeFn = manualDepsSubscribe(instance, adapterState, {\n dependencies: options.dependencies,\n });\n getSnapshotFn = manualDepsSnapshot(instance, adapterState, {\n dependencies: options.dependencies,\n });\n } else if (!autoTrackEnabled) {\n adapterState = noTrackInit(instance);\n subscribeFn = noTrackSubscribe(instance);\n getSnapshotFn = noTrackSnapshot(instance);\n } else {\n adapterState = autoTrackInit(instance);\n subscribeFn = autoTrackSubscribe(instance, adapterState);\n getSnapshotFn = autoTrackSnapshot(instance, adapterState);\n }\n\n return [\n adapterState.proxiedBloc as TBloc,\n subscribeFn,\n getSnapshotFn,\n instanceKey,\n adapterState,\n instance,\n ];\n }, [BlocClass, isStateless, isIsolated, options?.instanceId]);\n\n const state = useSyncExternalStore(subscribe, getSnapshot);\n\n const [, forceUpdate] = useReducer((x: number) => x + 1, 0);\n\n const externalDepsManager = useRef(new ExternalDepsManager());\n\n useEffect(() => {\n if (isStateless) return;\n if (options?.props !== initialPropsRef.current) {\n rawInstance.updateProps(options?.props);\n }\n }, [options?.props, rawInstance, isStateless]);\n\n useEffect(() => {\n if (isStateless || !adapterState) return;\n disableGetterTracking(adapterState, rawInstance);\n externalDepsManager.current.updateSubscriptions(\n adapterState.getterState,\n rawInstance,\n forceUpdate,\n );\n });\n\n useEffect(() => {\n if (isStateless) return;\n\n if (options?.onMount) {\n options.onMount(bloc as InstanceType<T>);\n }\n\n return () => {\n externalDepsManager.current.cleanup();\n\n if (options?.onUnmount) {\n options.onUnmount(bloc as InstanceType<T>);\n }\n\n release(BlocClass, instanceKey);\n\n if (isIsolated && !rawInstance.isDisposed) {\n rawInstance.dispose();\n }\n };\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, []);\n\n return [state, bloc, componentRef] as UseBlocReturn<T, ExtractState<T>>;\n}\n","import { useMemo, useEffect, useRef } from 'react';\nimport {\n StateContainerConstructor,\n isIsolatedClass,\n acquire,\n release,\n} from '@blac/core';\nimport type { ComponentRef } from './types';\nimport { generateInstanceKey } from './utils/instance-keys';\n\n/**\n * Configuration options for useBlocActions hook\n * @template TBloc - The state container type\n * @template TProps - Props type passed to the container\n */\nexport interface UseBlocActionsOptions<TBloc, TProps = any> {\n /** Props passed to bloc constructor or updateProps */\n props?: TProps;\n /** Custom instance identifier for shared or isolated instances */\n instanceId?: string | number;\n /** Callback invoked when bloc instance mounts */\n onMount?: (bloc: TBloc) => void;\n /** Callback invoked when bloc instance unmounts */\n onUnmount?: (bloc: TBloc) => void;\n}\n\n/**\n * React hook that connects to a state container instance without triggering re-renders.\n * Use this when you only need to call actions on the bloc without subscribing to state changes.\n *\n * **This is the recommended hook for stateless containers** (StatelessCubit, StatelessVertex).\n * It also works with regular Cubit/Vertex when you don't need state subscriptions.\n *\n * @template T - The state container constructor type (inferred from BlocClass)\n * @param BlocClass - The state container class to connect to (supports both stateful and stateless)\n * @param options - Configuration options for instance management and lifecycle\n * @returns The state container instance for calling actions\n *\n * @example Basic usage with stateless container\n * ```ts\n * class AnalyticsService extends StatelessCubit {\n * trackEvent(name: string) { ... }\n * }\n *\n * const analytics = useBlocActions(AnalyticsService);\n * analytics.trackEvent('page_view');\n * ```\n *\n * @example With stateful container (no re-renders)\n * ```ts\n * const myBloc = useBlocActions(MyBloc);\n * myBloc.someMethod(); // Won't cause re-renders\n * ```\n *\n * @example With isolated instance\n * ```ts\n * const myBloc = useBlocActions(MyBloc, {\n * instanceId: 'unique-id'\n * });\n * ```\n */\nexport function useBlocActions<\n T extends StateContainerConstructor = StateContainerConstructor,\n>(\n BlocClass: T,\n options?: UseBlocActionsOptions<InstanceType<T>>,\n): InstanceType<T> {\n const componentRef = useRef<ComponentRef>({});\n const initialPropsRef = useRef(options?.props);\n\n const [bloc, instanceKey] = useMemo(() => {\n const isIsolated = isIsolatedClass(BlocClass);\n\n const instanceKey = generateInstanceKey(\n componentRef.current,\n isIsolated,\n options?.instanceId,\n );\n\n const instance = acquire(BlocClass, instanceKey, {\n props: initialPropsRef.current,\n });\n\n return [instance, instanceKey] as const;\n }, [BlocClass]);\n\n useEffect(() => {\n if (options?.onMount) {\n options.onMount(bloc);\n }\n\n return () => {\n if (options?.onUnmount) {\n options.onUnmount(bloc);\n }\n\n release(BlocClass, instanceKey);\n\n if (isIsolatedClass(BlocClass) && !bloc.isDisposed) {\n bloc.dispose();\n }\n };\n }, []);\n\n return bloc as InstanceType<T>;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAoBA,SAAgB,oBACd,cACA,YACA,YACoB;AAEpB,KAAI,eAAe,OACjB,QAAO,OAAO,eAAe,WAAW,OAAO,WAAW,GAAG;AAI/D,KAAI,YAAY;AACd,MAAI,CAAC,aAAa,iBAChB,cAAa,wDAAwC;AAEvD,SAAO,aAAa;;;;;;AC1BxB,MAAM,gBAAiC,EACrC,WAAW,MACZ;AAED,IAAI,eAAgC,EAAE,GAAG,eAAe;;;;;;;;;;;;;;;;AAiBxD,SAAgB,mBAAmB,QAAwC;AACzE,gBAAe;EAAE,GAAG;EAAc,GAAG;EAAQ;;;;;;AAO/C,SAAgB,qBAAsC;AACpD,QAAO;;;;;;AAOT,SAAgB,uBAA6B;AAC3C,gBAAe,EAAE,GAAG,eAAe;;;;;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AY;AAE/B,uCAAoB,UAAU,IAAI,CAAC,KAAK,WACtC,MAAK,SAAS;;IAGjB,EAAE,CAAC;AAEN,QAAO"}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import type { ExtractProps } from '@blac/core';
|
|
2
|
+
import { ExtractState } from '@blac/core';
|
|
3
|
+
import { InstanceReadonlyState } from '@blac/core';
|
|
4
|
+
import { IsStatelessContainer } from '@blac/core';
|
|
5
|
+
import type { RefObject } from 'react';
|
|
6
|
+
import { StateContainerConstructor } from '@blac/core';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Global configuration for @blac/react
|
|
10
|
+
*/
|
|
11
|
+
export declare interface BlacReactConfig {
|
|
12
|
+
/** Enable automatic property tracking via Proxy (default: true) */
|
|
13
|
+
autoTrack: boolean;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/* Excluded from this release type: ComponentRef */
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Configure global defaults for @blac/react hooks.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* import { configureBlacReact } from '@blac/react';
|
|
24
|
+
*
|
|
25
|
+
* // Disable auto-tracking globally
|
|
26
|
+
* configureBlacReact({
|
|
27
|
+
* autoTrack: false
|
|
28
|
+
* });
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* @param config - Partial configuration to merge with defaults
|
|
32
|
+
*/
|
|
33
|
+
export declare function configureBlacReact(config: Partial<BlacReactConfig>): void;
|
|
34
|
+
|
|
35
|
+
/* Excluded from this release type: resetBlacReactConfig */
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Type that produces a TypeScript error when a stateless container is used.
|
|
39
|
+
* Evaluates to `never` for stateless containers, causing a type mismatch.
|
|
40
|
+
*/
|
|
41
|
+
declare type StatefulContainer<T extends StateContainerConstructor> = IsStatelessContainer<T> extends true ? never : T;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* React hook that connects a component to a state container with automatic re-render on state changes.
|
|
45
|
+
*
|
|
46
|
+
* Supports three tracking modes:
|
|
47
|
+
* - **Auto-tracking** (default): Automatically detects accessed state properties via Proxy
|
|
48
|
+
* - **Manual dependencies**: Explicit dependency array like useEffect
|
|
49
|
+
* - **No tracking**: Returns full state without optimization
|
|
50
|
+
*
|
|
51
|
+
* **Note:** This hook does NOT support stateless containers (StatelessCubit, StatelessVertex).
|
|
52
|
+
* Use `useBlocActions()` instead for stateless containers.
|
|
53
|
+
*
|
|
54
|
+
* @template T - The state container constructor type (inferred from BlocClass)
|
|
55
|
+
* @param BlocClass - The state container class to connect to (must not be stateless)
|
|
56
|
+
* @param options - Configuration options for tracking mode and instance management
|
|
57
|
+
* @returns Tuple with [state, bloc instance, ref]
|
|
58
|
+
*
|
|
59
|
+
* @example Basic usage
|
|
60
|
+
* ```ts
|
|
61
|
+
* const [state, myBloc, ref] = useBloc(MyBloc);
|
|
62
|
+
* ```
|
|
63
|
+
*
|
|
64
|
+
* @example With manual dependencies
|
|
65
|
+
* ```ts
|
|
66
|
+
* const [state, myBloc] = useBloc(MyBloc, {
|
|
67
|
+
* dependencies: (state) => [state.count]
|
|
68
|
+
* });
|
|
69
|
+
* ```
|
|
70
|
+
*
|
|
71
|
+
* @example With isolated instance
|
|
72
|
+
* ```ts
|
|
73
|
+
* const [state, myBloc] = useBloc(MyBloc, {
|
|
74
|
+
* instanceId: 'unique-id'
|
|
75
|
+
* });
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
export declare function useBloc<T extends StateContainerConstructor = StateContainerConstructor>(BlocClass: StatefulContainer<T>, options?: UseBlocOptions<T>): UseBlocReturn<T, ExtractState<T>>;
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* React hook that connects to a state container instance without triggering re-renders.
|
|
82
|
+
* Use this when you only need to call actions on the bloc without subscribing to state changes.
|
|
83
|
+
*
|
|
84
|
+
* **This is the recommended hook for stateless containers** (StatelessCubit, StatelessVertex).
|
|
85
|
+
* It also works with regular Cubit/Vertex when you don't need state subscriptions.
|
|
86
|
+
*
|
|
87
|
+
* @template T - The state container constructor type (inferred from BlocClass)
|
|
88
|
+
* @param BlocClass - The state container class to connect to (supports both stateful and stateless)
|
|
89
|
+
* @param options - Configuration options for instance management and lifecycle
|
|
90
|
+
* @returns The state container instance for calling actions
|
|
91
|
+
*
|
|
92
|
+
* @example Basic usage with stateless container
|
|
93
|
+
* ```ts
|
|
94
|
+
* class AnalyticsService extends StatelessCubit {
|
|
95
|
+
* trackEvent(name: string) { ... }
|
|
96
|
+
* }
|
|
97
|
+
*
|
|
98
|
+
* const analytics = useBlocActions(AnalyticsService);
|
|
99
|
+
* analytics.trackEvent('page_view');
|
|
100
|
+
* ```
|
|
101
|
+
*
|
|
102
|
+
* @example With stateful container (no re-renders)
|
|
103
|
+
* ```ts
|
|
104
|
+
* const myBloc = useBlocActions(MyBloc);
|
|
105
|
+
* myBloc.someMethod(); // Won't cause re-renders
|
|
106
|
+
* ```
|
|
107
|
+
*
|
|
108
|
+
* @example With isolated instance
|
|
109
|
+
* ```ts
|
|
110
|
+
* const myBloc = useBlocActions(MyBloc, {
|
|
111
|
+
* instanceId: 'unique-id'
|
|
112
|
+
* });
|
|
113
|
+
* ```
|
|
114
|
+
*/
|
|
115
|
+
export declare function useBlocActions<T extends StateContainerConstructor = StateContainerConstructor>(BlocClass: T, options?: UseBlocActionsOptions<InstanceType<T>>): InstanceType<T>;
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Configuration options for useBlocActions hook
|
|
119
|
+
* @template TBloc - The state container type
|
|
120
|
+
* @template TProps - Props type passed to the container
|
|
121
|
+
*/
|
|
122
|
+
export declare interface UseBlocActionsOptions<TBloc, TProps = any> {
|
|
123
|
+
/** Props passed to bloc constructor or updateProps */
|
|
124
|
+
props?: TProps;
|
|
125
|
+
/** Custom instance identifier for shared or isolated instances */
|
|
126
|
+
instanceId?: string | number;
|
|
127
|
+
/** Callback invoked when bloc instance mounts */
|
|
128
|
+
onMount?: (bloc: TBloc) => void;
|
|
129
|
+
/** Callback invoked when bloc instance unmounts */
|
|
130
|
+
onUnmount?: (bloc: TBloc) => void;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Configuration options for useBloc hook
|
|
135
|
+
* @template TBloc - The state container type
|
|
136
|
+
* @template TProps - Props type passed to the container
|
|
137
|
+
*/
|
|
138
|
+
export declare interface UseBlocOptions<TBloc extends StateContainerConstructor, TProps = ExtractProps<TBloc>> {
|
|
139
|
+
/** Props passed to bloc constructor or updateProps */
|
|
140
|
+
props?: TProps;
|
|
141
|
+
/** Custom instance identifier for shared or isolated instances */
|
|
142
|
+
instanceId?: string | number;
|
|
143
|
+
/** Manual dependency array like useEffect (disables autoTrack) */
|
|
144
|
+
dependencies?: (state: ExtractState<TBloc>, bloc: InstanceReadonlyState<TBloc>) => unknown[];
|
|
145
|
+
/** Enable automatic property tracking via Proxy (default: true) */
|
|
146
|
+
autoTrack?: boolean;
|
|
147
|
+
/** Disable caching for getter tracking */
|
|
148
|
+
disableGetterCache?: boolean;
|
|
149
|
+
/** Callback invoked when bloc instance mounts */
|
|
150
|
+
onMount?: (bloc: InstanceType<TBloc>) => void;
|
|
151
|
+
/** Callback invoked when bloc instance unmounts */
|
|
152
|
+
onUnmount?: (bloc: InstanceType<TBloc>) => void;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Tuple return type from useBloc hook containing state, bloc instance, and ref
|
|
157
|
+
* - [0] Current state value (with optional state type override)
|
|
158
|
+
* - [1] State container instance (bloc) for calling actions
|
|
159
|
+
* - [2] Ref object for accessing component ref (advanced use cases)
|
|
160
|
+
*
|
|
161
|
+
* @template TBloc - The state container type
|
|
162
|
+
*/
|
|
163
|
+
export declare type UseBlocReturn<TBloc extends StateContainerConstructor, S = ExtractState<TBloc>> = [S, InstanceReadonlyState<TBloc>, RefObject<ComponentRef>];
|
|
164
|
+
|
|
165
|
+
export { }
|
package/dist/index.d.ts
CHANGED
|
@@ -1,99 +1,165 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
1
|
+
import type { ExtractProps } from '@blac/core';
|
|
2
|
+
import { ExtractState } from '@blac/core';
|
|
3
|
+
import { InstanceReadonlyState } from '@blac/core';
|
|
4
|
+
import { IsStatelessContainer } from '@blac/core';
|
|
5
|
+
import type { RefObject } from 'react';
|
|
6
|
+
import { StateContainerConstructor } from '@blac/core';
|
|
6
7
|
|
|
7
8
|
/**
|
|
8
|
-
*
|
|
9
|
-
* @template B - Bloc generic type
|
|
10
|
-
* @property {string} [id] - Optional identifier for the Bloc instance
|
|
11
|
-
* @property {BlocHookDependencyArrayFn<B>} [dependencySelector] - Function to select dependencies for re-renders
|
|
12
|
-
* @property {InferPropsFromGeneric<B>} [props] - Props to pass to the Bloc
|
|
13
|
-
* @property {(bloc: B) => void} [onMount] - Callback function invoked when the Bloc is mounted
|
|
9
|
+
* Global configuration for @blac/react
|
|
14
10
|
*/
|
|
15
|
-
declare interface
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
props?: InferPropsFromGeneric<B>;
|
|
19
|
-
onMount?: (bloc: B) => void;
|
|
11
|
+
export declare interface BlacReactConfig {
|
|
12
|
+
/** Enable automatic property tracking via Proxy (default: true) */
|
|
13
|
+
autoTrack: boolean;
|
|
20
14
|
}
|
|
21
15
|
|
|
22
|
-
|
|
23
|
-
* Creates an external store that wraps a Bloc instance, providing a React-compatible interface
|
|
24
|
-
* for subscribing to and accessing bloc state.
|
|
25
|
-
*
|
|
26
|
-
* @template B - The type of the Bloc instance
|
|
27
|
-
* @template S - The type of the Bloc state
|
|
28
|
-
* @param bloc - The Bloc instance to wrap
|
|
29
|
-
* @param dependencyArray - Function that returns an array of dependencies for the subscription
|
|
30
|
-
* @param rid - Unique identifier for the subscription
|
|
31
|
-
* @returns An ExternalStore instance that provides methods to subscribe to and access bloc state
|
|
32
|
-
*/
|
|
33
|
-
export declare const externalBlocStore: <B extends InstanceType<BlocConstructor<any>>>(resolvedBloc: B, dependencyArray: BlocHookDependencyArrayFn<BlocState<B>>, rid: string) => ExternalStore<B>;
|
|
16
|
+
/* Excluded from this release type: ComponentRef */
|
|
34
17
|
|
|
35
18
|
/**
|
|
36
|
-
*
|
|
37
|
-
*
|
|
19
|
+
* Configure global defaults for @blac/react hooks.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* import { configureBlacReact } from '@blac/react';
|
|
38
24
|
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
25
|
+
* // Disable auto-tracking globally
|
|
26
|
+
* configureBlacReact({
|
|
27
|
+
* autoTrack: false
|
|
28
|
+
* });
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* @param config - Partial configuration to merge with defaults
|
|
41
32
|
*/
|
|
42
|
-
declare
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
* @param onStoreChange - Callback function that will be called whenever the store changes
|
|
46
|
-
* @returns A function that can be called to unsubscribe from store changes
|
|
47
|
-
*/
|
|
48
|
-
subscribe: (onStoreChange: (state: BlocState<B>) => void) => () => void;
|
|
49
|
-
/**
|
|
50
|
-
* Gets the current snapshot of the store state.
|
|
51
|
-
* @returns The current state of the store
|
|
52
|
-
*/
|
|
53
|
-
getSnapshot: () => BlocState<B>;
|
|
54
|
-
/**
|
|
55
|
-
* Gets the server snapshot of the store state.
|
|
56
|
-
* This is optional and defaults to the same value as getSnapshot.
|
|
57
|
-
* @returns The server state of the store
|
|
58
|
-
*/
|
|
59
|
-
getServerSnapshot?: () => BlocState<B>;
|
|
60
|
-
}
|
|
33
|
+
export declare function configureBlacReact(config: Partial<BlacReactConfig>): void;
|
|
34
|
+
|
|
35
|
+
/* Excluded from this release type: resetBlacReactConfig */
|
|
61
36
|
|
|
62
37
|
/**
|
|
63
|
-
* Type
|
|
64
|
-
*
|
|
38
|
+
* Type that produces a TypeScript error when a stateless container is used.
|
|
39
|
+
* Evaluates to `never` for stateless containers, causing a type mismatch.
|
|
65
40
|
*/
|
|
66
|
-
declare type
|
|
67
|
-
BlocState<InstanceType<B>>,
|
|
68
|
-
InstanceType<B>
|
|
69
|
-
];
|
|
41
|
+
declare type StatefulContainer<T extends StateContainerConstructor> = IsStatelessContainer<T> extends true ? never : T;
|
|
70
42
|
|
|
71
43
|
/**
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
*
|
|
44
|
+
* React hook that connects a component to a state container with automatic re-render on state changes.
|
|
45
|
+
*
|
|
46
|
+
* Supports three tracking modes:
|
|
47
|
+
* - **Auto-tracking** (default): Automatically detects accessed state properties via Proxy
|
|
48
|
+
* - **Manual dependencies**: Explicit dependency array like useEffect
|
|
49
|
+
* - **No tracking**: Returns full state without optimization
|
|
50
|
+
*
|
|
51
|
+
* **Note:** This hook does NOT support stateless containers (StatelessCubit, StatelessVertex).
|
|
52
|
+
* Use `useBlocActions()` instead for stateless containers.
|
|
53
|
+
*
|
|
54
|
+
* @template T - The state container constructor type (inferred from BlocClass)
|
|
55
|
+
* @param BlocClass - The state container class to connect to (must not be stateless)
|
|
56
|
+
* @param options - Configuration options for tracking mode and instance management
|
|
57
|
+
* @returns Tuple with [state, bloc instance, ref]
|
|
58
|
+
*
|
|
59
|
+
* @example Basic usage
|
|
60
|
+
* ```ts
|
|
61
|
+
* const [state, myBloc, ref] = useBloc(MyBloc);
|
|
62
|
+
* ```
|
|
63
|
+
*
|
|
64
|
+
* @example With manual dependencies
|
|
65
|
+
* ```ts
|
|
66
|
+
* const [state, myBloc] = useBloc(MyBloc, {
|
|
67
|
+
* dependencies: (state) => [state.count]
|
|
68
|
+
* });
|
|
69
|
+
* ```
|
|
70
|
+
*
|
|
71
|
+
* @example With isolated instance
|
|
72
|
+
* ```ts
|
|
73
|
+
* const [state, myBloc] = useBloc(MyBloc, {
|
|
74
|
+
* instanceId: 'unique-id'
|
|
75
|
+
* });
|
|
76
|
+
* ```
|
|
76
77
|
*/
|
|
78
|
+
export declare function useBloc<T extends StateContainerConstructor = StateContainerConstructor>(BlocClass: StatefulContainer<T>, options?: UseBlocOptions<T>): UseBlocReturn<T, ExtractState<T>>;
|
|
79
|
+
|
|
77
80
|
/**
|
|
78
|
-
* React hook
|
|
81
|
+
* React hook that connects to a state container instance without triggering re-renders.
|
|
82
|
+
* Use this when you only need to call actions on the bloc without subscribing to state changes.
|
|
79
83
|
*
|
|
80
|
-
* This
|
|
81
|
-
*
|
|
82
|
-
* and proper lifecycle management.
|
|
84
|
+
* **This is the recommended hook for stateless containers** (StatelessCubit, StatelessVertex).
|
|
85
|
+
* It also works with regular Cubit/Vertex when you don't need state subscriptions.
|
|
83
86
|
*
|
|
84
|
-
* @template
|
|
85
|
-
* @
|
|
86
|
-
* @param
|
|
87
|
-
* @
|
|
88
|
-
* @returns {HookTypes<B>} Tuple containing [state, bloc instance]
|
|
87
|
+
* @template T - The state container constructor type (inferred from BlocClass)
|
|
88
|
+
* @param BlocClass - The state container class to connect to (supports both stateful and stateless)
|
|
89
|
+
* @param options - Configuration options for instance management and lifecycle
|
|
90
|
+
* @returns The state container instance for calling actions
|
|
89
91
|
*
|
|
90
|
-
* @example
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
*
|
|
95
|
-
*
|
|
92
|
+
* @example Basic usage with stateless container
|
|
93
|
+
* ```ts
|
|
94
|
+
* class AnalyticsService extends StatelessCubit {
|
|
95
|
+
* trackEvent(name: string) { ... }
|
|
96
|
+
* }
|
|
97
|
+
*
|
|
98
|
+
* const analytics = useBlocActions(AnalyticsService);
|
|
99
|
+
* analytics.trackEvent('page_view');
|
|
100
|
+
* ```
|
|
101
|
+
*
|
|
102
|
+
* @example With stateful container (no re-renders)
|
|
103
|
+
* ```ts
|
|
104
|
+
* const myBloc = useBlocActions(MyBloc);
|
|
105
|
+
* myBloc.someMethod(); // Won't cause re-renders
|
|
106
|
+
* ```
|
|
107
|
+
*
|
|
108
|
+
* @example With isolated instance
|
|
109
|
+
* ```ts
|
|
110
|
+
* const myBloc = useBlocActions(MyBloc, {
|
|
111
|
+
* instanceId: 'unique-id'
|
|
112
|
+
* });
|
|
113
|
+
* ```
|
|
114
|
+
*/
|
|
115
|
+
export declare function useBlocActions<T extends StateContainerConstructor = StateContainerConstructor>(BlocClass: T, options?: UseBlocActionsOptions<InstanceType<T>>): InstanceType<T>;
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Configuration options for useBlocActions hook
|
|
119
|
+
* @template TBloc - The state container type
|
|
120
|
+
* @template TProps - Props type passed to the container
|
|
121
|
+
*/
|
|
122
|
+
export declare interface UseBlocActionsOptions<TBloc, TProps = any> {
|
|
123
|
+
/** Props passed to bloc constructor or updateProps */
|
|
124
|
+
props?: TProps;
|
|
125
|
+
/** Custom instance identifier for shared or isolated instances */
|
|
126
|
+
instanceId?: string | number;
|
|
127
|
+
/** Callback invoked when bloc instance mounts */
|
|
128
|
+
onMount?: (bloc: TBloc) => void;
|
|
129
|
+
/** Callback invoked when bloc instance unmounts */
|
|
130
|
+
onUnmount?: (bloc: TBloc) => void;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Configuration options for useBloc hook
|
|
135
|
+
* @template TBloc - The state container type
|
|
136
|
+
* @template TProps - Props type passed to the container
|
|
137
|
+
*/
|
|
138
|
+
export declare interface UseBlocOptions<TBloc extends StateContainerConstructor, TProps = ExtractProps<TBloc>> {
|
|
139
|
+
/** Props passed to bloc constructor or updateProps */
|
|
140
|
+
props?: TProps;
|
|
141
|
+
/** Custom instance identifier for shared or isolated instances */
|
|
142
|
+
instanceId?: string | number;
|
|
143
|
+
/** Manual dependency array like useEffect (disables autoTrack) */
|
|
144
|
+
dependencies?: (state: ExtractState<TBloc>, bloc: InstanceReadonlyState<TBloc>) => unknown[];
|
|
145
|
+
/** Enable automatic property tracking via Proxy (default: true) */
|
|
146
|
+
autoTrack?: boolean;
|
|
147
|
+
/** Disable caching for getter tracking */
|
|
148
|
+
disableGetterCache?: boolean;
|
|
149
|
+
/** Callback invoked when bloc instance mounts */
|
|
150
|
+
onMount?: (bloc: InstanceType<TBloc>) => void;
|
|
151
|
+
/** Callback invoked when bloc instance unmounts */
|
|
152
|
+
onUnmount?: (bloc: InstanceType<TBloc>) => void;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Tuple return type from useBloc hook containing state, bloc instance, and ref
|
|
157
|
+
* - [0] Current state value (with optional state type override)
|
|
158
|
+
* - [1] State container instance (bloc) for calling actions
|
|
159
|
+
* - [2] Ref object for accessing component ref (advanced use cases)
|
|
160
|
+
*
|
|
161
|
+
* @template TBloc - The state container type
|
|
96
162
|
*/
|
|
97
|
-
export declare
|
|
163
|
+
export declare type UseBlocReturn<TBloc extends StateContainerConstructor, S = ExtractState<TBloc>> = [S, InstanceReadonlyState<TBloc>, RefObject<ComponentRef>];
|
|
98
164
|
|
|
99
165
|
export { }
|