Skip to content

Commit

Permalink
Eliminated (I)ObservableValue.observe
Browse files Browse the repository at this point in the history
  • Loading branch information
mweststrate committed Feb 5, 2016
1 parent c3ffd75 commit c3a5520
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 79 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"scripts": {
"test": "npm run buildtest && tape test/*.js | faucet",
"test": "npm run buildtest && npm run tape",
"tape": "tape test/*.js | faucet",
"test-browser": "npm run buildtest && ( browserify test/*.js | tape-run )",
"test-travis": "npm run clean && npm run test-browser && istanbul cover tape test/*.js",
"coverage": "npm run buildtest && istanbul cover tape test/*.js && cat ./coverage/lcov.info|coveralls",
Expand Down
33 changes: 17 additions & 16 deletions src/api/observe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ export function observe(thing, property?, listener?): Lambda {
listener = property;
property = undefined;
}
if (typeof thing === "function") {
console.error("[mobservable.observe] is deprecated in combination with a function, use 'mobservable.autorun' instead");
return autorun(thing);
}
if (isObservableArray(thing))
return thing.observe(listener);
if (isObservableMap(thing)) {
Expand All @@ -43,19 +39,24 @@ export function observe(thing, property?, listener?): Lambda {
}
return thing.$mobservable.observe(listener);
}
if (thing instanceof ObservableValue || thing instanceof ComputedValue) {
let firstTime = true;
let prevValue = undefined;
return autorun(() => {
let newValue = thing.get();
if (!firstTime || fireImmediately) {
listener(newValue, prevValue);
}
firstTime = false;
prevValue = newValue;
});
}
if (thing instanceof ObservableValue || thing instanceof ComputedValue)
return observeObservableValue(thing, listener, fireImmediately);
if (thing.$mobservable instanceof ObservableValue || thing.$mobservable instanceof ComputedValue)
return observeObservableValue(thing.$mobservable, listener, fireImmediately);
if (isPlainObject(thing))
return (<any>observable(thing)).$mobservable.observe(listener);
throw new Error("[mobservable.observe] first argument should be an observable array, observable map, observable object, observable value, computed value or plain object.");
}

function observeObservableValue<T>(observable: ObservableValue<T>|ComputedValue<T>, listener: (newValue: T, oldValue: T) => void, fireImmediately?: boolean): Lambda {
let firstTime = true;
let prevValue = undefined;
return autorun(() => {
let newValue = observable.get();
if (!firstTime || fireImmediately) {
listener(newValue, prevValue);
}
firstTime = false;
prevValue = newValue;
});
}
5 changes: 2 additions & 3 deletions src/types/observablevalue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ export class ObservableValue<T> {
}
}

// TODO: deprecate?
export interface IObservableValue<T> {
(): T;
(value: T): void;
// TODO: remove observe:
observe(callback: (newValue: T, oldValue: T) => void, fireImmediately?: boolean): Lambda;
}

// TODO: deprecate?
export function toGetterSetterFunction<T>(observable: ObservableValue<T> | ComputedValue<T>): IObservableValue<T> {
const f: any = function(value?) {
if (arguments.length > 0)
Expand All @@ -58,7 +58,6 @@ export function toGetterSetterFunction<T>(observable: ObservableValue<T> | Compu
return observable.get();
};
f.$mobservable = observable;
f.observe = observe.bind(null, observable); // TODO: remove observe?
f.toString = function() {
return observable.toString();
};
Expand Down
2 changes: 1 addition & 1 deletion test/errorhandling.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ test('cycle3', function(t) {
var a = observable(function() { return z() ? 1 : b() * 2; });
var b = observable(function() { return a() * 2; });

b.observe(voidObserver);
m.observe(b, voidObserver);
t.equal(1, a());

t.throws(() => {
Expand Down
8 changes: 4 additions & 4 deletions test/makereactive.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@ test('observable4', function(t) {
]);

var b = buffer();
m.observable(function() {
m.observe(m.observable(function() {
return x.map(function(d) { return d.x });
}).observe(b, true);
}), b, true);

x[0].x = 3;
x.shift();
Expand All @@ -171,9 +171,9 @@ test('observable4', function(t) {
]));

var b2 = buffer();
m.observable(function() {
m.observe(m.observable(function() {
return x2.map(function(d) { return d.x });
}).observe(b2, true);
}), b2, true);

x2[0].x = 3;
x2.shift();
Expand Down
Loading

0 comments on commit c3a5520

Please sign in to comment.