Skip to content

Commit

Permalink
Test keepAlive
Browse files Browse the repository at this point in the history
  • Loading branch information
balthazar committed Oct 21, 2017
1 parent 6800321 commit 0d46379
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
7 changes: 4 additions & 3 deletions src/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,17 @@ export const userEventHandler = cb => msg => {
cb(userTransforms[type] ? userTransforms[type](rest) : { type, ...rest })
}

export const keepStreamAlive = (method, listenKey) => () => method({ listenKey })

const user = opts => cb => {
const { getDataStream, keepDataStream, closeDataStream } = httpMethods(opts)

return getDataStream().then(({ listenKey }) => {
const w = new WebSocket(`${BASE}/${listenKey}`)
w.on('message', userEventHandler(cb))

const int = setInterval(() => {
keepDataStream({ listenKey })
}, 42e3)
const int = setInterval(keepStreamAlive(keepDataStream, listenKey), 42e3)
keepStreamAlive(keepDataStream, listenKey)()

return () => {
clearInterval(int)
Expand Down
8 changes: 7 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ test.serial('[WS] trades', t => {
})
})

test.serial('[WS] userTransforms', t => {
test.serial('[WS] userEvents', t => {
const accountPayload = {
e: 'outboundAccountInfo',
E: 1499405658849,
Expand Down Expand Up @@ -312,6 +312,12 @@ test.serial('[WS] userTransforms', t => {
isBuyerMaker: false,
})
})(JSON.stringify(tradePayload))

const newEvent = { e: 'totallyNewEvent', yolo: 42 }

userEventHandler(res => {
t.deepEqual(res, { type: 'totallyNewEvent', yolo: 42 })
})(JSON.stringify(newEvent))
})

if (process.env.API_KEY) {
Expand Down

0 comments on commit 0d46379

Please sign in to comment.