2025-01-22 16:18:30 +01:00

3 lines
16 KiB
JSON

{
"/mnt/d/Projecten/Promise-parallel-throttle/src/throttle.ts":{"path":"/mnt/d/Projecten/Promise-parallel-throttle/src/throttle.ts","statementMap":{"1":{"start":{"line":1,"column":20},"end":{"line":1,"column":21}},"2":{"start":{"line":35,"column":44},"end":{"line":39,"column":1}},"3":{"start":{"line":36,"column":4},"end":{"line":38,"column":7}},"4":{"start":{"line":37,"column":8},"end":{"line":37,"column":53}},"5":{"start":{"line":41,"column":24},"end":{"line":45,"column":1}},"6":{"start":{"line":58,"column":4},"end":{"line":144,"column":7}},"7":{"start":{"line":59,"column":26},"end":{"line":59,"column":69}},"8":{"start":{"line":60,"column":34},"end":{"line":70,"column":9}},"9":{"start":{"line":72,"column":8},"end":{"line":74,"column":9}},"10":{"start":{"line":73,"column":12},"end":{"line":73,"column":35}},"11":{"start":{"line":76,"column":25},"end":{"line":76,"column":30}},"12":{"start":{"line":77,"column":31},"end":{"line":77,"column":32}},"13":{"start":{"line":78,"column":28},"end":{"line":106,"column":9}},"14":{"start":{"line":79,"column":12},"end":{"line":79,"column":35}},"15":{"start":{"line":81,"column":12},"end":{"line":105,"column":13}},"16":{"start":{"line":82,"column":16},"end":{"line":99,"column":18}},"17":{"start":{"line":84,"column":24},"end":{"line":84,"column":63}},"18":{"start":{"line":85,"column":24},"end":{"line":85,"column":59}},"19":{"start":{"line":86,"column":24},"end":{"line":86,"column":48}},"20":{"start":{"line":87,"column":24},"end":{"line":87,"column":35}},"21":{"start":{"line":90,"column":24},"end":{"line":90,"column":58}},"22":{"start":{"line":91,"column":24},"end":{"line":91,"column":59}},"23":{"start":{"line":92,"column":24},"end":{"line":92,"column":48}},"24":{"start":{"line":93,"column":24},"end":{"line":96,"column":25}},"25":{"start":{"line":94,"column":28},"end":{"line":94,"column":46}},"26":{"start":{"line":95,"column":28},"end":{"line":95,"column":50}},"27":{"start":{"line":97,"column":24},"end":{"line":97,"column":35}},"28":{"start":{"line":101,"column":16},"end":{"line":101,"column":34}},"29":{"start":{"line":102,"column":16},"end":{"line":104,"column":18}},"30":{"start":{"line":108,"column":25},"end":{"line":124,"column":9}},"31":{"start":{"line":110,"column":12},"end":{"line":112,"column":13}},"32":{"start":{"line":111,"column":16},"end":{"line":111,"column":23}},"33":{"start":{"line":114,"column":12},"end":{"line":114,"column":32}},"34":{"start":{"line":115,"column":12},"end":{"line":117,"column":13}},"35":{"start":{"line":116,"column":17},"end":{"line":116,"column":60}},"36":{"start":{"line":118,"column":12},"end":{"line":120,"column":13}},"37":{"start":{"line":119,"column":16},"end":{"line":119,"column":39}},"38":{"start":{"line":121,"column":12},"end":{"line":123,"column":13}},"39":{"start":{"line":122,"column":16},"end":{"line":122,"column":45}},"40":{"start":{"line":126,"column":25},"end":{"line":138,"column":9}},"41":{"start":{"line":128,"column":12},"end":{"line":137,"column":23}},"42":{"start":{"line":129,"column":16},"end":{"line":136,"column":17}},"43":{"start":{"line":131,"column":20},"end":{"line":131,"column":39}},"44":{"start":{"line":133,"column":20},"end":{"line":133,"column":51}},"45":{"start":{"line":134,"column":20},"end":{"line":134,"column":62}},"46":{"start":{"line":135,"column":20},"end":{"line":135,"column":31}},"47":{"start":{"line":141,"column":8},"end":{"line":143,"column":9}},"48":{"start":{"line":142,"column":12},"end":{"line":142,"column":41}},"49":{"start":{"line":57,"column":0},"end":{"line":145,"column":1}},"50":{"start":{"line":154,"column":4},"end":{"line":167,"column":7}},"51":{"start":{"line":155,"column":8},"end":{"line":166,"column":10}},"52":{"start":{"line":157,"column":16},"end":{"line":157,"column":44}},"53":{"start":{"line":160,"column":16},"end":{"line":164,"column":17}},"54":{"start":{"line":161,"column":20},"end":{"line":161,"column":34}},"55":{"start":{"line":163,"column":20},"end":{"line":163,"column":72}},"56":{"start":{"line":177,"column":22},"end":{"line":177,"column":86}},"57":{"start":{"line":178,"column":4},"end":{"line":178,"column":40}},"58":{"start":{"line":176,"column":0},"end":{"line":179,"column":1}},"59":{"start":{"line":188,"column":22},"end":{"line":188,"column":68}},"60":{"start":{"line":189,"column":4},"end":{"line":189,"column":40}},"61":{"start":{"line":187,"column":0},"end":{"line":190,"column":1}}},"fnMap":{"1":{"name":"(anonymous_0)","line":35,"loc":{"start":{"line":35,"column":82},"end":{"line":39,"column":1}}},"2":{"name":"(anonymous_1)","line":36,"loc":{"start":{"line":36,"column":39},"end":{"line":38,"column":5}}},"3":{"name":"raw","line":57,"loc":{"start":{"line":57,"column":57},"end":{"line":145,"column":1}}},"4":{"name":"(anonymous_3)","line":58,"loc":{"start":{"line":58,"column":39},"end":{"line":144,"column":5}}},"5":{"name":"(anonymous_4)","line":78,"loc":{"start":{"line":78,"column":42},"end":{"line":106,"column":9}}},"6":{"name":"(anonymous_5)","line":83,"loc":{"start":{"line":83,"column":30},"end":{"line":88,"column":21}}},"7":{"name":"(anonymous_6)","line":89,"loc":{"start":{"line":89,"column":25},"end":{"line":98,"column":21}}},"8":{"name":"(anonymous_7)","line":108,"loc":{"start":{"line":108,"column":25},"end":{"line":124,"column":9}}},"9":{"name":"(anonymous_8)","line":126,"loc":{"start":{"line":126,"column":39},"end":{"line":138,"column":9}}},"10":{"name":"(anonymous_9)","line":128,"loc":{"start":{"line":128,"column":70},"end":{"line":137,"column":13}}},"11":{"name":"executeRaw","line":153,"loc":{"start":{"line":153,"column":56},"end":{"line":168,"column":1}}},"12":{"name":"(anonymous_11)","line":154,"loc":{"start":{"line":154,"column":39},"end":{"line":167,"column":5}}},"13":{"name":"(anonymous_12)","line":156,"loc":{"start":{"line":156,"column":30},"end":{"line":158,"column":13}}},"14":{"name":"(anonymous_13)","line":159,"loc":{"start":{"line":159,"column":37},"end":{"line":165,"column":13}}},"15":{"name":"sync","line":176,"loc":{"start":{"line":176,"column":58},"end":{"line":179,"column":1}}},"16":{"name":"all","line":187,"loc":{"start":{"line":187,"column":57},"end":{"line":190,"column":1}}}},"branchMap":{"1":{"line":72,"type":"if","locations":[{"start":{"line":72,"column":8},"end":{"line":74,"column":9}},{"start":{"line":72,"column":8},"end":{"line":74,"column":9}}]},"2":{"line":81,"type":"if","locations":[{"start":{"line":81,"column":12},"end":{"line":105,"column":13}},{"start":{"line":81,"column":12},"end":{"line":105,"column":13}}]},"3":{"line":93,"type":"if","locations":[{"start":{"line":93,"column":24},"end":{"line":96,"column":25}},{"start":{"line":93,"column":24},"end":{"line":96,"column":25}}]},"4":{"line":110,"type":"if","locations":[{"start":{"line":110,"column":12},"end":{"line":112,"column":13}},{"start":{"line":110,"column":12},"end":{"line":112,"column":13}}]},"5":{"line":115,"type":"if","locations":[{"start":{"line":115,"column":12},"end":{"line":117,"column":13}},{"start":{"line":115,"column":12},"end":{"line":117,"column":13}}]},"6":{"line":118,"type":"if","locations":[{"start":{"line":118,"column":12},"end":{"line":120,"column":13}},{"start":{"line":118,"column":12},"end":{"line":120,"column":13}}]},"7":{"line":121,"type":"if","locations":[{"start":{"line":121,"column":12},"end":{"line":123,"column":13}},{"start":{"line":121,"column":12},"end":{"line":123,"column":13}}]},"8":{"line":129,"type":"if","locations":[{"start":{"line":129,"column":16},"end":{"line":136,"column":17}},{"start":{"line":129,"column":16},"end":{"line":136,"column":17}}]},"9":{"line":160,"type":"if","locations":[{"start":{"line":160,"column":16},"end":{"line":164,"column":17}},{"start":{"line":160,"column":16},"end":{"line":164,"column":17}}]}},"s":{"1":1,"2":1,"3":71,"4":71,"5":1,"6":19,"7":19,"8":19,"9":19,"10":1,"11":18,"12":18,"13":18,"14":81,"15":81,"16":78,"17":56,"18":56,"19":56,"20":56,"21":22,"22":22,"23":22,"24":22,"25":7,"26":7,"27":15,"28":3,"29":3,"30":18,"31":75,"32":8,"33":67,"34":67,"35":5,"36":67,"37":11,"38":56,"39":26,"40":18,"41":86,"42":85,"43":81,"44":4,"45":4,"46":4,"47":18,"48":60,"49":1,"50":8,"51":8,"52":4,"53":4,"54":2,"55":2,"56":4,"57":4,"58":1,"59":4,"60":4,"61":1},"b":{"1":[1,18],"2":[78,3],"3":[7,15],"4":[8,67],"5":[5,62],"6":[11,56],"7":[26,30],"8":[81,4],"9":[2,2]},"f":{"1":71,"2":71,"3":19,"4":19,"5":81,"6":56,"7":22,"8":75,"9":86,"10":85,"11":8,"12":8,"13":4,"14":4,"15":4,"16":4},"code":["const DEFAULT_MAX = 5;\r","\r","export interface Result<T> {\r"," amountDone: number;\r"," amountStarted: number;\r"," amountResolved: number;\r"," amountRejected: number;\r"," amountNextCheckFalsey: number;\r"," rejectedIndexes: number[];\r"," resolvedIndexes: number[];\r"," nextCheckFalseyIndexes: number[];\r"," taskResults: T[];\r","}\r","\r","export interface Options {\r"," maxInProgress?: number;\r"," failFast?: boolean;\r"," progressCallback?: <T>(result: Result<T>) => void;\r"," nextCheck?: nextTaskCheck;\r","}\r","\r","/**\r"," * Default checker which validates if a next task should begin.\r"," * This can be overwritten to write own checks for example checking the amount\r"," * of used ram and waiting till the ram is low enough for a next task.\r"," *\r"," * It should always resolve with a boolean, either `true` to start a next task\r"," * or `false` to stop executing a new task.\r"," *\r"," * If this method rejects, the error will propagate to the caller\r"," * @param status\r"," * @param tasks\r"," * @returns {Promise}\r"," */\r","const defaultNextTaskCheck: nextTaskCheck = <T>(status: Result<T>, tasks: Tasks<T>): Promise<boolean> => {\r"," return new Promise((resolve, reject) => {\r"," resolve(status.amountStarted < tasks.length);\r"," });\r","};\r","\r","const DEFAULT_OPTIONS = {\r"," maxInProgress: DEFAULT_MAX,\r"," failFast: false,\r"," nextCheck: defaultNextTaskCheck\r","};\r","\r","export type Task<T> = () => Promise<T>;\r","export type Tasks<T> = Array<Task<T>>;\r","export type nextTaskCheck = <T>(status: Result<T>, tasks: Tasks<T>) => Promise<boolean>;\r","\r","/**\r"," * Raw throttle function, which can return extra meta data.\r"," * @param tasks required array of tasks to be executed\r"," * @param options Options object\r"," * @returns {Promise}\r"," */\r","export function raw<T>(tasks: Tasks<T>, options?: Options): Promise<Result<T>> {\r"," return new Promise((resolve, reject) => {\r"," const myOptions = Object.assign({}, DEFAULT_OPTIONS, options);\r"," const result: Result<T> = {\r"," amountDone: 0,\r"," amountStarted: 0,\r"," amountResolved: 0,\r"," amountRejected: 0,\r"," amountNextCheckFalsey: 0,\r"," rejectedIndexes: [],\r"," resolvedIndexes: [],\r"," nextCheckFalseyIndexes: [],\r"," taskResults: []\r"," };\r","\r"," if (tasks.length === 0) {\r"," return resolve(result);\r"," }\r","\r"," let failedFast = false;\r"," let currentTaskIndex = 0;\r"," const executeTask = (index: number) => {\r"," result.amountStarted++;\r","\r"," if (typeof tasks[index] === 'function') {\r"," tasks[index]().then(\r"," taskResult => {\r"," result.taskResults[index] = taskResult;\r"," result.resolvedIndexes.push(index);\r"," result.amountResolved++;\r"," taskDone();\r"," },\r"," error => {\r"," result.taskResults[index] = error;\r"," result.rejectedIndexes.push(index);\r"," result.amountRejected++;\r"," if (myOptions.failFast === true) {\r"," failedFast = true;\r"," return reject(result);\r"," }\r"," taskDone();\r"," }\r"," );\r"," } else {\r"," failedFast = true;\r"," return reject(\r"," new Error('tasks[' + index + ']: ' + tasks[index] + ', is supposed to be of type function')\r"," );\r"," }\r"," };\r","\r"," const taskDone = () => {\r"," //make sure no more tasks are spawned when we failedFast\r"," if (failedFast === true) {\r"," return;\r"," }\r","\r"," result.amountDone++;\r"," if (typeof (myOptions as Options).progressCallback === 'function') {\r"," (myOptions as any).progressCallback(result);\r"," }\r"," if (result.amountDone === tasks.length) {\r"," return resolve(result);\r"," }\r"," if (currentTaskIndex < tasks.length) {\r"," nextTask(currentTaskIndex++);\r"," }\r"," };\r","\r"," const nextTask = (index: number) => {\r"," //check if we can execute the next task\r"," myOptions.nextCheck(result, tasks).then(canExecuteNextTask => {\r"," if (canExecuteNextTask === true) {\r"," //execute it\r"," executeTask(index);\r"," } else {\r"," result.amountNextCheckFalsey++;\r"," result.nextCheckFalseyIndexes.push(index);\r"," taskDone();\r"," }\r"," }, reject);\r"," };\r","\r"," //spawn the first X task\r"," for (let i = 0; i < Math.min(myOptions.maxInProgress, tasks.length); i++) {\r"," nextTask(currentTaskIndex++);\r"," }\r"," });\r","}\r","\r","/**\r"," * Executes the raw function, but only return the task array\r"," * @param tasks \r"," * @param options \r"," * @returns {Promise}\r"," */\r","function executeRaw<T>(tasks: Tasks<T>, options: Options): Promise<T[]> {\r"," return new Promise((resolve, reject) => {\r"," raw(tasks, options).then(\r"," (result: Result<T>) => {\r"," resolve(result.taskResults);\r"," },\r"," (error: Error | Result<T>) => {\r"," if (error instanceof Error) {\r"," reject(error);\r"," } else {\r"," reject(error.taskResults[error.rejectedIndexes[0]]);\r"," }\r"," }\r"," );\r"," });\r","}\r","\r","/**\r"," * Simply run all the promises after each other, so in synchronous manner\r"," * @param tasks required array of tasks to be executed\r"," * @param options Options object\r"," * @returns {Promise}\r"," */\r","export function sync<T>(tasks: Tasks<T>, options?: Options): Promise<T[]> {\r"," const myOptions = Object.assign({}, { maxInProgress: 1, failFast: true }, options);\r"," return executeRaw(tasks, myOptions);\r","}\r","\r","/**\r"," * Exposes the same behaviour as Promise.All(), but throttled!\r"," * @param tasks required array of tasks to be executed\r"," * @param options Options object\r"," * @returns {Promise}\r"," */\r","export function all<T>(tasks: Tasks<T>, options?: Options): Promise<T[]> {\r"," const myOptions = Object.assign({}, { failFast: true }, options);\r"," return executeRaw(tasks, myOptions);\r","}\r",""],"l":{"1":1,"35":1,"36":71,"37":71,"41":1,"57":1,"58":19,"59":19,"60":19,"72":19,"73":1,"76":18,"77":18,"78":18,"79":81,"81":81,"82":78,"84":56,"85":56,"86":56,"87":56,"90":22,"91":22,"92":22,"93":22,"94":7,"95":7,"97":15,"101":3,"102":3,"108":18,"110":75,"111":8,"114":67,"115":67,"116":5,"118":67,"119":11,"121":56,"122":26,"126":18,"128":86,"129":85,"131":81,"133":4,"134":4,"135":4,"141":18,"142":60,"154":8,"155":8,"157":4,"160":4,"161":2,"163":2,"176":1,"177":4,"178":4,"187":1,"188":4,"189":4}}}