react-native-debug-toolkit
Version:
A simple yet powerful debugging toolkit for React Native with a convenient floating UI for development
115 lines (103 loc) • 4.71 kB
JavaScript
import DebugToolKit from './DebugToolKit'
import { createNetworkFeature } from './features/NetworkFeature'
// import { createPerformanceFeature} from './features/PerformanceFeature'
import { createConsoleLogFeature } from './features/ConsoleLogFeature'
import { createZustandLogFeature, zustandLogMiddleware } from './features/ZustandLogFeature'
import { createNavigationLogFeature, addNavigationLog } from './features/NavigationLogFeature'
import { createThirdPartyLibsFeature } from './features/ThirdPartyLibsFeature'
import { createTrackFeature, addTrackLog } from './features/TrackFeature'
import NativeDebugLibs from './NativeDebugLibs'
// Registry mapping feature names (strings) to their creator functions
const featureRegistry = {
network: createNetworkFeature,
track: createTrackFeature,
console: createConsoleLogFeature,
// performance: createPerformanceFeature,
zustand: createZustandLogFeature,
navigation: createNavigationLogFeature,
thirdPartyLibs: createThirdPartyLibsFeature,
// Add other built-in features here
// 'storage': createStorageFeature,
}
// List of default features (can use strings now)
const defaultFeatures = ['network', 'track', 'navigation','zustand', 'thirdPartyLibs', 'console']
// Export the debug mode status - this is used by the app
// const isDebugMode = NativeDebugLibs.isDebugBuild();
const isDebugMode = true
/**
* Initializes and shows the Debug ToolKit panel with specified features.
* Only runs in debug mode.
* Features can be specified as strings (e.g., 'network') or creator functions.
* @param {Array<string|Function>} features - Array of feature names (strings) or creator functions. Defaults to standard features.
* @param {Object} options - Additional options like doraemonProductId for third-party debug libraries
*/
export function initializeDebugToolkit(features = defaultFeatures, options = {}) {
// Create instance but it will internally handle debug mode check
try {
const debugToolKit = new DebugToolKit();
// If not in debug mode, the class methods won't do anything
if (!isDebugMode) {
return debugToolKit;
}
// Initialize third-party debug libraries if configured
if (options.doraemonProductId) {
NativeDebugLibs.installDoraemonKit(options.doraemonProductId);
}
features.forEach(featureIdentifier => {
let feature = null;
let featureCreator = null;
if (typeof featureIdentifier === 'string') {
// It's a string, look it up in the registry
featureCreator = featureRegistry[featureIdentifier];
if (!featureCreator) {
console.warn(`[DebugToolKit] Unknown feature identifier string: "${featureIdentifier}". Skipping.`);
return; // Skip this identifier
}
feature = featureCreator();
} else if (typeof featureIdentifier === 'function') {
// It's a function, assume it's a creator
featureCreator = featureIdentifier;
try {
feature = featureCreator();
} catch (e) {
console.error(`[DebugToolKit] Error calling feature creator function:`, e);
return; // Skip if creator fails
}
} else if (typeof featureIdentifier === 'object' && featureIdentifier !== null && featureIdentifier.name) {
// It might be a pre-created feature object
feature = featureIdentifier;
console.warn(`[DebugToolKit] Passing pre-created feature objects is supported but using strings or creators is recommended.`);
} else {
console.warn('[DebugToolKit] Invalid feature identifier type:', typeof featureIdentifier, featureIdentifier, '. Skipping.');
return; // Skip invalid types
}
if (feature) {
debugToolKit.addFeature(feature);
} else {
// This case might happen if a creator function returns null/undefined
console.warn(`[DebugToolKit] Feature creator for identifier "${typeof featureIdentifier === 'string' ? featureIdentifier : 'custom function'}" did not return a valid feature object.`);
}
});
debugToolKit.showDebugPanel();
return debugToolKit; // Return instance if needed elsewhere
} catch (error) {
console.error('[DebugToolKit] Failed to initialize:', error);
return null;
}
}
// Export existing stuff and the new initializer
export {
DebugToolKit,
createNetworkFeature,
// createPerformanceFeature,
createConsoleLogFeature,
createZustandLogFeature,
createNavigationLogFeature,
createThirdPartyLibsFeature,
addNavigationLog,
createTrackFeature,
addTrackLog,
zustandLogMiddleware, // Export middleware for use in Zustand stores
featureRegistry,
isDebugMode
};