@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,18 @@
|
|
|
1
|
+
// Same as https://github.com/facebook/react/blob/main/packages/react-dom-bindings/src/client/ReactDOMComponentTree.js#L39
|
|
2
|
+
const randomKey = "__" + Math.random().toString( 36 ).slice( 2 );
|
|
3
|
+
|
|
4
|
+
export const REGISTER_INTERNAL_SYMBOL = Symbol( "REGISTER_INTERNAL" + randomKey );
|
|
5
|
+
export const UNREGISTER_INTERNAL_SYMBOL = Symbol( "UNREGISTER_INTERNAL" + randomKey );
|
|
6
|
+
export const GET_INTERNAL_SYMBOL = Symbol( "GET_INTERNAL" + randomKey );
|
|
7
|
+
export const GET_INTERNAL_MATCH_SYMBOL = Symbol( "GET_INTERNAL_MATCH" + randomKey );
|
|
8
|
+
export const SET_TO_CONTEXT_SYMBOL = Symbol( "SET_TO_CONTEXT" + randomKey );
|
|
9
|
+
|
|
10
|
+
export const INTERNAL_ON_LOAD = "__internalOnLoad" + randomKey;
|
|
11
|
+
export const INTERNAL_ON_MOUNT = "__internalOnMount" + randomKey;
|
|
12
|
+
export const INTERNAL_ON_UNMOUNT = "__internalOnUnmount" + randomKey;
|
|
13
|
+
export const INTERNAL_ON_UPDATE = "__internalOnUpdate" + randomKey;
|
|
14
|
+
export const INTERNAL_ON_CONTEXT_STATE_UPDATED = "__internalOnContextStateUpdated" + randomKey;
|
|
15
|
+
|
|
16
|
+
export const INTERNAL_PROPS = "__internalProps" + randomKey;
|
|
17
|
+
|
|
18
|
+
export const INTERNAL_STATE_UPDATED_EVENT = "__internalStateUpdated" + randomKey;
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
|
|
2
|
+
import {
|
|
3
|
+
GET_INTERNAL_MATCH_SYMBOL,
|
|
4
|
+
GET_INTERNAL_SYMBOL,
|
|
5
|
+
REGISTER_INTERNAL_SYMBOL,
|
|
6
|
+
SET_TO_CONTEXT_SYMBOL,
|
|
7
|
+
UNREGISTER_INTERNAL_SYMBOL
|
|
8
|
+
} from "./constants.js";
|
|
9
|
+
|
|
10
|
+
import type { DCoreContext, DCoreInterface, DCoreRegisterArgs } from "./definitions.js";
|
|
11
|
+
|
|
12
|
+
import type { DCommandSingleComponentContext } from "@zenflux/react-commander/definitions";
|
|
13
|
+
|
|
14
|
+
const context: DCoreContext = {};
|
|
15
|
+
|
|
16
|
+
// This will hold the sorted keys for the binary search
|
|
17
|
+
let sortedContextKeys: string[] = [];
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Core class is responsible for managing the command context for each component.
|
|
21
|
+
* It provides methods to register, unregister, get and link components.
|
|
22
|
+
*/
|
|
23
|
+
class Core implements DCoreInterface {
|
|
24
|
+
/**
|
|
25
|
+
* Registers the context for a React component.
|
|
26
|
+
*/
|
|
27
|
+
[ REGISTER_INTERNAL_SYMBOL ]( args: DCoreRegisterArgs ): void { // eslint-disable-line @typescript-eslint/explicit-member-accessibility
|
|
28
|
+
const {
|
|
29
|
+
componentNameUnique,
|
|
30
|
+
componentName,
|
|
31
|
+
commands,
|
|
32
|
+
emitter,
|
|
33
|
+
getComponentContext,
|
|
34
|
+
getState,
|
|
35
|
+
setState,
|
|
36
|
+
isMounted,
|
|
37
|
+
key,
|
|
38
|
+
lifecycleHandlers
|
|
39
|
+
} = args;
|
|
40
|
+
|
|
41
|
+
this.__devDebug( `Registering component '${ componentNameUnique }'` );
|
|
42
|
+
|
|
43
|
+
// Check if the component is registered
|
|
44
|
+
if ( context[ componentNameUnique ] ) {
|
|
45
|
+
throw new Error( `Component '${ componentNameUnique }' already registered` );
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Register the context
|
|
49
|
+
context[ componentNameUnique ] = {
|
|
50
|
+
commands,
|
|
51
|
+
componentName,
|
|
52
|
+
componentNameUnique,
|
|
53
|
+
emitter,
|
|
54
|
+
getComponentContext,
|
|
55
|
+
getState,
|
|
56
|
+
setState,
|
|
57
|
+
isMounted,
|
|
58
|
+
key,
|
|
59
|
+
props: undefined,
|
|
60
|
+
lifecycleHandlers,
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
// Update sorted keys
|
|
64
|
+
this.updateSortedContextKeys();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Unregisters the context.
|
|
69
|
+
*/
|
|
70
|
+
[ UNREGISTER_INTERNAL_SYMBOL ]( // eslint-disable-line @typescript-eslint/explicit-member-accessibility
|
|
71
|
+
componentNameUnique: string
|
|
72
|
+
): void {
|
|
73
|
+
this.__devDebug( `Unregistering component '${ componentNameUnique }' from the context ` );
|
|
74
|
+
|
|
75
|
+
// Check if the component is registered
|
|
76
|
+
if ( ! context[ componentNameUnique ] ) {
|
|
77
|
+
throw new Error( `Component '${ componentNameUnique }' not registered` );
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Clean up emitter
|
|
81
|
+
context[ componentNameUnique ].emitter.removeAllListeners();
|
|
82
|
+
|
|
83
|
+
// Clean up context
|
|
84
|
+
delete context[ componentNameUnique ];
|
|
85
|
+
|
|
86
|
+
// Update sorted keys
|
|
87
|
+
this.updateSortedContextKeys();
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Gets the context for a React component.
|
|
92
|
+
*/
|
|
93
|
+
[ GET_INTERNAL_SYMBOL ]( // eslint-disable-line @typescript-eslint/explicit-member-accessibility
|
|
94
|
+
componentNameUnique: string,
|
|
95
|
+
silent?: boolean,
|
|
96
|
+
): DCommandSingleComponentContext {
|
|
97
|
+
// Check if the component is registered
|
|
98
|
+
if ( ! silent && ! context[ componentNameUnique ] ) {
|
|
99
|
+
throw new Error( `Component '${ componentNameUnique }' not registered` );
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return context[ componentNameUnique ];
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
[ GET_INTERNAL_MATCH_SYMBOL ]( // eslint-disable-line @typescript-eslint/explicit-member-accessibility
|
|
106
|
+
componentName: string,
|
|
107
|
+
): DCommandSingleComponentContext[] {
|
|
108
|
+
if ( componentName.includes( "*" ) ) {
|
|
109
|
+
const result: DCommandSingleComponentContext[] = [];
|
|
110
|
+
|
|
111
|
+
const anyMatchComponent = componentName.substring( 0, componentName.length - 1 );
|
|
112
|
+
|
|
113
|
+
sortedContextKeys.forEach( ( componentNameUnique ) => {
|
|
114
|
+
if ( componentNameUnique.includes( anyMatchComponent ) ) {
|
|
115
|
+
result.push( context[ componentNameUnique ] );
|
|
116
|
+
}
|
|
117
|
+
} );
|
|
118
|
+
|
|
119
|
+
return result;
|
|
120
|
+
} else {
|
|
121
|
+
const index = this.binarySearch( sortedContextKeys, componentName );
|
|
122
|
+
if ( index !== -1 ) {
|
|
123
|
+
return [ context[ sortedContextKeys[ index ] ] ];
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
throw new Error( `Component '${ componentName }' not found` );
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
[ SET_TO_CONTEXT_SYMBOL ]( // eslint-disable-line @typescript-eslint/explicit-member-accessibility
|
|
131
|
+
componentNameUnique: string,
|
|
132
|
+
data: { [ key: string ]: any },
|
|
133
|
+
): void {
|
|
134
|
+
if ( ! context[ componentNameUnique ] ) {
|
|
135
|
+
throw new Error( `Component '${ componentNameUnique }' not registered` );
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
Object.entries( data ).forEach( ( [ key, value ] ) => {
|
|
139
|
+
// @ts-ignore
|
|
140
|
+
context[ componentNameUnique ][ key ] = value;
|
|
141
|
+
} );
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Updates the sorted keys for binary search.
|
|
146
|
+
*/
|
|
147
|
+
private updateSortedContextKeys() {
|
|
148
|
+
sortedContextKeys = Object.keys( context ).sort();
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Binary Search function to find a context entry by its symbol
|
|
153
|
+
* @param array - The sorted array of context entry keys.
|
|
154
|
+
* @param symbol - The symbol to find.
|
|
155
|
+
* @returns Index of the found context entry, or -1 if not found.
|
|
156
|
+
*/
|
|
157
|
+
private binarySearch( array: string[], symbol: string ): number {
|
|
158
|
+
let left = 0;
|
|
159
|
+
let right = array.length - 1;
|
|
160
|
+
|
|
161
|
+
while ( left <= right ) {
|
|
162
|
+
const mid = Math.floor( ( left + right ) / 2 );
|
|
163
|
+
if ( array[ mid ] === symbol ) {
|
|
164
|
+
return mid;
|
|
165
|
+
} else if ( array[ mid ] < symbol ) {
|
|
166
|
+
left = mid + 1;
|
|
167
|
+
} else {
|
|
168
|
+
right = mid - 1;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
return -1; // Symbol not found
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
176
|
+
public __devDebug( ... args: any[] ): void {
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
declare public __devGetContextLength: () => number;
|
|
180
|
+
declare public __devGetContextKeys: () => string[];
|
|
181
|
+
declare public __devGetContextValues: () => DCommandSingleComponentContext[];
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const core: DCoreInterface = new Core();
|
|
185
|
+
|
|
186
|
+
if ( ( import.meta as ImportMeta & { env?: { DEV?: boolean } } ).env?.DEV ) {
|
|
187
|
+
if ( ( window as any ).__DEBUG__ ) {
|
|
188
|
+
core.__devDebug = ( ... args: any[] ) => {
|
|
189
|
+
console.log( ... args );
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
core.__devGetContextLength = () => {
|
|
194
|
+
return Object.keys( context ).length;
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
core.__devGetContextKeys = () => {
|
|
198
|
+
return Object.keys( context );
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
core.__devGetContextValues = () => {
|
|
202
|
+
return Object.values( context ).map( ( context ) => context );
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
( window as any ).$$core = core;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
export default core;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type React from "react";
|
|
2
|
+
|
|
3
|
+
import type {
|
|
4
|
+
GET_INTERNAL_MATCH_SYMBOL,
|
|
5
|
+
GET_INTERNAL_SYMBOL,
|
|
6
|
+
REGISTER_INTERNAL_SYMBOL,
|
|
7
|
+
SET_TO_CONTEXT_SYMBOL,
|
|
8
|
+
UNREGISTER_INTERNAL_SYMBOL
|
|
9
|
+
} from "./constants.js";
|
|
10
|
+
|
|
11
|
+
import type { DCommandSingleComponentContext } from "@zenflux/react-commander/definitions";
|
|
12
|
+
|
|
13
|
+
import type { EventEmitter } from "eventemitter3";
|
|
14
|
+
|
|
15
|
+
export type DCoreContext = {
|
|
16
|
+
[componentNameUnique: string]: DCommandSingleComponentContext;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export type DCoreRegisterArgs = {
|
|
20
|
+
componentNameUnique: string;
|
|
21
|
+
componentName: string;
|
|
22
|
+
commands: DCommandSingleComponentContext[ "commands" ];
|
|
23
|
+
emitter: EventEmitter;
|
|
24
|
+
isMounted(): boolean;
|
|
25
|
+
key: React.Key;
|
|
26
|
+
getComponentContext: DCommandSingleComponentContext[ "getComponentContext" ];
|
|
27
|
+
getState: DCommandSingleComponentContext[ "getState" ];
|
|
28
|
+
setState: DCommandSingleComponentContext[ "setState" ];
|
|
29
|
+
lifecycleHandlers: any;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export interface DCoreInterface {
|
|
33
|
+
__devGetContextValues: () => DCommandSingleComponentContext[];
|
|
34
|
+
__devGetContextKeys: () => string[];
|
|
35
|
+
__devGetContextLength: () => number;
|
|
36
|
+
__devDebug: ( ... args: any[] ) => void;
|
|
37
|
+
|
|
38
|
+
[ REGISTER_INTERNAL_SYMBOL ]( args: DCoreRegisterArgs ): void;
|
|
39
|
+
|
|
40
|
+
[ UNREGISTER_INTERNAL_SYMBOL ]( componentNameUnique: string ): void;
|
|
41
|
+
|
|
42
|
+
[ GET_INTERNAL_SYMBOL ]( componentNameUnique: string, silent?: boolean ): DCommandSingleComponentContext;
|
|
43
|
+
|
|
44
|
+
[ GET_INTERNAL_MATCH_SYMBOL ]( componentName: string ): DCommandSingleComponentContext[];
|
|
45
|
+
|
|
46
|
+
[ SET_TO_CONTEXT_SYMBOL ]( componentNameUnique: string, data: { [ key: string ]: any } ): void;
|
|
47
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { EventEmitter } from "eventemitter3";
|
|
2
|
+
|
|
3
|
+
import type { DCommandOptions, DCommandArgs, DCommandRegisterArgs } from "@zenflux/react-commander/definitions";
|
|
4
|
+
|
|
5
|
+
import type React from "react";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Each created command is registered within the commands manager, and the instance created only once per command.
|
|
9
|
+
*/
|
|
10
|
+
export abstract class CommandBase<TState = React.ComponentState, TArgs = DCommandArgs> {
|
|
11
|
+
private static globalEmitter: EventEmitter = new EventEmitter();
|
|
12
|
+
|
|
13
|
+
public readonly commandName: string;
|
|
14
|
+
|
|
15
|
+
private options: DCommandOptions<TState> = {};
|
|
16
|
+
|
|
17
|
+
public static getName(): string {
|
|
18
|
+
throw new Error( "You have should implement `static getName()` method, since the commands run by name ;)" );
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
public static globalHook( callback: ( result?: any, args?: DCommandArgs ) => any ) {
|
|
22
|
+
this.globalEmitter.on( this.getName(), callback );
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
public static globalUnhook() {
|
|
26
|
+
this.globalEmitter.listeners( this.getName() ).forEach( ( listener: any ) => {
|
|
27
|
+
this.globalEmitter.off( this.getName(), listener );
|
|
28
|
+
} );
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
public constructor( private args: DCommandRegisterArgs ) {
|
|
32
|
+
this.commandName = ( new.target as typeof CommandBase ).getName();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
public global() {
|
|
36
|
+
const global = ( this.constructor as typeof CommandBase );
|
|
37
|
+
|
|
38
|
+
return global as unknown as {
|
|
39
|
+
getName: typeof CommandBase.getName;
|
|
40
|
+
|
|
41
|
+
globalEmitter: EventEmitter;
|
|
42
|
+
globalHook: typeof CommandBase.globalHook;
|
|
43
|
+
globalUnhook: typeof CommandBase.globalUnhook;
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
public async execute( emitter: EventEmitter, args: TArgs, options?: DCommandOptions<TState> ): Promise<any> {
|
|
48
|
+
if ( options ) {
|
|
49
|
+
this.options = options;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
this.validateArgs?.( args, options );
|
|
53
|
+
|
|
54
|
+
const result = await this.apply?.( args );
|
|
55
|
+
|
|
56
|
+
const listeners = emitter.listeners( this.commandName );
|
|
57
|
+
for ( const listener of listeners ) {
|
|
58
|
+
await listener( result, args );
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const globalListeners = this.global().globalEmitter.listeners( this.commandName );
|
|
62
|
+
for ( const globalListener of globalListeners ) {
|
|
63
|
+
await globalListener( result, args );
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
this.options = {};
|
|
67
|
+
|
|
68
|
+
return result;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
72
|
+
protected validateArgs?( args: TArgs, options?: DCommandOptions<TState> ) {
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
76
|
+
protected apply?( args: TArgs ): Promise<any> | any {
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
protected get state(): TState {
|
|
80
|
+
this.validateState();
|
|
81
|
+
|
|
82
|
+
return this.options.state;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
protected setState<K extends keyof TState>(
|
|
86
|
+
state: ( ( prevState: Readonly<TState> ) => Pick<TState, K> | TState | null ) | ( Pick<TState, K> | TState | null ),
|
|
87
|
+
callback?: ( state: TState ) => void,
|
|
88
|
+
) {
|
|
89
|
+
this.validateState();
|
|
90
|
+
|
|
91
|
+
return new Promise( ( resolve ) => {
|
|
92
|
+
this.options.setState!( state, ( currentState: React.ComponentState ) => {
|
|
93
|
+
callback?.( currentState );
|
|
94
|
+
|
|
95
|
+
resolve( currentState );
|
|
96
|
+
} );
|
|
97
|
+
} );
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
private validateState() {
|
|
101
|
+
if ( "undefined" === typeof this.options.state || "function" !== typeof this.options.setState ) {
|
|
102
|
+
throw new Error( "There is no state for the current command, you should use `withCommands( component, class, state, commands )` including the state to enable it" );
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
|
|
3
|
+
import type { DCommandComponentContextProps } from "@zenflux/react-commander/definitions";
|
|
4
|
+
|
|
5
|
+
const createErrorMessage = ( functionName: string ) => `ComponentCommandContext.Provider is not set. Using default ${ functionName } function.`;
|
|
6
|
+
|
|
7
|
+
// An internal context used by `use-commands`
|
|
8
|
+
export const ComponentIdContext = React.createContext<DCommandComponentContextProps>( {
|
|
9
|
+
isSet: false,
|
|
10
|
+
|
|
11
|
+
getNameUnique: () => {
|
|
12
|
+
throw new Error( createErrorMessage( "getUniqueName" ) );
|
|
13
|
+
},
|
|
14
|
+
getComponentName: () => {
|
|
15
|
+
throw new Error( createErrorMessage( "getComponentName" ) );
|
|
16
|
+
},
|
|
17
|
+
getComponentRef: () => {
|
|
18
|
+
throw new Error( createErrorMessage( "getComponentRef" ) );
|
|
19
|
+
}
|
|
20
|
+
} );
|