Total Complexity | 5 |
Complexity/F | 1.25 |
Lines of Code | 43 |
Function Count | 4 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | import { ControlValueAccessor } from '@angular/forms'; |
||
2 | |||
3 | |||
4 | export class ValueAccessorBase<T> implements ControlValueAccessor { |
||
5 | // @ts-ignore |
||
6 | private innerValue: T; |
||
7 | |||
8 | private changed = new Array<(value: T) => void>(); |
||
9 | private touched = new Array<() => void>(); |
||
10 | |||
11 | |||
12 | get value(): T { |
||
13 | return this.innerValue; |
||
14 | } |
||
15 | |||
16 | |||
17 | set value(value: T) { |
||
18 | if (this.innerValue !== value) { |
||
19 | this.innerValue = value; |
||
20 | this.changed.forEach(f => f(value)); |
||
21 | } |
||
22 | } |
||
23 | |||
24 | |||
25 | touch() { |
||
26 | this.touched.forEach(f => f()); |
||
27 | } |
||
28 | |||
29 | writeValue(value: T) { |
||
30 | this.innerValue = value; |
||
31 | } |
||
32 | |||
33 | |||
34 | registerOnChange(fn: (value: T) => void) { |
||
35 | this.changed.push(fn); |
||
36 | } |
||
37 | |||
38 | |||
39 | registerOnTouched(fn: () => void) { |
||
40 | this.touched.push(fn); |
||
41 | } |
||
42 | } |
||
43 |