Skip to content

Commit

Permalink
Add events, throw rather than return errors
Browse files Browse the repository at this point in the history
  • Loading branch information
erayd committed Jan 1, 2020
1 parent e7a51b7 commit c4813e8
Show file tree
Hide file tree
Showing 6 changed files with 286 additions and 209 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/node_modules
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,13 @@ unlimited, then maxItems will be zero.
#### .ringSize
The size of each circular buffer. The queue will grow / shrink by this many items
at a time.

### Events
#### ready
The queue has one or more items stored in it.

#### full
The queue is full.

#### empty
The queue is empty.
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "spique",
"version": "1.0.8",
"version": "2.0.0",
"description": "A spiral deque - high performance and dynamic queue size",
"main": "spique.js",
"scripts": {
Expand All @@ -25,5 +25,8 @@
"bugs": {
"url": "https://github.com/erayd/spique/issues"
},
"homepage": "https://github.com/erayd/spique#readme"
"homepage": "https://github.com/erayd/spique#readme",
"dependencies": {
"events": "^3.0.0"
}
}
113 changes: 113 additions & 0 deletions ringbuffer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/* ISC License
*
* Copyright (c) 2016-2020, Erayd LTD
*
* Permission to use, copy, modify, and/or distribute this software for any purpose
* with or without fee is hereby granted, provided that the above copyright notice
* and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
* OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/


"use strict";
const events = require("events");

module.exports = class Ringbuffer extends events.EventEmitter {
constructor(size) {
super();
var head = 0;
var items = 0;
var buffer = new Array(size);

// check whether the buffer is empty
this.isEmpty = function() {
return !items;
}

// get the number of free slots
this.available = function() {
return size - items;
}

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

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

// pop an item off the end of the buffer
this.pop = function() {
if(this.isEmpty())
return undefined;
var pos = (head + --items) % size;
var value = buffer[pos];
buffer[pos] = undefined;
if (this.isEmpty())
this.emit("empty", this);
return value;
};

// pop an item off the start of the buffer
this.shift = function() {
if(this.isEmpty())
return undefined;
var value = buffer[head];
buffer[head] = undefined;
if(++head == size)
head = 0;
items--;
if (this.isEmpty())
this.emit("empty", this);
return value;
};

// peek at the end of the buffer
this.peek = function() {
if(this.isEmpty())
return undefined;
return buffer[(head + (items - 1)) % size];
};

// peek at the start of the buffer
this.peekStart = function() {
if(this.isEmpty())
return undefined;
return buffer[head];
};

// get the number of items in the buffer
Object.defineProperty(this, 'length', {get: function() {
return items;
}});
}
}

Loading

0 comments on commit c4813e8

Please sign in to comment.