-
Notifications
You must be signed in to change notification settings - Fork 15
/
SequenceNumber.js
53 lines (42 loc) · 1.32 KB
/
SequenceNumber.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"use strict";
/**
* 48-bit Sequence number generator
*/
var SequenceNumber = function() {
this.current = new Buffer([ 0, 0, 0, 0, 0, 0 ]);
};
/**
* Resets the generator state to return the given value as the next number
*
* @param {Number[]} value - Next sequence number.
*/
SequenceNumber.prototype.setNext = function( value ) {
// Clone the value.
value.copy( this.current );
// The next invocation will increase current value by one so we need to
// assign current as value-1.
for( var i = 5; i >= 0; i-- ) {
this.current[i] = ( this.current[i] - 1 ) & 0xff;
// If the current 8-bit value isn't 255 (0xff) after subtraction there
// was no overflow and we can break.
if( this.current[i] !== 0xff )
break;
}
};
/**
* Retrieves the next value in sequence
*
* @returns {Number[]} 48-bit value that increases by 1 with every call.
*/
SequenceNumber.prototype.next = function() {
// Increase the current value by one minding the overflow.
for( var i = 5; i >= 0; i-- ) {
this.current[i] = ( this.current[i] + 1 ) & 0xff;
// If the current value isn't 0 there was no overflow and we can break
// the iteration.
if( this.current[i] )
break;
}
return this.current;
};
module.exports = SequenceNumber;