|
1 | | -import { |
2 | | - ChangeDetectorRef, |
3 | | - inject, |
4 | | - Injectable, |
5 | | - OnDestroy, |
6 | | -} from '@angular/core'; |
7 | | -import { Subscription } from 'rxjs'; |
8 | | - |
9 | | -import { Locale } from '../../lib/transloco-locale.types'; |
10 | | -import { TranslocoLocaleService } from '../transloco-locale.service'; |
11 | | - |
12 | | -type Deps = [TranslocoLocaleService, ChangeDetectorRef]; |
13 | | -@Injectable() |
14 | | -export abstract class BaseLocalePipe<VALUE = unknown, ARGS extends unknown[] = []> implements OnDestroy { |
15 | | - protected localeService = inject(TranslocoLocaleService); |
16 | | - protected cdr = inject(ChangeDetectorRef); |
17 | | - |
18 | | - private localeChangeSub: Subscription | null = |
19 | | - this.localeService.localeChanges$.subscribe(() => this.invalidate()); |
20 | | - |
21 | | - protected lastValue?: VALUE; |
22 | | - protected lastArgs?: string; |
23 | | - |
24 | | - protected lastResult = ''; |
25 | | - |
26 | | - protected getLocale(locale?: Locale): Locale { |
27 | | - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
28 | | - return locale || this.localeService.getLocale()!; |
29 | | - } |
30 | | - |
31 | | - transform(value: VALUE, ...args: ARGS): string { |
32 | | - if (this.isSameValue(value) && this.isSameArgs(...args)) { |
33 | | - return this.lastResult; |
34 | | - } |
35 | | - this.lastResult = this.doTransform(value, ...args); |
36 | | - this.lastValue = value; |
37 | | - this.lastArgs = JSON.stringify(args); |
38 | | - return this.lastResult; |
39 | | - } |
40 | | - |
41 | | - protected abstract doTransform(value: VALUE, ...args: ARGS): string; |
42 | | - |
43 | | - protected isSameValue(value: VALUE): boolean { |
44 | | - return this.lastValue === value; |
45 | | - } |
46 | | - |
47 | | - protected isSameArgs(...args: ARGS): boolean { |
48 | | - return JSON.stringify(args) === this.lastArgs; |
49 | | - } |
50 | | - |
51 | | - invalidate() { |
52 | | - this.lastValue = undefined; |
53 | | - this.lastArgs = undefined; |
54 | | - this.lastResult = ''; |
55 | | - this.cdr.markForCheck(); |
56 | | - } |
57 | | - |
58 | | - ngOnDestroy(): void { |
59 | | - this.localeChangeSub?.unsubscribe(); |
60 | | - // Caretaker note: it's important to clean up references to subscriptions since they save the `next` |
61 | | - // callback within its `destination` property, preventing classes from being GC'd. |
62 | | - this.localeChangeSub = null; |
63 | | - } |
64 | | -} |
| 1 | +import { |
| 2 | + ChangeDetectorRef, |
| 3 | + inject, |
| 4 | + Injectable, |
| 5 | + OnDestroy, |
| 6 | +} from '@angular/core'; |
| 7 | +import { Subscription } from 'rxjs'; |
| 8 | + |
| 9 | +import { Locale } from '../../lib/transloco-locale.types'; |
| 10 | +import { TranslocoLocaleService } from '../transloco-locale.service'; |
| 11 | + |
| 12 | +type Deps = [TranslocoLocaleService, ChangeDetectorRef]; |
| 13 | +@Injectable() |
| 14 | +export abstract class BaseLocalePipe<VALUE = unknown, ARGS extends unknown[] = []> implements OnDestroy { |
| 15 | + protected localeService = inject(TranslocoLocaleService); |
| 16 | + protected cdr = inject(ChangeDetectorRef); |
| 17 | + |
| 18 | + private localeChangeSub: Subscription | null = |
| 19 | + this.localeService.localeChanges$.subscribe(() => this.invalidate()); |
| 20 | + |
| 21 | + protected lastValue?: VALUE; |
| 22 | + protected lastArgs?: string; |
| 23 | + |
| 24 | + protected lastResult = ''; |
| 25 | + |
| 26 | + protected getLocale(locale?: Locale): Locale { |
| 27 | + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 28 | + return locale || this.localeService.getLocale()!; |
| 29 | + } |
| 30 | + |
| 31 | + transform(value: VALUE, ...args: ARGS): string { |
| 32 | + if (this.isSameValue(value) && this.isSameArgs(...args)) { |
| 33 | + return this.lastResult; |
| 34 | + } |
| 35 | + this.lastResult = this.doTransform(value, ...args); |
| 36 | + this.lastValue = value; |
| 37 | + this.lastArgs = JSON.stringify(args); |
| 38 | + return this.lastResult; |
| 39 | + } |
| 40 | + |
| 41 | + protected abstract doTransform(value: VALUE, ...args: ARGS): string; |
| 42 | + |
| 43 | + protected isSameValue(value: VALUE): boolean { |
| 44 | + return this.lastValue === value; |
| 45 | + } |
| 46 | + |
| 47 | + protected isSameArgs(...args: ARGS): boolean { |
| 48 | + return JSON.stringify(args) === this.lastArgs; |
| 49 | + } |
| 50 | + |
| 51 | + invalidate() { |
| 52 | + this.lastValue = undefined; |
| 53 | + this.lastArgs = undefined; |
| 54 | + this.lastResult = ''; |
| 55 | + this.cdr.markForCheck(); |
| 56 | + } |
| 57 | + |
| 58 | + ngOnDestroy(): void { |
| 59 | + this.localeChangeSub?.unsubscribe(); |
| 60 | + // Caretaker note: it's important to clean up references to subscriptions since they save the `next` |
| 61 | + // callback within its `destination` property, preventing classes from being GC'd. |
| 62 | + this.localeChangeSub = null; |
| 63 | + } |
| 64 | +} |
0 commit comments