-
Notifications
You must be signed in to change notification settings - Fork 18
/
d-base-state-set-impl-observable.ts
73 lines (63 loc) · 1.56 KB
/
d-base-state-set-impl-observable.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
* Copyright (C) 2020 Toshiba Corporation
* SPDX-License-Identifier: Apache-2.0
*/
import { DBaseStateSet } from "./d-base-state-set";
import { DBaseStateSetImpl } from "./d-base-state-set-impl";
export type DBaseStateSetImplObservableOnChange = (
newState: DBaseStateSet,
oldState: DBaseStateSet
) => void;
export class DBaseStateSetImplObservable extends DBaseStateSetImpl {
protected _onChange: DBaseStateSetImplObservableOnChange;
protected _isLocked: number;
protected _isSaved: boolean;
protected _doSave: boolean;
protected _saved?: DBaseStateSetImpl;
constructor(onChange: DBaseStateSetImplObservableOnChange) {
super();
this._onChange = onChange;
this._isLocked = 0;
this._isSaved = false;
this._doSave = true;
}
lock(callOnChange?: boolean): this {
this._isLocked += 1;
if (callOnChange === false) {
this._doSave = false;
}
return this;
}
unlock(): this {
this._isLocked -= 1;
return this.end();
}
protected get saved(): DBaseStateSetImpl {
let result = this._saved;
if (result == null) {
result = new DBaseStateSetImpl();
this._saved = result;
}
return result;
}
protected begin(): this {
if (this._doSave && !this._isSaved) {
this._isSaved = true;
this.saved.copy(this);
}
return this;
}
protected end(): this {
if (this._isLocked <= 0) {
this._doSave = true;
if (this._isSaved) {
this._isSaved = false;
this.onChange(this, this.saved);
}
}
return this;
}
protected onChange(newState: DBaseStateSet, oldState: DBaseStateSet): void {
this._onChange(newState, oldState);
}
}