@zenflux/react-commander 0.0.3 → 0.0.4

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.3",
5
+ "version": "0.0.4",
6
6
  "type": "module",
7
7
  "types": "src/index.ts",
8
8
  "files": [
@@ -2,19 +2,30 @@ import React from "react";
2
2
 
3
3
  import type { DCommandComponentContextProps } from "@zenflux/react-commander/definitions";
4
4
 
5
+ const CONTEXT_KEY = Symbol.for( "@zenflux/react-commander/ComponentIdContext" );
6
+
5
7
  const createErrorMessage = ( functionName: string ) => `ComponentCommandContext.Provider is not set. Using default ${ functionName } function.`;
6
8
 
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" ) );
9
+ function getOrCreateContext() {
10
+ const globalObj = globalThis as Record<symbol, React.Context<DCommandComponentContextProps>>;
11
+
12
+ if ( ! globalObj[ CONTEXT_KEY ] ) {
13
+ globalObj[ CONTEXT_KEY ] = React.createContext<DCommandComponentContextProps>( {
14
+ isSet: false,
15
+
16
+ getNameUnique: () => {
17
+ throw new Error( createErrorMessage( "getUniqueName" ) );
18
+ },
19
+ getComponentName: () => {
20
+ throw new Error( createErrorMessage( "getComponentName" ) );
21
+ },
22
+ getComponentRef: () => {
23
+ throw new Error( createErrorMessage( "getComponentRef" ) );
24
+ }
25
+ } );
19
26
  }
20
- } );
27
+
28
+ return globalObj[ CONTEXT_KEY ];
29
+ }
30
+
31
+ export const ComponentIdContext = getOrCreateContext();