11import { createSlice , createAsyncThunk } from '@reduxjs/toolkit' ;
22import { fetchData } from '../../api' ;
3+ import { isStaleRequest } from '../isStaleRequest' ;
34
45interface DiffState {
56 diff : string ;
@@ -8,6 +9,8 @@ interface DiffState {
89 diffPrevLoading : boolean ;
910 diffError : string | null ;
1011 diffPrevError : string | null ;
12+ latestDiffRequestId : string | null ;
13+ latestDiffPrevRequestId : string | null ;
1114}
1215
1316const initialState : DiffState = {
@@ -17,25 +20,27 @@ const initialState: DiffState = {
1720 diffPrevLoading : false ,
1821 diffError : null ,
1922 diffPrevError : null ,
23+ latestDiffRequestId : null ,
24+ latestDiffPrevRequestId : null ,
2025} ;
2126
2227const encodePath = ( path : string ) : string =>
2328 path . split ( '/' ) . map ( encodeURIComponent ) . join ( '/' ) ;
2429
2530export const fetchDiff = createAsyncThunk (
2631 'diff/fetchDiff' ,
27- async ( path : string | null ) => {
32+ async ( path : string | null , { signal } ) => {
2833 if ( ! path ) return '' ;
29- const data = await fetchData < string > ( `/api/diff/${ encodePath ( path ) } ` , 'text' ) ;
34+ const data = await fetchData < string > ( `/api/diff/${ encodePath ( path ) } ` , 'text' , signal ) ;
3035 return data || '' ;
3136 }
3237) ;
3338
3439export const fetchDiffPrev = createAsyncThunk (
3540 'diff/fetchDiffPrev' ,
36- async ( path : string | null ) => {
41+ async ( path : string | null , { signal } ) => {
3742 if ( ! path ) return '' ;
38- const data = await fetchData < string > ( `/api/diff-prev/${ encodePath ( path ) } ` , 'text' ) ;
43+ const data = await fetchData < string > ( `/api/diff-prev/${ encodePath ( path ) } ` , 'text' , signal ) ;
3944 return data || '' ;
4045 }
4146) ;
@@ -46,28 +51,42 @@ const diffSlice = createSlice({
4651 reducers : { } ,
4752 extraReducers : ( builder ) => {
4853 builder
49- . addCase ( fetchDiff . pending , ( state ) => {
54+ . addCase ( fetchDiff . pending , ( state , action ) => {
55+ state . latestDiffRequestId = action . meta . requestId ;
5056 state . diffLoading = true ;
5157 state . diffError = null ;
5258 } )
5359 . addCase ( fetchDiff . fulfilled , ( state , action ) => {
60+ if ( isStaleRequest ( { latestRequestId : state . latestDiffRequestId } , action ) ) {
61+ return ;
62+ }
5463 state . diffLoading = false ;
5564 state . diff = action . payload ;
5665 } )
5766 . addCase ( fetchDiff . rejected , ( state , action ) => {
67+ if ( isStaleRequest ( { latestRequestId : state . latestDiffRequestId } , action ) ) {
68+ return ;
69+ }
5870 state . diffLoading = false ;
5971 state . diff = '' ;
6072 state . diffError = action . error . message || 'Failed to fetch diff' ;
6173 } )
62- . addCase ( fetchDiffPrev . pending , ( state ) => {
74+ . addCase ( fetchDiffPrev . pending , ( state , action ) => {
75+ state . latestDiffPrevRequestId = action . meta . requestId ;
6376 state . diffPrevLoading = true ;
6477 state . diffPrevError = null ;
6578 } )
6679 . addCase ( fetchDiffPrev . fulfilled , ( state , action ) => {
80+ if ( isStaleRequest ( { latestRequestId : state . latestDiffPrevRequestId } , action ) ) {
81+ return ;
82+ }
6783 state . diffPrevLoading = false ;
6884 state . diffPrev = action . payload ;
6985 } )
7086 . addCase ( fetchDiffPrev . rejected , ( state , action ) => {
87+ if ( isStaleRequest ( { latestRequestId : state . latestDiffPrevRequestId } , action ) ) {
88+ return ;
89+ }
7190 state . diffPrevLoading = false ;
7291 state . diffPrev = '' ;
7392 state . diffPrevError = action . error . message || 'Failed to fetch previous diff' ;
0 commit comments