Skip to content

Commit 659b46f

Browse files
committed
chore: in frontend, xxStore files: moving autoRefresh as an optional functionality instead of starting timers all the time
1 parent 22b09ba commit 659b46f

6 files changed

Lines changed: 54 additions & 43 deletions

File tree

frontend/src/lib/admin/stores/__tests__/eventsStore.test.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ describe('EventsStore', () => {
9999
});
100100

101101
function createStore() {
102+
teardown = effect_root(() => {
103+
store = createEventsStore({ autoRefresh: false });
104+
});
105+
}
106+
107+
function createStoreWithAutoRefresh() {
102108
teardown = effect_root(() => {
103109
store = createEventsStore();
104110
});
@@ -108,6 +114,7 @@ describe('EventsStore', () => {
108114
vi.unstubAllGlobals();
109115
store?.cleanup();
110116
teardown?.();
117+
vi.clearAllTimers();
111118
});
112119

113120
describe('initial state', () => {
@@ -386,7 +393,7 @@ describe('EventsStore', () => {
386393

387394
describe('auto-refresh', () => {
388395
it('fires loadAll on 30s interval', async () => {
389-
createStore();
396+
createStoreWithAutoRefresh();
390397
vi.clearAllMocks();
391398

392399
await vi.advanceTimersByTimeAsync(30000);
@@ -397,7 +404,7 @@ describe('EventsStore', () => {
397404
});
398405

399406
it('stops on teardown', async () => {
400-
createStore();
407+
createStoreWithAutoRefresh();
401408
await vi.advanceTimersByTimeAsync(30000);
402409
expect(mocks.browseEventsApiV1AdminEventsBrowsePost).toHaveBeenCalled();
403410

frontend/src/lib/admin/stores/__tests__/executionsStore.test.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,10 @@ describe('ExecutionsStore', () => {
5656
setupDefaultMocks();
5757
});
5858

59-
/**
60-
* Creates a store with auto-refresh timers immediately cleared.
61-
* The $effect fires synchronously inside effect_root, starting a
62-
* setInterval. We clear all timers, reset mocks, and re-apply defaults
63-
* so individual tests control timing explicitly.
64-
*/
6559
function createStore() {
6660
teardown = effect_root(() => {
67-
store = createExecutionsStore();
61+
store = createExecutionsStore({ autoRefresh: false });
6862
});
69-
vi.clearAllTimers();
70-
vi.clearAllMocks();
71-
setupDefaultMocks();
7263
}
7364

7465
function createStoreWithAutoRefresh() {

frontend/src/lib/admin/stores/__tests__/sagasStore.test.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,20 @@ describe('SagasStore', () => {
2929
});
3030

3131
function createStore() {
32+
teardown = effect_root(() => {
33+
store = createSagasStore({ autoRefresh: false });
34+
});
35+
}
36+
37+
function createStoreWithAutoRefresh() {
3238
teardown = effect_root(() => {
3339
store = createSagasStore();
3440
});
3541
}
3642

3743
afterEach(() => {
3844
teardown?.();
45+
vi.clearAllTimers();
3946
});
4047

4148
describe('initial state', () => {
@@ -190,7 +197,7 @@ describe('SagasStore', () => {
190197

191198
describe('auto-refresh', () => {
192199
it('fires loadSagas on interval', async () => {
193-
createStore();
200+
createStoreWithAutoRefresh();
194201
vi.clearAllMocks();
195202

196203
await vi.advanceTimersByTimeAsync(5000);
@@ -206,7 +213,7 @@ describe('SagasStore', () => {
206213
data: { sagas, total: 1 },
207214
});
208215

209-
createStore();
216+
createStoreWithAutoRefresh();
210217
await store.loadExecutionSagas('exec-target');
211218
vi.clearAllMocks();
212219

@@ -224,7 +231,7 @@ describe('SagasStore', () => {
224231
});
225232

226233
it('stops when refreshEnabled set to false', async () => {
227-
createStore();
234+
createStoreWithAutoRefresh();
228235
await vi.advanceTimersByTimeAsync(5000);
229236
expect(mocks.listSagasApiV1SagasGet).toHaveBeenCalled();
230237

frontend/src/lib/admin/stores/eventsStore.svelte.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,15 @@ class EventsStore {
3939

4040
pagination = createPaginationState({ initialPageSize: 10 });
4141

42-
constructor() {
43-
$effect(() => {
44-
const id = setInterval(() => this.loadAll(), 30_000);
45-
return () => {
46-
clearInterval(id);
47-
};
48-
});
42+
constructor({ autoRefresh = true }: { autoRefresh?: boolean } = {}) {
43+
if (autoRefresh) {
44+
$effect(() => {
45+
const id = setInterval(() => this.loadAll(), 30_000);
46+
return () => {
47+
clearInterval(id);
48+
};
49+
});
50+
}
4951
}
5052

5153
async loadAll(): Promise<void> {
@@ -235,6 +237,6 @@ class EventsStore {
235237
}
236238
}
237239

238-
export function createEventsStore(): EventsStore {
239-
return new EventsStore();
240+
export function createEventsStore(options?: { autoRefresh?: boolean }): EventsStore {
241+
return new EventsStore(options);
240242
}

frontend/src/lib/admin/stores/executionsStore.svelte.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ class ExecutionsStore {
2323

2424
pagination = createPaginationState({ initialPageSize: 20 });
2525

26-
constructor() {
27-
$effect(() => {
28-
const id = setInterval(() => this.loadData(), 5000);
29-
return () => {
30-
clearInterval(id);
31-
};
32-
});
26+
constructor({ autoRefresh = true }: { autoRefresh?: boolean } = {}) {
27+
if (autoRefresh) {
28+
$effect(() => {
29+
const id = setInterval(() => this.loadData(), 5000);
30+
return () => {
31+
clearInterval(id);
32+
};
33+
});
34+
}
3335
}
3436

3537
async loadData(): Promise<void> {
@@ -80,6 +82,6 @@ class ExecutionsStore {
8082
}
8183
}
8284

83-
export function createExecutionsStore(): ExecutionsStore {
84-
return new ExecutionsStore();
85+
export function createExecutionsStore(options?: { autoRefresh?: boolean }): ExecutionsStore {
86+
return new ExecutionsStore(options);
8587
}

frontend/src/lib/admin/stores/sagasStore.svelte.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,16 @@ class SagasStore {
2020

2121
pagination = createPaginationState({ initialPageSize: 10 });
2222

23-
constructor() {
24-
$effect(() => {
25-
if (!this.refreshEnabled) return;
26-
const id = setInterval(() => this.loadSagas(), this.refreshRate * 1000);
27-
return () => {
28-
clearInterval(id);
29-
};
30-
});
23+
constructor({ autoRefresh = true }: { autoRefresh?: boolean } = {}) {
24+
if (autoRefresh) {
25+
$effect(() => {
26+
if (!this.refreshEnabled) return;
27+
const id = setInterval(() => this.loadSagas(), this.refreshRate * 1000);
28+
return () => {
29+
clearInterval(id);
30+
};
31+
});
32+
}
3133
}
3234

3335
async loadSagas(): Promise<void> {
@@ -77,6 +79,6 @@ class SagasStore {
7779
}
7880
}
7981

80-
export function createSagasStore(): SagasStore {
81-
return new SagasStore();
82+
export function createSagasStore(options?: { autoRefresh?: boolean }): SagasStore {
83+
return new SagasStore(options);
8284
}

0 commit comments

Comments
 (0)