SimpleChatTC:ChatMessageEx: 1st go at trying to track promises
This commit is contained in:
parent
7dbbc46390
commit
3d661793ef
|
|
@ -5,18 +5,19 @@
|
||||||
//
|
//
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Expects to get a message with identifier name and code to run
|
* Expects to get a message with id, name and code to run
|
||||||
* Posts message with identifier name and data captured from console.log outputs
|
* Posts message with id, name and data captured from console.log outputs
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
import * as tconsole from "./toolsconsole.mjs"
|
import * as tconsole from "./toolsconsole.mjs"
|
||||||
|
import * as xpromise from "./xpromise.mjs"
|
||||||
|
|
||||||
|
|
||||||
self.onmessage = function (ev) {
|
self.onmessage = async function (ev) {
|
||||||
tconsole.console_redir()
|
tconsole.console_redir()
|
||||||
try {
|
try {
|
||||||
eval(ev.data.code)
|
await xpromise.evalWithPromiseTracking(ev.data.code);
|
||||||
} catch (/** @type {any} */error) {
|
} catch (/** @type {any} */error) {
|
||||||
console.log(`\n\nTool/Function call "${ev.data.name}" raised an exception:${error.name}:${error.message}\n\n`)
|
console.log(`\n\nTool/Function call "${ev.data.name}" raised an exception:${error.name}:${error.message}\n\n`)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,46 @@
|
||||||
|
//@ts-check
|
||||||
|
// Helpers for a tracked promise land
|
||||||
|
// by Humans for All
|
||||||
|
//
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef {(resolve: (value: any) => void, reject: (reason?: any) => void) => void} PromiseExecutor
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Eval which allows promises generated by the evald code to be tracked.
|
||||||
|
* @param {string} codeToEval
|
||||||
|
*/
|
||||||
|
export function evalWithPromiseTracking(codeToEval) {
|
||||||
|
const _Promise = globalThis.Promise;
|
||||||
|
/** @type {any[]} */
|
||||||
|
const trackedPromises = [];
|
||||||
|
|
||||||
|
const Promise = function ( /** @type {PromiseExecutor} */ executor) {
|
||||||
|
const promise = new _Promise(executor);
|
||||||
|
trackedPromises.push(promise);
|
||||||
|
|
||||||
|
promise.then = function (...args) {
|
||||||
|
const newPromise = _Promise.prototype.then.apply(this, args);
|
||||||
|
trackedPromises.push(newPromise);
|
||||||
|
return newPromise;
|
||||||
|
};
|
||||||
|
|
||||||
|
promise.catch = function (...args) {
|
||||||
|
const newPromise = _Promise.prototype.catch.apply(this, args);
|
||||||
|
trackedPromises.push(newPromise);
|
||||||
|
return newPromise;
|
||||||
|
};
|
||||||
|
|
||||||
|
return promise;
|
||||||
|
};
|
||||||
|
|
||||||
|
Promise.prototype = _Promise.prototype;
|
||||||
|
Object.assign(Promise, _Promise);
|
||||||
|
|
||||||
|
eval(codeToEval);
|
||||||
|
|
||||||
|
return _Promise.all(trackedPromises);
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue