Skip to content

Commit

Permalink
fix: optimistic update history entry (#177)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
evan-buss authored Jul 1, 2024
1 parent 0074f2f commit 23de8d8
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion server/app/src/state/historySlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
}
});

Expand Down

0 comments on commit 23de8d8

Please sign in to comment.