2020-12-03 05:53:10 +03:00
|
|
|
const {isMainThread, parentPort, workerData} = require('bthreads');
|
2020-11-17 08:14:37 +03:00
|
|
|
const util = require('util');
|
|
|
|
const setTimeoutPromise = util.promisify(setTimeout);
|
|
|
|
|
|
|
|
const passTime = async (ms) => {
|
2020-12-02 11:26:15 +03:00
|
|
|
if (Number.isInteger(ms)) {
|
|
|
|
await setTimeoutPromise(ms);
|
|
|
|
} else {
|
|
|
|
await setTimeoutPromise(ms.ms);
|
|
|
|
}
|
2020-11-17 08:14:37 +03:00
|
|
|
};
|
|
|
|
|
2020-12-03 05:53:10 +03:00
|
|
|
if (isMainThread) {
|
|
|
|
module.exports = passTime;
|
|
|
|
} else {
|
|
|
|
(async () => {
|
|
|
|
await passTime(workerData.ms);
|
|
|
|
parentPort.postMessage('done');
|
|
|
|
// alternative way to signal "finished" work (not recommended)
|
|
|
|
// process.exit();
|
|
|
|
})();
|
|
|
|
}
|