|
1 | | -import { inject, Injectable, REQUEST_CONTEXT } from '@angular/core'; |
2 | 1 | import { Observable, shareReplay } from 'rxjs'; |
3 | 2 |
|
4 | 3 | import { ConfigFileDefinition } from './definition'; |
5 | 4 | import { ConfigFileLoader } from './loader'; |
6 | 5 |
|
7 | | -@Injectable({ |
8 | | - providedIn: 'root', |
9 | | - useFactory: () => |
10 | | - ConfigFilesPersistentCache.useFromRequestContext() ?? |
11 | | - new ConfigFilesPersistentCache(), |
12 | | -}) |
13 | | -export class ConfigFilesPersistentCache extends Map< |
14 | | - string, |
15 | | - Observable<unknown> |
16 | | -> { |
17 | | - static readonly key = 'ConfigFilesPersistentCache'; |
18 | | - |
19 | | - static useFromRequestContext(): ConfigFilesPersistentCache | null { |
20 | | - const context = inject(REQUEST_CONTEXT); |
21 | | - if (!context) return null; |
22 | | - if (typeof context !== 'object') return null; |
23 | | - const cache = Reflect.get(context, this.key); |
24 | | - if (!cache) return null; |
25 | | - return cache; |
26 | | - } |
27 | | - |
28 | | - constructor() { |
29 | | - super(); |
30 | | - } |
31 | | -} |
32 | | - |
33 | | -@Injectable({ providedIn: 'root' }) |
34 | | -export class ConfigFilesSessionCache extends Map< |
35 | | - ConfigFileDefinition<unknown, unknown>, |
36 | | - Observable<unknown> |
37 | | -> { |
38 | | - constructor() { |
39 | | - super(); |
40 | | - } |
41 | | -} |
42 | | - |
43 | 6 | /** |
44 | 7 | * Decorator for {@link ConfigFileLoader} |
45 | 8 | * that caches the loaded configuration objects of each definition. |
46 | | - * |
47 | | - * In SSR applications, a persistent cache can be provided from the server |
48 | | - * so that config files are only loaded once throughout the application |
49 | | - * lifecycle: |
50 | | - * ```ts |
51 | | - * const app = express(); |
52 | | - * const angularApp = new AngularNodeAppEngine(); |
53 | | - * const configFilesCache = new ConfigFilesPersistentCache(); |
54 | | - * ``` |
55 | | - * ```ts |
56 | | - * angularApp |
57 | | - * .handle(req, { [ConfigFilesPersistentCache.key]: configFilesCache }) |
58 | | - * .then((response) => |
59 | | - * response ? writeResponseToNodeResponse(response, res) : next(), |
60 | | - * ) |
61 | | - * .catch(next); |
62 | | - * ``` |
63 | 9 | */ |
64 | 10 | export class CacheConfigFiles implements ConfigFileLoader { |
65 | | - #sessionCache = inject(ConfigFilesSessionCache); |
66 | | - #persistentCache = inject(ConfigFilesPersistentCache); |
| 11 | + #cache = new Map< |
| 12 | + ConfigFileDefinition<unknown, unknown>, |
| 13 | + Observable<unknown> |
| 14 | + >(); |
67 | 15 |
|
68 | 16 | constructor(private kernel: ConfigFileLoader) {} |
69 | 17 |
|
70 | 18 | load<T, Schema>(def: ConfigFileDefinition<T, Schema>): Observable<T> { |
71 | | - const cached = |
72 | | - (def.id && this.#persistentCache.get(def.id)) ?? |
73 | | - this.#sessionCache.get(def); |
| 19 | + const cached = this.#cache.get(def); |
74 | 20 | if (cached) return cached as Observable<T>; |
75 | 21 | const result$ = this.kernel.load(def).pipe(shareReplay(1)); |
76 | | - if (def.id) this.#persistentCache.set(def.id, result$); |
77 | | - this.#sessionCache.set(def, result$); |
| 22 | + this.#cache.set(def, result$); |
78 | 23 | return result$; |
79 | 24 | } |
80 | 25 | } |
0 commit comments