-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
observableobject.ts
179 lines (155 loc) · 5.33 KB
/
observableobject.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import {ObservableValue, UNCHANGED} from "./observablevalue";
import {ComputedValue} from "../core/computedvalue";
import {ValueMode, AsStructure} from "./modifiers";
import {Lambda, getNextId, invariant, assertPropertyConfigurable, isPlainObject} from "../utils/utils";
import {throwingComputedValueSetter} from "../api/computeddecorator";
import {hasInterceptors, IInterceptable, registerInterceptor, interceptChange} from "./intercept-utils";
import {IListenable, registerListener, hasListeners, notifyListeners} from "./listen-utils";
import {isSpyEnabled, spyReportStart, spyReportEnd} from "../core/spy";
// In 3.0, change to IObjectDidChange
export interface IObjectChange {
name: string;
object: any;
type: "update" | "add";
oldValue?: any;
newValue: any;
}
export interface IObjectWillChange {
object: any;
type: "update" | "add";
name: string;
newValue: any;
}
export class ObservableObjectAdministration implements IInterceptable<IObjectWillChange>, IListenable {
values: {[key: string]: ObservableValue<any>|ComputedValue<any>} = {};
changeListeners = null;
interceptors = null;
constructor(public target: any, public name: string, public mode: ValueMode) { }
/**
* Observes this object. Triggers for the events 'add', 'update' and 'delete'.
* See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/observe
* for callback details
*/
observe(callback: (changes: IObjectChange) => void, fireImmediately?: boolean): Lambda {
invariant(fireImmediately !== true, "`observe` doesn't support the fire immediately property for observable objects.");
return registerListener(this, callback);
}
intercept(handler): Lambda {
return registerInterceptor(this, handler);
}
}
export interface IIsObservableObject {
$mobx: ObservableObjectAdministration;
}
export function asObservableObject(target, name: string, mode: ValueMode = ValueMode.Recursive): ObservableObjectAdministration {
if (isObservableObject(target))
return target.$mobx;
if (!isPlainObject(target))
name = target.constructor.name + "@" + getNextId();
if (!name)
name = "ObservableObject@" + getNextId();
const adm = new ObservableObjectAdministration(target, name, mode);
Object.defineProperty(target, "$mobx", {
enumerable: false,
configurable: false,
writable: false,
value: adm
});
return adm;
}
export function setObservableObjectProperty(adm: ObservableObjectAdministration, propName: string, value) {
if (adm.values[propName])
adm.target[propName] = value; // the property setter will make 'value' reactive if needed.
else
defineObservableProperty(adm, propName, value);
}
function defineObservableProperty(adm: ObservableObjectAdministration, propName: string, newValue) {
assertPropertyConfigurable(adm.target, propName);
let observable: ComputedValue<any>|ObservableValue<any>;
let name = `${adm.name}.${propName}`;
let isComputed = true;
if (typeof newValue === "function" && newValue.length === 0)
observable = new ComputedValue(newValue, adm.target, false, name);
else if (newValue instanceof AsStructure && typeof newValue.value === "function" && newValue.value.length === 0)
observable = new ComputedValue(newValue.value, adm.target, true, name);
else {
isComputed = false;
if (hasInterceptors(adm)) {
const change = interceptChange<IObjectWillChange>(adm, {
object: adm.target,
name: propName,
type: "add",
newValue
});
if (!change)
return;
newValue = change.newValue;
}
observable = new ObservableValue(newValue, adm.mode, name, false);
newValue = (observable as any).value; // observableValue might have changed it
}
adm.values[propName] = observable;
Object.defineProperty(adm.target, propName, {
configurable: true,
enumerable: !isComputed,
get: function() {
return observable.get();
},
set: isComputed
? throwingComputedValueSetter
: createSetter(adm, observable as ObservableValue<any>, propName)
});
if (!isComputed)
notifyPropertyAddition(adm, adm.target, propName, newValue);
}
function createSetter(adm: ObservableObjectAdministration, observable: ObservableValue<any>, name: string) {
return function (newValue) {
// intercept
if (hasInterceptors(adm)) {
const change = interceptChange<IObjectWillChange>(adm, {
type: "update",
object: this,
name, newValue
});
if (!change)
return;
newValue = change.newValue;
}
newValue = observable.prepareNewValue(newValue);
// notify spy & observers
if (newValue !== UNCHANGED) {
const notify = hasListeners(adm);
const notifySpy = isSpyEnabled();
const change = notifyListeners || hasListeners ? {
type: "update",
object: this,
oldValue: (observable as any).value,
name, newValue
} : null;
if (notifySpy)
spyReportStart(change);
observable.setNewValue(newValue);
if (notify)
notifyListeners(adm, change);
if (notifySpy)
spyReportEnd();
}
};
}
function notifyPropertyAddition(adm, object, name: string, newValue) {
const notify = hasListeners(adm);
const notifySpy = isSpyEnabled();
const change = notify || notifySpy ? {
type: "add",
object, name, newValue
} : null;
if (notifySpy)
spyReportStart(change);
if (notify)
notifyListeners(adm, change);
if (notifySpy)
spyReportEnd();
}
export function isObservableObject(thing): boolean {
return thing && thing.$mobx instanceof ObservableObjectAdministration;
}