@blac/react 2.0.12 → 2.0.14
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 +162 -8
- package/dist/BlocProvider.d.ts +33 -0
- package/dist/config.d.ts +10 -15
- package/dist/index.cjs +136 -140
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +136 -118
- package/dist/index.js.map +1 -1
- package/dist/testing.cjs +20 -4
- package/dist/testing.cjs.map +1 -1
- package/dist/testing.js +20 -3
- package/dist/testing.js.map +1 -1
- package/dist/types.d.ts +47 -17
- package/dist/useBloc.d.ts +27 -16
- package/dist/utils/instance-keys.d.ts +3 -1
- package/package.json +4 -4
package/dist/types.d.ts
CHANGED
|
@@ -1,33 +1,63 @@
|
|
|
1
|
-
import type { ExtractState, StateContainerConstructor } from '@blac/core';
|
|
2
|
-
import { InstanceReadonlyState } from '@blac/core';
|
|
1
|
+
import type { ExtractArgs, ExtractState, InstanceReadonlyState, StateContainerConstructor } from '@blac/core';
|
|
3
2
|
import type { RefObject } from 'react';
|
|
4
3
|
/**
|
|
5
|
-
*
|
|
6
|
-
*
|
|
4
|
+
* Conditional `args` field: required when the bloc declares Args, forbidden
|
|
5
|
+
* (type `never`) when the bloc uses the default `void` Args.
|
|
7
6
|
*/
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
type ArgsOption<T extends StateContainerConstructor> = ExtractArgs<T> extends void ? {
|
|
8
|
+
args?: never;
|
|
9
|
+
} : {
|
|
10
|
+
args: ExtractArgs<T>;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Configuration options for {@link useBloc}.
|
|
14
|
+
*
|
|
15
|
+
* @template TBloc - The state container constructor type
|
|
16
|
+
*
|
|
17
|
+
* @remarks
|
|
18
|
+
* The `dependencies` option from BlaC v1 has been removed — use `select`
|
|
19
|
+
* instead. Codemod: rename `dependencies` → `select`; the return value is
|
|
20
|
+
* still a tuple/array compared per-index via `Object.is`.
|
|
21
|
+
*/
|
|
22
|
+
export type UseBlocOptions<TBloc extends StateContainerConstructor> = ArgsOption<TBloc> & {
|
|
23
|
+
/**
|
|
24
|
+
* Per-consumer instance identifier. Overrides any `instanceId` provided
|
|
25
|
+
* by an ancestor {@link BlocProvider}.
|
|
26
|
+
*/
|
|
10
27
|
instanceId?: string | number;
|
|
11
|
-
/**
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
28
|
+
/**
|
|
29
|
+
* Per-consumer re-render selector. When provided, the hook re-renders
|
|
30
|
+
* only when the returned array's elements change (Object.is per index).
|
|
31
|
+
* When omitted, auto-tracking is used: any state path read during the
|
|
32
|
+
* render is observed, and the hook re-renders when any of those paths
|
|
33
|
+
* change.
|
|
34
|
+
*
|
|
35
|
+
* Keep the selector referentially stable across renders (e.g. via
|
|
36
|
+
* `useCallback`) — passing a fresh function each render forces the
|
|
37
|
+
* subscription to re-key, which the underlying channel treats as a new
|
|
38
|
+
* consumer.
|
|
39
|
+
*/
|
|
40
|
+
select?: (state: ExtractState<TBloc>, bloc: InstanceReadonlyState<TBloc>) => unknown[];
|
|
41
|
+
/** Callback invoked when this consumer mounts. */
|
|
16
42
|
onMount?: (bloc: InstanceType<TBloc>) => void;
|
|
17
|
-
/**
|
|
43
|
+
/**
|
|
44
|
+
* Callback invoked when this consumer unmounts. Fires *before* the
|
|
45
|
+
* registry releases its ref, so the bloc is still alive when this runs.
|
|
46
|
+
*/
|
|
18
47
|
onUnmount?: (bloc: InstanceType<TBloc>) => void;
|
|
19
|
-
}
|
|
48
|
+
};
|
|
20
49
|
/**
|
|
21
|
-
* Tuple return type from useBloc
|
|
22
|
-
* - [0] Current state value
|
|
50
|
+
* Tuple return type from {@link useBloc}.
|
|
51
|
+
* - [0] Current state value
|
|
23
52
|
* - [1] State container instance (bloc) for calling actions
|
|
24
53
|
* - [2] Ref object for accessing component ref (advanced use cases)
|
|
25
54
|
*
|
|
26
|
-
* @template TBloc - The state container type
|
|
55
|
+
* @template TBloc - The state container constructor type
|
|
27
56
|
*/
|
|
28
57
|
export type UseBlocReturn<TBloc extends StateContainerConstructor, S = ExtractState<TBloc>> = [S, InstanceReadonlyState<TBloc>, RefObject<ComponentRef>];
|
|
29
58
|
/**
|
|
30
|
-
* Internal ref structure for component-bloc binding
|
|
59
|
+
* Internal ref structure for component-bloc binding.
|
|
31
60
|
* @internal
|
|
32
61
|
*/
|
|
33
62
|
export type ComponentRef = Record<string, never>;
|
|
63
|
+
export {};
|
package/dist/useBloc.d.ts
CHANGED
|
@@ -1,35 +1,46 @@
|
|
|
1
|
-
import { type ExtractState, type StateContainerConstructor } from '@blac/
|
|
1
|
+
import { type ExtractState, type StateContainerConstructor } from '@blac/core';
|
|
2
2
|
import type { UseBlocOptions, UseBlocReturn } from './types';
|
|
3
3
|
/**
|
|
4
|
-
* React hook that connects a component to a state container with automatic
|
|
4
|
+
* React hook that connects a component to a state container with automatic
|
|
5
|
+
* re-render on state changes.
|
|
5
6
|
*
|
|
6
|
-
*
|
|
7
|
-
* - **Auto-tracking** (default):
|
|
8
|
-
*
|
|
9
|
-
*
|
|
7
|
+
* Two tracking modes:
|
|
8
|
+
* - **Auto-tracking** (default): the returned state value is a proxy that
|
|
9
|
+
* records read paths during render. The component re-renders when any
|
|
10
|
+
* recorded path changes. Backed by `@dirtytalk/structural`'s
|
|
11
|
+
* {@link trackRender} + the container's path-scoped `DirtyChannel`.
|
|
12
|
+
* - **Manual select**: pass `options.select` to opt out of auto-tracking.
|
|
13
|
+
* The hook re-renders only when the returned array's elements change
|
|
14
|
+
* (per-index `Object.is`). This replaces the v1 `dependencies` option.
|
|
15
|
+
*
|
|
16
|
+
* Lifecycle:
|
|
17
|
+
* - The bloc is acquired from the registry on mount and released on
|
|
18
|
+
* unmount. The instance key is the explicit `options.instanceId`, then
|
|
19
|
+
* the surrounding {@link BlocProvider} context, then the default key.
|
|
20
|
+
* - `options.onMount` fires after the bloc is acquired; `options.onUnmount`
|
|
21
|
+
* fires *before* the registry releases its ref, so the bloc is still
|
|
22
|
+
* alive when the callback runs.
|
|
10
23
|
*
|
|
11
24
|
* @template T - The state container constructor type (inferred from BlocClass)
|
|
12
25
|
* @param BlocClass - The state container class to connect to
|
|
13
|
-
* @param options - Configuration options
|
|
14
|
-
* @returns Tuple
|
|
26
|
+
* @param options - Configuration options
|
|
27
|
+
* @returns Tuple of `[state, bloc, ref]`
|
|
15
28
|
*
|
|
16
29
|
* @example Basic usage
|
|
17
30
|
* ```ts
|
|
18
|
-
* const [state,
|
|
31
|
+
* const [state, bloc] = useBloc(MyBloc);
|
|
19
32
|
* ```
|
|
20
33
|
*
|
|
21
|
-
* @example
|
|
34
|
+
* @example Manual select (replaces v1 `dependencies`)
|
|
22
35
|
* ```ts
|
|
23
|
-
* const [state,
|
|
24
|
-
*
|
|
36
|
+
* const [state, bloc] = useBloc(MyBloc, {
|
|
37
|
+
* select: (state) => [state.count],
|
|
25
38
|
* });
|
|
26
39
|
* ```
|
|
27
40
|
*
|
|
28
|
-
* @example
|
|
41
|
+
* @example Named instance
|
|
29
42
|
* ```ts
|
|
30
|
-
* const [state,
|
|
31
|
-
* instanceId: 'unique-id'
|
|
32
|
-
* });
|
|
43
|
+
* const [state, bloc] = useBloc(MyBloc, { instanceId: 'cart-42' });
|
|
33
44
|
* ```
|
|
34
45
|
*/
|
|
35
46
|
export declare function useBloc<T extends StateContainerConstructor = StateContainerConstructor>(BlocClass: T, options?: UseBlocOptions<T>): UseBlocReturn<T, ExtractState<T>>;
|
|
@@ -7,4 +7,6 @@
|
|
|
7
7
|
* @param providedId - User-provided instance ID (from options)
|
|
8
8
|
* @returns Instance key string or undefined for default
|
|
9
9
|
*/
|
|
10
|
-
export declare function generateInstanceKey(
|
|
10
|
+
export declare function generateInstanceKey(
|
|
11
|
+
providedId?: string | number,
|
|
12
|
+
): string | undefined;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@blac/react",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.14",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "Brendan Mullins <jsnanigans@gmail.com>",
|
|
6
6
|
"description": "React bindings for BlaC — useBloc hook with automatic re-render optimization",
|
|
@@ -65,13 +65,13 @@
|
|
|
65
65
|
"bloc-pattern"
|
|
66
66
|
],
|
|
67
67
|
"dependencies": {
|
|
68
|
-
"@
|
|
68
|
+
"@dirtytalk/structural": "^0.0.2"
|
|
69
69
|
},
|
|
70
70
|
"peerDependencies": {
|
|
71
71
|
"@testing-library/react": "^14.0.0 || ^15.0.0 || ^16.0.0",
|
|
72
72
|
"@types/react": "^18.0.0 || ^19.0.0",
|
|
73
73
|
"react": "^18.0.0 || ^19.0.0",
|
|
74
|
-
"@blac/core": "^2.0.
|
|
74
|
+
"@blac/core": "^2.0.14"
|
|
75
75
|
},
|
|
76
76
|
"peerDependenciesMeta": {
|
|
77
77
|
"@types/react": {
|
|
@@ -102,7 +102,7 @@
|
|
|
102
102
|
"vite": "npm:@voidzero-dev/vite-plus-core@latest",
|
|
103
103
|
"vitest": "npm:@voidzero-dev/vite-plus-test@latest",
|
|
104
104
|
"vite-plus": "latest",
|
|
105
|
-
"@blac/core": "2.0.
|
|
105
|
+
"@blac/core": "2.0.14"
|
|
106
106
|
},
|
|
107
107
|
"scripts": {
|
|
108
108
|
"dev": "vp pack --watch",
|