@blac/react
Advanced tools
+8
-19
@@ -37,19 +37,2 @@ let react = require("react"); | ||
| } | ||
| /** | ||
| * Lifecycle: INITIAL MOUNT | ||
| * 1. useMemo runs once - creates bloc, subscribeFn, getSnapshotFn | ||
| * 2. useSyncExternalStore calls getSnapshotFn (1st time) - lazy creates tracker, starts tracking, returns proxy | ||
| * 3. Component renders - proxy tracks property accesses | ||
| * 4. useSyncExternalStore calls getSnapshotFn (2nd time) - captures tracked paths, starts new tracking, returns proxy | ||
| * 5. useSyncExternalStore calls subscribeFn - sets up state change listener | ||
| * | ||
| * Lifecycle: STATE CHANGE | ||
| * 1. Bloc state changes | ||
| * 2. subscribeFn callback checks hasChanges() - only re-renders if tracked paths changed | ||
| * 3. If re-render: getSnapshotFn captures previous paths, starts tracking, returns proxy | ||
| * | ||
| * Lifecycle: RE-RENDER (parent re-render) | ||
| * 1. useMemo returns cached values (same bloc, subscribeFn, getSnapshotFn) | ||
| * 2. useSyncExternalStore calls getSnapshotFn - captures paths, starts tracking, returns proxy | ||
| */ | ||
| function useBloc(BlocClass, options) { | ||
@@ -59,5 +42,7 @@ const componentRef = (0, react.useRef)({}); | ||
| const isIsolated = (0, __blac_core.isIsolatedClass)(BlocClass); | ||
| const initialPropsRef = (0, react.useRef)(options?.props); | ||
| const [bloc, subscribe, getSnapshot, instanceKey, adapterState, rawInstance] = (0, react.useMemo)(() => { | ||
| const instanceKey$1 = generateInstanceKey(componentRef.current, isIsolated, options?.instanceId); | ||
| const instance = BlocClass.resolve(instanceKey$1, options?.staticProps); | ||
| const instance = BlocClass.resolve(instanceKey$1, initialPropsRef.current); | ||
| if (initialPropsRef.current !== void 0) instance.updateProps(initialPropsRef.current); | ||
| const { useManualDeps, autoTrackEnabled } = determineTrackingMode(options); | ||
@@ -93,2 +78,5 @@ let subscribeFn; | ||
| (0, react.useEffect)(() => { | ||
| if (options?.props !== initialPropsRef.current) rawInstance.updateProps(options?.props); | ||
| }, [options?.props, rawInstance]); | ||
| (0, react.useEffect)(() => { | ||
| (0, __blac_core.disableGetterTracking)(adapterState, rawInstance); | ||
@@ -117,2 +105,3 @@ externalDepsManager.current.updateSubscriptions(adapterState.getterTracker, rawInstance, forceUpdate); | ||
| const componentRef = (0, react.useRef)({}); | ||
| const initialPropsRef = (0, react.useRef)(options?.props); | ||
| const [bloc, instanceKey] = (0, react.useMemo)(() => { | ||
@@ -122,3 +111,3 @@ const isIsolated = (0, __blac_core.isIsolatedClass)(BlocClass); | ||
| const instanceKey$1 = generateInstanceKey(componentRef.current, isIsolated, options?.instanceId); | ||
| return [options?.staticProps ? Constructor.resolve(instanceKey$1, options.staticProps) : Constructor.resolve(instanceKey$1), instanceKey$1]; | ||
| return [Constructor.resolve(instanceKey$1, initialPropsRef.current), instanceKey$1]; | ||
| }, [BlocClass]); | ||
@@ -125,0 +114,0 @@ (0, react.useEffect)(() => { |
+8
-19
@@ -37,19 +37,2 @@ import { useEffect, useMemo, useReducer, useRef, useSyncExternalStore } from "react"; | ||
| } | ||
| /** | ||
| * Lifecycle: INITIAL MOUNT | ||
| * 1. useMemo runs once - creates bloc, subscribeFn, getSnapshotFn | ||
| * 2. useSyncExternalStore calls getSnapshotFn (1st time) - lazy creates tracker, starts tracking, returns proxy | ||
| * 3. Component renders - proxy tracks property accesses | ||
| * 4. useSyncExternalStore calls getSnapshotFn (2nd time) - captures tracked paths, starts new tracking, returns proxy | ||
| * 5. useSyncExternalStore calls subscribeFn - sets up state change listener | ||
| * | ||
| * Lifecycle: STATE CHANGE | ||
| * 1. Bloc state changes | ||
| * 2. subscribeFn callback checks hasChanges() - only re-renders if tracked paths changed | ||
| * 3. If re-render: getSnapshotFn captures previous paths, starts tracking, returns proxy | ||
| * | ||
| * Lifecycle: RE-RENDER (parent re-render) | ||
| * 1. useMemo returns cached values (same bloc, subscribeFn, getSnapshotFn) | ||
| * 2. useSyncExternalStore calls getSnapshotFn - captures paths, starts tracking, returns proxy | ||
| */ | ||
| function useBloc(BlocClass, options) { | ||
@@ -59,5 +42,7 @@ const componentRef = useRef({}); | ||
| const isIsolated = isIsolatedClass(BlocClass); | ||
| const initialPropsRef = useRef(options?.props); | ||
| const [bloc, subscribe, getSnapshot, instanceKey, adapterState, rawInstance] = useMemo(() => { | ||
| const instanceKey$1 = generateInstanceKey(componentRef.current, isIsolated, options?.instanceId); | ||
| const instance = BlocClass.resolve(instanceKey$1, options?.staticProps); | ||
| const instance = BlocClass.resolve(instanceKey$1, initialPropsRef.current); | ||
| if (initialPropsRef.current !== void 0) instance.updateProps(initialPropsRef.current); | ||
| const { useManualDeps, autoTrackEnabled } = determineTrackingMode(options); | ||
@@ -93,2 +78,5 @@ let subscribeFn; | ||
| useEffect(() => { | ||
| if (options?.props !== initialPropsRef.current) rawInstance.updateProps(options?.props); | ||
| }, [options?.props, rawInstance]); | ||
| useEffect(() => { | ||
| disableGetterTracking(adapterState, rawInstance); | ||
@@ -117,2 +105,3 @@ externalDepsManager.current.updateSubscriptions(adapterState.getterTracker, rawInstance, forceUpdate); | ||
| const componentRef = useRef({}); | ||
| const initialPropsRef = useRef(options?.props); | ||
| const [bloc, instanceKey] = useMemo(() => { | ||
@@ -122,3 +111,3 @@ const isIsolated = isIsolatedClass(BlocClass); | ||
| const instanceKey$1 = generateInstanceKey(componentRef.current, isIsolated, options?.instanceId); | ||
| return [options?.staticProps ? Constructor.resolve(instanceKey$1, options.staticProps) : Constructor.resolve(instanceKey$1), instanceKey$1]; | ||
| return [Constructor.resolve(instanceKey$1, initialPropsRef.current), instanceKey$1]; | ||
| }, [BlocClass]); | ||
@@ -125,0 +114,0 @@ useEffect(() => { |
+3
-3
| import type { ExtractState } from '@blac/core'; | ||
| import type { RefObject } from 'react'; | ||
| export interface UseBlocOptions<TBloc> { | ||
| staticProps?: any; | ||
| export interface UseBlocOptions<TBloc, TProps = any> { | ||
| props?: TProps; | ||
| instanceId?: string | number; | ||
@@ -12,3 +12,3 @@ dependencies?: (state: ExtractState<TBloc>, bloc: TBloc) => unknown[]; | ||
| } | ||
| export interface UseBlocOptionsWithDependencies<TBloc> extends UseBlocOptions<TBloc> { | ||
| export interface UseBlocOptionsWithDependencies<TBloc, TProps = any> extends UseBlocOptions<TBloc, TProps> { | ||
| dependencies: (state: ExtractState<TBloc>, bloc: TBloc) => unknown[]; | ||
@@ -15,0 +15,0 @@ autoTrack?: never; |
+1
-18
| import { type BlocConstructor, StateContainer } from '@blac/core'; | ||
| import type { UseBlocOptions, UseBlocReturn } from './types'; | ||
| /** | ||
| * Lifecycle: INITIAL MOUNT | ||
| * 1. useMemo runs once - creates bloc, subscribeFn, getSnapshotFn | ||
| * 2. useSyncExternalStore calls getSnapshotFn (1st time) - lazy creates tracker, starts tracking, returns proxy | ||
| * 3. Component renders - proxy tracks property accesses | ||
| * 4. useSyncExternalStore calls getSnapshotFn (2nd time) - captures tracked paths, starts new tracking, returns proxy | ||
| * 5. useSyncExternalStore calls subscribeFn - sets up state change listener | ||
| * | ||
| * Lifecycle: STATE CHANGE | ||
| * 1. Bloc state changes | ||
| * 2. subscribeFn callback checks hasChanges() - only re-renders if tracked paths changed | ||
| * 3. If re-render: getSnapshotFn captures previous paths, starts tracking, returns proxy | ||
| * | ||
| * Lifecycle: RE-RENDER (parent re-render) | ||
| * 1. useMemo returns cached values (same bloc, subscribeFn, getSnapshotFn) | ||
| * 2. useSyncExternalStore calls getSnapshotFn - captures paths, starts tracking, returns proxy | ||
| */ | ||
| export declare function useBloc<T extends new (...args: any[]) => StateContainer<any>>(BlocClass: T & BlocConstructor<InstanceType<T>>, options?: UseBlocOptions<InstanceType<T>>): UseBlocReturn<InstanceType<T>>; | ||
| export declare function useBloc<T extends new (...args: any[]) => StateContainer<any, any>>(BlocClass: T & BlocConstructor<InstanceType<T>>, options?: UseBlocOptions<InstanceType<T>>): UseBlocReturn<InstanceType<T>>; | ||
| //# sourceMappingURL=useBloc.d.ts.map |
| import { type BlocConstructor, StateContainer } from '@blac/core'; | ||
| export interface UseBlocActionsOptions<TBloc> { | ||
| staticProps?: any; | ||
| export interface UseBlocActionsOptions<TBloc, TProps = any> { | ||
| props?: TProps; | ||
| instanceId?: string | number; | ||
@@ -8,3 +8,3 @@ onMount?: (bloc: TBloc) => void; | ||
| } | ||
| export declare function useBlocActions<T extends new (...args: any[]) => StateContainer<any>>(BlocClass: T & BlocConstructor<InstanceType<T>>, options?: UseBlocActionsOptions<InstanceType<T>>): InstanceType<T>; | ||
| export declare function useBlocActions<T extends new (...args: any[]) => StateContainer<any, any>>(BlocClass: T & BlocConstructor<InstanceType<T>>, options?: UseBlocActionsOptions<InstanceType<T>>): InstanceType<T>; | ||
| //# sourceMappingURL=useBlocActions.d.ts.map |
+3
-3
| { | ||
| "name": "@blac/react", | ||
| "version": "2.0.0-rc.9", | ||
| "version": "2.0.0-rc.10", | ||
| "license": "MIT", | ||
@@ -51,3 +51,3 @@ "author": "Brendan Mullins <jsnanigans@gmail.com>", | ||
| "react": "^18.0.0 || ^19.0.0", | ||
| "@blac/core": "2.0.0-rc.9" | ||
| "@blac/core": "2.0.0-rc.10" | ||
| }, | ||
@@ -81,3 +81,3 @@ "peerDependenciesMeta": { | ||
| "vitest": "3.2.4", | ||
| "@blac/core": "2.0.0-rc.9" | ||
| "@blac/core": "2.0.0-rc.10" | ||
| }, | ||
@@ -84,0 +84,0 @@ "scripts": { |
17247
-10.78%300
-11.5%