round-robin-js
An implementation of the round robin as a data structure. The following strategies are implemented:
SequentialRoundRobin | selects the next item based on the order of insertion |
RandomRoundRobin | selects the next item randomly |
PriorityRoundRobin | selects the next item based on its priority |
Contents
Install
npm install --save round-robin-js
require
const {
SequentialRoundRobin,
RandomRoundRobin,
PriorityRoundRobin
} = require('round-robin-js');
import
import {
SequentialRoundRobin,
RandomRoundRobin,
PriorityRoundRobin,
RoundRobinItem
} from 'round-robin-js';
API
constructor
All types accept an initial list of values. PriorityRoundRobin requires a compare function to select next item based on priority.
JS
const cpusTable = new SequentialRoundRobin([1, 2, 3]);
const rockPaperScissors = new RandomRoundRobin(['Rock', 'Paper', 'Scissors']);
const availableServers = new PriorityRoundRobin(
(a, b) => a.load - b.load,
[{ hostname: 's1.test.com', load: 40 }, { hostname: 's2.test.com', load: 30 }]
);
TS
const cpusTable = new SequentialRoundRobin<number>([1, 2, 3]);
const rockPaperScissors = new RandomRoundRobin<string>(['Rock', 'Paper', 'Scissors']);
interface IServer {
hostname: string;
load: number;
}
const availableServers = new PriorityRoundRobin<IServer>(
(a: IServer, b: IServer) => a.load - b.load,
[{ hostname: 's1.test.com', load: 40 }, { hostname: 's2.test.com', load: 30 }]
);
add
adds a new item to the table.
cpusTable.add(4);
cpusTable.add(5);
availableServers.add({ hostname: 's3.test.com', load: 15 });
availableServers.add({ hostname: 's4.test.com', load: 60 });
next
selects and returns the next item in the round.
cpusTable.next();
cpusTable.next();
cpusTable.next();
cpusTable.next();
cpusTable.next();
cpusTable.next();
rockPaperScissors.next();
rockPaperScissors.next();
rockPaperScissors.next();
rockPaperScissors.next();
availableServers.next();
availableServers.next();
availableServers.next();
availableServers.next();
availableServers.next();
count
returns the number of items in the table.
cpusTable.count();
rockPaperScissors.count();
availableServers.count();
deleteByKey
deletes an item from the table by its key.
cpusTable.deleteByKey(1);
cpusTable.count();
availableServers.deleteByKey(2);
availableServers.count();
deleteByValue
accepts a callback to delete items that match a criteria from the table and returns the count of deleted.
availableServers.deleteByValue((s) => s.load > 30);
availableServers.next();
availableServers.next();
reset
resets the round selection from the start.
cpusTable.next();
cpusTable.next();
cpusTable.reset();
cpusTable.next();
cpusTable.next();
availableServers.next();
availableServers.add({ hostname: 's99.test.com', load: 10 });
availableServers.next();
availableServers.reset();
availableServers.next();
clear
clears all values in the table.
cpusTable.clear();
cpusTable.count();
cpusTable.next();
rockPaperScissors.clear();
rockPaperScissors.count();
rockPaperScissors.next();
availableServers.clear();
availableServers.count();
availableServers.next();
Build
grunt build
License
The MIT License. Full License is here