-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCacheService.ts
38 lines (29 loc) · 971 Bytes
/
CacheService.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Copyright (c) 2021-2022. Heusala Group Oy <info@heusalagroup.fi>. All rights reserved.
import { reduce } from "./functions/reduce";
export interface CacheClearCallback {
() : Promise<void> | void;
}
/**
* This service is used to clear any internal caches.
*
* It is implemented mainly for the ReactJS SSR server so that requests can clear internal state between requests.
*/
export class CacheService {
private static _clearCallbacks : CacheClearCallback[] = [];
public static registerClearCallback (callback: CacheClearCallback) {
CacheService._clearCallbacks.push(callback);
}
/**
* Clear caches
*/
public static async clearCaches () {
await reduce(
CacheService._clearCallbacks,
async (p: Promise<void>, callback: CacheClearCallback) : Promise<void> => {
await p;
await callback();
},
Promise.resolve()
);
}
}