Skip to content

Commit 1029a07

Browse files
committed
docco
1 parent a07eed2 commit 1029a07

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

lib/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,41 @@ module.exports = class RingBuffer{
1212
this.pointer = 0
1313
}
1414

15+
// Add a new item
1516
add (item) {
1617
this.buffer[this.pointer] = item
1718
this.pointer = ( this.size + this.pointer + 1 ) % this.size
1819
return true
1920
}
2021

22+
// Get any index
2123
get (i) {
2224
return this.buffer[i]
2325
}
2426

27+
// Last entry added, one before pointer
2528
last () {
2629
let index = ( this.size + this.pointer - 1 ) % this.size
2730
return this.buffer[index]
2831
}
2932

33+
// Chop the array at pointer forward (oldest).
34+
// Append the array from pointer back (newest).
3035
toArray(){
3136
let res = this.buffer.slice(this.pointer, this.size)
3237
if ( this.pointer > 0 )
3338
res = res.concat(this.buffer.slice(0, this.pointer))
3439
return res
3540
}
3641

42+
// Clear/reset the ring buffer
3743
clear(){
3844
this.buffer = new Array(this.size)
3945
this.pointer = 0
4046
return true
4147
}
4248

49+
// Return a write stream that uses the ring buffer (deployable-log/pino)
4350
writeStream(){
4451
if ( this.stream ) return this.stream
4552
return this.stream = new require('stream').Writable({

0 commit comments

Comments
 (0)