Total Complexity | 0 |
Complexity/F | 0 |
Lines of Code | 31 |
Function Count | 0 |
Duplicated Lines | 0 |
Ratio | 0 % |
Coverage | 100% |
Changes | 0 |
1 | export interface IOnChangeCallback<T> { |
||
2 | (newValue: T): void; |
||
3 | } |
||
4 | |||
5 | export interface IObserverAdapter<T> { |
||
6 | onChange: IOnChangeCallback<T> |
||
7 | } |
||
8 | |||
9 | export interface IObserver<T> { |
||
10 | value: T |
||
11 | } |
||
12 | |||
13 | export default class Observer<T> implements IObserver<T> { |
||
14 | protected adapter: IObserverAdapter<T>; |
||
15 | protected current: T; |
||
16 | |||
17 | constructor(initialValue: T, adapter: IObserverAdapter<T>) { |
||
18 | 1 | this.current = initialValue; |
|
19 | 1 | this.adapter = adapter; |
|
20 | } |
||
21 | |||
22 | public get value(): T { |
||
23 | 2 | return this.current; |
|
24 | } |
||
25 | |||
26 | public set value(newValue: T) { |
||
27 | 1 | this.current = newValue; |
|
28 | 1 | this.adapter.onChange(newValue); |
|
29 | } |
||
30 | } |
||
31 |