forked from jsnbuchanan/crowd-funder-for-time-pwa
- Remove duplicate property declarations from TopMessage component - Use (this as any) type assertion for mixin methods - Resolves 'Data property already defined' warnings - Fixes 'this.dbQuery is not a function' runtime errors
72 lines
2.3 KiB
TypeScript
72 lines
2.3 KiB
TypeScript
import { PlatformService } from "./PlatformService";
|
|
import { WebPlatformService } from "./platforms/WebPlatformService";
|
|
import { CapacitorPlatformService } from "./platforms/CapacitorPlatformService";
|
|
|
|
/**
|
|
* Factory class for creating platform-specific service implementations.
|
|
* Implements the Singleton pattern to ensure only one instance of PlatformService exists.
|
|
*
|
|
* The factory determines which platform implementation to use based on the VITE_PLATFORM
|
|
* environment variable. Supported platforms are:
|
|
* - capacitor: Mobile platform using Capacitor
|
|
* - web: Default web platform (fallback)
|
|
*
|
|
* @example
|
|
* ```typescript
|
|
* const platformService = PlatformServiceFactory.getInstance();
|
|
* await platformService.takePicture();
|
|
* ```
|
|
*/
|
|
export class PlatformServiceFactory {
|
|
private static instance: PlatformService | null = null;
|
|
private static callCount = 0; // Debug counter
|
|
private static creationLogged = false; // Only log creation once
|
|
|
|
/**
|
|
* Gets or creates the singleton instance of PlatformService.
|
|
* Creates the appropriate platform-specific implementation based on environment.
|
|
*
|
|
* @returns {PlatformService} The singleton instance of PlatformService
|
|
*/
|
|
public static getInstance(): PlatformService {
|
|
PlatformServiceFactory.callCount++;
|
|
|
|
if (PlatformServiceFactory.instance) {
|
|
// Normal case - return existing instance silently
|
|
return PlatformServiceFactory.instance;
|
|
}
|
|
|
|
// Only log when actually creating the instance
|
|
const platform = process.env.VITE_PLATFORM || "web";
|
|
|
|
if (!PlatformServiceFactory.creationLogged) {
|
|
console.log(
|
|
`[PlatformServiceFactory] Creating singleton instance for platform: ${platform}`,
|
|
);
|
|
PlatformServiceFactory.creationLogged = true;
|
|
}
|
|
|
|
switch (platform) {
|
|
case "capacitor":
|
|
PlatformServiceFactory.instance = new CapacitorPlatformService();
|
|
break;
|
|
case "web":
|
|
default:
|
|
PlatformServiceFactory.instance = new WebPlatformService();
|
|
break;
|
|
}
|
|
|
|
return PlatformServiceFactory.instance;
|
|
}
|
|
|
|
/**
|
|
* Debug method to check singleton usage stats
|
|
*/
|
|
public static getStats(): { callCount: number; instanceExists: boolean } {
|
|
return {
|
|
callCount: PlatformServiceFactory.callCount,
|
|
instanceExists: PlatformServiceFactory.instance !== null,
|
|
};
|
|
}
|
|
}
|