Compare commits
15 Commits
1.1.2
...
refactor-i
| Author | SHA1 | Date | |
|---|---|---|---|
| 2ca2c1d4b3 | |||
|
|
cb8d8bed4c | ||
|
|
11658140ae | ||
|
|
85a64b06d7 | ||
|
|
72e23a9109 | ||
|
|
ceaf4ede11 | ||
|
|
523f88fd0d | ||
|
|
ee57fe9ea6 | ||
| 5050156beb | |||
|
|
471bdd6b92 | ||
|
|
c26b8daaf7 | ||
|
|
16db790c5f | ||
|
|
59434ff5f7 | ||
|
|
239666e137 | ||
| d265a9f78c |
@@ -1156,6 +1156,9 @@ gem_path=$(which gem)
|
||||
shortened_path="${gem_path:h:h}"
|
||||
export GEM_HOME=$shortened_path
|
||||
export GEM_PATH=$shortened_path
|
||||
|
||||
cd ios/App
|
||||
pod install
|
||||
```
|
||||
|
||||
##### 1. Bump the version in package.json for `MARKETING_VERSION`, then `grep CURRENT_PROJECT_VERSION ios/App/App.xcodeproj/project.pbxproj` and add 1 for the numbered version;
|
||||
@@ -1197,7 +1200,8 @@ npm run build:ios:prod
|
||||
- It can take 15 minutes for the build to show up in the list of builds.
|
||||
- You'll probably have to "Manage" something about encryption, disallowed in France.
|
||||
- Then "Save" and "Add to Review" and "Resubmit to App Review".
|
||||
- Eventually it'll be "Ready for Distribution" which means
|
||||
- Eventually it'll be "Ready for Distribution" which means it's live
|
||||
- When finished, bump package.json version
|
||||
|
||||
### Android Build
|
||||
|
||||
@@ -1379,6 +1383,8 @@ At play.google.com/console:
|
||||
- Note that if you add testers, you have to go to "Publishing Overview" and send
|
||||
those changes or your (closed) testers won't see it.
|
||||
|
||||
- When finished, bump package.json version
|
||||
|
||||
### Capacitor Operations
|
||||
|
||||
```bash
|
||||
|
||||
@@ -31,8 +31,8 @@ android {
|
||||
applicationId "app.timesafari.app"
|
||||
minSdkVersion rootProject.ext.minSdkVersion
|
||||
targetSdkVersion rootProject.ext.targetSdkVersion
|
||||
versionCode 47
|
||||
versionName "1.1.2"
|
||||
versionCode 48
|
||||
versionName "1.1.3-beta"
|
||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||
aaptOptions {
|
||||
// Files and dirs to omit from the packaged assets dir, modified to accommodate modern web apps.
|
||||
|
||||
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "timesafari",
|
||||
"version": "1.1.2",
|
||||
"version": "1.1.3-beta",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "timesafari",
|
||||
"version": "1.1.2",
|
||||
"version": "1.1.3-beta",
|
||||
"dependencies": {
|
||||
"@capacitor-community/electron": "^5.0.1",
|
||||
"@capacitor-community/sqlite": "6.0.2",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "timesafari",
|
||||
"version": "1.1.2",
|
||||
"version": "1.1.3-beta",
|
||||
"description": "Time Safari Application",
|
||||
"author": {
|
||||
"name": "Time Safari Team"
|
||||
|
||||
@@ -436,7 +436,21 @@ fi
|
||||
log_info "Cleaning dist directory..."
|
||||
clean_build_artifacts "dist"
|
||||
|
||||
# Step 4: Build Capacitor version with mode
|
||||
# Step 4: Run TypeScript type checking for test and production builds
|
||||
if [ "$BUILD_MODE" = "production" ] || [ "$BUILD_MODE" = "test" ]; then
|
||||
log_info "Running TypeScript type checking for $BUILD_MODE mode..."
|
||||
|
||||
if ! measure_time npm run type-check; then
|
||||
log_error "TypeScript type checking failed for $BUILD_MODE mode!"
|
||||
exit 2
|
||||
fi
|
||||
|
||||
log_success "TypeScript type checking completed for $BUILD_MODE mode"
|
||||
else
|
||||
log_debug "Skipping TypeScript type checking for development mode"
|
||||
fi
|
||||
|
||||
# Step 5: Build Capacitor version with mode
|
||||
if [ "$BUILD_MODE" = "development" ]; then
|
||||
safe_execute "Building Capacitor version (development)" "npm run build:capacitor" || exit 3
|
||||
elif [ "$BUILD_MODE" = "test" ]; then
|
||||
@@ -445,23 +459,23 @@ elif [ "$BUILD_MODE" = "production" ]; then
|
||||
safe_execute "Building Capacitor version (production)" "npm run build:capacitor -- --mode production" || exit 3
|
||||
fi
|
||||
|
||||
# Step 5: Clean Gradle build
|
||||
# Step 6: Clean Gradle build
|
||||
safe_execute "Cleaning Gradle build" "cd android && ./gradlew clean && cd .." || exit 4
|
||||
|
||||
# Step 6: Build based on type
|
||||
# Step 7: Build based on type
|
||||
if [ "$BUILD_TYPE" = "debug" ]; then
|
||||
safe_execute "Assembling debug build" "cd android && ./gradlew assembleDebug && cd .." || exit 5
|
||||
elif [ "$BUILD_TYPE" = "release" ]; then
|
||||
safe_execute "Assembling release build" "cd android && ./gradlew assembleRelease && cd .." || exit 5
|
||||
fi
|
||||
|
||||
# Step 7: Sync with Capacitor
|
||||
# Step 8: Sync with Capacitor
|
||||
safe_execute "Syncing with Capacitor" "npx cap sync android" || exit 6
|
||||
|
||||
# Step 8: Generate assets
|
||||
# Step 9: Generate assets
|
||||
safe_execute "Generating assets" "npx capacitor-assets generate --android" || exit 7
|
||||
|
||||
# Step 9: Build APK/AAB if requested
|
||||
# Step 10: Build APK/AAB if requested
|
||||
if [ "$BUILD_APK" = true ]; then
|
||||
if [ "$BUILD_TYPE" = "debug" ]; then
|
||||
safe_execute "Building debug APK" "cd android && ./gradlew assembleDebug && cd .." || exit 5
|
||||
@@ -474,7 +488,7 @@ if [ "$BUILD_AAB" = true ]; then
|
||||
safe_execute "Building AAB" "cd android && ./gradlew bundleRelease && cd .." || exit 5
|
||||
fi
|
||||
|
||||
# Step 10: Auto-run app if requested
|
||||
# Step 11: Auto-run app if requested
|
||||
if [ "$AUTO_RUN" = true ]; then
|
||||
log_step "Auto-running Android app..."
|
||||
safe_execute "Launching app" "npx cap run android" || {
|
||||
@@ -485,7 +499,7 @@ if [ "$AUTO_RUN" = true ]; then
|
||||
log_success "Android app launched successfully!"
|
||||
fi
|
||||
|
||||
# Step 11: Open Android Studio if requested
|
||||
# Step 12: Open Android Studio if requested
|
||||
if [ "$OPEN_STUDIO" = true ]; then
|
||||
safe_execute "Opening Android Studio" "npx cap open android" || exit 8
|
||||
fi
|
||||
|
||||
@@ -381,7 +381,21 @@ safe_execute "Cleaning iOS build" "clean_ios_build" || exit 1
|
||||
log_info "Cleaning dist directory..."
|
||||
clean_build_artifacts "dist"
|
||||
|
||||
# Step 4: Build Capacitor version with mode
|
||||
# Step 4: Run TypeScript type checking for test and production builds
|
||||
if [ "$BUILD_MODE" = "production" ] || [ "$BUILD_MODE" = "test" ]; then
|
||||
log_info "Running TypeScript type checking for $BUILD_MODE mode..."
|
||||
|
||||
if ! measure_time npm run type-check; then
|
||||
log_error "TypeScript type checking failed for $BUILD_MODE mode!"
|
||||
exit 2
|
||||
fi
|
||||
|
||||
log_success "TypeScript type checking completed for $BUILD_MODE mode"
|
||||
else
|
||||
log_debug "Skipping TypeScript type checking for development mode"
|
||||
fi
|
||||
|
||||
# Step 5: Build Capacitor version with mode
|
||||
if [ "$BUILD_MODE" = "development" ]; then
|
||||
safe_execute "Building Capacitor version (development)" "npm run build:capacitor" || exit 3
|
||||
elif [ "$BUILD_MODE" = "test" ]; then
|
||||
@@ -390,16 +404,16 @@ elif [ "$BUILD_MODE" = "production" ]; then
|
||||
safe_execute "Building Capacitor version (production)" "npm run build:capacitor -- --mode production" || exit 3
|
||||
fi
|
||||
|
||||
# Step 5: Sync with Capacitor
|
||||
# Step 6: Sync with Capacitor
|
||||
safe_execute "Syncing with Capacitor" "npx cap sync ios" || exit 6
|
||||
|
||||
# Step 6: Generate assets
|
||||
# Step 7: Generate assets
|
||||
safe_execute "Generating assets" "npx capacitor-assets generate --ios" || exit 7
|
||||
|
||||
# Step 7: Build iOS app
|
||||
# Step 8: Build iOS app
|
||||
safe_execute "Building iOS app" "build_ios_app" || exit 5
|
||||
|
||||
# Step 8: Build IPA/App if requested
|
||||
# Step 9: Build IPA/App if requested
|
||||
if [ "$BUILD_IPA" = true ]; then
|
||||
log_info "Building IPA package..."
|
||||
cd ios/App
|
||||
@@ -426,12 +440,12 @@ if [ "$BUILD_APP" = true ]; then
|
||||
log_success "App bundle built successfully"
|
||||
fi
|
||||
|
||||
# Step 9: Auto-run app if requested
|
||||
# Step 10: Auto-run app if requested
|
||||
if [ "$AUTO_RUN" = true ]; then
|
||||
safe_execute "Auto-running iOS app" "auto_run_ios_app" || exit 9
|
||||
fi
|
||||
|
||||
# Step 10: Open Xcode if requested
|
||||
# Step 11: Open Xcode if requested
|
||||
if [ "$OPEN_STUDIO" = true ]; then
|
||||
safe_execute "Opening Xcode" "npx cap open ios" || exit 8
|
||||
fi
|
||||
|
||||
@@ -1686,7 +1686,10 @@ export async function register(
|
||||
"Registration thrown error:",
|
||||
errorMessage || JSON.stringify(err),
|
||||
);
|
||||
return { error: errorMessage || "Got a server error when registering." };
|
||||
return {
|
||||
error:
|
||||
(errorMessage as string) || "Got a server error when registering.",
|
||||
};
|
||||
}
|
||||
return { error: "Got a server error when registering." };
|
||||
}
|
||||
|
||||
@@ -21,14 +21,8 @@ import IndexedDBBackend from "absurd-sql/dist/indexeddb-backend";
|
||||
import { runMigrations } from "../db-sql/migration";
|
||||
import type { DatabaseService, QueryExecResult } from "../interfaces/database";
|
||||
import { logger } from "@/utils/logger";
|
||||
|
||||
interface QueuedOperation {
|
||||
type: "run" | "query";
|
||||
sql: string;
|
||||
params: unknown[];
|
||||
resolve: (value: unknown) => void;
|
||||
reject: (reason: unknown) => void;
|
||||
}
|
||||
import { OperationQueue, QueueExecutor } from "./platforms/OperationQueue";
|
||||
import { QueuedOperation } from "./platforms/types";
|
||||
|
||||
interface AbsurdSqlDatabase {
|
||||
exec: (sql: string, params?: unknown[]) => Promise<QueryExecResult[]>;
|
||||
@@ -43,8 +37,7 @@ class AbsurdSqlDatabaseService implements DatabaseService {
|
||||
private db: AbsurdSqlDatabase | null;
|
||||
private initialized: boolean;
|
||||
private initializationPromise: Promise<void> | null = null;
|
||||
private operationQueue: Array<QueuedOperation> = [];
|
||||
private isProcessingQueue: boolean = false;
|
||||
private operationQueue = new OperationQueue<AbsurdSqlDatabase>();
|
||||
|
||||
private constructor() {
|
||||
this.db = null;
|
||||
@@ -161,42 +154,30 @@ class AbsurdSqlDatabaseService implements DatabaseService {
|
||||
this.processQueue();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create executor adapter for AbsurdSQL API
|
||||
*/
|
||||
private createExecutor(): QueueExecutor<AbsurdSqlDatabase> {
|
||||
return {
|
||||
executeRun: async (db, sql, params) => {
|
||||
return await db.run(sql, params);
|
||||
},
|
||||
executeQuery: async (db, sql, params) => {
|
||||
return await db.exec(sql, params);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
private async processQueue(): Promise<void> {
|
||||
if (this.isProcessingQueue || !this.initialized || !this.db) {
|
||||
if (!this.initialized || !this.db) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.isProcessingQueue = true;
|
||||
|
||||
while (this.operationQueue.length > 0) {
|
||||
const operation = this.operationQueue.shift();
|
||||
if (!operation) continue;
|
||||
|
||||
try {
|
||||
let result: unknown;
|
||||
switch (operation.type) {
|
||||
case "run":
|
||||
result = await this.db.run(operation.sql, operation.params);
|
||||
break;
|
||||
case "query":
|
||||
result = await this.db.exec(operation.sql, operation.params);
|
||||
break;
|
||||
}
|
||||
operation.resolve(result);
|
||||
} catch (error) {
|
||||
logger.error(
|
||||
"Error while processing SQL queue:",
|
||||
error,
|
||||
" ... for sql:",
|
||||
operation.sql,
|
||||
" ... with params:",
|
||||
operation.params,
|
||||
);
|
||||
operation.reject(error);
|
||||
}
|
||||
}
|
||||
|
||||
this.isProcessingQueue = false;
|
||||
await this.operationQueue.processQueue(
|
||||
this.db,
|
||||
this.createExecutor(),
|
||||
"AbsurdSqlDatabaseService",
|
||||
);
|
||||
}
|
||||
|
||||
private async queueOperation<R>(
|
||||
@@ -204,21 +185,24 @@ class AbsurdSqlDatabaseService implements DatabaseService {
|
||||
sql: string,
|
||||
params: unknown[] = [],
|
||||
): Promise<R> {
|
||||
return new Promise<R>((resolve, reject) => {
|
||||
const operation: QueuedOperation = {
|
||||
type,
|
||||
sql,
|
||||
params,
|
||||
resolve: (value: unknown) => resolve(value as R),
|
||||
reject,
|
||||
};
|
||||
this.operationQueue.push(operation);
|
||||
const operation: QueuedOperation = {
|
||||
type,
|
||||
sql,
|
||||
params,
|
||||
resolve: (_value: unknown) => {
|
||||
// No-op, will be wrapped by OperationQueue
|
||||
},
|
||||
reject: () => {
|
||||
// No-op, will be wrapped by OperationQueue
|
||||
},
|
||||
};
|
||||
|
||||
// If we're already initialized, start processing the queue
|
||||
if (this.initialized && this.db) {
|
||||
this.processQueue();
|
||||
}
|
||||
});
|
||||
return this.operationQueue.queueOperation<R>(
|
||||
operation,
|
||||
this.initialized,
|
||||
this.db,
|
||||
() => this.processQueue(),
|
||||
);
|
||||
}
|
||||
|
||||
private async waitForInitialization(): Promise<void> {
|
||||
|
||||
@@ -4,76 +4,144 @@ import { CapacitorPlatformService } from "./platforms/CapacitorPlatformService";
|
||||
import { ElectronPlatformService } from "./platforms/ElectronPlatformService";
|
||||
|
||||
/**
|
||||
* Factory class for creating platform-specific service implementations.
|
||||
* Implements the Singleton pattern to ensure only one instance of PlatformService exists.
|
||||
* HMR-safe global singleton storage for PlatformService
|
||||
*
|
||||
* The factory determines which platform implementation to use based on the VITE_PLATFORM
|
||||
* environment variable. Supported platforms are:
|
||||
* - capacitor: Mobile platform using Capacitor
|
||||
* - electron: Desktop platform using Electron with Capacitor
|
||||
* - web: Default web platform (fallback)
|
||||
* Uses multiple fallbacks to ensure persistence across module reloads:
|
||||
* 1. globalThis (standard, works in most environments)
|
||||
* 2. window (browser fallback)
|
||||
* 3. self (web worker fallback)
|
||||
*/
|
||||
declare global {
|
||||
// eslint-disable-next-line no-var
|
||||
var __PLATFORM_SERVICE_SINGLETON__: PlatformService | undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the global object for singleton storage
|
||||
* Uses multiple fallbacks to ensure compatibility
|
||||
*/
|
||||
function getGlobal(): typeof globalThis {
|
||||
if (typeof globalThis !== "undefined") return globalThis;
|
||||
if (typeof window !== "undefined") return window as typeof globalThis;
|
||||
if (typeof self !== "undefined") return self as typeof globalThis;
|
||||
// Fallback for Node.js environments
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
return {} as any;
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory function to create platform-specific service implementation
|
||||
*
|
||||
* Uses console.log instead of logger to avoid circular dependency
|
||||
* (logger imports PlatformServiceFactory)
|
||||
*/
|
||||
function create(): PlatformService {
|
||||
const which = import.meta.env?.VITE_PLATFORM ?? "web";
|
||||
|
||||
if (which === "capacitor") return new CapacitorPlatformService();
|
||||
if (which === "electron") return new ElectronPlatformService();
|
||||
return new WebPlatformService();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get or create the HMR-safe singleton instance of PlatformService
|
||||
*
|
||||
* Uses lazy initialization to avoid circular dependency issues at module load time.
|
||||
*/
|
||||
function getPlatformSvc(): PlatformService {
|
||||
const global = getGlobal();
|
||||
|
||||
const exists = global.__PLATFORM_SERVICE_SINGLETON__ !== undefined;
|
||||
|
||||
if (!exists) {
|
||||
global.__PLATFORM_SERVICE_SINGLETON__ = create();
|
||||
// Verify it was stored
|
||||
if (!global.__PLATFORM_SERVICE_SINGLETON__) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.error(
|
||||
"[PlatformServiceFactory] ERROR: Singleton creation failed - storage returned undefined",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Type guard: ensure singleton exists (should never be undefined at this point)
|
||||
const singleton = global.__PLATFORM_SERVICE_SINGLETON__;
|
||||
if (!singleton) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.error(
|
||||
"[PlatformServiceFactory] CRITICAL: Singleton is undefined after creation/retrieval",
|
||||
);
|
||||
// Fallback: create a new one
|
||||
global.__PLATFORM_SERVICE_SINGLETON__ = create();
|
||||
return global.__PLATFORM_SERVICE_SINGLETON__;
|
||||
}
|
||||
|
||||
return singleton;
|
||||
}
|
||||
|
||||
/**
|
||||
* HMR-safe singleton instance of PlatformService
|
||||
*
|
||||
* This is the ONLY way to access PlatformService throughout the application.
|
||||
* Do not create new instances of platform services directly.
|
||||
*
|
||||
* Uses lazy initialization via Proxy to avoid circular dependency issues at module load time.
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const platformService = PlatformServiceFactory.getInstance();
|
||||
* await platformService.takePicture();
|
||||
* import { PlatformSvc } from "./services/PlatformServiceFactory";
|
||||
* await PlatformSvc.takePicture();
|
||||
* ```
|
||||
*/
|
||||
export class PlatformServiceFactory {
|
||||
private static instance: PlatformService | null = null;
|
||||
private static callCount = 0; // Debug counter
|
||||
private static creationLogged = false; // Only log creation once
|
||||
export const PlatformSvc = new Proxy({} as PlatformService, {
|
||||
get(_target, prop) {
|
||||
const svc = getPlatformSvc();
|
||||
const value = (svc as unknown as Record<string, unknown>)[prop as string];
|
||||
// Bind methods to maintain 'this' context
|
||||
if (typeof value === "function") {
|
||||
return value.bind(svc);
|
||||
}
|
||||
return value;
|
||||
},
|
||||
});
|
||||
|
||||
// Preserve singleton across Vite HMR
|
||||
if (import.meta?.hot) {
|
||||
import.meta.hot.accept(() => {
|
||||
// Don't recreate on HMR - keep existing instance
|
||||
const global = getGlobal();
|
||||
if (!global.__PLATFORM_SERVICE_SINGLETON__) {
|
||||
// Restore singleton if it was lost during HMR
|
||||
global.__PLATFORM_SERVICE_SINGLETON__ = getPlatformSvc();
|
||||
}
|
||||
});
|
||||
import.meta.hot.dispose(() => {
|
||||
// Don't delete - keep the global instance
|
||||
// The singleton will persist in globalThis/window/self
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Legacy factory class for backward compatibility
|
||||
* @deprecated Use `PlatformSvc` directly instead
|
||||
*/
|
||||
export class PlatformServiceFactory {
|
||||
/**
|
||||
* Gets or creates the singleton instance of PlatformService.
|
||||
* Creates the appropriate platform-specific implementation based on environment.
|
||||
*
|
||||
* @returns {PlatformService} The singleton instance of PlatformService
|
||||
* Gets the singleton instance of PlatformService.
|
||||
* @deprecated Use `PlatformSvc` directly instead
|
||||
*/
|
||||
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) {
|
||||
// Use console for critical startup message to avoid circular dependency
|
||||
// eslint-disable-next-line no-console
|
||||
console.log(
|
||||
`[PlatformServiceFactory] Creating singleton instance for platform: ${platform}`,
|
||||
);
|
||||
PlatformServiceFactory.creationLogged = true;
|
||||
}
|
||||
|
||||
switch (platform) {
|
||||
case "capacitor":
|
||||
PlatformServiceFactory.instance = new CapacitorPlatformService();
|
||||
break;
|
||||
case "electron":
|
||||
// Use a specialized electron service that extends CapacitorPlatformService
|
||||
PlatformServiceFactory.instance = new ElectronPlatformService();
|
||||
break;
|
||||
case "web":
|
||||
default:
|
||||
PlatformServiceFactory.instance = new WebPlatformService();
|
||||
break;
|
||||
}
|
||||
|
||||
return PlatformServiceFactory.instance;
|
||||
return PlatformSvc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Debug method to check singleton usage stats
|
||||
*/
|
||||
public static getStats(): { callCount: number; instanceExists: boolean } {
|
||||
const global = getGlobal();
|
||||
return {
|
||||
callCount: PlatformServiceFactory.callCount,
|
||||
instanceExists: PlatformServiceFactory.instance !== null,
|
||||
callCount: 0, // Deprecated - no longer tracking
|
||||
instanceExists: global.__PLATFORM_SERVICE_SINGLETON__ !== undefined,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
135
src/services/platforms/OperationQueue.ts
Normal file
135
src/services/platforms/OperationQueue.ts
Normal file
@@ -0,0 +1,135 @@
|
||||
/**
|
||||
* Shared operation queue handler for database services
|
||||
*
|
||||
* Provides a reusable queue mechanism for database operations that need to
|
||||
* wait for initialization before execution.
|
||||
*/
|
||||
|
||||
import { QueuedOperation } from "./types";
|
||||
import { logger } from "../../utils/logger";
|
||||
|
||||
export interface QueueExecutor<TDb> {
|
||||
executeRun(db: TDb, sql: string, params: unknown[]): Promise<unknown>;
|
||||
executeQuery(db: TDb, sql: string, params: unknown[]): Promise<unknown>;
|
||||
executeRawQuery?(db: TDb, sql: string, params: unknown[]): Promise<unknown>;
|
||||
}
|
||||
|
||||
export class OperationQueue<TDb> {
|
||||
private operationQueue: Array<QueuedOperation> = [];
|
||||
private isProcessingQueue: boolean = false;
|
||||
|
||||
/**
|
||||
* Process queued operations
|
||||
*/
|
||||
async processQueue(
|
||||
db: TDb,
|
||||
executor: QueueExecutor<TDb>,
|
||||
serviceName: string,
|
||||
): Promise<void> {
|
||||
if (this.isProcessingQueue || !db) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.isProcessingQueue = true;
|
||||
|
||||
while (this.operationQueue.length > 0) {
|
||||
const operation = this.operationQueue.shift();
|
||||
if (!operation) continue;
|
||||
|
||||
try {
|
||||
let result: unknown;
|
||||
switch (operation.type) {
|
||||
case "run":
|
||||
result = await executor.executeRun(
|
||||
db,
|
||||
operation.sql,
|
||||
operation.params,
|
||||
);
|
||||
break;
|
||||
case "query":
|
||||
result = await executor.executeQuery(
|
||||
db,
|
||||
operation.sql,
|
||||
operation.params,
|
||||
);
|
||||
break;
|
||||
case "rawQuery":
|
||||
if (executor.executeRawQuery) {
|
||||
result = await executor.executeRawQuery(
|
||||
db,
|
||||
operation.sql,
|
||||
operation.params,
|
||||
);
|
||||
} else {
|
||||
// Fallback to query if rawQuery not supported
|
||||
result = await executor.executeQuery(
|
||||
db,
|
||||
operation.sql,
|
||||
operation.params,
|
||||
);
|
||||
}
|
||||
break;
|
||||
}
|
||||
operation.resolve(result);
|
||||
} catch (error) {
|
||||
logger.error(
|
||||
`[${serviceName}] Error while processing SQL queue:`,
|
||||
error,
|
||||
);
|
||||
logger.error(
|
||||
`[${serviceName}] Failed operation - Type: ${operation.type}, SQL: ${operation.sql}`,
|
||||
);
|
||||
logger.error(
|
||||
`[${serviceName}] Failed operation - Params:`,
|
||||
operation.params,
|
||||
);
|
||||
operation.reject(error);
|
||||
}
|
||||
}
|
||||
|
||||
this.isProcessingQueue = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Queue an operation for later execution
|
||||
*
|
||||
* @param operation - Pre-constructed operation object (allows platform-specific parameter conversion)
|
||||
* @param initialized - Whether the database is initialized
|
||||
* @param db - Database connection (if available)
|
||||
* @param onQueue - Callback to trigger queue processing
|
||||
*/
|
||||
queueOperation<R>(
|
||||
operation: QueuedOperation,
|
||||
initialized: boolean,
|
||||
db: TDb | null,
|
||||
onQueue: () => void,
|
||||
): Promise<R> {
|
||||
return new Promise<R>((resolve, reject) => {
|
||||
// Wrap the operation's resolve/reject to match our Promise
|
||||
const wrappedOperation: QueuedOperation = {
|
||||
...operation,
|
||||
resolve: (value: unknown) => {
|
||||
operation.resolve(value);
|
||||
resolve(value as R);
|
||||
},
|
||||
reject: (reason: unknown) => {
|
||||
operation.reject(reason);
|
||||
reject(reason);
|
||||
},
|
||||
};
|
||||
this.operationQueue.push(wrappedOperation);
|
||||
|
||||
// If already initialized, trigger queue processing
|
||||
if (initialized && db) {
|
||||
onQueue();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current queue length (for debugging)
|
||||
*/
|
||||
getQueueLength(): number {
|
||||
return this.operationQueue.length;
|
||||
}
|
||||
}
|
||||
20
src/services/platforms/sqlite.ts
Normal file
20
src/services/platforms/sqlite.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
/**
|
||||
* Shared SQLite connection manager for Capacitor platform
|
||||
*
|
||||
* Ensures only one SQLiteConnection instance exists across the application,
|
||||
* preventing connection desync issues and unnecessary connection recreation.
|
||||
*/
|
||||
|
||||
import { CapacitorSQLite, SQLiteConnection } from "@capacitor-community/sqlite";
|
||||
|
||||
/**
|
||||
* Native Capacitor SQLite plugin instance
|
||||
* This is the bridge to the native SQLite implementation
|
||||
*/
|
||||
export const CAP_SQLITE = CapacitorSQLite;
|
||||
|
||||
/**
|
||||
* Shared SQLite connection manager
|
||||
* Use this instance throughout the application - do not create new SQLiteConnection instances
|
||||
*/
|
||||
export const SQLITE = new SQLiteConnection(CAP_SQLITE);
|
||||
13
src/services/platforms/types.ts
Normal file
13
src/services/platforms/types.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
/**
|
||||
* Types for platform services
|
||||
*/
|
||||
|
||||
export interface QueuedOperation {
|
||||
type: "run" | "query" | "rawQuery";
|
||||
sql: string;
|
||||
params: unknown[];
|
||||
resolve: (value: unknown) => void;
|
||||
reject: (reason: unknown) => void;
|
||||
}
|
||||
|
||||
export type QueuedOperationType = QueuedOperation["type"];
|
||||
@@ -1,4 +1,5 @@
|
||||
import { NotificationIface } from "@/constants/app";
|
||||
import router from "@/router";
|
||||
|
||||
const SEED_REMINDER_KEY = "seedPhraseReminderLastShown";
|
||||
const REMINDER_COOLDOWN_MS = 24 * 60 * 60 * 1000; // 24 hours in milliseconds
|
||||
@@ -53,8 +54,8 @@ export function createSeedReminderNotification(): NotificationIface {
|
||||
yesText: "Backup Identifier Seed",
|
||||
noText: "Remind me Later",
|
||||
onYes: async () => {
|
||||
// Navigate to seed backup page
|
||||
window.location.href = "/seed-backup";
|
||||
// Navigate to seed backup page using SPA routing
|
||||
await router.push({ path: "/seed-backup" });
|
||||
},
|
||||
onNo: async () => {
|
||||
// Mark as shown so it won't appear again for 24 hours
|
||||
|
||||
@@ -157,11 +157,27 @@ export default class DeepLinkRedirectView extends Vue {
|
||||
}
|
||||
|
||||
try {
|
||||
const capabilities = this.platformService.getCapabilities();
|
||||
|
||||
// If we're already in the native app, use router navigation instead
|
||||
// of window.location.href (which doesn't work properly in Capacitor)
|
||||
if (capabilities.isNativeApp) {
|
||||
// Navigate directly using the router
|
||||
const destinationPath = `/${this.destinationUrl}`;
|
||||
this.$router.push(destinationPath).catch((error) => {
|
||||
logger.error("Router navigation failed: " + errorStringForLog(error));
|
||||
this.pageError =
|
||||
"Unable to navigate to the destination. Please use a manual option below.";
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// For web contexts, use window.location.href to redirect to app
|
||||
// For mobile, try the deep link URL; for desktop, use the web URL
|
||||
const redirectUrl = this.isMobile ? this.deepLinkUrl : this.webUrl;
|
||||
|
||||
// Method 1: Try window.location.href (works on most browsers)
|
||||
window.location.href = redirectUrl;
|
||||
window.location.href = redirectUrl; // Do not use this on native apps! The channel to Capacitor gets messed up.
|
||||
|
||||
// Method 2: Fallback - create and click a link element
|
||||
setTimeout(() => {
|
||||
|
||||
Reference in New Issue
Block a user