[runtime] Add cancelation of bound method calls and context passing

This commit is contained in:
stffabi
2024-03-04 10:34:14 +01:00
parent 7e687750b2
commit bfe9a9d015
25 changed files with 319 additions and 124 deletions
+1
View File
@@ -35,6 +35,7 @@ tasks:
- build:production
cmds:
- cmd: wails3 tool cp ../commands/build_assets/runtime/runtime.js ../../examples/binding/assets/runtime.js
- cmd: wails3 tool cp ../commands/build_assets/runtime/runtime.js ../../examples/frameless/assets/runtime.js
- cmd: wails3 tool cp ../commands/build_assets/runtime/runtime.js ../../examples/window-api/assets/runtime.js
- cmd: wails3 tool cp ../commands/build_assets/runtime/runtime.js ../../examples/wml/assets/runtime.js
@@ -20,6 +20,7 @@ window._wails.callErrorHandler = errorHandler;
const CallBinding = 0;
const call = newRuntimeCallerWithID(objectNames.Call, '');
const cancelCall = newRuntimeCallerWithID(objectNames.CancelCall, '');
let callResponses = new Map();
/**
@@ -84,18 +85,36 @@ function getAndDeleteResponse(id) {
*
* @param {string|number} type - The type of call to execute.
* @param {Object} [options={}] - Additional options for the call.
* @return {Promise} - A promise that will be resolved or rejected based on the result of the call.
* @return {Promise} - A promise that will be resolved or rejected based on the result of the call. It also has a cancel method to cancel a long running request.
*/
function callBinding(type, options = {}) {
return new Promise((resolve, reject) => {
const id = generateID();
const id = generateID();
const doCancel = () => { cancelCall(type, {"call-id": id}) };
var queuedCancel = false, callRunning = false;
var p = new Promise((resolve, reject) => {
options["call-id"] = id;
callResponses.set(id, { resolve, reject });
call(type, options).catch((error) => {
reject(error);
callResponses.delete(id);
});
call(type, options).
then((_) => {
callRunning = true;
if (queuedCancel) {
doCancel();
}
}).
catch((error) => {
reject(error);
callResponses.delete(id);
});
});
p.cancel = () => {
if (callRunning) {
doCancel();
} else {
queuedCancel = true;
}
};
return p;
}
/**
@@ -25,6 +25,7 @@ export const objectNames = {
Screens: 7,
System: 8,
Browser: 9,
CancelCall: 10,
}
export let clientId = nanoid();