mirror of
https://github.com/wavetermdev/wails.git
synced 2026-08-05 13:53:43 -07:00
[runtime] Add cancelation of bound method calls and context passing
This commit is contained in:
@@ -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();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user