Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Remove eventName in unsubscribe API arguments #2844

Merged
merged 1 commit into from
Oct 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions js/src/api/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ export default class Api {
return this._subscriptions.subscribe(subscriptionName, callback);
}

unsubscribe (subscriptionName, subscriptionId) {
return this._subscriptions.unsubscribe(subscriptionName, subscriptionId);
unsubscribe (subscriptionId) {
return this._subscriptions.unsubscribe(subscriptionId);
}

pollMethod (method, input, validate) {
Expand Down
51 changes: 23 additions & 28 deletions js/src/api/subscriptions/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,14 @@ const events = {
'personal_requestsToConfirm': { module: 'signer' }
};

let nextSubscriptionId = 0;

export default class Manager {
constructor (api) {
this._api = api;

this.subscriptions = {};
this.subscriptions = [];
this.values = {};

Object.keys(events).forEach((subscriptionName) => {
this.subscriptions[subscriptionName] = {};
this.values[subscriptionName] = {
error: null,
data: null
Expand Down Expand Up @@ -71,61 +68,59 @@ export default class Manager {
return;
}

const subscriptionId = nextSubscriptionId++;
const subscriptionId = this.subscriptions.length;
const { error, data } = this.values[subscriptionName];
const engine = this[`_${subscription.module}`];

this.subscriptions[subscriptionName][subscriptionId] = callback;
this.subscriptions[subscriptionId] = {
name: subscriptionName,
id: subscriptionId,
callback
};

if (!engine.isStarted) {
engine.start();
} else {
this._sendData(subscriptionName, subscriptionId, callback, error, data);
this._sendData(subscriptionId, error, data);
}

resolve(subscriptionId);
});
}

unsubscribe (subscriptionName, subscriptionId) {
unsubscribe (subscriptionId) {
return new Promise((resolve, reject) => {
const subscription = this._validateType(subscriptionName);

if (isError(subscription)) {
reject(subscription);
if (!this.subscriptions[subscriptionId]) {
reject(new Error(`Cannot find subscription ${subscriptionId}`));
return;
}

if (!this.subscriptions[subscriptionName][subscriptionId]) {
reject(new Error(`Cannot find subscription ${subscriptionId} for type ${subscriptionName}`));
return;
}

delete this.subscriptions[subscriptionName][subscriptionId];
delete this.subscriptions[subscriptionId];
resolve();
});
}

_sendData (subscriptionName, subscriptionId, callback, error, data) {
_sendData (subscriptionId, error, data) {
const { callback } = this.subscriptions[subscriptionId];

try {
callback(error, data);
} catch (error) {
console.error(`Unable to update callback for ${subscriptionName}, subscriptionId ${subscriptionId}`, error);
this.unsubscribe(subscriptionName, subscriptionId);
console.error(`Unable to update callback for subscriptionId ${subscriptionId}`, error);
this.unsubscribe(subscriptionId);
}
}

_updateSubscriptions = (subscriptionName, error, data) => {
if (!this.subscriptions[subscriptionName]) {
throw new Error(`Cannot find entry point for subscriptions of type ${subscriptionName}`);
}
const subscriptions = this.subscriptions
.filter(subscription => subscription.name === subscriptionName);

this.values[subscriptionName] = { error, data };
Object.keys(this.subscriptions[subscriptionName]).forEach((subscriptionId) => {
const callback = this.subscriptions[subscriptionName][subscriptionId];

this._sendData(subscriptionName, subscriptionId, callback, error, data);
});
subscriptions
.forEach((subscription) => {
this._sendData(subscription.id, error, data);
});
}
}

Expand Down
40 changes: 38 additions & 2 deletions js/src/api/subscriptions/manager.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import Manager, { events } from './manager';

function newStub () {
const start = () => manager._updateSubscriptions(manager.__test, null, 'test');

const manager = new Manager({
transport: {
isConnected: true
Expand Down Expand Up @@ -53,7 +54,7 @@ describe('api/subscriptions/manager', () => {

describe('constructor', () => {
it('sets up the subscription types & defaults', () => {
expect(Object.keys(manager.subscriptions)).to.deep.equal(Object.keys(events));
expect(manager.subscriptions).to.be.an.array;
expect(Object.keys(manager.values)).to.deep.equal(Object.keys(events));
});
});
Expand All @@ -74,6 +75,7 @@ describe('api/subscriptions/manager', () => {
manager.__test = eventName;
cb = sinon.stub();
sinon.spy(engine, 'start');

return manager
.subscribe(eventName, cb)
.then((_subscriptionId) => {
Expand All @@ -86,7 +88,7 @@ describe('api/subscriptions/manager', () => {
});

it('returns a subscriptionId', () => {
expect(subscriptionId).to.be.ok;
expect(subscriptionId).to.be.a.number;
});

it('calls the subscription callback with updated values', () => {
Expand All @@ -95,4 +97,38 @@ describe('api/subscriptions/manager', () => {
});
});
});

describe('unsubscriptions', () => {
Object
.keys(events)
.filter((eventName) => eventName.indexOf('_') !== -1)
.forEach((eventName) => {
const { module } = events[eventName];
let engine;
let cb;

describe(eventName, () => {
beforeEach(() => {
engine = manager[`_${module}`];
manager.__test = eventName;
cb = sinon.stub();
sinon.spy(engine, 'start');

return manager
.subscribe(eventName, cb)
.then((_subscriptionId) => {
manager.unsubscribe(_subscriptionId);
})
.then(() => {
manager._updateSubscriptions(manager.__test, null, 'test2');
});
});

it('does not call the callback after unsibscription', () => {
expect(cb).to.have.been.calledWith(null, 'test');
expect(cb).to.not.have.been.calledWith(null, 'test2');
});
});
});
});
});
2 changes: 1 addition & 1 deletion js/src/ui/TxHash/txHash.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class TxHash extends Component {
const { api } = this.context;
const { subscriptionId } = this.state;

api.unsubscribe('eth_blockNumber', subscriptionId);
api.unsubscribe(subscriptionId);
}

render () {
Expand Down
2 changes: 1 addition & 1 deletion js/src/views/Contract/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class Contract extends Component {
const { api } = this.context;
const { subscriptionId, blockSubscriptionId, contract } = this.state;

api.unsubscribe('eth_blockNumber', blockSubscriptionId);
api.unsubscribe(blockSubscriptionId);
contract.unsubscribe(subscriptionId);
}

Expand Down