Skip to content

Commit

Permalink
Return errors if attempting to push items into a full buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
erayd committed Sep 29, 2016
1 parent 075b2cd commit e852aee
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions spique.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,23 @@ function RingBuffer(size) {

// push item onto the end of the buffer
this.push = function(value) {
var pos = (head + items++) % size;
buffer[pos] = value;
if(items < size) {
var pos = (head + items++) % size;
buffer[pos] = value;
}
else
return new Error('Buffer is full');
};

// push item onto the start of the buffer
this.unshift = function(value) {
var pos = head ? --head : (head = size - 1);
buffer[pos] = value;
items++;
if(items < size) {
var pos = head ? --head : (head = size - 1);
buffer[pos] = value;
items++;
}
else
return new Error('Buffer is full');
};

// pop an item off the end of the buffer
Expand Down

0 comments on commit e852aee

Please sign in to comment.