@afriapps/fs-client
Version:
React Native FileServer Client SDK for file synchronization and data management. Requires React Native environment with SQLite and File System support.
113 lines (91 loc) • 4.18 kB
text/typescript
import DatabaseService from './database/DatabaseService';
import DataHolderManager from './dataholder/DataHolderManager';
import FS_FileManager from './storage/FS_FileManager';
import { initializeEnvironment } from './abstraction/initializeEnvironment';
import NetworkService from './network/NetworkService';
import { CompletableFutureManager } from './utils/CompletableFutureManager';
import { logger } from './utils/logger';
import { type FsNetworkConfig } from '../config/FsNetworkConfig';
interface IFileJobProcessor {
processAllJobs(): Promise<void>;
processUploadJobs(groups?: string): Promise<boolean>;
}
interface ISyncListener {
register(): void;
}
interface FileServerOptions {
defaultDbConfig?: string;
networkConfig?: FsNetworkConfig;
/** @deprecated Use networkConfig instead. Only kept for legacy file-based configurations */
configPath?: string;
}
class FileJobProcessor implements IFileJobProcessor {
constructor(private readonly fileManager: FS_FileManager) {}
async processAllJobs(): Promise<void> {
await this.fileManager.processAllFileJobs('raw');
await this.fileManager.processAllFileJobs('All');
}
async processUploadJobs(groups = 'All'): Promise<boolean> {
return this.fileManager.processUploadJobs(groups);
}
}
// class NetworkSyncManager implements ISyncListener {
// constructor(
// private readonly fileJobProcessor: FileJobProcessor,
// private readonly isSyncing: Observable<boolean>
// ) {}
// register(): void {
// this.networkInfoManager.addHasInternetListener(async (hasInternet) => {
// if (hasInternet && !this.isSyncing.get()) {
// this.isSyncing.set(true);
// logger.debug('🌐 Internet is back — syncing files...');
// await this.fileJobProcessor.processUploadJobs();
// this.isSyncing.set(false);
// }
// });
// }
// }
class FileServer {
private dbService: DatabaseService | null = null;
private fileManager: FS_FileManager | null = null;
private fileJobProcessor: FileJobProcessor | null = null;
private dataHolderManager: DataHolderManager | null = null;
private readonly config: { defaultDbConfig: string; networkConfig?: FsNetworkConfig; configPath?: string };
constructor(options: FileServerOptions = {}) {
this.config = {
defaultDbConfig: options.defaultDbConfig ?? 'fsclient.db',
networkConfig: options.networkConfig,
// Keep configPath for legacy support only
configPath: options.configPath || "default",
};
}
public async initialize(): Promise<void> {
await this.initializeWithConfig(this.config.networkConfig, this.config.configPath);
}
public async processFileJobs(): Promise<void> {
if (!this.fileJobProcessor) throw new Error('FileJobProcessor not initialized.');
await this.fileJobProcessor.processAllJobs();
}
public async processUploadJobs(groups = 'All'): Promise<boolean> {
if (!this.fileJobProcessor) throw new Error('FileJobProcessor not initialized.');
return this.fileJobProcessor.processUploadJobs(groups);
}
public getDataHolderManager(): DataHolderManager | null {
return this.dataHolderManager;
}
private async initializeWithConfig(networkConfig?: FsNetworkConfig, legacyConfigPath?: string): Promise<void> {
const { database, fileSystem } = await initializeEnvironment(this.config.defaultDbConfig);
this.dbService = new DatabaseService(database, fileSystem);
const networkService = await NetworkService.getInstance(legacyConfigPath, networkConfig);
this.fileManager = new FS_FileManager(networkService, this.dbService, fileSystem, new CompletableFutureManager());
this.fileJobProcessor = new FileJobProcessor(this.fileManager);
await this.fileJobProcessor.processAllJobs();
this.dataHolderManager = new DataHolderManager(this.fileManager, this.dbService);
await this.dataHolderManager.initialize();
// FIXME: Uncomment and implement network sync manager if needed
// const syncManager = new NetworkSyncManager(this.fileJobProcessor);
// syncManager.register();
logger.debug('✅ FileServer initialization complete.');
}
}
export default FileServer;