Skip to content

Commit fe5d96e

Browse files
committed
Use getAdministration at several places
1 parent 09bce46 commit fe5d96e

6 files changed

Lines changed: 31 additions & 66 deletions

File tree

src/api/extras.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {ComputedValue} from "../core/computedvalue";
66
import {Reaction} from "../core/reaction";
77
import {isObservableArray} from "../types/observablearray";
88
import {isObservableMap} from "../types/observablemap";
9-
import {isObservableObject, observeObservableObject} from "../types/observableobject";
9+
import {isObservableObject} from "../types/observableobject";
1010

1111
export interface IDependencyTree {
1212
id: number;
@@ -56,13 +56,13 @@ export function getAtom(thing: any, property?: string): IDepTreeNode {
5656
invariant(false, "Cannot obtain atom from " + thing);
5757
}
5858

59-
export function getAdministration(thing, property?) {
59+
export function getAdministration(thing: any, property?: string) {
6060
invariant(thing, "Expection some object");
6161
if (property !== undefined)
6262
return getAdministration(getAtom(thing, property));
6363
if (thing instanceof Atom || thing instanceof ComputedValue || thing instanceof Reaction)
6464
return thing;
65-
if (isObservableMap(thing))
65+
if (isObservableArray(thing) || isObservableMap(thing))
6666
return thing;
6767
if (thing.$mobx)
6868
return thing.$mobx;

src/api/intercept.ts

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import {IInterceptor} from "../types/intercept-utils";
2-
import {IObservableArray, IArrayWillChange, IArrayWillSplice, isObservableArray} from "../types/observablearray";
3-
import {ObservableMap, IMapWillChange, isObservableMap} from "../types/observablemap";
4-
import {IObjectWillChange, IIsObservableObject, isObservableObject} from "../types/observableobject";
2+
import {IObservableArray, IArrayWillChange, IArrayWillSplice} from "../types/observablearray";
3+
import {ObservableMap, IMapWillChange} from "../types/observablemap";
4+
import {IObjectWillChange, isObservableObject} from "../types/observableobject";
55
import {IObservableValue, observable} from "./observable";
6-
import {ObservableValue, IValueWillChange} from "../types/observablevalue";
7-
import {Lambda, isPlainObject, invariant, deprecated} from "../utils/utils";
8-
import {isObservable} from "./isobservable";
6+
import {IValueWillChange} from "../types/observablevalue";
7+
import {Lambda, isPlainObject, deprecated} from "../utils/utils";
98
import {extendObservable} from "./extendobservable";
9+
import {getAdministration} from "./extras";
1010

1111
export function intercept<T>(value: IObservableValue<T>, handler: IInterceptor<IValueWillChange<T>>): Lambda;
1212
export function intercept<T>(observableArray: IObservableArray<T>, handler: IInterceptor<IArrayWillChange<T> | IArrayWillSplice<T>>): Lambda;
@@ -22,36 +22,20 @@ export function intercept(thing, propOrHandler?, handler?): Lambda {
2222
}
2323

2424
function interceptInterceptable(thing, handler) {
25-
if (isObservableArray(thing))
26-
return thing.intercept(handler);
27-
if (isObservableMap(thing))
28-
return thing.intercept(handler);
29-
if (thing instanceof ObservableValue)
30-
return thing.intercept(handler);
31-
if (isPlainObject(thing) || isObservableObject(thing)) {
25+
if (isPlainObject(thing) && !isObservableObject(thing)) {
3226
deprecated("Passing plain objects to intercept / observe is deprecated and will be removed in 3.0");
33-
return (observable(thing) as any as IIsObservableObject).$mobx.intercept(handler);
27+
return getAdministration(observable(thing) as any).intercept(handler);
3428
}
35-
invariant(false, "first argument of intercept should be some observable value or plain object");
29+
return getAdministration(thing).intercept(handler);
3630
}
3731

3832
function interceptProperty(thing, property, handler) {
39-
const propError = "[mobx.intercept] the provided observable map has no key with name: " + property;
40-
if (isObservableMap(thing)) {
41-
if (!thing._has(property))
42-
throw new Error(propError);
43-
return interceptInterceptable(thing._data[property], handler);
44-
}
45-
if (isObservableObject(thing)) {
46-
if (!isObservable(thing, property))
47-
throw new Error(propError);
48-
return interceptInterceptable(thing.$mobx.values[property], handler);
49-
}
50-
if (isPlainObject(thing)) {
33+
if (isPlainObject(thing) && !isObservableObject(thing)) {
34+
deprecated("Passing plain objects to intercept / observe is deprecated and will be removed in 3.0");
5135
extendObservable(thing, {
5236
property: thing[property]
5337
});
5438
return interceptProperty(thing, property, handler);
5539
}
56-
invariant(false, "first argument of intercept should be an (observable)object or observableMap if a property name is given");
40+
return getAdministration(thing, property).intercept(handler);
5741
}

src/api/observe.ts

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import {IObservableArray, IArrayChange, IArraySplice, isObservableArray} from "../types/observablearray";
2-
import {ObservableMap, IMapChange, isObservableMap} from "../types/observablemap";
3-
import {IObjectChange, isObservableObject, observeObservableObject} from "../types/observableobject";
1+
import {IObservableArray, IArrayChange, IArraySplice} from "../types/observablearray";
2+
import {ObservableMap, IMapChange} from "../types/observablemap";
3+
import {IObjectChange, isObservableObject} from "../types/observableobject";
44
import {IObservableValue, observable} from "./observable";
5-
import {ComputedValue} from "../core/computedvalue";
6-
import {ObservableValue} from "../types/observablevalue";
7-
import {Lambda, isPlainObject, invariant, deprecated} from "../utils/utils";
8-
import {isObservable} from "./isobservable";
5+
import {Lambda, isPlainObject, deprecated} from "../utils/utils";
96
import {extendObservable} from "./extendobservable";
7+
import {getAdministration} from "./extras";
108

119
export function observe<T>(value: IObservableValue<T>, listener: (newValue: T, oldValue: T) => void, fireImmediately?: boolean): Lambda;
1210
export function observe<T>(observableArray: IObservableArray<T>, listener: (change: IArrayChange<T> | IArraySplice<T>) => void, fireImmediately?: boolean): Lambda;
@@ -22,38 +20,20 @@ export function observe(thing, propOrCb?, cbOrFire?, fireImmediately?): Lambda {
2220
}
2321

2422
function observeObservable(thing, listener, fireImmediately: boolean) {
25-
if (isObservableArray(thing))
26-
return thing.observe(listener);
27-
if (isObservableMap(thing))
28-
return thing.observe(listener);
29-
if (isObservableObject(thing))
30-
return observeObservableObject(thing, listener, fireImmediately);
31-
if (thing instanceof ObservableValue || thing instanceof ComputedValue)
32-
return thing.observe(listener, fireImmediately);
33-
if (isPlainObject(thing)) {
23+
if (isPlainObject(thing) && !isObservableObject(thing)) {
3424
deprecated("Passing plain objects to intercept / observe is deprecated and will be removed in 3.0");
35-
return observeObservable(observable(<Object> thing), listener, fireImmediately);
25+
return getAdministration(observable(thing) as any).observe(listener, fireImmediately);
3626
}
37-
invariant(false, "first argument of observe should be some observable value or plain object");
27+
return getAdministration(thing).observe(listener, fireImmediately);
3828
}
3929

4030
function observeObservableProperty(thing, property, listener, fireImmediately: boolean) {
41-
const propError = "[mobx.observe] the provided observable map has no key with name: " + property;
42-
if (isObservableMap(thing)) {
43-
if (!thing._has(property))
44-
throw new Error(propError);
45-
return observe(thing._data[property], listener);
46-
}
47-
if (isObservableObject(thing)) {
48-
if (!isObservable(thing, property))
49-
throw new Error(propError);
50-
return observe(thing.$mobx.values[property], listener, fireImmediately);
51-
}
52-
if (isPlainObject(thing)) {
31+
if (isPlainObject(thing) && !isObservableObject(thing)) {
32+
deprecated("Passing plain objects to intercept / observe is deprecated and will be removed in 3.0");
5333
extendObservable(thing, {
5434
property: thing[property]
5535
});
5636
return observeObservableProperty(thing, property, listener, fireImmediately);
5737
}
58-
invariant(false, "first argument of observe should be an (observable)object or observableMap if a property name is given");
38+
return getAdministration(thing, property).observe(listener, fireImmediately);
5939
}

src/types/listen-utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {untracked} from "../core/observable";
33

44
export interface IListenable {
55
changeListeners: Function[];
6+
observe(handler: (change: any, oldValue?: any) => void, fireImmediately?: boolean): Lambda;
67
}
78

89
export function hasListeners(listenable: IListenable) {

src/types/observableobject.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export function asObservableObject(target, name: string, mode: ValueMode = Value
5858
target, name, mode,
5959
interceptors: null,
6060
intercept: interceptObjectChange,
61+
observe: observeObservableObject,
6162
changeListeners: null
6263
};
6364
Object.defineProperty(target, "$mobx", {
@@ -179,10 +180,9 @@ function notifyPropertyAddition(adm, object, name: string, newValue) {
179180
* See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/observe
180181
* for callback details
181182
*/
182-
export function observeObservableObject(object: IIsObservableObject, callback: (changes: IObjectChange) => void, fireImmediately?: boolean): Lambda {
183-
invariant(isObservableObject(object), "Expected observable object");
183+
function observeObservableObject(callback: (changes: IObjectChange) => void, fireImmediately?: boolean): Lambda {
184184
invariant(fireImmediately !== true, "`observe` doesn't support the fire immediately property for observable objects.");
185-
return registerListener(object.$mobx, callback);
185+
return registerListener(this, callback);
186186
}
187187

188188
export function isObservableObject(thing): boolean {

test/extras.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ test('get administration', function(t) {
356356
t.equal(adm(c, "b"), ovClassName);
357357
t.throws(() => adm(c, "c"), /the entry 'c' does not exist in the observable map 'ObservableMap@4'/, "expected throw");
358358

359-
t.equal(adm(d), "Object");
359+
t.equal(adm(d), mobx.observable([]).constructor.name);
360360
t.throws(() => adm(d, 0), /It is not possible to get index atoms from arrays/, "expected throw");
361361

362362
t.equal(adm(e), mobx.computed(() => {}).constructor.name);

0 commit comments

Comments
 (0)