Skip to content

Commit

Permalink
Fix #25076 Invalidate Cache for Reindex (#25165)
Browse files Browse the repository at this point in the history
* dev: remove retry pages request base on modDate

* fix: tests

* remove
  • Loading branch information
rjvelazco committed Jun 6, 2023
1 parent 885084d commit c2efd86
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 158 deletions.
Expand Up @@ -503,80 +503,8 @@ describe('DotPageStore', () => {
expect(dotHttpErrorManagerService.handle).toHaveBeenCalledWith(error500, true);
});

it('should keep fetching Pages data until new value comes from the DB in store', fakeAsync(() => {
dotPageStore.setPages({ items: favoritePagesInitialTestData });
const old = {
contentTook: 0,
jsonObjectView: {
contentlets: favoritePagesInitialTestData as unknown as DotCMSContentlet[]
},
queryTook: 1,
resultsSize: 2
};

const updated = {
contentTook: 0,
jsonObjectView: {
contentlets: [
{ ...favoritePagesInitialTestData[0], modDate: '2020-09-02 16:50:15.569' },
{ ...favoritePagesInitialTestData[1] }
] as unknown as DotCMSContentlet[]
},
queryTook: 1,
resultsSize: 4
};

const mockFunction = (times) => {
let count = 1;

return Observable.create((observer) => {
if (count++ > times) {
observer.next(updated);
} else {
observer.next(old);
}
});
};

spyOn(dotESContentService, 'get').and.returnValue(mockFunction(3));
spyOn(dotPageStore, 'setPagesStatus').and.callThrough();

dotPageStore.updateSinglePageData({ identifier: '123', isFavoritePage: false });

tick(3000);

// dotESContentService.get only is called 1 time, but "retryWhen" operator makes several request to the SpyOn
expect(dotESContentService.get).toHaveBeenCalledTimes(1);

// Testing to setPagesStatus to LOADING on the first fetch
expect((dotPageStore.setPagesStatus as jasmine.Spy).calls.argsFor(0).toString()).toBe(
ComponentStatus.LOADING
);

// Testing to pages.status to be LOADED on the last fetch (there can only be 2 calls during the whole process)
dotPageStore.state$.subscribe((data) => {
expect(data.pages.status).toBe(ComponentStatus.LOADED);
});

// Since dotESContentService.get can only be called 1 time (and once called the data will be changed on "mockFunction"),
// we test that the last fetch contains the updated data
(dotESContentService.get as jasmine.Spy).calls
.mostRecent()
.returnValue.subscribe((data) => {
expect(data).toEqual(updated);
});
}));

it('should remove page archived from pages collection and add undefined at the bottom', fakeAsync(() => {
dotPageStore.setPages({ items: favoritePagesInitialTestData });
const old = {
contentTook: 0,
jsonObjectView: {
contentlets: favoritePagesInitialTestData as unknown as DotCMSContentlet[]
},
queryTook: 1,
resultsSize: 2
};

const updated = {
contentTook: 0,
Expand All @@ -587,19 +515,7 @@ describe('DotPageStore', () => {
resultsSize: 4
};

const mockFunction = (times) => {
let count = 1;

return Observable.create((observer) => {
if (count++ > times) {
observer.next(updated);
} else {
observer.next(old);
}
});
};

spyOn(dotESContentService, 'get').and.returnValue(mockFunction(3));
spyOn(dotESContentService, 'get').and.returnValue(of(updated));

dotPageStore.updateSinglePageData({ identifier: '123', isFavoritePage: false });

Expand Down
Expand Up @@ -7,17 +7,7 @@ import { Injectable } from '@angular/core';
import { MenuItem, SelectItem } from 'primeng/api';
import { DialogService } from 'primeng/dynamicdialog';

import {
catchError,
delay,
filter,
map,
mergeMap,
retryWhen,
switchMap,
take,
tap
} from 'rxjs/operators';
import { catchError, filter, map, mergeMap, switchMap, take, tap } from 'rxjs/operators';

import { DotFavoritePageService } from '@dotcms/app/api/services/dot-favorite-page/dot-favorite-page.service';
import { DotHttpErrorManagerService } from '@dotcms/app/api/services/dot-http-error-manager/dot-http-error-manager.service';
Expand Down Expand Up @@ -380,76 +370,50 @@ export class DotPageStore extends ComponentStore<DotPagesState> {
) => {
return params$.pipe(
mergeMap((params) => {
let localPageData: DotCMSContentlet;
let retries = 0;
const { identifier, isFavoritePage } = params;
const sortOrderValue = this.getSortOrderValue();

if (isFavoritePage) {
localPageData = this.get().favoritePages.items.filter(
(item) => item?.identifier === identifier
)[0];
} else {
localPageData = this.get().pages.items.filter(
(item) => item?.identifier === identifier
)[0];
this.setPagesStatus(ComponentStatus.LOADING);
}
if (!isFavoritePage) this.setPagesStatus(ComponentStatus.LOADING);

return this.getPagesDataFn(isFavoritePage, sortOrderValue, identifier).pipe(
tap(
(items) => {
// Finished fetch loop and will proceed to set data on store
if (isFavoritePage) {
const pagesData = this.get().favoritePages.items.map((page) => {
return page?.identifier === identifier
? items.jsonObjectView.contentlets[0]
: page;
});

this.setFavoritePages({ items: pagesData });
} else {
let pagesData = this.get().pages.items;

if (items.jsonObjectView.contentlets[0] === undefined) {
pagesData = pagesData.filter((page) => {
return page?.identifier !== identifier;
});

return this.getPagesDataFn(isFavoritePage, sortOrderValue, identifier)
.pipe(
tap(
(items) => {
retries++;

// Will continue repeating fetch until data has changed or limit fetch reached
if (
localPageData?.modDate ===
items.jsonObjectView.contentlets[0]?.modDate &&
retries < 10
) {
throw false;
// Add undefined to keep the same length of the array,
// otherwise the pagination(endless scroll) will break
pagesData.push(undefined);
} else {
// Finished fetch loop and will proceed to set data on store
if (isFavoritePage) {
const pagesData = this.get().favoritePages.items.map(
(page) => {
return page?.identifier === identifier
? items.jsonObjectView.contentlets[0]
: page;
}
);

this.setFavoritePages({ items: pagesData });
} else {
let pagesData = this.get().pages.items;

if (items.jsonObjectView.contentlets[0] === undefined) {
pagesData = pagesData.filter((page) => {
return page?.identifier !== identifier;
});

// Add undefined to keep the same length of the array,
// otherwise the pagination(endless scroll) will break
pagesData.push(undefined);
} else {
pagesData = pagesData.map((page) => {
return page?.identifier === identifier
? items.jsonObjectView.contentlets[0]
: page;
});
}

this.setPages({ items: pagesData });
}
pagesData = pagesData.map((page) => {
return page?.identifier === identifier
? items.jsonObjectView.contentlets[0]
: page;
});
}
},
(error: HttpErrorResponse) => {
return this.httpErrorManagerService.handle(error);

this.setPages({ items: pagesData });
}
)
},
(error: HttpErrorResponse) => {
return this.httpErrorManagerService.handle(error);
}
)
.pipe(retryWhen((errors) => errors.pipe(delay(1000))));
);
})
);
}
Expand Down

0 comments on commit c2efd86

Please sign in to comment.