@zenflux/react-commander 0.0.4 → 0.0.5

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.4",
5
+ "version": "0.0.5",
6
6
  "type": "module",
7
7
  "types": "src/index.ts",
8
8
  "files": [
@@ -1,18 +1,17 @@
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 );
1
+ const NAMESPACE = "@zenflux/react-commander";
3
2
 
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 );
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 = "__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;
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 = "__internalProps" + randomKey;
15
+ export const INTERNAL_PROPS = `${ NAMESPACE }/__internalProps`;
17
16
 
18
- export const INTERNAL_STATE_UPDATED_EVENT = "__internalStateUpdated" + randomKey;
17
+ export const INTERNAL_STATE_UPDATED_EVENT = `${ NAMESPACE }/__internalStateUpdated`;
@@ -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 context: DCoreContext = {};
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
- // This will hold the sorted keys for the binary search
17
- let sortedContextKeys: string[] = [];
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
- sortedContextKeys.forEach( ( componentNameUnique ) => {
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 index = this.binarySearch( sortedContextKeys, componentName );
144
+ const sortedKeys = getSortedContextKeys();
145
+ const index = this.binarySearch( sortedKeys, componentName );
122
146
  if ( index !== -1 ) {
123
- return [ context[ sortedContextKeys[ index ] ] ];
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
- sortedContextKeys = Object.keys( context ).sort();
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 core: DCoreInterface = new Core();
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__ ) {