bfx-api-node-plugin-wd
Version:
Connection watchdog plugin for the official Bitfinex Node API
30 lines (23 loc) • 594 B
JavaScript
const _isFinite = require('lodash/isFinite')
module.exports = (h = {}, args = {}) => ({ state = {} } = {}) => {
const { getState, emit } = h
const { packetWDDelay } = args
const { ev } = state
const pState = getState(state)
const { wdTimeout } = pState
if (!_isFinite(packetWDDelay) || packetWDDelay <= 0) {
return null
}
if (wdTimeout !== null) {
clearTimeout(wdTimeout)
}
const newWDTimeout = setTimeout(
emit.bind(null, ev, 'close'),
packetWDDelay
)
return [null, {
wdTimeout: newWDTimeout,
lastPacketMTS: Date.now()
}]
}