Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

custom equals when creating an atom #79

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/focal/src/atom/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ export abstract class AbstractAtom<T>
}

export class JsonAtom<T> extends AbstractAtom<T> {
constructor(initialValue: T) {
constructor(initialValue: T, private _eq: (x: T, y: T) => boolean = structEq) {
super(initialValue)
}

Expand All @@ -309,14 +309,14 @@ export class JsonAtom<T> extends AbstractAtom<T> {
const prevValue = this.getValue()
const next = updateFn(prevValue)

if (!structEq(prevValue, next))
if (!this._eq(prevValue, next))
this.next(next)
}

set(x: T) {
const prevValue = this.getValue()

if (!structEq(prevValue, x))
if (!this._eq(prevValue, x))
this.next(x)
}
}
Expand Down
18 changes: 16 additions & 2 deletions packages/focal/src/atom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,24 @@ export namespace Atom {
* @export
* @template T type of atom values
* @param initialValue initial value for this atom
* @param eq - optional custom equality check function,
* used to compare prev and next atom values to emit value only when it has changed.
*
* !!!BEWARE!!!: make sure that your custom equality check function is optimal
* (e.g. checks for value reference equality first),
* as it will be called EVERY time an atom value is changed
*
* (when setting(modifying) an atom value, a new value is only emitted by atom if it has changed,
* i.e. it does not equal to the previous value)
*
* If not specified, a default private implementation of structure equality is used.
*
* As an alternative, a value may have an 'equals' method that will be recognized
* by default structure equality implementation
* @returns fresh atom
*/
export function create<T>(initialValue: T): Atom<T> {
return new JsonAtom(initialValue)
export function create<T>(initialValue: T, eq?: (x: T, y: T) => boolean): Atom<T> {
return new JsonAtom(initialValue, eq)
}

// tslint:disable no-unused-vars
Expand Down
79 changes: 67 additions & 12 deletions packages/focal/test/atom.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -500,42 +500,36 @@ describe('atom', () => {

let called1 = 0
const a1 = source.view(x => {
console.log('a1', called1, x)
called1++
return x + 1
})

let called2 = 0
const a2 = a1.view(x => {
console.log('a2', called2, x)
called2++
return -x
})

let called3 = 0
const a3 = a2.view(x => {
console.log('a3', called3, x)
called3++
return x * 2
})

let called4 = 0
const a4 = a3.view(x => {
console.log('a4', called4, x)
called4++
return `Hi ${x}`
})

let called5 = 0
const a5 = a3.view(x => {
console.log('a5', called5, x)
called5++
return `Ho ${x}`
})

let called6 = 0
const a6 = a3.view(x => {
console.log('a6', called6, x)
called6++
return `HU ${x}`
})
Expand Down Expand Up @@ -583,42 +577,36 @@ describe('atom', () => {

let called1 = 0
const a1 = source.view(x => {
console.log('a1', called1, x)
called1++
return x + 1
})

let called2 = 0
const a2 = a1.view(x => {
console.log('a2', called2, x)
called2++
return -x
})

let called3 = 0
const a3 = a2.view(x => {
console.log('a3', called3, x)
called3++
return x * 2
})

let called4 = 0
const a4 = a3.view(x => {
console.log('a4', called4, x)
called4++
return `Hi ${x}`
})

let called5 = 0
const a5 = a3.view(x => {
console.log('a5', called5, x)
called5++
return `Ho ${x}`
})

let called6 = 0
const a6 = a3.view(x => {
console.log('a6', called6, x)
called6++
return `HU ${x}`
})
Expand Down Expand Up @@ -879,4 +867,71 @@ describe('atom', () => {
).toEqual(['N', 'C'])
})
})

describe('value equality', () => {
interface TestValue {
a: number,
b: number
}

const eqByA = (a: TestValue, b: TestValue): boolean => {
console.log('equals', a.a === b.a)
return a.a === b.a
}

it('custom equals is called on modify', () => {
const eqMock = jest.fn(eqByA)
const a = Atom.create<TestValue>({ a: 1, b: 2 }, eqMock)

a.modify(v => ({ ...v, b: 3 }))

expect(eqMock.mock.calls.length).toBe(1)
})

it('custom equals used on modify', () => {
const a = Atom.create<TestValue>({ a: 1, b: 2 }, eqByA)
const observations: TestValue[] = []
const cb = (x: TestValue) => observations.push(x)
const subscription = a.subscribe(cb)

a.modify(v => ({ ...v, b: 3 }))
a.modify(v => ({ ...v, a: 2 }))

expect(observations.length).toBe(2) // including initial value
expect(observations).toEqual([{ a: 1, b: 2 }, { a: 2, b: 2 }])

subscription.unsubscribe()
})

it('custom equals used on set', () => {
const a = Atom.create<TestValue>({ a: 1, b: 2 }, eqByA)
const observations: TestValue[] = []
const cb = (x: TestValue) => observations.push(x)
const subscription = a.subscribe(cb)

a.set({ a: 1, b: 3 })
a.set({ a: 2, b: 2 })

expect(observations.length).toBe(2) // including initial value
expect(observations).toEqual([{ a: 1, b: 2 }, { a: 2, b: 2 }])

subscription.unsubscribe()
})

it('custom equals used in child atom', () => {
const a = Atom.create<TestValue>({ a: 1, b: 2 }, eqByA)
const b = a.view(v => v.b)
const bObservations: number[] = []
const cb = (x: number) => bObservations.push(x)
const subscription = b.subscribe(cb)

a.set({ a: 1, b: 3 })
a.set({ a: 2, b: 4 })

expect(bObservations.length).toBe(2) // including initial value
expect(bObservations).toEqual([2, 4])

subscription.unsubscribe()
})
})
})