@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,363 @@
|
|
|
1
|
+
import { EventEmitter } from "eventemitter3";
|
|
2
|
+
|
|
3
|
+
import React from "react";
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
GET_INTERNAL_SYMBOL,
|
|
7
|
+
INTERNAL_PROPS,
|
|
8
|
+
REGISTER_INTERNAL_SYMBOL,
|
|
9
|
+
INTERNAL_ON_UNMOUNT,
|
|
10
|
+
UNREGISTER_INTERNAL_SYMBOL,
|
|
11
|
+
SET_TO_CONTEXT_SYMBOL,
|
|
12
|
+
INTERNAL_ON_MOUNT,
|
|
13
|
+
INTERNAL_ON_UPDATE,
|
|
14
|
+
INTERNAL_ON_LOAD,
|
|
15
|
+
INTERNAL_ON_CONTEXT_STATE_UPDATED,
|
|
16
|
+
INTERNAL_STATE_UPDATED_EVENT
|
|
17
|
+
} from "./_internal/constants.js";
|
|
18
|
+
|
|
19
|
+
import core from "./_internal/core.js";
|
|
20
|
+
|
|
21
|
+
import { ComponentIdContext } from "@zenflux/react-commander/commands-context";
|
|
22
|
+
|
|
23
|
+
import commandsManager from "@zenflux/react-commander/commands-manager";
|
|
24
|
+
|
|
25
|
+
import { ComponentIdProvider } from "@zenflux/react-commander/commands-provider";
|
|
26
|
+
|
|
27
|
+
import type {
|
|
28
|
+
DCommandFunctionComponent,
|
|
29
|
+
DCommandNewInstanceWithArgs,
|
|
30
|
+
DCommandComponentContextProps
|
|
31
|
+
} from "@zenflux/react-commander/definitions";
|
|
32
|
+
|
|
33
|
+
export function withCommands<TProps = any, TState = undefined>(
|
|
34
|
+
componentName: string,
|
|
35
|
+
Component: DCommandFunctionComponent<TProps, TState>,
|
|
36
|
+
state: TState,
|
|
37
|
+
commands: DCommandNewInstanceWithArgs<TState>[]
|
|
38
|
+
): DCommandFunctionComponent<TProps, TState>;
|
|
39
|
+
|
|
40
|
+
export function withCommands<TProps>(
|
|
41
|
+
componentName: string,
|
|
42
|
+
Component: DCommandFunctionComponent<TProps>,
|
|
43
|
+
commands: DCommandNewInstanceWithArgs[]
|
|
44
|
+
): DCommandFunctionComponent<TProps>;
|
|
45
|
+
|
|
46
|
+
export function withCommands(
|
|
47
|
+
componentName: string,
|
|
48
|
+
Component: DCommandFunctionComponent,
|
|
49
|
+
commands: DCommandNewInstanceWithArgs[]
|
|
50
|
+
): DCommandFunctionComponent;
|
|
51
|
+
|
|
52
|
+
export function withCommands(
|
|
53
|
+
componentName: string,
|
|
54
|
+
Component: DCommandFunctionComponent,
|
|
55
|
+
... args: any[]
|
|
56
|
+
): DCommandFunctionComponent {
|
|
57
|
+
let commands: DCommandNewInstanceWithArgs[],
|
|
58
|
+
state: React.ComponentState;
|
|
59
|
+
|
|
60
|
+
if ( args.length === 1 ) {
|
|
61
|
+
commands = args[ 0 ];
|
|
62
|
+
} else if ( args.length === 2 ) {
|
|
63
|
+
state = args[ 0 ];
|
|
64
|
+
commands = args[ 1 ];
|
|
65
|
+
} else {
|
|
66
|
+
throw new Error( "Invalid arguments" );
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function shallowEqual<T extends Record<string, unknown>>( a: T, b: T ): boolean {
|
|
70
|
+
if ( a === b ) return true;
|
|
71
|
+
if ( ! a || ! b ) return false;
|
|
72
|
+
|
|
73
|
+
const aKeys = Object.keys( a );
|
|
74
|
+
const bKeys = Object.keys( b );
|
|
75
|
+
if ( aKeys.length !== bKeys.length ) return false;
|
|
76
|
+
|
|
77
|
+
for ( const key of aKeys ) {
|
|
78
|
+
if ( ( a as Record<string, unknown> )[ key ] !== ( b as Record<string, unknown> )[ key ] ) {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return true;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
class Store {
|
|
86
|
+
private silentState: any;
|
|
87
|
+
private currentState: any;
|
|
88
|
+
private prevState: any;
|
|
89
|
+
|
|
90
|
+
public constructor( initialState: any ) {
|
|
91
|
+
this.currentState = initialState;
|
|
92
|
+
this.prevState = initialState;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
public getState() {
|
|
96
|
+
return this.silentState || this.currentState;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
public getPrevState() {
|
|
100
|
+
return this.prevState;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
public setState( newState: any, silent = false ) {
|
|
104
|
+
this.prevState = this.currentState;
|
|
105
|
+
|
|
106
|
+
if ( silent ) {
|
|
107
|
+
this.silentState = newState;
|
|
108
|
+
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
this.silentState = null;
|
|
113
|
+
this.currentState = newState;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
public hasChanged() {
|
|
117
|
+
if ( this.prevState === this.currentState ) {
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return ! shallowEqual( this.prevState, this.currentState );
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if ( state ) {
|
|
127
|
+
const originalFunction = Component,
|
|
128
|
+
originalName = Component.displayName || Component.name || "Component";
|
|
129
|
+
|
|
130
|
+
// This approach give us ability to inject second argument to the functional component.
|
|
131
|
+
Component = function ( props: any ) {
|
|
132
|
+
return originalFunction( props, state );
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
Object.defineProperty( Component, originalName, { value: originalName, writable: false } );
|
|
136
|
+
|
|
137
|
+
Component.displayName = `withInjectedState(${ originalName })`;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const WrappedComponent = class WrappedComponent extends React.PureComponent<any, React.ComponentState> {
|
|
141
|
+
public static displayName = `withCommands(${ componentName })`;
|
|
142
|
+
|
|
143
|
+
public static contextType = ComponentIdContext;
|
|
144
|
+
|
|
145
|
+
public context: DCommandComponentContextProps;
|
|
146
|
+
|
|
147
|
+
private store: Store;
|
|
148
|
+
|
|
149
|
+
private $$commander = {
|
|
150
|
+
isMounted: false,
|
|
151
|
+
lifecycleHandlers: {} as any,
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
private isMounted() {
|
|
155
|
+
return this.$$commander.isMounted;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
public constructor( props: any, context: any ) {
|
|
159
|
+
super( props );
|
|
160
|
+
|
|
161
|
+
this.context = context;
|
|
162
|
+
|
|
163
|
+
this.state = {};
|
|
164
|
+
this.store = new Store( state );
|
|
165
|
+
|
|
166
|
+
this.registerInternalContext();
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
private registerInternalContext() {
|
|
170
|
+
const id = this.context.getNameUnique();
|
|
171
|
+
|
|
172
|
+
if ( this.props[ INTERNAL_PROPS ]?.handlers ) {
|
|
173
|
+
this.$$commander.lifecycleHandlers = this.props[ INTERNAL_PROPS ].handlers;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if ( commandsManager.isContextRegistered( id ) ) {
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const self = this;
|
|
181
|
+
|
|
182
|
+
core[ REGISTER_INTERNAL_SYMBOL ]( {
|
|
183
|
+
componentName,
|
|
184
|
+
componentNameUnique: id,
|
|
185
|
+
|
|
186
|
+
commands: commandsManager.get( componentName ),
|
|
187
|
+
emitter: new EventEmitter(),
|
|
188
|
+
|
|
189
|
+
key: self.props.$$key,
|
|
190
|
+
|
|
191
|
+
isMounted: () => self.isMounted(),
|
|
192
|
+
|
|
193
|
+
getComponentContext: () => self.context,
|
|
194
|
+
|
|
195
|
+
getState: () => this.store ? this.store.getState() : this.state,
|
|
196
|
+
setState: ( state: React.ComponentState, callback?: ( state: React.ComponentState ) => void ) => {
|
|
197
|
+
const prevState = this.store.getState();
|
|
198
|
+
|
|
199
|
+
this.store.setState(
|
|
200
|
+
{
|
|
201
|
+
... prevState,
|
|
202
|
+
... state
|
|
203
|
+
},
|
|
204
|
+
! this.isMounted(),
|
|
205
|
+
);
|
|
206
|
+
|
|
207
|
+
const currentState = this.store.getState();
|
|
208
|
+
|
|
209
|
+
if ( this.isMounted() ) {
|
|
210
|
+
const hasChanged = ! shallowEqual( prevState, currentState );
|
|
211
|
+
|
|
212
|
+
if ( hasChanged ) {
|
|
213
|
+
queueMicrotask( () => {
|
|
214
|
+
if ( this.isMounted() ) {
|
|
215
|
+
const ctx = core[ GET_INTERNAL_SYMBOL ]( this.context.getNameUnique() );
|
|
216
|
+
|
|
217
|
+
ctx.emitter.emit( INTERNAL_STATE_UPDATED_EVENT );
|
|
218
|
+
this.$$commander.lifecycleHandlers[ INTERNAL_ON_CONTEXT_STATE_UPDATED ]?.( ctx, true );
|
|
219
|
+
}
|
|
220
|
+
} );
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
if ( callback ) {
|
|
225
|
+
callback( currentState );
|
|
226
|
+
}
|
|
227
|
+
},
|
|
228
|
+
|
|
229
|
+
lifecycleHandlers: this.$$commander.lifecycleHandlers,
|
|
230
|
+
} );
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
public componentWillUnmount() {
|
|
234
|
+
this.$$commander.isMounted = false;
|
|
235
|
+
|
|
236
|
+
const onUnmount = this.$$commander.lifecycleHandlers[ INTERNAL_ON_UNMOUNT ];
|
|
237
|
+
|
|
238
|
+
if ( onUnmount ) {
|
|
239
|
+
onUnmount( core[ GET_INTERNAL_SYMBOL ]( this.context.getNameUnique() ) );
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
const componentNameUnique = this.context.getNameUnique();
|
|
243
|
+
|
|
244
|
+
core[ UNREGISTER_INTERNAL_SYMBOL ]( componentNameUnique );
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Using `componentDidMount` in a strict mode causes component to unmount therefor the context need to be
|
|
249
|
+
* re-registered.
|
|
250
|
+
*/
|
|
251
|
+
public componentDidMount() {
|
|
252
|
+
const id = this.context.getNameUnique();
|
|
253
|
+
|
|
254
|
+
this.$$commander.isMounted = true;
|
|
255
|
+
|
|
256
|
+
this.registerInternalContext();
|
|
257
|
+
|
|
258
|
+
core[ SET_TO_CONTEXT_SYMBOL ]( id, { props: this.props } );
|
|
259
|
+
|
|
260
|
+
if ( this.$$commander.lifecycleHandlers[ INTERNAL_ON_MOUNT ] ) {
|
|
261
|
+
this.$$commander.lifecycleHandlers[ INTERNAL_ON_MOUNT ]( core[ GET_INTERNAL_SYMBOL ]( this.context.getNameUnique() ) );
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
public componentDidUpdate( prevProps: any, prevState: any, snapshot?: any ) {
|
|
266
|
+
if ( this.$$commander.lifecycleHandlers[ INTERNAL_ON_UPDATE ] ) {
|
|
267
|
+
const context = core[ GET_INTERNAL_SYMBOL ]( this.context.getNameUnique() );
|
|
268
|
+
|
|
269
|
+
this.$$commander.lifecycleHandlers[ INTERNAL_ON_UPDATE ]( context, {
|
|
270
|
+
currentProps: this.props,
|
|
271
|
+
currentState: this.store.getState(),
|
|
272
|
+
prevProps,
|
|
273
|
+
prevState: this.store.getPrevState(),
|
|
274
|
+
snapshot,
|
|
275
|
+
} );
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
public render() {
|
|
280
|
+
if ( this.$$commander.lifecycleHandlers[ INTERNAL_ON_LOAD ] ) {
|
|
281
|
+
this.$$commander.lifecycleHandlers[ INTERNAL_ON_LOAD ]( core[ GET_INTERNAL_SYMBOL ]( this.context.getNameUnique() ) );
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
return <Component { ... this.props } />;
|
|
285
|
+
}
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Function `handleAncestorContexts()` - Manage and manipulate the hierarchical context structure
|
|
290
|
+
* This function is designed to handle the relationship between a provided context and its parent context within a given
|
|
291
|
+
* component hierarchy. By checking if the parent context is already set, it assigns the parent context to the current
|
|
292
|
+
* context and ensures the correct child-parent relationships are established. Moreover, it also manages the children of the
|
|
293
|
+
* current context by validating their existence in the internal mapping system, removing any child contexts that do not have
|
|
294
|
+
* a corresponding internal context. This ensures a consistent and error-free context hierarchy within the application's
|
|
295
|
+
* command component structure.
|
|
296
|
+
**/
|
|
297
|
+
function handleAncestorContexts( context: DCommandComponentContextProps, parentContext: DCommandComponentContextProps ) {
|
|
298
|
+
|
|
299
|
+
if ( parentContext.isSet ) {
|
|
300
|
+
context.parent = parentContext;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
if ( context.parent ) {
|
|
304
|
+
if ( ! context.parent.children ) {
|
|
305
|
+
context.parent.children = {};
|
|
306
|
+
}
|
|
307
|
+
context.parent.children[ context.getNameUnique() ] = context;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
if ( context.children ) {
|
|
311
|
+
for ( const key in context.children ) {
|
|
312
|
+
const child = context.children[ key ];
|
|
313
|
+
|
|
314
|
+
const internalContext = core[ GET_INTERNAL_SYMBOL ]( child.getNameUnique(), true );
|
|
315
|
+
|
|
316
|
+
if ( ! internalContext ) {
|
|
317
|
+
delete context.children[ key ];
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* React `useId` behave differently in production and development mode, because of `<React.StrictMode>`
|
|
325
|
+
* https://github.com/facebook/react/issues/27103#issuecomment-1763359077
|
|
326
|
+
*/
|
|
327
|
+
const UniqueWrappedComponent = React.forwardRef( ( props: any, _ref: React.ForwardedRef<any> ) => {
|
|
328
|
+
const parentContext = React.useContext( ComponentIdContext );
|
|
329
|
+
|
|
330
|
+
const componentNameUnique = `${ componentName }-${ React.useId() }`;
|
|
331
|
+
const componentRef = React.useRef( null );
|
|
332
|
+
|
|
333
|
+
const context: DCommandComponentContextProps = {
|
|
334
|
+
isSet: true,
|
|
335
|
+
|
|
336
|
+
getNameUnique: () => componentNameUnique,
|
|
337
|
+
getComponentName: () => componentName,
|
|
338
|
+
getComponentRef: () => componentRef,
|
|
339
|
+
};
|
|
340
|
+
|
|
341
|
+
React.useLayoutEffect( () => {
|
|
342
|
+
handleAncestorContexts( context, parentContext );
|
|
343
|
+
}, [ context ] );
|
|
344
|
+
|
|
345
|
+
React.useImperativeHandle( _ref, () => componentRef.current, [ componentRef.current ] );
|
|
346
|
+
|
|
347
|
+
return (
|
|
348
|
+
<ComponentIdProvider context={ context }>
|
|
349
|
+
<WrappedComponent { ... props } ref={ componentRef } $$key={ performance.now() }/>
|
|
350
|
+
</ComponentIdProvider>
|
|
351
|
+
);
|
|
352
|
+
} ) as DCommandFunctionComponent;
|
|
353
|
+
|
|
354
|
+
UniqueWrappedComponent.getName = () => componentName;
|
|
355
|
+
|
|
356
|
+
commandsManager.register( {
|
|
357
|
+
componentName,
|
|
358
|
+
commands,
|
|
359
|
+
} );
|
|
360
|
+
|
|
361
|
+
return UniqueWrappedComponent;
|
|
362
|
+
}
|
|
363
|
+
|