Skip to content

The open alternative to Salesforce, designed for AI.

fix(sdk): rerun dev sync when changes arrive during sync (#24514)

Félix Malfait6d80b7ec8912
diff --git a/packages/twenty-sdk/src/cli/utilities/dev/orchestrator/__tests__/dev-mode-orchestrator.spec.ts b/packages/twenty-sdk/src/cli/utilities/dev/orchestrator/__tests__/dev-mode-orchestrator.spec.ts
new file mode 100644
index 00000000000..43b13496ac8
--- /dev/null
+++ b/packages/twenty-sdk/src/cli/utilities/dev/orchestrator/__tests__/dev-mode-orchestrator.spec.ts
@@ -0,0 +1,146 @@
+import { afterEach, describe, expect, it, vi } from 'vitest';
+
+vi.mock('@/cli/utilities/api/api-service', () => ({
+ ApiService: class ApiService {},
+}));
+vi.mock('@/cli/utilities/auth/build-app-token-pair-fetcher', () => ({
+ buildAppTokenPairFetcher: vi.fn(),
+}));
+vi.mock('@/cli/utilities/client/client-service', () => ({
+ ClientService: class ClientService {},
+}));
+vi.mock('@/cli/utilities/config/config-service', () => ({
+ ConfigService: class ConfigService {
+ static getActiveRemote() {
+ return 'test';
+ }
+ },
+}));
+vi.mock(
+ '@/cli/utilities/dev/orchestrator/steps/build-manifest-orchestrator-step',
+ () => ({ BuildManifestOrchestratorStep: class {} }),
+);
+vi.mock(
+ '@/cli/utilities/dev/orchestrator/steps/check-server-orchestrator-step',
+ () => ({ CheckServerOrchestratorStep: class {} }),
+);
+vi.mock(
+ '@/cli/utilities/dev/orchestrator/steps/generate-api-client-orchestrator-step',
+ () => ({ GenerateApiClientOrchestratorStep: class {} }),
+);
+vi.mock(
+ '@/cli/utilities/dev/orchestrator/steps/register-app-orchestrator-step',
+ () => ({ RegisterAppOrchestratorStep: class {} }),
+);
+vi.mock(
+ '@/cli/utilities/dev/orchestrator/steps/start-watchers-orchestrator-step',
+ () => ({
+ StartWatchersOrchestratorStep: class {
+ async close() {}
+ },
+ }),
+);
+vi.mock(
+ '@/cli/utilities/dev/orchestrator/steps/sync-application-orchestrator-step',
+ () => ({ SyncApplicationOrchestratorStep: class {} }),
+);
+vi.mock(
+ '@/cli/utilities/dev/orchestrator/steps/upload-files-orchestrator-step',
+ () => ({ UploadFilesOrchestratorStep: class {} }),
+);
+vi.mock('@/cli/utilities/error/serialize-error', () => ({
+ serializeError: vi.fn(),
+}));
+vi.mock('@/cli/utilities/file/fs-utils', () => ({
+ emptyDir: vi.fn(),
+ ensureDir: vi.fn(),
+}));
+vi.mock('twenty-shared/application', () => ({
+ OUTPUT_DIR: '.twenty',
+ SyncableEntity: {
+ Object: 'object',
+ Field: 'field',
+ LogicFunction: 'logicFunction',
+ FrontComponent: 'frontComponent',
+ Role: 'role',
+ Skill: 'skill',
+ ConnectionProvider: 'connectionProvider',
+ View: 'view',
+ ViewField: 'viewField',
+ NavigationMenuItem: 'navigationMenuItem',
+ PageLayout: 'pageLayout',
+ PageLayoutTab: 'pageLayoutTab',
+ CommandMenuItem: 'commandMenuItem',
+ },
+}));
+
+import { DevModeOrchestrator } from '@/cli/utilities/dev/orchestrator/dev-mode-orchestrator';
+import { OrchestratorState } from '@/cli/utilities/dev/orchestrator/dev-mode-orchestrator-state';
+
+type DevModeOrchestratorTestHarness = {
+ runSyncPipeline: () => Promise<void>;
+ scheduleSync: () => void;
+};
+
+describe('DevModeOrchestrator', () => {
+ afterEach(() => {
+ vi.useRealTimers();
+ });
+
+ it('runs another sync when changes arrive during an active sync', async () => {
+ vi.useFakeTimers();
+
+ const state = new OrchestratorState({ appPath: '/tmp/app' });
+ const orchestrator = new DevModeOrchestrator({
+ state,
+ debounceMs: 10,
+ });
+ const testHarness =
+ orchestrator as unknown as DevModeOrchestratorTestHarness;
+ let resolveFirstSync: (() => void) | undefined;
+ const firstSync = new Promise<void>((resolve) => {
+ resolveFirstSync = resolve;
+ });
+ const runSyncPipeline = vi
+ .fn<() => Promise<void>>()
+ .mockImplementationOnce(() => firstSync)
+ .mockResolvedValueOnce();
+
+ testHarness.runSyncPipeline = runSyncPipeline;
+
+ testHarness.scheduleSync();
+ await vi.advanceTimersByTimeAsync(10);
+
+ testHarness.scheduleSync();
+ await vi.advanceTimersByTimeAsync(10);
+
+ expect(runSyncPipeline).toHaveBeenCalledTimes(1);
+
+ resolveFirstSync?.();
+ await firstSync;
+ await vi.advanceTimersByTimeAsync(10);
+
+ expect(runSyncPipeline).toHaveBeenCalledTimes(2);
+ });
+
+ it('cancels a scheduled sync when closed', async () => {
+ vi.useFakeTimers();
+
+ const state = new OrchestratorState({ appPath: '/tmp/app' });
+ const orchestrator = new DevModeOrchestrator({
+ state,
+ debounceMs: 10,
+ });
+ const testHarness =
+ orchestrator as unknown as DevModeOrchestratorTestHarness;
+ const runSyncPipeline = vi.fn<() => Promise<void>>().mockResolvedValue();
+
+ testHarness.runSyncPipeline = runSyncPipeline;
+ testHarness.scheduleSync();
+
+ await orchestrator.close();
+ await vi.advanceTimersByTimeAsync(10);
+
+ expect(runSyncPipeline).not.toHaveBeenCalled();
+ });
+});
diff --git a/packages/twenty-sdk/src/cli/utilities/dev/orchestrator/dev-mode-orchestrator.ts b/packages/twenty-sdk/src/cli/utilities/dev/orchestrator/dev-mode-orchestrator.ts
index 1725d52dc65..c89eb1387b8 100644
--- a/packages/twenty-sdk/src/cli/utilities/dev/orchestrator/dev-mode-orchestrator.ts
+++ b/packages/twenty-sdk/src/cli/utilities/dev/orchestrator/dev-mode-orchestrator.ts
@@ -33,6 +33,8 @@ export class DevModeOrchestrator {
private debounceMs: number;
private syncTimer: NodeJS.Timeout | null = null;
private serverCheckInterval: NodeJS.Timeout | null = null;
+ private syncRequestedWhileRunning = false;
+ private isClosed = false;
private apiService: ApiService;
private clientService: ClientService;
@@ -94,6 +96,8 @@ export class DevModeOrchestrator {
}
async start(): Promise<void> {
+ this.isClosed = false;
+
const outputDir = path.join(this.state.appPath, OUTPUT_DIR);
await ensureDir(outputDir);
@@ -121,8 +125,16 @@ export class DevModeOrchestrator {
}
async close(): Promise<void> {
+ this.isClosed = true;
+
+ if (this.syncTimer) {
+ clearTimeout(this.syncTimer);
+ this.syncTimer = null;
+ }
+
if (this.serverCheckInterval) {
clearInterval(this.serverCheckInterval);
+ this.serverCheckInterval = null;
}
await this.startWatchersStep.close();
@@ -152,6 +164,10 @@ export class DevModeOrchestrator {
}
private scheduleSync(): void {
+ if (this.isClosed) {
+ return;
+ }
+
if (this.syncTimer) {
clearTimeout(this.syncTimer);
}
@@ -164,6 +180,7 @@ export class DevModeOrchestrator {
private async performSync(): Promise<void> {
if (this.state.pipeline.isSyncing) {
+ this.syncRequestedWhileRunning = true;
return;
}
@@ -180,6 +197,11 @@ export class DevModeOrchestrator {
this.state.updateAllEntitiesStatus('error');
} finally {
this.state.updatePipeline({ isSyncing: false });
+
+ if (this.syncRequestedWhileRunning && !this.isClosed) {
+ this.syncRequestedWhileRunning = false;
+ this.scheduleSync();
+ }
}
}