early/ts/index.ts
2023-08-22 20:37:45 +02:00

43 lines
1.2 KiB
TypeScript

import * as consolecolor from '@push.rocks/consolecolor';
import * as smartpromise from '@push.rocks/smartpromise';
import { HrtMeasurement } from './early.hrtMeasurement.js';
export { HrtMeasurement };
let doText: boolean = false;
let moduleName: string = 'undefined module name';
let startHrt: HrtMeasurement;
if (process.argv.indexOf('-v') === -1) {
doText = true;
}
/**
* start the loading
*/
export let start = function (moduleNameArg: string = '', loaderLengthArg: string = '10') {
moduleName = moduleNameArg;
startHrt = new HrtMeasurement();
startHrt.start();
if (doText && process.env.CLI_CALL_MODULENAME === moduleName) {
console.log(`**** starting ${consolecolor.coloredString(moduleNameArg, 'green')} ****`);
}
};
export let stop = (): Promise<number> => {
let done = smartpromise.defer<number>();
let earlyExecutionTime = startHrt.stop().milliSeconds;
let earlyExecutionTimeString: string = (earlyExecutionTime / 1000).toString();
if (doText && process.env.CLI_CALL_MODULENAME === moduleName) {
console.log(
`OK! -> finished loading within ${consolecolor.coloredString(
earlyExecutionTimeString,
'blue'
)}`
);
}
done.resolve(earlyExecutionTime);
return done.promise;
};