da738ffa8c6c373703432217c7648fdfcc7e3fa3
[iotcloud.git] / src / js / iotjs / orig / slotindexer.js
1 class SlotIndexer{
2         constructor(_updates,_buffer){
3                 // updates is an array of slot objects
4                 // buffer is an instanceof slotbuffer constructor object in slotbuffer.js
5                 this.updates = _updates;
6                 if(_buffer && _buffer instanceof SlotBuffer){
7                         this.buffer = _buffer;
8                 }else{
9                         throw new Error("Argument error Buffer should be an instance of SlotBuffer");
10                 }
11                 this.firstslotseqnum = this.updates[0].getSequenceNumber();     
12         }
13
14         getSlot(seqnum){
15                 if(seqnum >= this.firstslotseqnum){
16                         var offset = seqnum - this.firstslotseqnum;
17                         if(offset >= this.updates.length){
18                                 throw new Error('Invalid Slot Sequence Number Reference');
19                         }else{
20                                 return this.updates[offset];
21                         }
22                 }else{
23                         return this.buffer.getSlot(seqnum);
24                 }
25         }
26
27 }