Skip to content

Commit

Permalink
add cache param to dataApi.findOne (#4653)
Browse files Browse the repository at this point in the history
  • Loading branch information
petrjasek authored Oct 4, 2024
1 parent ab558e1 commit e93bac1
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
13 changes: 7 additions & 6 deletions scripts/core/helpers/CrudManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,21 +121,22 @@ export function generatePatchIArticle(a: IArticle, b: IArticle) {
return patch;
}

const cache = {};
const _cache = {};

function findOne<T>(endpoint: string, id: string): Promise<T> {
function findOne<T>(endpoint: string, id: string, cache: boolean = true): Promise<T> {
const key = `${endpoint}:${id}`;

if (cache[key] == null) {
cache[key] = httpRequestJsonLocal({
if (_cache[key] == null) {
_cache[key] = httpRequestJsonLocal({
method: 'GET',
path: '/' + endpoint + '/' + id,
cache: cache === false ? 'reload' : 'default',
}).finally(() => {
delete cache[key];
delete _cache[key];
});
}

return cache[key];
return _cache[key];
}

export function fetchChangedResources<T extends IBaseRestApiResponse>(
Expand Down
1 change: 1 addition & 0 deletions scripts/core/helpers/network.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ interface IHttpRequestOptions {
payload?: {};
headers?: {[key: string]: any};
urlParams?: {[key: string]: any};
cache?: 'default' | 'no-store' | 'reload' | 'no-cache' | 'force-cache' | 'only-if-cached';

abortSignal?: AbortSignal;
}
Expand Down
2 changes: 1 addition & 1 deletion scripts/core/superdesk-api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2553,7 +2553,7 @@ declare module 'superdesk-api' {
}

export interface IDataApi {
findOne<T>(endpoint: string, id: string): Promise<T>;
findOne<T>(endpoint: string, id: string, cache?: boolean): Promise<T>;
create<T>(endpoint: string, item: Partial<T>, urlParams?: Dictionary<string, any>): Promise<T>;
query<T extends IBaseRestApiResponse>(
endpoint: string,
Expand Down

0 comments on commit e93bac1

Please sign in to comment.