WIP: Migrate to using taskfile #187

Closed
schmelczer wants to merge 26 commits from asch/taskfiles into main
Showing only changes of commit d91993f249 - Show all commits

Unsubscribe in SyncClient

Andras Schmelczer 2025-12-14 11:31:48 +00:00

View file

@ -34,6 +34,8 @@ export class SyncClient {
private hasStarted = false; private hasStarted = false;
private hasBeenDestroyed = false; private hasBeenDestroyed = false;
private unloadTelemetry?: () => void; private unloadTelemetry?: () => void;
private isDestroying = false;
private readonly eventUnsubscribers: (() => void)[] = [];
private constructor( private constructor(
private readonly history: SyncHistory, private readonly history: SyncHistory,
@ -53,8 +55,9 @@ export class SyncClient {
settings: Partial<SyncSettings>; settings: Partial<SyncSettings>;
database: Partial<StoredDatabase>; database: Partial<StoredDatabase>;
}> }>
> >,
) {} ) {
}
public get documentCount(): number { public get documentCount(): number {
return this.database.length; return this.database.length;
@ -159,11 +162,6 @@ export class SyncClient {
settings.getSettings().isSyncEnabled, settings.getSettings().isSyncEnabled,
logger logger
); );
settings.onSettingsChanged.add((newSettings, oldSettings) => {
if (oldSettings.isSyncEnabled != newSettings.isSyncEnabled) {
fetchController.canFetch = newSettings.isSyncEnabled;
}
});
const syncService = new SyncService( const syncService = new SyncService(
deviceId, deviceId,
@ -258,13 +256,23 @@ export class SyncClient {
this.unloadTelemetry = setUpTelemetry(); this.unloadTelemetry = setUpTelemetry();
} }
this.logger.onLogEmitted.add((log): void => { this.eventUnsubscribers.push(this.settings.onSettingsChanged.add((newSettings, oldSettings) => {
if (log.level === LogLevel.ERROR && Sentry.isInitialized()) { if (oldSettings.isSyncEnabled != newSettings.isSyncEnabled) {
Sentry.captureMessage(log.message); this.fetchController.canFetch = newSettings.isSyncEnabled;
} }
}); }));
this.settings.onSettingsChanged.add(this.onSettingsChange.bind(this)); this.eventUnsubscribers.push(
this.logger.onLogEmitted.add((log): void => {
if (log.level === LogLevel.ERROR && Sentry.isInitialized()) {
Sentry.captureMessage(log.message);
}
})
);
this.eventUnsubscribers.push(
this.settings.onSettingsChanged.add(this.onSettingsChange.bind(this))
);
if (this.settings.getSettings().isSyncEnabled) { if (this.settings.getSettings().isSyncEnabled) {
this.logger.info("Starting SyncClient"); this.logger.info("Starting SyncClient");
@ -431,6 +439,13 @@ export class SyncClient {
public async destroy(): Promise<void> { public async destroy(): Promise<void> {
this.checkIfDestroyed("destroy"); this.checkIfDestroyed("destroy");
// Prevent concurrent destroy calls
if (this.isDestroying) {
this.logger.warn("destroy() called while already destroying, ignoring");
return;
}
this.isDestroying = true;
// cancel everything that's in progress // cancel everything that's in progress
await this.pause(); await this.pause();
@ -438,6 +453,10 @@ export class SyncClient {
this.resetInMemoryState(); this.resetInMemoryState();
// Clean up event listeners to prevent memory leaks
this.eventUnsubscribers.forEach((unsubscribe) => unsubscribe());
this.eventUnsubscribers.length = 0;
this.logger.info("SyncClient has been successfully disposed"); this.logger.info("SyncClient has been successfully disposed");
this.unloadTelemetry?.(); this.unloadTelemetry?.();