Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 44 additions & 10 deletions src/components/renderer/form-button.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
:disabled="showSpinner"
>
<b-spinner v-if="showSpinner" small></b-spinner>
{{ showSpinner ? (!loadingLabel ? 'Loading...': loadingLabel) : label }}
{{ showSpinner ? (!loadingLabel ? "Loading..." : loadingLabel) : label }}
</button>
</div>
</template>
Expand All @@ -19,17 +19,31 @@
import Mustache from 'mustache';
import { mapActions, mapState } from "vuex";
import { getValidPath } from '@/mixins';
import Worker from "@/workers/worker.js?worker&inline";

export default {
mixins: [getValidPath],
props: ['variant', 'label', 'event', 'eventData', 'name', 'fieldValue', 'value', 'tooltip', 'transientData', 'loading', 'loadingLabel', 'handler'],
props: [
"variant",
"label",
"event",
"eventData",
"name",
"fieldValue",
"value",
"tooltip",
"transientData",
"loading",
"loadingLabel",
"handler"
],
data() {
return {
showSpinner: false
};
},
computed: {
...mapState('globalErrorsModule', ['valid']),
...mapState("globalErrorsModule", ["valid"]),
classList() {
let variant = this.variant || 'primary';
return {
Expand Down Expand Up @@ -106,15 +120,35 @@ export default {
async runHandler() {
if (this.handler) {
try {
const data = this.getScreenDataReference(null, (screen, name, value) => {
// Enable the data reference to be updated by the handler
screen.$set(screen.vdata, name, value);
const data = this.getScreenDataReference(
null,
(screen, name, value) => {
// Enable the data reference to be updated by the handler
screen.$set(screen.vdata, name, value);
}
);

const rawData = data[Symbol.for("__v_raw")];

const worker = new Worker();
worker.postMessage({
fn: this.handler,
data: rawData,
});
await new Function(['toRaw'], this.handler).apply(data, [(item) => {
return item[Symbol.for('__v_raw')];
}]);

worker.onmessage = (e) => {
if (e.data.error) {
console.error("Worker error:", e.data.error);
} else if (e.data.result) {

// Update the data with the result
Object.keys(e.data.result).forEach(key => {
rawData[key] = e.data.result[key];
});
}
};
} catch (error) {
console.error('❌ There is an error in the button handler', error);
console.error("❌ There is an error in the button handler", error);
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions src/workers/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// worker.js
self.onmessage = function (e) {
const { fn, data } = e.data;

try {
// Execute the handler as a function with data as the context
const func = new Function("data", fn);
// const func = new Function('data', `return (${fn})(data)`)
const result = func(data);

self.postMessage({ result });
} catch (error) {
console.error("Error executing handler:", error);
self.postMessage({ error: error.message });
}
};
6 changes: 3 additions & 3 deletions tests/e2e/fixtures/button_click_handler.json
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@
"name": "button_with_handler",
"fieldValue": null,
"tooltip": {},
"handler": "this.form_input_2=\"value changed by handler\";"
"handler": "return {\n form_input_2: \"value changed by handler\",\n}"
},
"inspector": [
{
Expand Down Expand Up @@ -1392,7 +1392,7 @@
"name": "record_list_button_with_handler",
"fieldValue": null,
"tooltip": {},
"handler": "this.form_input_1=\"value changed by handler\";console.log('handler executed')"
"handler": "console.log('handler executed');\nreturn {\n form_input_1: \"value changed by handler\",\n}"
},
"inspector": [
{
Expand Down Expand Up @@ -1818,4 +1818,4 @@
],
"screen_categories": [],
"scripts": []
}
}