Skip to content

Commit 1949a39

Browse files
Antreesybackportbot[bot]
authored andcommitted
fix(logging.ts): do not poll log entries without lastReqId
Signed-off-by: Maksim Sukharev <[email protected]>
1 parent dd00529 commit 1949a39

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
@@ -269,17 +269,34 @@ describe('store:logging', () => {
269269
expect(store.entries).toEqual([{ message: 'hello' }])
270270
})
271271

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

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

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

@@ -334,12 +351,12 @@ describe('store:logging', () => {
334351
}))
335352

336353
const store = useLogStore()
337-
store.allEntries = []
354+
store.allEntries = [{ reqId: '123' }]
338355
store.startPolling()
339356
expect(mocks.pollLog).not.toBeCalled()
340357
vi.advanceTimersByTime(POLLING_INTERVAL)
341358
expect(mocks.pollLog).toBeCalledTimes(1)
342-
expect(mocks.pollLog).toBeCalledWith({ lastReqId: '' })
359+
expect(mocks.pollLog).toBeCalledWith({ lastReqId: '123' })
343360
})
344361

345362
it('can poll for new entries with old available', async () => {
@@ -360,26 +377,27 @@ describe('store:logging', () => {
360377

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

366383
const store = useLogStore()
367-
store.allEntries = []
384+
store.allEntries = [{ reqId: '123' }]
368385
store.startPolling()
369386
expect(mocks.pollLog).not.toBeCalled()
370387
vi.advanceTimersByTime(POLLING_INTERVAL)
371388
store.stopPolling()
372389
vi.advanceTimersByTime(POLLING_INTERVAL)
373390
expect(mocks.pollLog).toBeCalledTimes(1)
391+
expect(mocks.pollLog).toBeCalledWith({ lastReqId: '123' })
374392
})
375393

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

381399
const store = useLogStore()
382-
store.allEntries = []
400+
store.allEntries = [{ reqId: '123' }]
383401
store.startPolling()
384402
expect(mocks.pollLog).not.toBeCalled()
385403
await vi.advanceTimersByTimeAsync(POLLING_INTERVAL / 2)
@@ -425,6 +443,7 @@ describe('store:logging', () => {
425443
})
426444

427445
const store = useLogStore()
446+
store.allEntries = [{ reqId: '123' }]
428447
store.startPolling()
429448
await vi.advanceTimersByTimeAsync(POLLING_INTERVAL)
430449
expect(mocks.pollLog).toBeCalled()
@@ -443,6 +462,7 @@ describe('store:logging', () => {
443462
})
444463

445464
const store = useLogStore()
465+
store.allEntries = [{ reqId: '123' }]
446466
store.startPolling()
447467
await vi.advanceTimersByTimeAsync(POLLING_INTERVAL)
448468
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)