Skip to content

Commit 0afaf3f

Browse files
authored
Merge pull request #17 from nash-io/better-orderbooks
Change onUpdatedOrderbook so the first result is the current orderbook
2 parents 1d16cf9 + f513396 commit 0afaf3f

File tree

1 file changed

+69
-8
lines changed

1 file changed

+69
-8
lines changed

src/client/client.ts

Lines changed: 69 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ import { LIST_ASSETS_QUERY } from '../queries/asset/listAsset'
9797

9898
import { NEW_ACCOUNT_TRADES } from '../subscriptions/newAccountTrades'
9999
import { UPDATED_ACCOUNT_ORDERS } from '../subscriptions/updatedAccountOrders'
100-
import { UPDATED_ORDER_BOOK } from '../subscriptions/updatedOrderBook'
100+
// import { UPDATED_ORDER_BOOK } from '../subscriptions/updatedOrderBook'
101101
import { NEW_TRADES } from '../subscriptions/newTrades'
102102
import { UPDATED_TICKERS } from '../subscriptions/updatedTickers'
103103
import { UPDATED_CANDLES } from '../subscriptions/updatedCandles'
@@ -641,6 +641,45 @@ export class Client {
641641
* ```
642642
*/
643643
createSocketConnection(): NashSocketEvents {
644+
let _publicSocket: any = null
645+
const getSocket = () => {
646+
if (!_publicSocket) {
647+
_publicSocket = new PhoenixSocket(this.wsUri)
648+
_publicSocket.connect()
649+
_publicSocket.disconnect = (c, code, reason) => {
650+
if (!_publicSocket.conn) {
651+
if (c) {
652+
c()
653+
}
654+
return
655+
}
656+
657+
// https://github.com/mcampa/phoenix-channels/blob/master/src/socket.js#L137
658+
_publicSocket.conn.onclose = event => {
659+
_publicSocket.log('transport', 'close', event)
660+
// https://github.com/mcampa/phoenix-channels/blob/master/src/constants.js#L14
661+
_publicSocket.channels.forEach(channel =>
662+
channel.trigger('phx_close')
663+
)
664+
clearInterval(_publicSocket.heartbeatTimer)
665+
_publicSocket.stateChangeCallbacks.close.forEach(callback =>
666+
callback(event)
667+
)
668+
}
669+
670+
if (code) {
671+
_publicSocket.conn.close(code, reason || '')
672+
} else {
673+
_publicSocket.conn.close()
674+
}
675+
_publicSocket.conn = null
676+
if (c) {
677+
c()
678+
}
679+
}
680+
}
681+
return _publicSocket
682+
}
644683
if (this.wsUri == null) {
645684
throw new Error('wsUri config parameter missing')
646685
}
@@ -664,6 +703,9 @@ export class Client {
664703
return
665704
}
666705
disconnected = true
706+
if (_publicSocket) {
707+
_publicSocket.disconnect()
708+
}
667709
socket.disconnect()
668710
},
669711
onUpdatedAccountOrders: async (payload, handlers) => {
@@ -709,14 +751,33 @@ export class Client {
709751
)
710752
},
711753
onUpdatedOrderbook: (variables, handlers) => {
712-
AbsintheSocket.observe(
713-
absintheSocket,
714-
AbsintheSocket.send(absintheSocket, {
715-
operation: gqlToString(UPDATED_ORDER_BOOK),
716-
variables
717-
}),
718-
handlers
754+
const publicSocket = getSocket()
755+
const channel = publicSocket.channel(
756+
'public_order_book:' + variables.marketName,
757+
{}
719758
)
759+
760+
channel
761+
.join()
762+
.receive('ok', initial => {
763+
handlers.onStart(initial)
764+
if (handlers.onResult) {
765+
handlers.onResult(initial)
766+
}
767+
channel.on('update', update => {
768+
if (handlers.onResult) {
769+
handlers.onResult(update)
770+
}
771+
})
772+
})
773+
.receive('error', resp => {
774+
if (handlers.onAbort) {
775+
handlers.onAbort(resp)
776+
}
777+
if (handlers.onError) {
778+
handlers.onError(resp)
779+
}
780+
})
720781
},
721782
onAccountTrade: async (payload, handlers) => {
722783
authCheck('onAccountTrade')

0 commit comments

Comments
 (0)