From 75251bb2309505c20dc7500117a17649d41412d8 Mon Sep 17 00:00:00 2001 From: Florrie Date: Mon, 25 Feb 2019 11:27:11 -0400 Subject: Metadata (in memory) --- general-util.js | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) (limited to 'general-util.js') diff --git a/general-util.js b/general-util.js index 0b9f081..708e150 100644 --- a/general-util.js +++ b/general-util.js @@ -92,3 +92,88 @@ module.exports.shuffleArray = function(array) { return workingArray } +module.exports.throttlePromise = function(maximumAtOneTime = 10) { + // Returns a function that takes a callback to create a promise and either + // runs it now, if there is an available slot, or enqueues it to be run + // later, if there is not. + + let activeCount = 0 + const queue = [] + + const execute = function(callback) { + activeCount++ + return callback().finally(() => { + activeCount-- + + if (queue.length) { + return execute(queue.shift()) + } + }) + } + + return function(callback) { + if (activeCount >= maximumAtOneTime) { + return new Promise((resolve, reject) => { + queue.push(function() { + return callback().then(resolve, reject) + }) + }) + } else { + return execute(callback) + } + } +} + +module.exports.getTimeStringsFromSec = function(curSecTotal, lenSecTotal) { + const percentVal = (100 / lenSecTotal) * curSecTotal + const percentDone = ( + (Math.trunc(percentVal * 100) / 100).toFixed(2) + '%' + ) + + const leftSecTotal = lenSecTotal - curSecTotal + let leftHour = Math.floor(leftSecTotal / 3600) + let leftMin = Math.floor((leftSecTotal - leftHour * 3600) / 60) + let leftSec = Math.floor(leftSecTotal - leftHour * 3600 - leftMin * 60) + + // Yeah, yeah, duplicate math. + let curHour = Math.floor(curSecTotal / 3600) + let curMin = Math.floor((curSecTotal - curHour * 3600) / 60) + let curSec = Math.floor(curSecTotal - curHour * 3600 - curMin * 60) + + // Wee! + let lenHour = Math.floor(lenSecTotal / 3600) + let lenMin = Math.floor((lenSecTotal - lenHour * 3600) / 60) + let lenSec = Math.floor(lenSecTotal - lenHour * 3600 - lenMin * 60) + + const pad = val => val.toString().padStart(2, '0') + curMin = pad(curMin) + curSec = pad(curSec) + lenMin = pad(lenMin) + lenSec = pad(lenSec) + leftMin = pad(leftMin) + leftSec = pad(leftSec) + + // We don't want to display hour counters if the total length is less + // than an hour. + let timeDone, timeLeft, duration + if (parseInt(lenHour) > 0) { + timeDone = `${curHour}:${curMin}:${curSec}` + timeLeft = `${leftHour}:${leftMin}:${leftSec}` + duration = `${lenHour}:${lenMin}:${lenSec}` + } else { + timeDone = `${curMin}:${curSec}` + timeLeft = `${leftMin}:${leftSec}` + duration = `${lenMin}:${lenSec}` + } + + return {percentDone, timeDone, timeLeft, duration, curSecTotal, lenSecTotal} +} + +module.exports.getTimeStrings = function({curHour, curMin, curSec, lenHour, lenMin, lenSec}) { + // Multiplication casts to numbers; addition prioritizes strings. + // Thanks, JavaScript! + const curSecTotal = (3600 * curHour) + (60 * curMin) + (1 * curSec) + const lenSecTotal = (3600 * lenHour) + (60 * lenMin) + (1 * lenSec) + + return module.exports.getTimeStringsFromSec(curSecTotal, lenSecTotal) +} -- cgit 1.3.0-6-gf8a5