import { OrderbookL2Response , OrderbookResponse } from '../../../../types' ; import { WebsocketData } from '../../websocket-insider' ; param source export function transform ( source ) { const orderbook = { bids : [ ] , asks : [ ] , } ; for ( const item of source . data ) { const orderbookItem = [ `${ item . price }` , `${ item . size }` ] ; if ( item . side === 'Sell' ) { orderbook . asks . push ( orderbookItem ) ; } else { orderbook . bids . push ( orderbookItem ) ; } } orderbook . asks . reverse ( ) ; return orderbook ; } export function update ( originSource , updateSource ) { const originData = originSource . data ; const updateData = updateSource . data ; originSource . data = originData ; let currentPosition = 0 ; for ( const updateItem of updateData ) { const position = findPosition ( originData , updateItem , currentPosition ) ; if ( originData [ position ] && originData [ position ] . id === updateItem . id ) { if ( updateSource . action === 'update' ) { originData [ position ] . size = updateItem . size ; currentPosition = position + 1 ; } else if ( updateSource . action === 'delete' ) { originData . splice ( position , 1 ) ; currentPosition = position ; } } else if ( updateSource . action === 'insert' ) { originData . splice ( position , 0 , updateItem ) ; currentPosition = position + 1 ; } } return originSource ; } param originSource param updateSource param fromId function findPosition ( originSource , updateSource , fromId ) { let i = fromId ; for ( ; i < originSource . length ; i ++ ) { if ( originSource [ i ] . id >= updateSource . id ) { break ; } } return i ; }