blob: 6d9556336ad976d752615e6bac88247ee55b68a1 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
import * as _ from 'lodash';
import {BigNumber} from 'bignumber.js';
import {utils} from '../utils/utils';
import {intervalUtils} from '../utils/interval_utils';
import {SignedOrder, ZeroExError} from '../types';
import {Heap} from '../utils/heap';
import {ZeroEx} from '../0x';
// Order prunning is very fast
const DEFAULT_ORDER_EXPIRATION_CHECKING_INTERVAL_MS = 50;
/**
* This class includes the functionality to detect expired orders.
* It stores them in a min heap by expiration time and checks for expired ones every `orderExpirationCheckingIntervalMs`
*/
export class ExpirationWatcher {
private orderHashHeapByExpiration: Heap<string>;
private expiration: {[orderHash: string]: BigNumber} = {};
private callbackIfExists?: (orderHash: string) => void;
private orderExpirationCheckingIntervalMs: number;
private orderExpirationCheckingIntervalIdIfExists?: NodeJS.Timer;
constructor(orderExpirationCheckingIntervalMs?: number) {
this.orderExpirationCheckingIntervalMs = orderExpirationCheckingIntervalMs ||
DEFAULT_ORDER_EXPIRATION_CHECKING_INTERVAL_MS;
const scoreFunction = (orderHash: string) => this.expiration[orderHash].toNumber();
this.orderHashHeapByExpiration = new Heap(scoreFunction);
}
public subscribe(callback: (orderHash: string) => void): void {
if (!_.isUndefined(this.callbackIfExists)) {
throw new Error(ZeroExError.SubscriptionAlreadyPresent);
}
this.callbackIfExists = callback;
this.orderExpirationCheckingIntervalIdIfExists = intervalUtils.setAsyncExcludingInterval(
this.pruneExpiredOrders.bind(this), this.orderExpirationCheckingIntervalMs,
);
}
public unsubscribe(): void {
intervalUtils.clearAsyncExcludingInterval(this.orderExpirationCheckingIntervalIdIfExists as NodeJS.Timer);
delete this.callbackIfExists;
}
public addOrder(orderHash: string, expirationUnixTimestampSec: BigNumber): void {
this.expiration[orderHash] = expirationUnixTimestampSec;
// We don't remove hashes on order remove because it's slow (linear).
// We just skip them later if the order was already removed from the order watcher.
this.orderHashHeapByExpiration.push(orderHash);
}
private pruneExpiredOrders(): void {
const currentUnixTimestampSec = utils.getCurrentUnixTimestamp();
while (
this.orderHashHeapByExpiration.size() !== 0 &&
this.expiration[this.orderHashHeapByExpiration.head()].lessThan(currentUnixTimestampSec) &&
!_.isUndefined(this.callbackIfExists)
) {
const orderHash = this.orderHashHeapByExpiration.pop();
delete this.expiration[orderHash];
this.callbackIfExists(orderHash);
}
}
}
|