Skip to content
This repository has been archived by the owner on May 24, 2022. It is now read-only.

Commit

Permalink
Merge c7b9e50 into d2d7cd7
Browse files Browse the repository at this point in the history
  • Loading branch information
amaury1093 authored Sep 7, 2018
2 parents d2d7cd7 + c7b9e50 commit 9ce4689
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 25 deletions.
47 changes: 25 additions & 22 deletions packages/light.js/src/utils/operators/distinctValues.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,51 @@
// SPDX-License-Identifier: MIT

import BigNumber from 'bignumber.js';
import { concat, of } from 'rxjs';
import { mapTo } from 'rxjs/operators';
import { of } from 'rxjs';
import { finalize, mapTo } from 'rxjs/operators';

import { distinctValues } from './distinctValues';

// Observable that fires 2 times
const fireTwice$ = () => concat(of(0), of(1));
// Observable that fires twice
const fireTwice$ = () => of(0, 1);

it('should fire twice if values are different', done => {
let numberOfTimesCalled = 0;
fireTwice$()
.pipe(
finalize(() => {
expect(numberOfTimesCalled).toEqual(2);
done();
})
)
.subscribe(() => {
++numberOfTimesCalled;
});
});

/**
* Test that distinctValues work on a specific value.
*
* @param value - A value to test
* @param value - A value to test.
*/
const testValue = (value: any, type: string) =>
it('should only fire once for `type`', done => {
it(`should only fire once for ${type}`, done => {
let numberOfTimesCalled = 0;
fireTwice$()
.pipe(
mapTo(value),
distinctValues()
distinctValues(),
finalize(() => {
expect(numberOfTimesCalled).toEqual(1);
done();
})
)
.subscribe(() => {
++numberOfTimesCalled;
});
setTimeout(() => {
expect(numberOfTimesCalled).toEqual(1);
done();
}, 100);
});

testValue(2, 'number');
testValue('foo', 'string');
testValue({ foo: 'bar' }, 'object');
testValue(new BigNumber(2), 'BigNumber');

it('should only fire twice for difference values', done => {
let numberOfTimesCalled = 0;
fireTwice$().subscribe(() => {
++numberOfTimesCalled;
});
setTimeout(() => {
expect(numberOfTimesCalled).toEqual(2);
done();
}, 100);
});
6 changes: 3 additions & 3 deletions packages/light.js/src/utils/operators/distinctValues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import { distinctUntilChanged } from 'rxjs/operators';
import { isObject } from '@parity/api/lib/util/types';

/**
* An intelligent distinctUntilChanged()
* An intelligent distinctUntilChanged().
*
* @ignore
*/
export const distinctValues = <T>() =>
distinctUntilChanged<T>((x, y) => {
if (x instanceof BigNumber && y instanceof BigNumber) {
return x.eq(y);
if (BigNumber.isBigNumber(x) && BigNumber.isBigNumber(y)) {
return ((x as any) as BigNumber).eq((y as any) as BigNumber);
}
if (isObject(x) && isObject(y)) {
return JSON.stringify(x) === JSON.stringify(y); // TODO Do a deep equal instead
Expand Down

0 comments on commit 9ce4689

Please sign in to comment.