mirror of
https://github.com/serverless/serverless.git
synced 2026-01-18 14:58:43 +00:00
54 lines
1.8 KiB
JavaScript
54 lines
1.8 KiB
JavaScript
'use strict';
|
|
|
|
const logWarning = require('./Error').logWarning;
|
|
|
|
class PromiseTracker {
|
|
constructor() {
|
|
this.promiseList = [];
|
|
this.promiseMap = {};
|
|
this.startTime = Date.now();
|
|
}
|
|
start() {
|
|
this.interval = setInterval(this.report.bind(this), 2500);
|
|
}
|
|
report() {
|
|
const delta = Date.now() - this.startTime;
|
|
logWarning('################################################################################');
|
|
logWarning(`# ${delta}: ${this.getSettled().length} of ${
|
|
this.getAll().length} promises have settled`);
|
|
const pending = this.getPending();
|
|
logWarning(`# ${delta}: ${pending.length} unsettled promises:`);
|
|
pending.forEach((promise) => {
|
|
logWarning(`# ${delta}: ${promise.waitList}`);
|
|
});
|
|
logWarning('################################################################################');
|
|
}
|
|
stop() {
|
|
clearInterval(this.interval);
|
|
}
|
|
add(variable, prms, specifier) {
|
|
const promise = prms;
|
|
promise.waitList = `${variable} waited on by: ${specifier}`;
|
|
promise.state = 'pending';
|
|
promise.then( // creates a promise with the following effects but that we otherwise ignore
|
|
() => { promise.state = 'resolved'; },
|
|
() => { promise.state = 'rejected'; });
|
|
this.promiseList.push(promise);
|
|
this.promiseMap[variable] = promise;
|
|
return promise;
|
|
}
|
|
contains(variable) {
|
|
return variable in this.promiseMap;
|
|
}
|
|
get(variable, specifier) {
|
|
const promise = this.promiseMap[variable];
|
|
promise.waitList += ` ${specifier}`;
|
|
return promise;
|
|
}
|
|
getPending() { return this.promiseList.filter(p => (p.state === 'pending')); }
|
|
getSettled() { return this.promiseList.filter(p => (p.state !== 'pending')); }
|
|
getAll() { return this.promiseList; }
|
|
}
|
|
|
|
module.exports = PromiseTracker;
|