Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
comply with strict and strictNullChecks (#11161)
Browse files Browse the repository at this point in the history
  • Loading branch information
alunturner authored Jun 29, 2023
1 parent 879832a commit ce479c5
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/Searching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand All @@ -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) {
Expand All @@ -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";
}

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit ce479c5

Please sign in to comment.