Skip to content

Commit 86a0979

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

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

@@ -336,12 +353,12 @@ describe('store:logging', () => {
336353
}))
337354

338355
const store = useLogStore()
339-
store.allEntries = []
356+
store.allEntries = [{ reqId: '123' }]
340357
store.startPolling()
341358
expect(mocks.pollLog).not.toBeCalled()
342359
vi.advanceTimersByTime(POLLING_INTERVAL)
343360
expect(mocks.pollLog).toBeCalledTimes(1)
344-
expect(mocks.pollLog).toBeCalledWith({ lastReqId: '' })
361+
expect(mocks.pollLog).toBeCalledWith({ lastReqId: '123' })
345362
})
346363

347364
it('can poll for new entries with old available', async () => {
@@ -362,26 +379,27 @@ describe('store:logging', () => {
362379

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

368385
const store = useLogStore()
369-
store.allEntries = []
386+
store.allEntries = [{ reqId: '123' }]
370387
store.startPolling()
371388
expect(mocks.pollLog).not.toBeCalled()
372389
vi.advanceTimersByTime(POLLING_INTERVAL)
373390
store.stopPolling()
374391
vi.advanceTimersByTime(POLLING_INTERVAL)
375392
expect(mocks.pollLog).toBeCalledTimes(1)
393+
expect(mocks.pollLog).toBeCalledWith({ lastReqId: '123' })
376394
})
377395

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

383401
const store = useLogStore()
384-
store.allEntries = []
402+
store.allEntries = [{ reqId: '123' }]
385403
store.startPolling()
386404
expect(mocks.pollLog).not.toBeCalled()
387405
await vi.advanceTimersByTimeAsync(POLLING_INTERVAL / 2)
@@ -427,6 +445,7 @@ describe('store:logging', () => {
427445
})
428446

429447
const store = useLogStore()
448+
store.allEntries = [{ reqId: '123' }]
430449
store.startPolling()
431450
await vi.advanceTimersByTimeAsync(POLLING_INTERVAL)
432451
expect(mocks.pollLog).toBeCalled()
@@ -445,6 +464,7 @@ describe('store:logging', () => {
445464
})
446465

447466
const store = useLogStore()
467+
store.allEntries = [{ reqId: '123' }]
448468
store.startPolling()
449469
await vi.advanceTimersByTimeAsync(POLLING_INTERVAL)
450470
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)