@zenflux/react-commander 0.0.9 → 0.0.11

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.9",
5
+ "version": "0.0.11",
6
6
  "type": "module",
7
7
  "sideEffects": false,
8
8
  "types": "src/index.ts",
@@ -1,3 +1,5 @@
1
+ type WindowWithDebug = Window & { __ZENFLUX_DEBUG__?: boolean; __DEBUG__?: boolean };
2
+
1
3
  const NAMESPACE = "@zenflux/react-commander";
2
4
 
3
5
  export const REGISTER_INTERNAL_SYMBOL = Symbol.for( `${ NAMESPACE }/REGISTER_INTERNAL` );
@@ -15,3 +17,19 @@ export const INTERNAL_ON_CONTEXT_STATE_UPDATED = `${ NAMESPACE }/__internalOnCon
15
17
  export const INTERNAL_PROPS = `${ NAMESPACE }/__internalProps`;
16
18
 
17
19
  export const INTERNAL_STATE_UPDATED_EVENT = `${ NAMESPACE }/__internalStateUpdated`;
20
+
21
+ export const DEBUG_ENABLED = ( () => {
22
+ if ( typeof window !== "undefined" ) {
23
+ const w = window as WindowWithDebug;
24
+ if ( w.__ZENFLUX_DEBUG__ !== undefined ) return !!w.__ZENFLUX_DEBUG__;
25
+ if ( w.__DEBUG__ !== undefined ) return !!w.__DEBUG__;
26
+ }
27
+ if ( typeof import.meta !== "undefined" && "env" in import.meta ) {
28
+ const meta = import.meta as ImportMeta & { env?: Record<string, string | undefined> };
29
+ if ( meta.env?.ZENFLUX_DEBUG !== undefined ) return !!meta.env.ZENFLUX_DEBUG;
30
+ }
31
+ if ( typeof process !== "undefined" && process.env?.ZENFLUX_DEBUG !== undefined ) {
32
+ return !!process.env.ZENFLUX_DEBUG;
33
+ }
34
+ return false;
35
+ } )();
@@ -1,5 +1,6 @@
1
1
 
2
2
  import {
3
+ DEBUG_ENABLED,
3
4
  GET_INTERNAL_MATCH_SYMBOL,
4
5
  GET_INTERNAL_SYMBOL,
5
6
  REGISTER_INTERNAL_SYMBOL,
@@ -224,8 +225,8 @@ function getOrCreateCore(): DCoreInterface {
224
225
  const core = getOrCreateCore();
225
226
 
226
227
  if ( /* from vite */ import.meta.env.DEV ) {
227
- if ( ( window as any ).__DEBUG__ ) {
228
- core.__devDebug = ( ... args: any[] ) => {
228
+ if ( DEBUG_ENABLED ) {
229
+ core.__devDebug = ( ... args: Parameters<DCoreInterface[ "__devDebug" ]> ) => {
229
230
  console.log( ... args );
230
231
  };
231
232
  }
@@ -1,5 +1,9 @@
1
1
  import type React from "react";
2
2
 
3
+ import { DEBUG_ENABLED } from "./constants";
4
+
5
+ export { DEBUG_ENABLED };
6
+
3
7
  import type {
4
8
  GET_INTERNAL_MATCH_SYMBOL,
5
9
  GET_INTERNAL_SYMBOL,
@@ -1,5 +1,5 @@
1
1
 
2
- import { GET_INTERNAL_SYMBOL } from "./_internal/constants";
2
+ import { DEBUG_ENABLED, GET_INTERNAL_SYMBOL } from "./_internal/constants";
3
3
 
4
4
  import core from "./_internal/core.ts";
5
5
 
@@ -65,7 +65,7 @@ class CommandsManager {
65
65
  throw new Error( `Command '${ commandName }' not registered for component '${ componentName }'` );
66
66
  }
67
67
 
68
- console.log( `Commands.run() '${ commandName }' for component '${ componentNameUnique }'`, args );
68
+ if ( DEBUG_ENABLED ) console.log( `Commands.run() '${ commandName }' for component '${ componentNameUnique }'`, args );
69
69
 
70
70
  const singleComponentContext = core[ GET_INTERNAL_SYMBOL ]( componentNameUnique );
71
71
 
@@ -180,8 +180,8 @@ class CommandsManager {
180
180
  throw new Error( `Component '${ componentName }' not registered` );
181
181
  }
182
182
 
183
- // @ts-ignore - If it hot reloads then skip the error
184
- const shouldSilentError = !! typeof import.meta.hot?.hmrClient.pruneMap.size;
183
+ // If HMR is active, suppress errors for missing contexts during hot reload
184
+ const shouldSilentError = !! import.meta.hot;
185
185
 
186
186
  const singleComponentContext = core[ GET_INTERNAL_SYMBOL ]( componentNameUnique, shouldSilentError ) as DCommandSingleComponentContext;
187
187
 
@@ -47,7 +47,7 @@ export function useCommand( commandName: string, ref?: React.RefObject<any> ) {
47
47
  const debug = ( ..._args: Array<unknown> ) => {
48
48
  try {
49
49
  // TODO: Enable when needed
50
- console.debug( "[react-commander/useCommand]", ..._args );
50
+ // console.debug( "[react-commander/useCommand]", ..._args );
51
51
  } catch {}
52
52
  };
53
53
 
@@ -1,3 +1,4 @@
1
+ import { DEBUG_ENABLED } from "@zenflux/react-commander/constants";
1
2
  import { QueryRouterBase } from "@zenflux/react-commander/query/router";
2
3
 
3
4
  import type { DCommandFunctionComponent } from "@zenflux/react-commander/definitions";
@@ -130,15 +131,17 @@ export abstract class QueryModuleBase<TResource extends object = object> {
130
131
  throw new Error( `Cannot find route for ${ name }` );
131
132
  }
132
133
 
134
+ if ( DEBUG_ENABLED ) console.log( "request", { name, args, found: { method: found.method, path: found.route.path } } );
133
135
  const payload = args ?? {};
134
136
 
135
- const result = await this.api.fetch( found.method!, found.route.path, payload, async ( response ) => {
137
+ const result = await this.api.fetch( found.method!, found.route.path, payload, async( response ) => {
136
138
  if ( found!.route.handlers.responseHandler ) {
137
139
  return await ( found!.route.handlers.responseHandler as ( e: DCommandFunctionComponent<Record<string, unknown>, React.ComponentState>, r: Response ) => Promise<unknown> )( undefined as unknown as DCommandFunctionComponent<Record<string, unknown>, React.ComponentState>, response );
138
140
  }
139
141
  return await response.json();
140
142
  } );
141
143
 
144
+ if ( DEBUG_ENABLED ) console.log( "request result", { name, result } );
142
145
  return result as TResult;
143
146
  }
144
147