Rename blockAndLogStreamer to blockAndLogStreamerIfExists

This commit is contained in:
Leonid Logvinov 2017-12-12 13:32:54 +01:00
parent a6f9718131
commit f94dc1fe44
No known key found for this signature in database
GPG Key ID: 0DD294BFDE8C95D4

View File

@ -34,7 +34,7 @@ export class ContractWrapper {
protected _web3Wrapper: Web3Wrapper; protected _web3Wrapper: Web3Wrapper;
private _networkId: number; private _networkId: number;
private _abiDecoder?: AbiDecoder; private _abiDecoder?: AbiDecoder;
private _blockAndLogStreamer: BlockAndLogStreamer|undefined; private _blockAndLogStreamerIfExists: BlockAndLogStreamer|undefined;
private _blockAndLogStreamInterval: NodeJS.Timer; private _blockAndLogStreamInterval: NodeJS.Timer;
private _filters: {[filterToken: string]: Web3.FilterObject}; private _filters: {[filterToken: string]: Web3.FilterObject};
private _filterCallbacks: {[filterToken: string]: EventCallback<ContractEventArgs>}; private _filterCallbacks: {[filterToken: string]: EventCallback<ContractEventArgs>};
@ -46,7 +46,7 @@ export class ContractWrapper {
this._abiDecoder = abiDecoder; this._abiDecoder = abiDecoder;
this._filters = {}; this._filters = {};
this._filterCallbacks = {}; this._filterCallbacks = {};
this._blockAndLogStreamer = undefined; this._blockAndLogStreamerIfExists = undefined;
this._onLogAddedSubscriptionToken = undefined; this._onLogAddedSubscriptionToken = undefined;
this._onLogRemovedSubscriptionToken = undefined; this._onLogRemovedSubscriptionToken = undefined;
} }
@ -77,7 +77,7 @@ export class ContractWrapper {
address: string, eventName: ContractEvents, indexFilterValues: IndexedFilterValues, abi: Web3.ContractAbi, address: string, eventName: ContractEvents, indexFilterValues: IndexedFilterValues, abi: Web3.ContractAbi,
callback: EventCallback<ArgsType>): string { callback: EventCallback<ArgsType>): string {
const filter = filterUtils.getFilter(address, eventName, indexFilterValues, abi); const filter = filterUtils.getFilter(address, eventName, indexFilterValues, abi);
if (_.isUndefined(this._blockAndLogStreamer)) { if (_.isUndefined(this._blockAndLogStreamerIfExists)) {
this._startBlockAndLogStream(); this._startBlockAndLogStream();
} }
const filterToken = filterUtils.generateUUID(); const filterToken = filterUtils.generateUUID();
@ -146,39 +146,43 @@ export class ContractWrapper {
}); });
} }
private _startBlockAndLogStream(): void { private _startBlockAndLogStream(): void {
this._blockAndLogStreamer = new BlockAndLogStreamer( if (!_.isUndefined(this._blockAndLogStreamerIfExists)) {
throw new Error(ZeroExError.SubscriptionAlreadyPresent);
}
this._blockAndLogStreamerIfExists = new BlockAndLogStreamer(
this._web3Wrapper.getBlockAsync.bind(this._web3Wrapper), this._web3Wrapper.getBlockAsync.bind(this._web3Wrapper),
this._web3Wrapper.getLogsAsync.bind(this._web3Wrapper), this._web3Wrapper.getLogsAsync.bind(this._web3Wrapper),
); );
const catchAllLogFilter = {}; const catchAllLogFilter = {};
this._blockAndLogStreamer.addLogFilter(catchAllLogFilter); this._blockAndLogStreamerIfExists.addLogFilter(catchAllLogFilter);
this._blockAndLogStreamInterval = intervalUtils.setAsyncExcludingInterval( this._blockAndLogStreamInterval = intervalUtils.setAsyncExcludingInterval(
this._reconcileBlockAsync.bind(this), constants.DEFAULT_BLOCK_POLLING_INTERVAL, this._reconcileBlockAsync.bind(this), constants.DEFAULT_BLOCK_POLLING_INTERVAL,
); );
let isRemoved = false; let isRemoved = false;
this._onLogAddedSubscriptionToken = this._blockAndLogStreamer.subscribeToOnLogAdded( this._onLogAddedSubscriptionToken = this._blockAndLogStreamerIfExists.subscribeToOnLogAdded(
this._onLogStateChanged.bind(this, isRemoved), this._onLogStateChanged.bind(this, isRemoved),
); );
isRemoved = true; isRemoved = true;
this._onLogRemovedSubscriptionToken = this._blockAndLogStreamer.subscribeToOnLogRemoved( this._onLogRemovedSubscriptionToken = this._blockAndLogStreamerIfExists.subscribeToOnLogRemoved(
this._onLogStateChanged.bind(this, isRemoved), this._onLogStateChanged.bind(this, isRemoved),
); );
} }
private _stopBlockAndLogStream(): void { private _stopBlockAndLogStream(): void {
(this._blockAndLogStreamer as BlockAndLogStreamer).unsubscribeFromOnLogAdded( if (_.isUndefined(this._blockAndLogStreamerIfExists)) {
this._onLogAddedSubscriptionToken as string); throw new Error(ZeroExError.SubscriptionNotFound);
(this._blockAndLogStreamer as BlockAndLogStreamer).unsubscribeFromOnLogRemoved( }
this._onLogRemovedSubscriptionToken as string); this._blockAndLogStreamerIfExists.unsubscribeFromOnLogAdded(this._onLogAddedSubscriptionToken as string);
this._blockAndLogStreamerIfExists.unsubscribeFromOnLogRemoved(this._onLogRemovedSubscriptionToken as string);
intervalUtils.clearAsyncExcludingInterval(this._blockAndLogStreamInterval); intervalUtils.clearAsyncExcludingInterval(this._blockAndLogStreamInterval);
delete this._blockAndLogStreamer; delete this._blockAndLogStreamerIfExists;
} }
private async _reconcileBlockAsync(): Promise<void> { private async _reconcileBlockAsync(): Promise<void> {
try { try {
const latestBlock = await this._web3Wrapper.getBlockAsync(BlockParamLiteral.Latest); const latestBlock = await this._web3Wrapper.getBlockAsync(BlockParamLiteral.Latest);
// We need to coerce to Block type cause Web3.Block includes types for mempool blocks // We need to coerce to Block type cause Web3.Block includes types for mempool blocks
if (!_.isUndefined(this._blockAndLogStreamer)) { if (!_.isUndefined(this._blockAndLogStreamerIfExists)) {
// If we clear the interval while fetching the block - this._blockAndLogStreamer will be undefined // If we clear the interval while fetching the block - this._blockAndLogStreamer will be undefined
await this._blockAndLogStreamer.reconcileNewBlock(latestBlock as any as Block); await this._blockAndLogStreamerIfExists.reconcileNewBlock(latestBlock as any as Block);
} }
} catch (err) { } catch (err) {
const filterTokens = _.keys(this._filterCallbacks); const filterTokens = _.keys(this._filterCallbacks);