Skip to content

Commit

Permalink
Merge 025b524 into 3186d2a
Browse files Browse the repository at this point in the history
  • Loading branch information
mweststrate committed Jan 21, 2019
2 parents 3186d2a + 025b524 commit a6050a8
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 7 deletions.
6 changes: 4 additions & 2 deletions src/api/observable.ts
@@ -1,5 +1,6 @@
import {
IEnhancer,
IEqualsComparer,
IObservableArray,
IObservableDecorator,
IObservableMapInitialValues,
Expand All @@ -25,6 +26,7 @@ import {

export type CreateObservableOptions = {
name?: string
equals?: IEqualsComparer<any>
deep?: boolean
defaultDecorator?: IObservableDecorator
proxy?: boolean
Expand All @@ -41,7 +43,7 @@ export const defaultCreateObservableOptions: CreateObservableOptions = {
Object.freeze(defaultCreateObservableOptions)

function assertValidOption(key: string) {
if (!/^(deep|name|defaultDecorator|proxy)$/.test(key))
if (!/^(deep|name|equals|defaultDecorator|proxy)$/.test(key))
fail(`invalid option for (extend)observable: ${key}`)
}

Expand Down Expand Up @@ -142,7 +144,7 @@ const observableFactories: IObservableFactories = {
box<T = any>(value?: T, options?: CreateObservableOptions): IObservableValue<T> {
if (arguments.length > 2) incorrectlyUsedAsDecorator("box")
const o = asCreateObservableOptions(options)
return new ObservableValue(value, getEnhancerFromOptions(o), o.name)
return new ObservableValue(value, getEnhancerFromOptions(o), o.name, true, o.equals)
},
array<T = any>(initialValues?: T[], options?: CreateObservableOptions): IObservableArray<T> {
if (arguments.length > 2) incorrectlyUsedAsDecorator("array")
Expand Down
9 changes: 6 additions & 3 deletions src/types/observablevalue.ts
Expand Up @@ -2,10 +2,12 @@ import {
Atom,
IEnhancer,
IInterceptable,
IEqualsComparer,
IInterceptor,
IListenable,
Lambda,
checkIfStateModificationsAreAllowed,
comparer,
createInstanceofPredicate,
getNextId,
hasInterceptors,
Expand Down Expand Up @@ -51,8 +53,9 @@ export class ObservableValue<T> extends Atom
constructor(
value: T,
public enhancer: IEnhancer<T>,
name = "ObservableValue@" + getNextId(),
notifySpy = true
public name = "ObservableValue@" + getNextId(),
notifySpy = true,
private equals: IEqualsComparer<any> = comparer.default
) {
super(name)
this.value = enhancer(value, undefined, name)
Expand Down Expand Up @@ -98,7 +101,7 @@ export class ObservableValue<T> extends Atom
}
// apply modifier
newValue = this.enhancer(newValue, this.value, this.name)
return this.value !== newValue ? newValue : globalState.UNCHANGED
return this.equals(this.value, newValue) ? globalState.UNCHANGED : newValue
}

setNewValue(newValue: T) {
Expand Down
58 changes: 56 additions & 2 deletions test/base/observables.js
Expand Up @@ -149,6 +149,60 @@ test("dynamic2", function(done) {
}
})

test("box uses equals", function(done) {
try {
var x = observable.box("a", {
equals: (oldValue, newValue) => {
return oldValue.toLowerCase() === newValue.toLowerCase()
}
})

var b = buffer()
m.observe(x, b)

x.set("A")
x.set("b")
x.set("B")
x.set("C")

expect(["b", "C"]).toEqual(b.toArray())
expect(mobx._isComputingDerivation()).toBe(false)

done()
} catch (e) {
console.log(e.stack)
}
})

test("box uses equals2", function(done) {
try {
var x = observable.box("01", {
equals: (oldValue, newValue) => {
return parseInt(oldValue) === parseInt(newValue)
}
})

var y = computed(function() {
return parseInt(x)
})

var b = buffer()
m.observe(y, b)

x.set("2")
x.set("02")
x.set("002")
x.set("03")

expect([2, 3]).toEqual(b.toArray())
expect(mobx._isComputingDerivation()).toBe(false)

done()
} catch (e) {
console.log(e.stack)
}
})

test("readme1", function(done) {
try {
var b = buffer()
Expand All @@ -157,7 +211,7 @@ test("readme1", function(done) {
var order = {}
order.price = observable.box(10)
// Prints: New price: 24
//in TS, just: value(() => this.price() * (1+vat()))
// in TS, just: value(() => this.price() * (1+vat()))
order.priceWithVat = computed(function() {
return order.price.get() * (1 + vat.get())
})
Expand Down Expand Up @@ -191,7 +245,7 @@ test("batch", function() {

a.set(4)
b.set(5)
// Note, 60 should not happen! (that is d beign computed before c after update of b)
// Note, 60 should not happen! (that is d begin computed before c after update of b)
expect(buf.toArray()).toEqual([36, 100])

var x = mobx.transaction(function() {
Expand Down

0 comments on commit a6050a8

Please sign in to comment.