From ce479c57747ac7a953c166bb4219350c9dc27cf9 Mon Sep 17 00:00:00 2001 From: alunturner <56027671+alunturner@users.noreply.github.com> Date: Thu, 29 Jun 2023 11:20:24 +0100 Subject: [PATCH] comply with `strict` and `strictNullChecks` (#11161) --- src/Searching.ts | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/Searching.ts b/src/Searching.ts index feb56cb3fe..894be0cf9c 100644 --- a/src/Searching.ts +++ b/src/Searching.ts @@ -29,6 +29,7 @@ import { MatrixClient } from "matrix-js-sdk/src/matrix"; import { ISearchArgs } from "./indexing/BaseEventIndexManager"; import EventIndexPeg from "./indexing/EventIndexPeg"; +import { isNotUndefined } from "./Typeguards"; const SEARCH_LIMIT = 10; @@ -468,8 +469,8 @@ function combineEvents( */ function combineResponses( previousSearchResult: ISeshatSearchResults, - localEvents: IResultRoomEvents, - serverEvents: IResultRoomEvents, + localEvents?: IResultRoomEvents, + serverEvents?: IResultRoomEvents, ): IResultRoomEvents { // Combine our events first. const response = combineEvents(previousSearchResult, localEvents, serverEvents); @@ -480,11 +481,14 @@ function combineResponses( if (previousSearchResult.count) { response.count = previousSearchResult.count; } else { - response.count = localEvents.count + serverEvents.count; + const localEventCount = localEvents?.count ?? 0; + const serverEventCount = serverEvents?.count ?? 0; + + response.count = localEventCount + serverEventCount; } // Update our next batch tokens for the given search sources. - if (localEvents) { + if (localEvents && isNotUndefined(previousSearchResult.seshatQuery)) { previousSearchResult.seshatQuery.next_batch = localEvents.next_batch; } if (serverEvents) { @@ -505,7 +509,11 @@ function combineResponses( // pagination request. // // Provide a fake next batch token for that case. - if (!response.next_batch && previousSearchResult.cachedEvents.length > 0) { + if ( + !response.next_batch && + isNotUndefined(previousSearchResult.cachedEvents) && + previousSearchResult.cachedEvents.length > 0 + ) { response.next_batch = "cached"; } @@ -565,16 +573,12 @@ async function combinedPagination( // Fetch events from the server if we have a token for it and if it's the // local indexes turn or the local index has exhausted its results. - if (searchResult.serverSideNextBatch && (oldestEventFrom === "local" || !searchArgs.next_batch)) { + if (searchResult.serverSideNextBatch && (oldestEventFrom === "local" || !searchArgs?.next_batch)) { const body = { body: searchResult._query!, next_batch: searchResult.serverSideNextBatch }; serverSideResult = await client.search(body); } - let serverEvents: IResultRoomEvents | undefined; - - if (serverSideResult) { - serverEvents = serverSideResult.search_categories.room_events; - } + const serverEvents: IResultRoomEvents | undefined = serverSideResult?.search_categories.room_events; // Combine our events. const combinedResult = combineResponses(searchResult, localResult, serverEvents);