UNPKG

code-craft-studio

Version:

A comprehensive QR code and barcode scanning/generation library for React. Works with or without Capacitor. Supports 22+ QR data types and 14+ barcode formats (EAN, UPC, Code 128, etc.), with customizable designs, analytics, and React components. Provider

76 lines 2.61 kB
import { platformDetector } from './detector'; import { WebPlatformAdapter } from './web/web-adapter'; import { logger } from '../utils/logger'; class PlatformManager { constructor() { this.adapter = null; this.initPromise = null; } static getInstance() { if (!PlatformManager.instance) { PlatformManager.instance = new PlatformManager(); } return PlatformManager.instance; } async getAdapter() { if (this.adapter) { return this.adapter; } if (this.initPromise) { return this.initPromise; } this.initPromise = this.initializeAdapter(); this.adapter = await this.initPromise; return this.adapter; } async initializeAdapter() { // Check if we're in a Capacitor environment if (platformDetector.isCapacitor()) { try { // Dynamically import Capacitor adapter const { CapacitorPlatformAdapter } = await import('./capacitor/capacitor-adapter'); return new CapacitorPlatformAdapter(); } catch (error) { logger.warn('Failed to load Capacitor adapter, falling back to web', error); } } // Default to web adapter return new WebPlatformAdapter(); } // Allow manual override for testing or specific use cases setAdapter(adapter) { this.adapter = adapter; } // Reset the platform manager (useful for testing) reset() { this.adapter = null; this.initPromise = null; } } // Export singleton instance methods export const platformManager = PlatformManager.getInstance(); // Export platform detection utilities export { platformDetector } from './detector'; // Convenience function for getting the current platform adapter export async function getPlatform() { return platformManager.getAdapter(); } // Convenience functions for common operations export async function scanQRCode(options) { const platform = await getPlatform(); return platform.scanQRCode(options); } export async function generateQRCode(data, options) { const platform = await getPlatform(); return platform.generateQRCode(data, options); } export async function scanBarcode(options) { const platform = await getPlatform(); return platform.scanBarcode(options); } export async function generateBarcode(data, format, options) { const platform = await getPlatform(); return platform.generateBarcode(data, format, options); } //# sourceMappingURL=index.js.map