Passed
Push — depfu/update/npm/lodash-4.17.1... ( 152c97 )
by
unknown
04:58
created

src/app/fragments/abstract/ValueAccessorBase.ts   A

Complexity

Total Complexity 5
Complexity/F 1.25

Size

Lines of Code 43
Function Count 4

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 43
rs 10
c 0
b 0
f 0
wmc 5
mnd 1
bc 1
fnc 4
bpm 0.25
cpm 1.25
noi 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
A ValueAccessorBase.touch 0 4 2
A ValueAccessorBase.registerOnTouched 0 4 1
A ValueAccessorBase.registerOnChange 0 4 1
A ValueAccessorBase.writeValue 0 3 1
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