@zenflux/react-commander 0.0.2 → 0.0.3

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/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "$schema": "https://raw.githubusercontent.com/SchemaStore/schemastore/master/src/schemas/json/package.json",
3
3
  "name": "@zenflux/react-commander",
4
4
  "author": "Leonid Vinikov <leonidvinikov@gmail.com> (https://github.com/iNewLegend)",
5
- "version": "0.0.2",
5
+ "version": "0.0.3",
6
6
  "type": "module",
7
7
  "types": "src/index.ts",
8
8
  "files": [
@@ -2,6 +2,7 @@
2
2
  export { useCommand } from "./use-command/use-command";
3
3
  export { useComponent } from "./use-component/use-component";
4
4
  export { useCommandState } from "./use-command/use-command-state";
5
+ export { useCommandStateWithRef } from "./use-command/use-command-state-with-ref";
5
6
  export { useComponentWithRef } from "./use-component/use-component-with-ref";
6
7
  export { useCommandRunner } from "./use-command/use-command-runner";
7
8
  export { useCommandHook } from "./use-command/use-command-hook";
@@ -0,0 +1,148 @@
1
+
2
+ import React from "react";
3
+
4
+ import { shallowEqual } from "../utils";
5
+
6
+ import core from "../../_internal/core";
7
+ import { INTERNAL_STATE_UPDATED_EVENT, GET_INTERNAL_MATCH_SYMBOL } from "../../_internal/constants";
8
+
9
+ function resolveInternalContextByRef( componentName: string, ref: React.RefObject<Element | null> ) {
10
+ try {
11
+ const contexts = core[ GET_INTERNAL_MATCH_SYMBOL ]( componentName + "*" );
12
+
13
+ const currentContext = contexts.find( ( ctx ) => ctx.getComponentContext().getComponentRef().current === ref.current );
14
+
15
+ return currentContext ?? null;
16
+ } catch {
17
+ return null;
18
+ }
19
+ }
20
+
21
+ function useCommandStateWithRefInternal<const TState extends React.ComponentState>(
22
+ componentName: string,
23
+ ref: React.RefObject<Element | null>
24
+ ) {
25
+ const [ internalContext, setInternalContext ] = React.useState( () => resolveInternalContextByRef( componentName, ref ) );
26
+
27
+ React.useEffect( () => {
28
+ setInternalContext( resolveInternalContextByRef( componentName, ref ) );
29
+ }, [ componentName, ref.current ] );
30
+
31
+ if ( ! internalContext ) {
32
+ return null;
33
+ }
34
+
35
+ return [
36
+ internalContext.getState<TState>,
37
+ internalContext.setState<TState>,
38
+ internalContext.isMounted,
39
+ ] as const;
40
+ }
41
+
42
+ function useCommandStateWithRefSelectorInternal<TState, TSelector>(
43
+ componentName: string,
44
+ ref: React.RefObject<Element | null>,
45
+ selector: ( state: TState ) => TSelector,
46
+ options?: { equalityFn?: ( a: TSelector, b: TSelector ) => boolean }
47
+ ) {
48
+ const [ internalContext, setInternalContext ] = React.useState( () => resolveInternalContextByRef( componentName, ref ) );
49
+
50
+ React.useEffect( () => {
51
+ setInternalContext( resolveInternalContextByRef( componentName, ref ) );
52
+ }, [ componentName, ref.current ] );
53
+
54
+ const selectorRef = React.useRef( selector );
55
+ selectorRef.current = selector;
56
+
57
+ const equalityFn = options?.equalityFn ?? ( shallowEqual as ( a: TSelector, b: TSelector ) => boolean );
58
+
59
+ const valueRef = React.useRef<TSelector | null>( internalContext ? selector( internalContext.getState<TState>() ) : null );
60
+ const onChangeRef = React.useRef<( () => void ) | null>( null );
61
+
62
+ const handlerRef = React.useRef<( () => void ) | null>( null );
63
+
64
+ React.useEffect( () => {
65
+ if ( ! internalContext ) return;
66
+
67
+ if ( ! handlerRef.current ) {
68
+ const handleStateChange = () => {
69
+ const currentState = internalContext.getState<TState>();
70
+ const newValue = selectorRef.current( currentState );
71
+
72
+ if ( valueRef.current === null || ! equalityFn( valueRef.current, newValue ) ) {
73
+ valueRef.current = newValue;
74
+
75
+ if ( onChangeRef.current ) {
76
+ onChangeRef.current();
77
+ }
78
+ }
79
+ };
80
+
81
+ handlerRef.current = handleStateChange;
82
+ internalContext.emitter.on( INTERNAL_STATE_UPDATED_EVENT, handleStateChange );
83
+ }
84
+
85
+ return () => {
86
+ if ( handlerRef.current ) {
87
+ internalContext.emitter.off( INTERNAL_STATE_UPDATED_EVENT, handlerRef.current );
88
+ handlerRef.current = null;
89
+ }
90
+ };
91
+ }, [ internalContext, componentName ] );
92
+
93
+ const subscribe = React.useCallback( ( onChange: () => void ) => {
94
+ onChangeRef.current = onChange;
95
+
96
+ return () => {
97
+ onChangeRef.current = null;
98
+ };
99
+ }, [ componentName ] );
100
+
101
+ const getSnapshot = React.useCallback( () => {
102
+ return valueRef.current;
103
+ }, [] );
104
+
105
+ const selectedState = React.useSyncExternalStore( subscribe, getSnapshot, getSnapshot );
106
+
107
+ if ( ! internalContext ) {
108
+ return null;
109
+ }
110
+
111
+ return [
112
+ selectedState as TSelector,
113
+ internalContext.setState<TState>,
114
+ internalContext.isMounted,
115
+ ] as const;
116
+ }
117
+
118
+ export function useCommandStateWithRef<TState>(
119
+ componentName: string,
120
+ ref: React.RefObject<Element | null>
121
+ ): readonly [
122
+ () => TState,
123
+ <K extends keyof TState = keyof TState>( state: TState | Pick<TState, K>, callback?: ( state: TState ) => void ) => void,
124
+ () => boolean
125
+ ] | null;
126
+
127
+ export function useCommandStateWithRef<TState, TSelector>(
128
+ componentName: string,
129
+ ref: React.RefObject<Element | null>,
130
+ selector: ( state: TState ) => TSelector,
131
+ options?: { equalityFn?: ( a: TSelector, b: TSelector ) => boolean }
132
+ ): readonly [
133
+ TSelector,
134
+ <K extends keyof TState = keyof TState>( state: TState | Pick<TState, K>, callback?: ( state: TState ) => void ) => void,
135
+ () => boolean
136
+ ] | null;
137
+
138
+ export function useCommandStateWithRef<TState, TSelector = never>(
139
+ componentName: string,
140
+ ref: React.RefObject<Element | null>,
141
+ selector?: ( state: TState ) => TSelector,
142
+ options?: { equalityFn?: ( a: TSelector, b: TSelector ) => boolean }
143
+ ) {
144
+ if ( typeof selector === "function" ) {
145
+ return useCommandStateWithRefSelectorInternal<TState, TSelector>( componentName, ref, selector, options );
146
+ }
147
+ return useCommandStateWithRefInternal<TState>( componentName, ref );
148
+ }