src/ValueObserver.ts
last analyzed

Complexity

Total Complexity 0
Complexity/F 0

Size

Lines of Code 31
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 0
eloc 23
mnd 0
bc 0
fnc 0
dl 0
loc 31
ccs 5
cts 5
cp 1
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
export interface OnChangeCallback<Type> {
2
  (newValue: Type): void;
3
}
4
5
export interface ObserverAdapter<Type> {
6
  onChange: OnChangeCallback<Type>
7
}
8
9
export interface Observer<Type> {
10
  value: Type
11
}
12
13
export default class ValueObserver<Type> implements Observer<Type> {
14
  protected adapter: ObserverAdapter<Type>;
15
  protected current: Type;
16
17
  constructor(initialValue: Type, adapter: ObserverAdapter<Type>) {
18 1
    this.current = initialValue;
19 1
    this.adapter = adapter;
20
  }
21
22
  public get value(): Type {
23 2
    return this.current;
24
  }
25
26
  public set value(newValue: Type) {
27 1
    this.current = newValue;
28 1
    this.adapter.onChange(newValue);
29
  }
30
}
31