Skip to content

Commit f556eb3

Browse files
committed
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.
1 parent 97e723c commit f556eb3

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

server/app/src/state/historySlice.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,20 @@ export const historySlice = createSlice({
5959
}
6060
},
6161
extraReducers: (builder) => {
62-
builder.addMatcher(api.endpoints.search.matchFulfilled, (state, action) => {
62+
// Optimistic updates for search requests as we might get back
63+
// the search results before the HTTP request "completes".
64+
builder.addMatcher(api.endpoints.search.matchPending, (state, action) => {
6365
const timestamp = new Date().getTime();
6466
const query = action.meta.arg.originalArgs;
6567

6668
state.active = timestamp;
6769
state.items = [{ query, timestamp }, ...state.items].slice(0, 16);
6870
});
71+
// If the request actually fails, remove the optimistic update
72+
builder.addMatcher(api.endpoints.search.matchRejected, (state) => {
73+
state.active = undefined;
74+
state.items.shift();
75+
});
6976
}
7077
});
7178

0 commit comments

Comments
 (0)