From 54dc7bf6c344110c442e23dca8591ce2c4e36d22 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Mon, 19 Sep 2022 17:22:41 -0500 Subject: [PATCH] fix (webapp): Add method without 'set_' prefix in generated js We already do this for 'get_' prefixes --- src/web/api/src/DynamicService.ts | 35 ++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/src/web/api/src/DynamicService.ts b/src/web/api/src/DynamicService.ts index 7c9c4f924..37a88d664 100644 --- a/src/web/api/src/DynamicService.ts +++ b/src/web/api/src/DynamicService.ts @@ -95,18 +95,33 @@ export class DynamicService { streams : Array<[string,number,string]>) { const svc = { context, name, qualifier } + // HACK ALERT: Method prefixes 'get_' and 'set_' seem to be ignored by fable bindings. This + // was observed in IAdminService.get_filtered_admin_data, IAdminService.get_admin_data, and + // IMarginService.set_cash_factor. Calls to those methods (from fsharp) failed in javascript + // due to undefined methods 'filtered_admin_data', 'admin_data', and 'cash_factor' + // respectively. Adding the methods with the prefix removed seems to fix the problem. + // + // WARNING: This hack assumes services won't have methods get_ and set_ (if both + // are present, one of them will be overwritten). + // + // TODO: Replace this hack with a stable solution. + let strip_prefix = (prefix:string) => { + if (!methodName.startsWith(prefix)) { + return false + } + let without_prefix = methodName.substring(prefix.length) + if (without_prefix in this) { + throw new Error( + `The method '${without_prefix}' has already been defined. Services cannot have ` + + `methods named get_ and set_ due to a hack in our js generation impl`); + } + this[without_prefix] = this[methodName] + return true + } + for (var [methodName, id, inType, outType] of methods) { this[methodName] = gen_method(svc, id, inType, outType) - // HACK ALERT: the 'get_' prefix seems to be ignored by the fable binding. This was - // observed specifically in IAdminService.get_filtered_admin_data, and - // IAdminService.get_admin_data. Calls to those methods (from fsharp) failed in - // javascript due to undefined methods 'filtered_admin_data' and 'admin_data' - // respectively. Adding the methods with the prefix removed seems to fix the problem. - // TODO: replace this precarious fix (aka hack) with a stable solution. - const getter_prefix = "get_" - if (methodName.startsWith(getter_prefix)) { - this[methodName.substring(getter_prefix.length)] = this[methodName] - } + strip_prefix('get_') || strip_prefix('set_') } for (var [streamName,id,typeName] of streams) { const fnName = "subscribe_" + streamName;