import { LocoPacket , LocoPacketDataCodec } from '../packet' ; /**
 * Construct LocoPacket object from packet data.
 * Deconstruct LocoPacket into packet data.
 */ export class PacketAssembler < T , R > { private _currentId : number ; private _dataCodec : LocoPacketDataCodec < T , R > ; constructor ( dataCodec ) { this . _currentId = 1 ; this . _dataCodec = dataCodec ; } param { string } method param { T } data return { LocoPacket } construct ( method , data ) { const packetData = this . _dataCodec . encode ( data ) ; return { header : { id : ( this . _currentId = ( this . _currentId + 1 ) % 100000 ) , method , status : 0 , } , data : packetData , } ; } template R param { LocoPacket } packet return { R } deconstruct ( packet ) { if ( ! this . _dataCodec . canDecode ( packet . data [ 0 ] ) ) throw new Error ( `Cannot decode dataType ${ packet . data [ 0 ] }` ) ; return this . _dataCodec . decode ( packet . data [ 1 ] ) ; } }