Skip to content

Commit c58cb1a

Browse files
committed
fix(logging.ts): do not poll log entries without lastReqId
Signed-off-by: Maksim Sukharev <[email protected]>
1 parent 6dfb2f7 commit c58cb1a

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
lines changed

src/store/logging.spec.ts

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -270,17 +270,34 @@ describe('store:logging', () => {
270270
expect(store.entries).toEqual([{ message: 'hello' }])
271271
})
272272

273-
it('loads more newer entries from server', async () => {
273+
it('loads more newer entries from server (with pollLog)', async () => {
274274
vi.mocked(mocks.pollLog).mockImplementationOnce(() => ({
275-
data: [{ message: 'hello' }],
275+
data: [{ reqId: '456', message: 'hello' }],
276+
}))
277+
278+
const store = useLogStore()
279+
store.allEntries = [{ reqId: '123', message: 'hello' }]
280+
281+
await store.loadMore(false)
282+
expect(mocks.pollLog).toBeCalledWith({ lastReqId: '123' })
283+
expect(store.entries).toEqual([{ reqId: '456', message: 'hello' }, { reqId: '123', message: 'hello' }])
284+
})
285+
286+
it('loads more newer entries from server (with getLog)', async () => {
287+
vi.mocked(mocks.getLog).mockImplementationOnce(() => ({
288+
data: {
289+
data: [{ message: 'hello' }],
290+
remain: false,
291+
},
276292
}))
277293

278294
const store = useLogStore()
279295
store.allEntries = []
280296
expect(store.entries).toEqual([])
281297

282298
await store.loadMore(false)
283-
expect(mocks.pollLog).toBeCalledWith({ lastReqId: '' })
299+
expect(mocks.pollLog).not.toBeCalled()
300+
expect(mocks.getLog).toBeCalledWith({ offset: 0, query: '' })
284301
expect(store.entries).toEqual([{ message: 'hello' }])
285302
})
286303

@@ -335,12 +352,12 @@ describe('store:logging', () => {
335352
}))
336353

337354
const store = useLogStore()
338-
store.allEntries = []
355+
store.allEntries = [{ reqId: '123' }]
339356
store.startPolling()
340357
expect(mocks.pollLog).not.toBeCalled()
341358
vi.advanceTimersByTime(POLLING_INTERVAL)
342359
expect(mocks.pollLog).toBeCalledTimes(1)
343-
expect(mocks.pollLog).toBeCalledWith({ lastReqId: '' })
360+
expect(mocks.pollLog).toBeCalledWith({ lastReqId: '123' })
344361
})
345362

346363
it('can poll for new entries with old available', async () => {
@@ -361,26 +378,27 @@ describe('store:logging', () => {
361378

362379
it('can stop polling for new entries', async () => {
363380
vi.mocked(mocks.pollLog).mockImplementationOnce(() => ({
364-
data: [{ reqId: '123' }],
381+
data: [{ reqId: '456' }],
365382
}))
366383

367384
const store = useLogStore()
368-
store.allEntries = []
385+
store.allEntries = [{ reqId: '123' }]
369386
store.startPolling()
370387
expect(mocks.pollLog).not.toBeCalled()
371388
vi.advanceTimersByTime(POLLING_INTERVAL)
372389
store.stopPolling()
373390
vi.advanceTimersByTime(POLLING_INTERVAL)
374391
expect(mocks.pollLog).toBeCalledTimes(1)
392+
expect(mocks.pollLog).toBeCalledWith({ lastReqId: '123' })
375393
})
376394

377395
it('only starts one polling timer', async () => {
378396
vi.mocked(mocks.pollLog).mockImplementationOnce(() => ({
379-
data: [{ reqId: '123' }],
397+
data: [{ reqId: '456' }],
380398
}))
381399

382400
const store = useLogStore()
383-
store.allEntries = []
401+
store.allEntries = [{ reqId: '123' }]
384402
store.startPolling()
385403
expect(mocks.pollLog).not.toBeCalled()
386404
await vi.advanceTimersByTimeAsync(POLLING_INTERVAL / 2)
@@ -426,6 +444,7 @@ describe('store:logging', () => {
426444
})
427445

428446
const store = useLogStore()
447+
store.allEntries = [{ reqId: '123' }]
429448
store.startPolling()
430449
await vi.advanceTimersByTimeAsync(POLLING_INTERVAL)
431450
expect(mocks.pollLog).toBeCalled()
@@ -444,6 +463,7 @@ describe('store:logging', () => {
444463
})
445464

446465
const store = useLogStore()
466+
store.allEntries = [{ reqId: '123' }]
447467
store.startPolling()
448468
await vi.advanceTimersByTimeAsync(POLLING_INTERVAL)
449469
expect(mocks.pollLog).toBeCalled()

src/store/logging.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,12 @@ export const useLogStore = defineStore('logreader-logs', () => {
7575
}
7676

7777
try {
78-
if (older) {
78+
if (older || !allEntries.value.length) {
7979
const { data } = await getLog({ offset: allEntries.value.length, query: query.value })
8080
allEntries.value.push(...data.data.map(parseRawLogEntry))
8181
hasRemainingEntries.value = data.remain
8282
} else {
83-
const { data } = await pollLog({ lastReqId: allEntries.value[0]?.reqId || '' })
83+
const { data } = await pollLog({ lastReqId: allEntries.value[0]!.reqId })
8484
allEntries.value.splice(0, 0, ...data.map(parseRawLogEntry))
8585
}
8686
} catch (e) {
@@ -147,9 +147,9 @@ export const useLogStore = defineStore('logreader-logs', () => {
147147

148148
const doPolling = async () => {
149149
try {
150-
// Only poll if not using a local file
151-
if (_settings.isEnabled && query.value === '') {
152-
const { data } = await pollLog({ lastReqId: allEntries.value[0]?.reqId || '' })
150+
// Only poll if not using a local file and store already has some known entries
151+
if (_settings.isEnabled && query.value === '' && allEntries.value.length) {
152+
const { data } = await pollLog({ lastReqId: allEntries.value[0]!.reqId })
153153
allEntries.value.splice(0, 0, ...data.map(parseRawLogEntry))
154154
}
155155
} catch (e) {

0 commit comments

Comments
 (0)