Skip to content

Commit

Permalink
Merge pull request #2838 from ethereum/issue/2833
Browse files Browse the repository at this point in the history
SyncingSubscription fix
  • Loading branch information
nivida authored May 24, 2019
2 parents 08388f4 + 735c7c1 commit 6961fa2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,27 @@ export default class SyncingSubscription extends AbstractSubscription {
* @returns {Object}
*/
onNewSubscriptionItem(subscriptionItem) {
const isSyncing = subscriptionItem.result.syncing;
if (typeof subscriptionItem !== 'boolean') {
const isSyncing = subscriptionItem.syncing;

if (this.isSyncing === null) {
this.isSyncing = isSyncing;
this.emit('changed', this.isSyncing);
}
if (this.isSyncing === null) {
this.isSyncing = isSyncing;
this.emit('changed', this.isSyncing);

if (this.isSyncing === true && isSyncing === false) {
this.isSyncing = isSyncing;
this.emit('changed', this.isSyncing);
}
return subscriptionItem.status;
}

if (this.isSyncing === false && isSyncing === true) {
this.isSyncing = isSyncing;
this.emit('changed', this.isSyncing);
if (this.isSyncing !== isSyncing) {
this.isSyncing = isSyncing;
this.emit('changed', this.isSyncing);
}

return subscriptionItem.status;
}

return this.formatters.outputSyncingFormatter(subscriptionItem);
this.isSyncing = subscriptionItem;
this.emit('changed', subscriptionItem);

return subscriptionItem;
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import {formatters} from 'web3-core-helpers';
import SyncingSubscription from '../../../../src/subscriptions/eth/SyncingSubscription';

// Mocks
jest.mock('web3-core-helpers');

/**
* SyncingSubscription test
*/
describe('SyncingSubscriptionTest', () => {
let syncingSubscription;

beforeEach(() => {
syncingSubscription = new SyncingSubscription({}, formatters, {});
syncingSubscription = new SyncingSubscription({}, {}, {});
});

it('constructor check', () => {
Expand All @@ -22,16 +18,10 @@ describe('SyncingSubscriptionTest', () => {
expect(syncingSubscription.type).toEqual('eth_subscribe');

expect(syncingSubscription.options).toEqual(null);

expect(syncingSubscription.utils).toEqual({});

expect(syncingSubscription.formatters).toEqual(formatters);

expect(syncingSubscription.moduleInstance).toEqual({});
});

it('onNewSubscriptionItem calls outputSyncingFormatter and emits "changed" event (isSyncing: null)', (done) => {
const item = {result: {syncing: true}};
it('calls onNewSubscriptionItem and emits the initial "changed" event', (done) => {
const item = {syncing: true};

syncingSubscription.on('changed', (subscriptionItem) => {
expect(subscriptionItem).toEqual(true);
Expand All @@ -40,37 +30,28 @@ describe('SyncingSubscriptionTest', () => {
});

syncingSubscription.onNewSubscriptionItem(item);

expect(formatters.outputSyncingFormatter).toHaveBeenCalledWith(item);
});

it('onNewSubscriptionItem calls outputSyncingFormatter and emits "changed" event (isSyncing: true)', (done) => {
const item = {result: {syncing: false}};
it('calls onNewSubscriptionItem and emits the "changed" event', (done) => {
const item = {syncing: false};

syncingSubscription.on('changed', (subscriptionItem) => {
expect(subscriptionItem).toEqual(item.result.syncing);
expect(subscriptionItem).toEqual(false);

done();
});

syncingSubscription.isSyncing = true;
syncingSubscription.onNewSubscriptionItem(item);

expect(formatters.outputSyncingFormatter).toHaveBeenCalledWith(item);
});

it('onNewSubscriptionItem calls outputSyncingFormatter and emits "changed" event (isSyncing: false)', (done) => {
const item = {result: {syncing: true}};

syncingSubscription.on('changed', (subscriptionItem) => {
expect(subscriptionItem).toEqual(item.result.syncing);

done();
});

syncingSubscription.isSyncing = false;
syncingSubscription.onNewSubscriptionItem(item);
it('calls onNewSubscriptionItem and returns the boolean', () => {
expect(syncingSubscription.onNewSubscriptionItem(true)).toEqual(true);
});

expect(formatters.outputSyncingFormatter).toHaveBeenCalledWith(item);
it('calls onNewSubscriptionItem and returns the syncing status', () => {
expect(
syncingSubscription.onNewSubscriptionItem({status: true, syncing: true})
).toEqual(true);
});
});
10 changes: 5 additions & 5 deletions packages/web3-eth/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,11 @@ export interface Method {
}

export interface Syncing {
startingBlock: number;
currentBlock: number;
highestBlock: number;
knownStates: number;
pulledStates: number;
StartingBlock: number;
CurrentBlock: number;
HighestBlock: number;
KnownStates: number;
PulledStates: number;
}

export interface BlockHeader {
Expand Down

0 comments on commit 6961fa2

Please sign in to comment.