| 12 | |
| 13 | @Injectable({ providedIn: 'root' }) |
| 14 | export class IsrServerService implements IsrServiceInterface { |
| 15 | private readonly router = inject(Router); |
| 16 | private state: IsrState = initialState; |
| 17 | |
| 18 | getState(): IsrState { |
| 19 | return this.state; |
| 20 | } |
| 21 | |
| 22 | patchState(partialState: Partial<IsrState>): void { |
| 23 | this.state = { ...this.state, ...partialState }; |
| 24 | } |
| 25 | |
| 26 | getExtra(): Record<string, unknown> { |
| 27 | return this.state.extra; |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Activate the service and listen to router events |
| 32 | * @returns void |
| 33 | */ |
| 34 | activate(): void { |
| 35 | this.router.events |
| 36 | .pipe( |
| 37 | filter((e) => 'snapshot' in e), |
| 38 | map((event) => { |
| 39 | let snapshot = (event as ChildActivationEnd).snapshot; |
| 40 | // get the last child route |
| 41 | while (snapshot.firstChild !== null) { |
| 42 | snapshot = snapshot.firstChild; |
| 43 | } |
| 44 | // get the data from the last child route |
| 45 | return snapshot.data; |
| 46 | }), |
| 47 | take(1), |
| 48 | ) |
| 49 | .subscribe((data) => { |
| 50 | // if revalidate is defined, set it |
| 51 | if (typeof data?.['revalidate'] === 'number') { |
| 52 | this.patchState({ revalidate: data['revalidate'] }); |
| 53 | } |
| 54 | }); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Add error to the state |
| 59 | * @param error HttpErrorResponse |
| 60 | * @returns void |
| 61 | * @example |
| 62 | * ```typescript |
| 63 | * this.isrService.addError(err); |
| 64 | * ``` |
| 65 | */ |
| 66 | addError(error: HttpErrorResponse | Error): void { |
| 67 | this.patchState({ errors: [...this.getState().errors, error] }); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Add extra data to the state |
nothing calls this directly
no outgoing calls
no test coverage detected