Add ability to disallow API subscriptions (#5366)

This commit is contained in:
Jaco Greeff 2017-03-31 22:30:39 +02:00 committed by GitHub
parent d146ae7275
commit 8b0f9759ec

View File

@ -26,7 +26,7 @@ import { isFunction } from './util/types';
import { LocalAccountsMiddleware } from './local'; import { LocalAccountsMiddleware } from './local';
export default class Api extends EventEmitter { export default class Api extends EventEmitter {
constructor (transport) { constructor (transport, allowSubscriptions = true) {
super(); super();
if (!transport || !isFunction(transport.execute)) { if (!transport || !isFunction(transport.execute)) {
@ -45,7 +45,9 @@ export default class Api extends EventEmitter {
this._trace = new Trace(transport); this._trace = new Trace(transport);
this._web3 = new Web3(transport); this._web3 = new Web3(transport);
this._subscriptions = new Subscriptions(this); if (allowSubscriptions) {
this._subscriptions = new Subscriptions(this);
}
// Doing a request here in test env would cause an error // Doing a request here in test env would cause an error
if (process.env.NODE_ENV !== 'test') { if (process.env.NODE_ENV !== 'test') {
@ -112,10 +114,18 @@ export default class Api extends EventEmitter {
} }
subscribe (subscriptionName, callback) { subscribe (subscriptionName, callback) {
if (!this._subscriptions) {
return Promise.resolve(1);
}
return this._subscriptions.subscribe(subscriptionName, callback); return this._subscriptions.subscribe(subscriptionName, callback);
} }
unsubscribe (subscriptionId) { unsubscribe (subscriptionId) {
if (!this._subscriptions) {
return Promise.resolve(true);
}
return this._subscriptions.unsubscribe(subscriptionId); return this._subscriptions.unsubscribe(subscriptionId);
} }