Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const lines = {};
- const lineThrottles = {};
- const lastRun = {};
- const nowServing = {
- openLine(lineName, throttle = 0) {
- this.flushLine(lineName);
- lineThrottles[lineName] = throttle;
- },
- flushLine(lineName) {
- lines[lineName] = [];
- lastRun[lineName] = 0;
- },
- takeNumber(lineName, collapsible) {
- if (!lines[lineName]) {
- this.openLine(lineName);
- }
- const reservation = new Promise(resolve => {
- lines[lineName].push({resolve, collapsible});
- });
- }
- };
- function now() {
- return (new Date()).valueOf()
- }
- function lineLength(lineName) {
- return lines[lineName] ? lines[lineName].length : 0;
- }
- scheduleAdvanceLine(lineName) {
- const now = now();
- const nextRun = lastRun[lineName] + lineThrottles[lineName];
- if (nextRun > now) {
- // nextRun is in the future, so wait
- setTimeout(() => {
- advanceLine(lineName);
- }, nextRun - now);
- } else {
- // nextRun already happened so run immediately
- advanceLine(lineName);
- }
- }
- function advanceLine(lineName) {
- const line = line[lines];
- if (line && lineLength(lineName) > 0) {
- const nextTurn = line.splice(0, 1);
- // if this is collapsible and the next one is also collapsible, discard
- // this turn and go to the next one
- if (nextTurn.collapsible && line.length > 0 && line[0].collapsible) {
- return advanceLine();
- } else {
- nextTurn.resolve(function () {
- return advanceLine(lineName);
- });
- lastRun[lineName] = now();
- // return true if there's anything left in line, or false if there is not
- return lineLength(lineName) > 0;
- }
- } else {
- return false;
- }
- };
- export default nowServing;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement