From 53df2e75cd2027c4d4cffe4956af5f6e93ec03b1 Mon Sep 17 00:00:00 2001 From: Florian Blasius Date: Thu, 26 Oct 2023 11:42:32 +0200 Subject: [PATCH 1/2] node: store callbacks in map --- api/node/index.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/api/node/index.ts b/api/node/index.ts index 05cb9a26193..ae175e6d807 100644 --- a/api/node/index.ts +++ b/api/node/index.ts @@ -347,6 +347,10 @@ export interface LoadFileOptions { libraryPaths?: Record } +let ComponentStore = { + callbacks: {} +}; + /** * Loads the given slint file and returns an objects that contains a functions to construct the exported * component of the slint file. @@ -425,7 +429,10 @@ export function loadFile(filePath: string, options?: LoadFileOptions) : Object { return function () { return instance!.invoke(cb, Array.from(arguments)); }; }, set(callback) { - instance!.setCallback(cb, callback); + ComponentStore.callbacks[cb] = callback; + instance!.setCallback(cb, function (args) { + ComponentStore.callbacks[cb](args); + }); }, enumerable: true, }) From 24d0e919c7e732a3d8dde8dbbf04d56a4cdffec0 Mon Sep 17 00:00:00 2001 From: Florian Blasius Date: Thu, 26 Oct 2023 11:57:53 +0200 Subject: [PATCH 2/2] use symbols on instance --- api/node/index.ts | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/api/node/index.ts b/api/node/index.ts index ae175e6d807..4d82cb24b4f 100644 --- a/api/node/index.ts +++ b/api/node/index.ts @@ -347,10 +347,6 @@ export interface LoadFileOptions { libraryPaths?: Record } -let ComponentStore = { - callbacks: {} -}; - /** * Loads the given slint file and returns an objects that contains a functions to construct the exported * component of the slint file. @@ -424,14 +420,15 @@ export function loadFile(filePath: string, options?: LoadFileOptions) : Object { }); instance!.definition().callbacks.forEach((cb) => { + let cbSymbol = Symbol("_callback-" + cb); Object.defineProperty(componentHandle, cb.replace(/-/g, '_') , { get() { return function () { return instance!.invoke(cb, Array.from(arguments)); }; }, set(callback) { - ComponentStore.callbacks[cb] = callback; + instance![cbSymbol] = callback; instance!.setCallback(cb, function (args) { - ComponentStore.callbacks[cb](args); + instance![cbSymbol](args); }); }, enumerable: true,