@zenflux/react-commander 0.0.4 → 0.0.6
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 +2 -1
- package/src/_internal/constants.ts +13 -14
- package/src/_internal/core.ts +44 -8
package/package.json
CHANGED
|
@@ -2,8 +2,9 @@
|
|
|
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.
|
|
5
|
+
"version": "0.0.6",
|
|
6
6
|
"type": "module",
|
|
7
|
+
"sideEffects": false,
|
|
7
8
|
"types": "src/index.ts",
|
|
8
9
|
"files": [
|
|
9
10
|
"src"
|
|
@@ -1,18 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
const randomKey = "__" + Math.random().toString( 36 ).slice( 2 );
|
|
1
|
+
const NAMESPACE = "@zenflux/react-commander";
|
|
3
2
|
|
|
4
|
-
export const REGISTER_INTERNAL_SYMBOL = Symbol(
|
|
5
|
-
export const UNREGISTER_INTERNAL_SYMBOL = Symbol(
|
|
6
|
-
export const GET_INTERNAL_SYMBOL = Symbol(
|
|
7
|
-
export const GET_INTERNAL_MATCH_SYMBOL = Symbol(
|
|
8
|
-
export const SET_TO_CONTEXT_SYMBOL = Symbol(
|
|
3
|
+
export const REGISTER_INTERNAL_SYMBOL = Symbol.for( `${ NAMESPACE }/REGISTER_INTERNAL` );
|
|
4
|
+
export const UNREGISTER_INTERNAL_SYMBOL = Symbol.for( `${ NAMESPACE }/UNREGISTER_INTERNAL` );
|
|
5
|
+
export const GET_INTERNAL_SYMBOL = Symbol.for( `${ NAMESPACE }/GET_INTERNAL` );
|
|
6
|
+
export const GET_INTERNAL_MATCH_SYMBOL = Symbol.for( `${ NAMESPACE }/GET_INTERNAL_MATCH` );
|
|
7
|
+
export const SET_TO_CONTEXT_SYMBOL = Symbol.for( `${ NAMESPACE }/SET_TO_CONTEXT` );
|
|
9
8
|
|
|
10
|
-
export const INTERNAL_ON_LOAD =
|
|
11
|
-
export const INTERNAL_ON_MOUNT =
|
|
12
|
-
export const INTERNAL_ON_UNMOUNT =
|
|
13
|
-
export const INTERNAL_ON_UPDATE =
|
|
14
|
-
export const INTERNAL_ON_CONTEXT_STATE_UPDATED =
|
|
9
|
+
export const INTERNAL_ON_LOAD = `${ NAMESPACE }/__internalOnLoad`;
|
|
10
|
+
export const INTERNAL_ON_MOUNT = `${ NAMESPACE }/__internalOnMount`;
|
|
11
|
+
export const INTERNAL_ON_UNMOUNT = `${ NAMESPACE }/__internalOnUnmount`;
|
|
12
|
+
export const INTERNAL_ON_UPDATE = `${ NAMESPACE }/__internalOnUpdate`;
|
|
13
|
+
export const INTERNAL_ON_CONTEXT_STATE_UPDATED = `${ NAMESPACE }/__internalOnContextStateUpdated`;
|
|
15
14
|
|
|
16
|
-
export const INTERNAL_PROPS =
|
|
15
|
+
export const INTERNAL_PROPS = `${ NAMESPACE }/__internalProps`;
|
|
17
16
|
|
|
18
|
-
export const INTERNAL_STATE_UPDATED_EVENT =
|
|
17
|
+
export const INTERNAL_STATE_UPDATED_EVENT = `${ NAMESPACE }/__internalStateUpdated`;
|
package/src/_internal/core.ts
CHANGED
|
@@ -11,10 +11,33 @@ import type { DCoreContext, DCoreInterface, DCoreRegisterArgs } from "./definiti
|
|
|
11
11
|
|
|
12
12
|
import type { DCommandSingleComponentContext } from "@zenflux/react-commander/definitions";
|
|
13
13
|
|
|
14
|
-
const
|
|
14
|
+
const CORE_CONTEXT_KEY = Symbol.for( "@zenflux/react-commander/core-context" );
|
|
15
|
+
const CORE_SORTED_KEYS_KEY = Symbol.for( "@zenflux/react-commander/core-sorted-keys" );
|
|
15
16
|
|
|
16
|
-
|
|
17
|
-
|
|
17
|
+
type GlobalStore = {
|
|
18
|
+
[ CORE_CONTEXT_KEY ]: DCoreContext;
|
|
19
|
+
[ CORE_SORTED_KEYS_KEY ]: string[];
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const globalObj = globalThis as unknown as GlobalStore;
|
|
23
|
+
|
|
24
|
+
if ( ! globalObj[ CORE_CONTEXT_KEY ] ) {
|
|
25
|
+
globalObj[ CORE_CONTEXT_KEY ] = {};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if ( ! globalObj[ CORE_SORTED_KEYS_KEY ] ) {
|
|
29
|
+
globalObj[ CORE_SORTED_KEYS_KEY ] = [];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const context = globalObj[ CORE_CONTEXT_KEY ];
|
|
33
|
+
|
|
34
|
+
function getSortedContextKeys() {
|
|
35
|
+
return globalObj[ CORE_SORTED_KEYS_KEY ];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function setSortedContextKeys( keys: string[] ) {
|
|
39
|
+
globalObj[ CORE_SORTED_KEYS_KEY ] = keys;
|
|
40
|
+
}
|
|
18
41
|
|
|
19
42
|
/**
|
|
20
43
|
* Core class is responsible for managing the command context for each component.
|
|
@@ -110,7 +133,7 @@ class Core implements DCoreInterface {
|
|
|
110
133
|
|
|
111
134
|
const anyMatchComponent = componentName.substring( 0, componentName.length - 1 );
|
|
112
135
|
|
|
113
|
-
|
|
136
|
+
getSortedContextKeys().forEach( ( componentNameUnique ) => {
|
|
114
137
|
if ( componentNameUnique.includes( anyMatchComponent ) ) {
|
|
115
138
|
result.push( context[ componentNameUnique ] );
|
|
116
139
|
}
|
|
@@ -118,9 +141,10 @@ class Core implements DCoreInterface {
|
|
|
118
141
|
|
|
119
142
|
return result;
|
|
120
143
|
} else {
|
|
121
|
-
const
|
|
144
|
+
const sortedKeys = getSortedContextKeys();
|
|
145
|
+
const index = this.binarySearch( sortedKeys, componentName );
|
|
122
146
|
if ( index !== -1 ) {
|
|
123
|
-
return [ context[
|
|
147
|
+
return [ context[ sortedKeys[ index ] ] ];
|
|
124
148
|
}
|
|
125
149
|
}
|
|
126
150
|
|
|
@@ -145,7 +169,7 @@ class Core implements DCoreInterface {
|
|
|
145
169
|
* Updates the sorted keys for binary search.
|
|
146
170
|
*/
|
|
147
171
|
private updateSortedContextKeys() {
|
|
148
|
-
|
|
172
|
+
setSortedContextKeys( Object.keys( context ).sort() );
|
|
149
173
|
}
|
|
150
174
|
|
|
151
175
|
/**
|
|
@@ -181,7 +205,19 @@ class Core implements DCoreInterface {
|
|
|
181
205
|
declare public __devGetContextValues: () => DCommandSingleComponentContext[];
|
|
182
206
|
}
|
|
183
207
|
|
|
184
|
-
const
|
|
208
|
+
const CORE_INSTANCE_KEY = Symbol.for( "@zenflux/react-commander/core-instance" );
|
|
209
|
+
|
|
210
|
+
function getOrCreateCore(): DCoreInterface {
|
|
211
|
+
const globalStore = globalThis as unknown as Record<symbol, DCoreInterface>;
|
|
212
|
+
|
|
213
|
+
if ( ! globalStore[ CORE_INSTANCE_KEY ] ) {
|
|
214
|
+
globalStore[ CORE_INSTANCE_KEY ] = new Core();
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
return globalStore[ CORE_INSTANCE_KEY ];
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
const core = getOrCreateCore();
|
|
185
221
|
|
|
186
222
|
if ( /* from vite */ import.meta.env.DEV ) {
|
|
187
223
|
if ( ( window as any ).__DEBUG__ ) {
|