(service: string, method: string, args: any[], noUIContext?: boolean)
| 99 | } |
| 100 | |
| 101 | function callBackendService(service: string, method: string, args: any[], noUIContext?: boolean): Promise<any> { |
| 102 | const startTs = Date.now(); |
| 103 | let uiContext: UIContext = null; |
| 104 | if (!noUIContext && globalThis.window != null) { |
| 105 | uiContext = globalStore.get(((window as any).globalAtoms as GlobalAtomsType).uiContext); |
| 106 | } |
| 107 | const waveCall: WebCallType = { |
| 108 | service: service, |
| 109 | method: method, |
| 110 | args: args, |
| 111 | uicontext: uiContext, |
| 112 | }; |
| 113 | // usp is just for debugging (easier to filter URLs) |
| 114 | const methodName = `${service}.${method}`; |
| 115 | const usp = new URLSearchParams(); |
| 116 | usp.set("service", service); |
| 117 | usp.set("method", method); |
| 118 | const webEndpoint = getWebServerEndpoint(); |
| 119 | if (webEndpoint == null) throw new Error(`cannot call ${methodName}: no web endpoint`); |
| 120 | const url = webEndpoint + "/wave/service?" + usp.toString(); |
| 121 | const fetchPromise = fetch(url, { |
| 122 | method: "POST", |
| 123 | body: JSON.stringify(waveCall), |
| 124 | }); |
| 125 | const prtn = fetchPromise |
| 126 | .then((resp) => { |
| 127 | if (!resp.ok) { |
| 128 | throw new Error(`call ${methodName} failed: ${resp.status} ${resp.statusText}`); |
| 129 | } |
| 130 | return resp.json(); |
| 131 | }) |
| 132 | .then((respData: WebReturnType) => { |
| 133 | if (respData == null) { |
| 134 | return null; |
| 135 | } |
| 136 | if (respData.updates != null) { |
| 137 | updateWaveObjects(respData.updates); |
| 138 | } |
| 139 | if (respData.error != null) { |
| 140 | throw new Error(`call ${methodName} error: ${respData.error}`); |
| 141 | } |
| 142 | const durationStr = Date.now() - startTs + "ms"; |
| 143 | debugLogBackendCall(methodName, durationStr, args); |
| 144 | return respData.data; |
| 145 | }); |
| 146 | return prtn; |
| 147 | } |
| 148 | |
| 149 | const waveObjectValueCache = new Map<string, WaveObjectValue<any>>(); |
| 150 |
no test coverage detected