/
observablevalue.ts
130 lines (111 loc) · 3.43 KB
/
observablevalue.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import {BaseAtom} from "../core/atom";
import {checkIfStateModificationsAreAllowed} from "../core/derivation";
import {Lambda, getNextId, createInstanceofPredicate, primitiveSymbol, toPrimitive} from "../utils/utils";
import {hasInterceptors, IInterceptable, IInterceptor, registerInterceptor, interceptChange} from "./intercept-utils";
import {IListenable, registerListener, hasListeners, notifyListeners} from "./listen-utils";
import {isSpyEnabled, spyReportStart, spyReportEnd, spyReport} from "../core/spy";
import {IEnhancer} from "../types/modifiers";
export interface IValueWillChange<T> {
object: any;
type: "update";
newValue: T;
}
export interface IValueDidChange<T> extends IValueWillChange<T> {
oldValue: T | undefined;
}
export type IUNCHANGED = {};
export const UNCHANGED: IUNCHANGED = {};
export interface IObservableValue<T> {
get(): T;
set(value: T): void;
intercept(handler: IInterceptor<IValueWillChange<T>>): Lambda;
observe(listener: (change: IValueDidChange<T>) => void, fireImmediately?: boolean): Lambda;
}
declare var Symbol;
export class ObservableValue<T> extends BaseAtom implements IObservableValue<T>, IInterceptable<IValueWillChange<T>>, IListenable {
hasUnreportedChange = false;
interceptors;
changeListeners;
protected value;
constructor(value: T, protected enhancer: IEnhancer<T>, name = "ObservableValue@" + getNextId(), notifySpy = true) {
super(name);
this.value = enhancer(value, undefined, name);
if (notifySpy && isSpyEnabled()) {
// only notify spy if this is a stand-alone observable
spyReport({ type: "create", object: this, newValue: this.value });
}
}
public set(newValue: T) {
const oldValue = this.value;
newValue = this.prepareNewValue(newValue) as any;
if (newValue !== UNCHANGED) {
const notifySpy = isSpyEnabled();
if (notifySpy) {
spyReportStart({
type: "update",
object: this,
newValue, oldValue
});
}
this.setNewValue(newValue);
if (notifySpy)
spyReportEnd();
}
}
private prepareNewValue(newValue): T | IUNCHANGED {
checkIfStateModificationsAreAllowed();
if (hasInterceptors(this)) {
const change = interceptChange<IValueWillChange<T>>(this, { object: this, type: "update", newValue });
if (!change)
return UNCHANGED;
newValue = change.newValue;
}
// apply modifier
newValue = this.enhancer(newValue, this.value, this.name);
return this.value !== newValue
? newValue
: UNCHANGED
;
}
setNewValue(newValue: T) {
const oldValue = this.value;
this.value = newValue;
this.reportChanged();
if (hasListeners(this)) {
notifyListeners(this, {
type: "update",
object: this,
newValue,
oldValue
});
}
}
public get(): T {
this.reportObserved();
return this.value;
}
public intercept(handler: IInterceptor<IValueWillChange<T>>): Lambda {
return registerInterceptor(this, handler);
}
public observe(listener: (change: IValueDidChange<T>) => void, fireImmediately?: boolean): Lambda {
if (fireImmediately)
listener({
object: this,
type: "update",
newValue: this.value,
oldValue: undefined
});
return registerListener(this, listener);
}
toJSON() {
return this.get();
}
toString() {
return `${this.name}[${this.value}]`;
}
valueOf(): T {
return toPrimitive(this.get());
}
}
ObservableValue.prototype[primitiveSymbol()] = ObservableValue.prototype.valueOf;
export const isObservableValue = createInstanceofPredicate("ObservableValue", ObservableValue);