@zenflux/react-commander 0.0.1-dev
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 +61 -0
- package/readme.md +1422 -0
- package/src/_internal/constants.ts +18 -0
- package/src/_internal/core.ts +208 -0
- package/src/_internal/definitions.ts +47 -0
- package/src/command-base.ts +106 -0
- package/src/commands-context.ts +20 -0
- package/src/commands-manager.ts +357 -0
- package/src/commands-provider.tsx +37 -0
- package/src/definitions.ts +165 -0
- package/src/hooks/index.ts +10 -0
- package/src/hooks/use-child-command/use-child-command-hook.ts +44 -0
- package/src/hooks/use-child-command/use-child-command-runner.ts +32 -0
- package/src/hooks/use-command/use-command-hook.ts +23 -0
- package/src/hooks/use-command/use-command-runner.ts +16 -0
- package/src/hooks/use-command/use-command-state.ts +132 -0
- package/src/hooks/use-command/use-command.ts +260 -0
- package/src/hooks/use-component/use-component-with-ref.ts +30 -0
- package/src/hooks/use-component/use-component.ts +56 -0
- package/src/hooks/utils.ts +90 -0
- package/src/index.ts +9 -0
- package/src/query/query-auto-save-manager.ts +54 -0
- package/src/query/query-cache.ts +91 -0
- package/src/query/query-client.tsx +67 -0
- package/src/query/query-component.tsx +101 -0
- package/src/query/query-definitions.ts +32 -0
- package/src/query/query-list-diff.ts +8 -0
- package/src/query/query-list-module-base.ts +20 -0
- package/src/query/query-module-base.ts +200 -0
- package/src/query/query-provider.tsx +47 -0
- package/src/query/query-router.ts +85 -0
- package/src/query/query-utils.ts +30 -0
- package/src/with-commands.tsx +363 -0
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
|
|
2
|
+
import React from "react";
|
|
3
|
+
|
|
4
|
+
import { useCommand } from "./use-command.js";
|
|
5
|
+
|
|
6
|
+
import { getSafeContext, shallowEqual } from "../utils.js";
|
|
7
|
+
|
|
8
|
+
import core from "../../_internal/core.js";
|
|
9
|
+
import { INTERNAL_STATE_UPDATED_EVENT, GET_INTERNAL_SYMBOL } from "../../_internal/constants.js";
|
|
10
|
+
|
|
11
|
+
function resolveInternalContext( name: string ) {
|
|
12
|
+
const cmd = useCommand( name );
|
|
13
|
+
const byCommand = cmd?.getInternalContext?.();
|
|
14
|
+
if ( byCommand ) return byCommand;
|
|
15
|
+
|
|
16
|
+
try {
|
|
17
|
+
const componentContext = getSafeContext( name );
|
|
18
|
+
const id = componentContext.getNameUnique();
|
|
19
|
+
return core[ GET_INTERNAL_SYMBOL ]( id );
|
|
20
|
+
} catch {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function useCommandStateInternal<const TState extends React.ComponentState>( commandName: string ) {
|
|
26
|
+
const internalContext = resolveInternalContext( commandName );
|
|
27
|
+
if ( ! internalContext ) {
|
|
28
|
+
throw new Error( `useCommandState(\"${ commandName }\") cannot resolve internal context` );
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return [
|
|
32
|
+
internalContext.getState<TState>,
|
|
33
|
+
internalContext.setState<TState>,
|
|
34
|
+
|
|
35
|
+
internalContext.isMounted,
|
|
36
|
+
] as const;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function useCommandStateSelectorInternal<TState, TSelector>(
|
|
40
|
+
commandName: string,
|
|
41
|
+
selector: ( state: TState ) => TSelector,
|
|
42
|
+
options?: { equalityFn?: ( a: TSelector, b: TSelector ) => boolean }
|
|
43
|
+
) {
|
|
44
|
+
const internalContext = resolveInternalContext( commandName );
|
|
45
|
+
if ( ! internalContext ) {
|
|
46
|
+
throw new Error( `useCommandState(\"${ commandName }\") cannot resolve internal context` );
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const selectorRef = React.useRef( selector );
|
|
50
|
+
selectorRef.current = selector;
|
|
51
|
+
|
|
52
|
+
const equalityFn = options?.equalityFn ?? ( shallowEqual as unknown as ( a: TSelector, b: TSelector ) => boolean );
|
|
53
|
+
|
|
54
|
+
const valueRef = React.useRef<TSelector>( selector( internalContext.getState<TState>() ) );
|
|
55
|
+
const onChangeRef = React.useRef<( () => void ) | null>( null );
|
|
56
|
+
|
|
57
|
+
const handlerRef = React.useRef<( () => void ) | null>( null );
|
|
58
|
+
|
|
59
|
+
if ( ! handlerRef.current ) {
|
|
60
|
+
const handleStateChange = () => {
|
|
61
|
+
const currentState = internalContext.getState<TState>();
|
|
62
|
+
const newValue = selectorRef.current( currentState );
|
|
63
|
+
|
|
64
|
+
if ( ! equalityFn( valueRef.current, newValue ) ) {
|
|
65
|
+
valueRef.current = newValue;
|
|
66
|
+
|
|
67
|
+
if ( onChangeRef.current ) {
|
|
68
|
+
onChangeRef.current();
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
handlerRef.current = handleStateChange;
|
|
74
|
+
internalContext.emitter.on( INTERNAL_STATE_UPDATED_EVENT, handleStateChange );
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
React.useEffect( () => {
|
|
78
|
+
return () => {
|
|
79
|
+
if ( handlerRef.current ) {
|
|
80
|
+
internalContext.emitter.off( INTERNAL_STATE_UPDATED_EVENT, handlerRef.current );
|
|
81
|
+
handlerRef.current = null;
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
}, [ internalContext, commandName ] );
|
|
85
|
+
|
|
86
|
+
const subscribe = React.useCallback( ( onChange: () => void ) => {
|
|
87
|
+
onChangeRef.current = onChange;
|
|
88
|
+
|
|
89
|
+
return () => {
|
|
90
|
+
onChangeRef.current = null;
|
|
91
|
+
};
|
|
92
|
+
}, [ commandName ] );
|
|
93
|
+
|
|
94
|
+
const getSnapshot = React.useCallback( () => {
|
|
95
|
+
return valueRef.current;
|
|
96
|
+
}, [] );
|
|
97
|
+
|
|
98
|
+
const selectedState = React.useSyncExternalStore( subscribe, getSnapshot, getSnapshot );
|
|
99
|
+
|
|
100
|
+
return [
|
|
101
|
+
selectedState as TSelector,
|
|
102
|
+
internalContext.setState<TState>,
|
|
103
|
+
internalContext.isMounted,
|
|
104
|
+
] as const;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function useCommandState<TState>( commandName: string ): readonly [
|
|
108
|
+
() => TState,
|
|
109
|
+
<K extends keyof TState = keyof TState>( state: TState | Pick<TState, K>, callback?: ( state: TState ) => void ) => void,
|
|
110
|
+
() => boolean
|
|
111
|
+
];
|
|
112
|
+
|
|
113
|
+
export function useCommandState<TState, TSelector>(
|
|
114
|
+
commandName: string,
|
|
115
|
+
selector: ( state: TState ) => TSelector,
|
|
116
|
+
options?: { equalityFn?: ( a: TSelector, b: TSelector ) => boolean }
|
|
117
|
+
) : readonly [
|
|
118
|
+
TSelector,
|
|
119
|
+
<K extends keyof TState = keyof TState>( state: TState | Pick<TState, K>, callback?: ( state: TState ) => void ) => void,
|
|
120
|
+
() => boolean
|
|
121
|
+
];
|
|
122
|
+
|
|
123
|
+
export function useCommandState<TState, TSelector = never>(
|
|
124
|
+
commandName: string,
|
|
125
|
+
selector?: ( state: TState ) => TSelector,
|
|
126
|
+
options?: { equalityFn?: ( a: TSelector, b: TSelector ) => boolean }
|
|
127
|
+
) {
|
|
128
|
+
if ( typeof selector === "function" ) {
|
|
129
|
+
return useCommandStateSelectorInternal<TState, TSelector>( commandName, selector, options );
|
|
130
|
+
}
|
|
131
|
+
return useCommandStateInternal<TState>( commandName );
|
|
132
|
+
}
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
|
|
2
|
+
import React from "react";
|
|
3
|
+
|
|
4
|
+
import { GET_INTERNAL_SYMBOL, GET_INTERNAL_MATCH_SYMBOL } from "../../_internal/constants.js";
|
|
5
|
+
|
|
6
|
+
import core from "../../_internal/core.js";
|
|
7
|
+
import { ComponentIdContext } from "../../commands-context.js";
|
|
8
|
+
import commandsManager from "../../commands-manager.js";
|
|
9
|
+
|
|
10
|
+
import type { DCommandArgs, DCommandIdArgs, DCommandSingleComponentContext } from "../../definitions.js";
|
|
11
|
+
|
|
12
|
+
type CommandRun = ( args?: DCommandArgs, callback?: ( result: unknown ) => void ) => unknown
|
|
13
|
+
type CommandHook = ( callback: ( result: unknown, args?: DCommandArgs ) => void ) => void
|
|
14
|
+
type CommandUnhook = () => void
|
|
15
|
+
//
|
|
16
|
+
type CommandGetInternalContext = () => DCommandSingleComponentContext | null | undefined
|
|
17
|
+
|
|
18
|
+
type UseCommandAdapter = {
|
|
19
|
+
run: CommandRun
|
|
20
|
+
hook: CommandHook
|
|
21
|
+
unhook: CommandUnhook
|
|
22
|
+
getInternalContext: CommandGetInternalContext
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function useCommand( commandName: string ): UseCommandAdapter;
|
|
26
|
+
export function useCommand( commandName: string, ref: React.RefObject<any> ): UseCommandAdapter | null;
|
|
27
|
+
export function useCommand( commandName: string, ref?: React.RefObject<any> ) {
|
|
28
|
+
const componentContext = React.useContext( ComponentIdContext );
|
|
29
|
+
|
|
30
|
+
const ownerIdRef = React.useRef<string | null>( null );
|
|
31
|
+
if ( ! ownerIdRef.current ) {
|
|
32
|
+
ownerIdRef.current = `${ commandName }:${ Math.random().toString( 36 ).slice( 2 ) }`;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
type HookHandle = { componentNameUnique: string; commandName: string; ownerId: string; dispose: () => void };
|
|
36
|
+
const hookedCallbacksRef = React.useRef<Map<Function, HookHandle>>( new Map() );
|
|
37
|
+
|
|
38
|
+
React.useEffect( () => {
|
|
39
|
+
return () => {
|
|
40
|
+
for ( const handle of hookedCallbacksRef.current.values() ) {
|
|
41
|
+
try { commandsManager.unhookHandle( handle ); } catch {}
|
|
42
|
+
}
|
|
43
|
+
hookedCallbacksRef.current.clear();
|
|
44
|
+
};
|
|
45
|
+
}, [] );
|
|
46
|
+
|
|
47
|
+
const debug = ( ..._args: Array<unknown> ) => {
|
|
48
|
+
try {
|
|
49
|
+
// TODO: Enable when needed
|
|
50
|
+
console.debug( "[react-commander/useCommand]", ..._args );
|
|
51
|
+
} catch {}
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const lastIdRef = React.useRef<DCommandIdArgs | null>( null );
|
|
55
|
+
|
|
56
|
+
function defer( fn: () => void ) {
|
|
57
|
+
try {
|
|
58
|
+
if ( typeof queueMicrotask === "function" ) {
|
|
59
|
+
queueMicrotask( fn );
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
} catch {}
|
|
63
|
+
setTimeout( fn, 0 );
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function createAdapterFromId(
|
|
67
|
+
id: DCommandIdArgs,
|
|
68
|
+
getInternal?: () => DCommandSingleComponentContext,
|
|
69
|
+
): UseCommandAdapter {
|
|
70
|
+
lastIdRef.current = id;
|
|
71
|
+
return {
|
|
72
|
+
run: (
|
|
73
|
+
_args: DCommandArgs = {},
|
|
74
|
+
callback?: ( result: unknown ) => void,
|
|
75
|
+
) => {
|
|
76
|
+
return commandsManager.run(
|
|
77
|
+
id,
|
|
78
|
+
_args,
|
|
79
|
+
callback as ( _r: unknown ) => void,
|
|
80
|
+
);
|
|
81
|
+
},
|
|
82
|
+
hook: ( callback: ( result: unknown, args?: DCommandArgs ) => void ) => {
|
|
83
|
+
if ( hookedCallbacksRef.current.has( callback ) ) return;
|
|
84
|
+
if ( ! commandsManager.isContextRegistered( id.componentNameUnique ) ) {
|
|
85
|
+
defer( () => {
|
|
86
|
+
if ( hookedCallbacksRef.current.has( callback ) ) return;
|
|
87
|
+
if ( ! commandsManager.isContextRegistered( id.componentNameUnique ) ) return;
|
|
88
|
+
const h = commandsManager.hookScoped(
|
|
89
|
+
id,
|
|
90
|
+
ownerIdRef.current as string,
|
|
91
|
+
callback as ( r: unknown, a?: DCommandArgs ) => void,
|
|
92
|
+
);
|
|
93
|
+
if ( h ) hookedCallbacksRef.current.set( callback, h );
|
|
94
|
+
} );
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
const handle = commandsManager.hookScoped(
|
|
98
|
+
id,
|
|
99
|
+
ownerIdRef.current as string,
|
|
100
|
+
callback as ( r: unknown, a?: DCommandArgs ) => void,
|
|
101
|
+
);
|
|
102
|
+
if ( handle ) hookedCallbacksRef.current.set( callback, handle );
|
|
103
|
+
},
|
|
104
|
+
unhook: () => {
|
|
105
|
+
for ( const handle of hookedCallbacksRef.current.values() ) {
|
|
106
|
+
try {
|
|
107
|
+
commandsManager.unhookHandle( handle );
|
|
108
|
+
} catch {}
|
|
109
|
+
}
|
|
110
|
+
hookedCallbacksRef.current.clear();
|
|
111
|
+
},
|
|
112
|
+
getInternalContext: () => getInternal?.() ?? null,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function createAdapterFromResolver(
|
|
117
|
+
resolveId: () => DCommandIdArgs | null,
|
|
118
|
+
getInternalForId: ( id: DCommandIdArgs ) => DCommandSingleComponentContext | null,
|
|
119
|
+
): UseCommandAdapter {
|
|
120
|
+
return {
|
|
121
|
+
run: (
|
|
122
|
+
_args: DCommandArgs = {},
|
|
123
|
+
callback?: ( result: unknown ) => void,
|
|
124
|
+
) => {
|
|
125
|
+
const id = resolveId();
|
|
126
|
+
if ( ! id ) return;
|
|
127
|
+
return commandsManager.run(
|
|
128
|
+
id,
|
|
129
|
+
_args,
|
|
130
|
+
callback as ( _r: unknown ) => void,
|
|
131
|
+
);
|
|
132
|
+
},
|
|
133
|
+
hook: ( callback: ( result: unknown, args?: DCommandArgs ) => void ) => {
|
|
134
|
+
if ( hookedCallbacksRef.current.has( callback ) ) return;
|
|
135
|
+
const id = resolveId();
|
|
136
|
+
if ( ! id ) return;
|
|
137
|
+
if ( ! commandsManager.isContextRegistered( id.componentNameUnique ) ) {
|
|
138
|
+
defer( () => {
|
|
139
|
+
if ( hookedCallbacksRef.current.has( callback ) ) return;
|
|
140
|
+
const next = resolveId();
|
|
141
|
+
if ( ! next ) return;
|
|
142
|
+
if ( ! commandsManager.isContextRegistered( next.componentNameUnique ) ) return;
|
|
143
|
+
const h = commandsManager.hookScoped(
|
|
144
|
+
next,
|
|
145
|
+
ownerIdRef.current as string,
|
|
146
|
+
callback as ( r: unknown, a?: DCommandArgs ) => void,
|
|
147
|
+
);
|
|
148
|
+
if ( h ) hookedCallbacksRef.current.set( callback, h );
|
|
149
|
+
} );
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
const handle = commandsManager.hookScoped(
|
|
153
|
+
id,
|
|
154
|
+
ownerIdRef.current as string,
|
|
155
|
+
callback as ( r: unknown, a?: DCommandArgs ) => void,
|
|
156
|
+
);
|
|
157
|
+
if ( handle ) hookedCallbacksRef.current.set( callback, handle );
|
|
158
|
+
},
|
|
159
|
+
unhook: () => {
|
|
160
|
+
for ( const handle of hookedCallbacksRef.current.values() ) {
|
|
161
|
+
try {
|
|
162
|
+
commandsManager.unhookHandle( handle );
|
|
163
|
+
} catch {}
|
|
164
|
+
}
|
|
165
|
+
hookedCallbacksRef.current.clear();
|
|
166
|
+
},
|
|
167
|
+
getInternalContext: () => {
|
|
168
|
+
const id = resolveId();
|
|
169
|
+
if ( ! id ) return null;
|
|
170
|
+
return getInternalForId( id );
|
|
171
|
+
},
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const getAdapterInCurrentContext = (): UseCommandAdapter | null => {
|
|
176
|
+
const mapped = commandsManager.getComponentName( commandName );
|
|
177
|
+
if ( ! mapped ) { debug( "current", commandName, "no-mapped" ); return null; }
|
|
178
|
+
|
|
179
|
+
const unique = componentContext.getNameUnique();
|
|
180
|
+
const internal = core[ GET_INTERNAL_SYMBOL ]( unique );
|
|
181
|
+
if ( internal.componentName !== mapped ) { debug( "current", commandName, "component-mismatch", { current: internal.componentName, mapped } ); return null; }
|
|
182
|
+
if ( internal.commands && ! internal.commands[ commandName ] ) { debug( "current", commandName, "not-in-current" ); return null; }
|
|
183
|
+
|
|
184
|
+
const id: DCommandIdArgs = { commandName, componentNameUnique: unique, componentName: internal.componentName };
|
|
185
|
+
debug( "current", commandName, "resolved", id );
|
|
186
|
+
return createAdapterFromId( id, () => internal );
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
const getAdapterByName = (): UseCommandAdapter | null => {
|
|
190
|
+
const mapped = commandsManager.getComponentName( commandName );
|
|
191
|
+
if ( ! mapped ) { debug( "byName", commandName, "no-mapped" ); return null; }
|
|
192
|
+
|
|
193
|
+
try {
|
|
194
|
+
const contexts = core[ GET_INTERNAL_MATCH_SYMBOL ]( mapped + "*" ) as Array<any>;
|
|
195
|
+
for ( let i = contexts.length - 1 ; i >= 0 ; i-- ) {
|
|
196
|
+
const ctx = contexts[ i ];
|
|
197
|
+
if ( ctx.commands && ctx.commands[ commandName ] ) {
|
|
198
|
+
const id: DCommandIdArgs = { commandName, componentName: ctx.componentName, componentNameUnique: ctx.componentNameUnique };
|
|
199
|
+
debug( "byName", commandName, "resolved", id );
|
|
200
|
+
return createAdapterFromId(
|
|
201
|
+
id,
|
|
202
|
+
() => core[ GET_INTERNAL_SYMBOL ]( ctx.componentNameUnique ),
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
} catch { debug( "byName", commandName, "error" ); return null; }
|
|
207
|
+
debug( "byName", commandName, "none" );
|
|
208
|
+
return null;
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
const getAdapterInRef = (): UseCommandAdapter | null => {
|
|
212
|
+
if ( ! ref?.current ) { debug( "inRef", commandName, "no-ref" ); return null; }
|
|
213
|
+
|
|
214
|
+
try {
|
|
215
|
+
const mapped = commandsManager.getComponentName( commandName );
|
|
216
|
+
if ( ! mapped ) { debug( "inRef", commandName, "no-mapped" ); return null; }
|
|
217
|
+
const contexts = core[ GET_INTERNAL_MATCH_SYMBOL ]( mapped + "*" );
|
|
218
|
+
const ctx = contexts.find( ( c: any ) => c.getComponentContext().getComponentRef().current === ref.current );
|
|
219
|
+
if ( ! ctx ) { debug( "inRef", commandName, "no-match" ); return null; }
|
|
220
|
+
|
|
221
|
+
const id: DCommandIdArgs = { commandName, componentName: ctx.componentName, componentNameUnique: ctx.componentNameUnique };
|
|
222
|
+
debug( "inRef", commandName, "resolved", id );
|
|
223
|
+
return createAdapterFromId(
|
|
224
|
+
id,
|
|
225
|
+
() => core[ GET_INTERNAL_SYMBOL ]( ctx.componentNameUnique ),
|
|
226
|
+
);
|
|
227
|
+
} catch { debug( "inRef", commandName, "error" ); return null; }
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
const getAdapterOnDemand = (): UseCommandAdapter => {
|
|
231
|
+
const resolveLatestId = () => {
|
|
232
|
+
const mapped = commandsManager.getComponentName( commandName );
|
|
233
|
+
if ( ! mapped ) { debug( "onDemand", commandName, "no-mapped" ); return null; }
|
|
234
|
+
try {
|
|
235
|
+
const contexts = core[ GET_INTERNAL_MATCH_SYMBOL ]( mapped + "*" );
|
|
236
|
+
for ( let i = contexts.length - 1 ; i >= 0 ; i-- ) {
|
|
237
|
+
const ctx = contexts[ i ];
|
|
238
|
+
if ( ctx.commands && ctx.commands[ commandName ] ) {
|
|
239
|
+
const id = { commandName, componentName: ctx.componentName, componentNameUnique: ctx.componentNameUnique } as DCommandIdArgs;
|
|
240
|
+
debug( "onDemand", commandName, "resolved", id );
|
|
241
|
+
return id;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
debug( "onDemand", commandName, "none" );
|
|
245
|
+
return null;
|
|
246
|
+
} catch { debug( "onDemand", commandName, "error" ); return null; }
|
|
247
|
+
};
|
|
248
|
+
|
|
249
|
+
return createAdapterFromResolver(
|
|
250
|
+
resolveLatestId,
|
|
251
|
+
( id ) => core[ GET_INTERNAL_SYMBOL ]( id.componentNameUnique ),
|
|
252
|
+
);
|
|
253
|
+
};
|
|
254
|
+
|
|
255
|
+
const adapter = ref
|
|
256
|
+
? ( getAdapterInRef() || getAdapterByName() || getAdapterOnDemand() )
|
|
257
|
+
: ( getAdapterInCurrentContext() || getAdapterByName() || getAdapterOnDemand() );
|
|
258
|
+
|
|
259
|
+
return adapter;
|
|
260
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
|
|
2
|
+
import React from "react";
|
|
3
|
+
|
|
4
|
+
import { useComponent } from "./use-component.js";
|
|
5
|
+
|
|
6
|
+
import core from "../../_internal/core.js";
|
|
7
|
+
|
|
8
|
+
import { GET_INTERNAL_MATCH_SYMBOL } from "../../_internal/constants.js";
|
|
9
|
+
|
|
10
|
+
export function useComponentWithRef( componentName: string, ref: React.RefObject<any> ): ReturnType<typeof useComponent> | null {
|
|
11
|
+
const [ id, setId ] = React.useState<ReturnType<typeof useComponent> | null>( null );
|
|
12
|
+
|
|
13
|
+
React.useEffect( () => {
|
|
14
|
+
try {
|
|
15
|
+
const contexts = core[ GET_INTERNAL_MATCH_SYMBOL ]( componentName + "*" );
|
|
16
|
+
|
|
17
|
+
const currentContext = contexts.find( ( ctx ) => ctx.getComponentContext().getComponentRef().current === ref.current );
|
|
18
|
+
|
|
19
|
+
if ( currentContext ) {
|
|
20
|
+
setId( useComponent( componentName, currentContext.getComponentContext() ) );
|
|
21
|
+
}
|
|
22
|
+
} catch {
|
|
23
|
+
setId( null );
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
}, [ componentName, ref.current ] );
|
|
27
|
+
|
|
28
|
+
return id;
|
|
29
|
+
}
|
|
30
|
+
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
|
|
2
|
+
import React from "react";
|
|
3
|
+
|
|
4
|
+
import { GET_INTERNAL_SYMBOL } from "../../_internal/constants.js";
|
|
5
|
+
|
|
6
|
+
import core from "../../_internal/core.js";
|
|
7
|
+
import commandsManager from "../../commands-manager.js";
|
|
8
|
+
import { ComponentIdContext } from "../../commands-context.js";
|
|
9
|
+
|
|
10
|
+
import type { DCommandArgs, DCommandComponentContextProps, DCommandHookHandle } from "../../definitions.js";
|
|
11
|
+
|
|
12
|
+
export function getSafeContext( componentName: string, context?: DCommandComponentContextProps ) {
|
|
13
|
+
function maybeWrongContext( componentName: string, componentNameUnique: string ) {
|
|
14
|
+
if ( componentName === componentNameUnique ) {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
throw new Error(
|
|
18
|
+
`You are not in: '${ componentName }', you are in '${ componentNameUnique }' which is not your context\n` +
|
|
19
|
+
"If you are trying to reach sub-component context, it has to rendered, before you can use it\n",
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const componentContext = context || React.useContext( ComponentIdContext );
|
|
24
|
+
|
|
25
|
+
const componentNameContext = componentContext.getComponentName();
|
|
26
|
+
|
|
27
|
+
maybeWrongContext( componentName, componentNameContext );
|
|
28
|
+
|
|
29
|
+
return componentContext;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function useComponent( componentName: string, context?: DCommandComponentContextProps, options = { silent: false } ) {
|
|
33
|
+
if ( ! options.silent ) {
|
|
34
|
+
context = getSafeContext( componentName, context );
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const id = context!.getNameUnique();
|
|
38
|
+
|
|
39
|
+
return {
|
|
40
|
+
run: ( commandName: string, args: DCommandArgs, callback?: ( result: any ) => void ) =>
|
|
41
|
+
commandsManager.run( { commandName, componentName, componentNameUnique: id }, args, callback ),
|
|
42
|
+
hook: ( commandName: string, callback: ( result?: any, args?: DCommandArgs ) => void ) =>
|
|
43
|
+
commandsManager.hook( { commandName, componentName, componentNameUnique: id }, callback ),
|
|
44
|
+
unhook: ( commandName: string ) =>
|
|
45
|
+
commandsManager.unhook( { commandName, componentName, componentNameUnique: id } ),
|
|
46
|
+
unhookHandle: ( handle: DCommandHookHandle ) =>
|
|
47
|
+
commandsManager.unhookHandle( handle ),
|
|
48
|
+
|
|
49
|
+
getId: () => id,
|
|
50
|
+
getKey: () => core[ GET_INTERNAL_SYMBOL ]( id ).key,
|
|
51
|
+
isAlive: () => !! core[ GET_INTERNAL_SYMBOL ]( id, true ),
|
|
52
|
+
getInternalContext: () => core[ GET_INTERNAL_SYMBOL ]( id ),
|
|
53
|
+
getContext: () => context!,
|
|
54
|
+
getState: <TState extends React.ComponentState>() => core[ GET_INTERNAL_SYMBOL ]( id ).getState() as TState,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
|
|
2
|
+
import React from "react";
|
|
3
|
+
|
|
4
|
+
import { useComponent } from "./use-component/use-component.js";
|
|
5
|
+
|
|
6
|
+
import { ComponentIdContext } from "../commands-context.js";
|
|
7
|
+
import commandsManager from "../commands-manager.js";
|
|
8
|
+
|
|
9
|
+
import type { DCommandComponentContextProps } from "../definitions.js";
|
|
10
|
+
|
|
11
|
+
export { getSafeContext } from "./use-component/use-component.js";
|
|
12
|
+
|
|
13
|
+
export function shallowEqual<T extends Record<string, unknown>>( a: T, b: T ): boolean {
|
|
14
|
+
if ( a === b ) return true;
|
|
15
|
+
if ( ! a || ! b ) return false;
|
|
16
|
+
|
|
17
|
+
const aKeys = Object.keys( a );
|
|
18
|
+
const bKeys = Object.keys( b );
|
|
19
|
+
if ( aKeys.length !== bKeys.length ) return false;
|
|
20
|
+
|
|
21
|
+
for ( const key of aKeys ) {
|
|
22
|
+
if ( ( a as Record<string, unknown> )[ key ] !== ( b as Record<string, unknown> )[ key ] ) {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function useCommanderChildrenComponents(
|
|
30
|
+
componentName: string,
|
|
31
|
+
onChildrenUpdate?: ( commands: ReturnType<typeof useComponent>[] ) => ( () => void ) | void,
|
|
32
|
+
) {
|
|
33
|
+
const componentContext = React.useContext( ComponentIdContext );
|
|
34
|
+
|
|
35
|
+
const [ childrenComponents, setChildrenComponents ] = React.useState<ReturnType<typeof useComponent>[]>( [] );
|
|
36
|
+
|
|
37
|
+
function getDescendantsKeys( context: DCommandComponentContextProps ) {
|
|
38
|
+
let keys: string[] = [];
|
|
39
|
+
|
|
40
|
+
if ( context.children ) {
|
|
41
|
+
for ( const key in context.children ) {
|
|
42
|
+
keys.push( key );
|
|
43
|
+
|
|
44
|
+
keys = keys.concat( getDescendantsKeys( context.children[ key ] ) );
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return keys.join( "-" );
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
React.useEffect( () => {
|
|
52
|
+
const children = componentContext.children;
|
|
53
|
+
|
|
54
|
+
if ( ! children ) {
|
|
55
|
+
throw new Error( `Current component: '${ componentContext.getComponentName() }' cannot find: '${ componentName }' children` );
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const newChildrenComponents: ReturnType<typeof useComponent>[] = [];
|
|
59
|
+
|
|
60
|
+
const loopChildren = ( children: { [ x: string ]: DCommandComponentContextProps; } ) => {
|
|
61
|
+
for ( const childName in children ) {
|
|
62
|
+
const child = children[ childName ];
|
|
63
|
+
|
|
64
|
+
if ( child.getComponentName() === componentName ) {
|
|
65
|
+
const id = child.getNameUnique();
|
|
66
|
+
if ( commandsManager.isContextRegistered( id ) ) {
|
|
67
|
+
const childComponent = useComponent( componentName, child );
|
|
68
|
+
newChildrenComponents.push( childComponent );
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if ( child.children ) {
|
|
73
|
+
loopChildren( child.children );
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
loopChildren( children );
|
|
79
|
+
|
|
80
|
+
setChildrenComponents( newChildrenComponents );
|
|
81
|
+
|
|
82
|
+
const callback = onChildrenUpdate?.( newChildrenComponents );
|
|
83
|
+
|
|
84
|
+
return () => {
|
|
85
|
+
callback?.();
|
|
86
|
+
};
|
|
87
|
+
}, [ getDescendantsKeys( componentContext ) ] );
|
|
88
|
+
|
|
89
|
+
return childrenComponents;
|
|
90
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from "@zenflux/react-commander/command-base";
|
|
2
|
+
export * from "@zenflux/react-commander/commands-manager";
|
|
3
|
+
export * from "@zenflux/react-commander/commands-context";
|
|
4
|
+
export * from "@zenflux/react-commander/commands-provider";
|
|
5
|
+
export * from "@zenflux/react-commander/with-commands";
|
|
6
|
+
export * from "@zenflux/react-commander/hooks";
|
|
7
|
+
export * from "@zenflux/react-commander/definitions";
|
|
8
|
+
export * from "@zenflux/react-commander/constants";
|
|
9
|
+
export * from "@zenflux/react-commander/core";
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
export type QuerySaveInput = { key: string } & object;
|
|
2
|
+
|
|
3
|
+
export type QueryCreateAutoSaveManagerOptions<TState extends object, TSave extends QuerySaveInput> = {
|
|
4
|
+
getKey( state: TState ): string;
|
|
5
|
+
pickToSave( state: TState ): TSave;
|
|
6
|
+
save( input: TSave ): Promise<void>;
|
|
7
|
+
debounceMs?: number;
|
|
8
|
+
intervalMs?: number;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export type QueryAutoSaveManager<TState extends object, _TSave extends QuerySaveInput> = {
|
|
12
|
+
queryUpsert( state: TState, immediate?: boolean ): Promise<void>;
|
|
13
|
+
queryFlush(): Promise<void>;
|
|
14
|
+
queryFlushKey( key: string ): Promise<void>;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export function queryCreateAutoSaveManager<TState extends object, TSave extends QuerySaveInput>( options: QueryCreateAutoSaveManagerOptions<TState, TSave> ): QueryAutoSaveManager<TState, TSave> {
|
|
18
|
+
let debounceId: number | undefined;
|
|
19
|
+
const pending = new Map<string, TState>();
|
|
20
|
+
const debounceMs = options.debounceMs ?? 800;
|
|
21
|
+
const intervalMs = options.intervalMs ?? 5000;
|
|
22
|
+
|
|
23
|
+
const queryFlush = async () => {
|
|
24
|
+
const entries = Array.from( pending.values() );
|
|
25
|
+
if ( ! entries.length ) return;
|
|
26
|
+
pending.clear();
|
|
27
|
+
await Promise.all( entries.map( s => options.save( options.pickToSave( s ) ) ) );
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const schedule = ( immediate: boolean ) => {
|
|
31
|
+
if ( immediate ) return queryFlush();
|
|
32
|
+
if ( debounceId ) clearTimeout( debounceId );
|
|
33
|
+
debounceId = setTimeout( () => { void queryFlush(); }, debounceMs ) as unknown as number;
|
|
34
|
+
return Promise.resolve();
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const queryUpsert = ( state: TState, immediate?: boolean ) => {
|
|
38
|
+
pending.set( options.getKey( state ), state );
|
|
39
|
+
return schedule( Boolean( immediate ) );
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const queryFlushKey = async ( key: string ) => {
|
|
43
|
+
const s = pending.get( key );
|
|
44
|
+
if ( ! s ) return;
|
|
45
|
+
pending.delete( key );
|
|
46
|
+
await options.save( options.pickToSave( s ) );
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
setInterval( () => { void schedule( false ); }, intervalMs );
|
|
50
|
+
window.addEventListener( "beforeunload", () => { void queryFlush(); } );
|
|
51
|
+
|
|
52
|
+
return { queryUpsert, queryFlush, queryFlushKey };
|
|
53
|
+
}
|
|
54
|
+
|