From 23de8d836c50354284ad4ca211f2589de75c67d2 Mon Sep 17 00:00:00 2001 From: Evan Buss Date: Mon, 1 Jul 2024 12:05:51 -0400 Subject: [PATCH] fix: optimistic update history entry (#177) If the IRC server processes the request fast enough we were setting the search results faster than we could create a new history entry for them. This resulted in writing to the previous history entry and the latest entry stuck in loading state. --- server/app/src/state/historySlice.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/server/app/src/state/historySlice.ts b/server/app/src/state/historySlice.ts index 587034c..1a8ae57 100644 --- a/server/app/src/state/historySlice.ts +++ b/server/app/src/state/historySlice.ts @@ -59,13 +59,20 @@ export const historySlice = createSlice({ } }, extraReducers: (builder) => { - builder.addMatcher(api.endpoints.search.matchFulfilled, (state, action) => { + // Optimistic updates for search requests as we might get back + // the search results before the HTTP request "completes". + builder.addMatcher(api.endpoints.search.matchPending, (state, action) => { const timestamp = new Date().getTime(); const query = action.meta.arg.originalArgs; state.active = timestamp; state.items = [{ query, timestamp }, ...state.items].slice(0, 16); }); + // If the request actually fails, remove the optimistic update + builder.addMatcher(api.endpoints.search.matchRejected, (state) => { + state.active = undefined; + state.items.shift(); + }); } });