comparison Deprecated/Platforms/Wasm/wasm-application-runner.ts @ 1400:419d0320c344

moved Platforms into Deprecated
author Alain Mazy <alain@mazy.be>
date Wed, 29 Apr 2020 20:45:14 +0200
parents Platforms/Wasm/wasm-application-runner.ts@861c080ef47b
children
comparison
equal deleted inserted replaced
1399:ff8d2e46ac63 1400:419d0320c344
1 import * as Stone from './stone-framework-loader'
2 import * as StoneViewport from './wasm-viewport'
3 import * as Logger from './logger'
4
5 if (!('WebAssembly' in window)) {
6 alert('Sorry, your browser does not support WebAssembly :(');
7 }
8
9 //var StoneFrameworkModule : Stone.Framework = (<any>window).StoneFrameworkModule;
10 //export declare var StoneFrameworkModule : Stone.Framework;
11
12 // global functions
13 var WasmWebService_NotifyError: Function = null;
14 var WasmWebService_NotifySuccess: Function = null;
15 var WasmWebService_NotifyCachedSuccess: Function = null;
16 var WasmDelayedCallExecutor_ExecuteCallback: Function = null;
17 var WasmDoAnimation: Function = null;
18 var SetStartupParameter: Function = null;
19 var CreateWasmApplication: Function = null;
20 export var CreateCppViewport: Function = null;
21 var ReleaseCppViewport: Function = null;
22 var StartWasmApplication: Function = null;
23 export var SendSerializedMessageToStoneApplication: Function = null;
24
25 var auxiliaryParameters : Map<string,string> = null;
26
27 export function SetApplicationParameters(params : Map<string,string>) {
28 if (auxiliaryParameters != null) {
29 console.warn("wasm-application-runner.SetApplicationParameters: about to overwrite the existing application parameters!")
30 }
31 auxiliaryParameters = params;
32 }
33
34 function DoAnimationThread() {
35 if (WasmDoAnimation != null) {
36 WasmDoAnimation();
37 }
38
39 // Update the viewport content every 100ms if need be
40 setTimeout(DoAnimationThread, 100);
41 }
42
43
44 function GetUriParameters(): Map<string, string> {
45 var parameters = window.location.search.substr(1);
46
47 if (parameters != null &&
48 parameters != '') {
49 var result = new Map<string, string>();
50 var tokens = parameters.split('&');
51
52 for (var i = 0; i < tokens.length; i++) {
53 var tmp = tokens[i].split('=');
54 if (tmp.length == 2) {
55 result[tmp[0]] = decodeURIComponent(tmp[1]);
56 } else if(tmp.length == 1) {
57 // if there is no '=', we treat ot afterwards as a flag-style param
58 result[tmp[0]] = "";
59 }
60 }
61 return result;
62 }
63 else {
64 return new Map<string, string>();
65 }
66 }
67
68 // function UpdateWebApplication(statusUpdateMessage: string) {
69 // console.log(statusUpdateMessage);
70 // }
71
72 function _InitializeWasmApplication(orthancBaseUrl: string): void {
73
74 CreateWasmApplication();
75
76 // transmit the API-specified parameters to the app before initializing it
77 for (let key in auxiliaryParameters) {
78 if (auxiliaryParameters.hasOwnProperty(key)) {
79 Logger.defaultLogger.debug(
80 `About to call SetStartupParameter("${key}","${auxiliaryParameters[key]}")`);
81 SetStartupParameter(key, auxiliaryParameters[key]);
82 }
83 }
84
85 // parse uri and transmit the URI parameters to the app before initializing it
86 let parameters = GetUriParameters();
87
88 for (let key in parameters) {
89 if (parameters.hasOwnProperty(key)) {
90 Logger.defaultLogger.debug(
91 `About to call SetStartupParameter("${key}","${parameters[key]}")`);
92 SetStartupParameter(key, parameters[key]);
93 }
94 }
95
96 StartWasmApplication(orthancBaseUrl);
97
98 // trigger a first resize of the canvas that has just been initialized
99 StoneViewport.WasmViewport.ResizeAll();
100
101 DoAnimationThread();
102 }
103
104 export function InitializeWasmApplication(wasmModuleName: string, orthancBaseUrl: string) {
105
106 Stone.Framework.Configure(wasmModuleName);
107
108 // Wait for the Orthanc Framework to be initialized (this initializes
109 // the WebAssembly environment) and then, create and initialize the Wasm application
110 Stone.Framework.Initialize(true, function () {
111
112 Logger.defaultLogger.debug("Connecting C++ methods to JS methods");
113
114 SetStartupParameter = (<any> window).StoneFrameworkModule.cwrap('SetStartupParameter', null, ['string', 'string']);
115 CreateWasmApplication = (<any> window).StoneFrameworkModule.cwrap('CreateWasmApplication', null, ['number']);
116 CreateCppViewport = (<any> window).StoneFrameworkModule.cwrap('CreateCppViewport', 'number', []);
117 ReleaseCppViewport = (<any> window).StoneFrameworkModule.cwrap('ReleaseCppViewport', null, ['number']);
118 StartWasmApplication = (<any> window).StoneFrameworkModule.cwrap('StartWasmApplication', null, ['string']);
119 (<any> window).IsTraceLevelEnabled = (<any> window).StoneFrameworkModule.cwrap('WasmIsTraceLevelEnabled', 'boolean', null);
120 (<any> window).IsInfoLevelEnabled = (<any> window).StoneFrameworkModule.cwrap('WasmIsInfoLevelEnabled', 'boolean', null);
121
122 (<any> window).WasmWebService_NotifyCachedSuccess = (<any> window).StoneFrameworkModule.cwrap('WasmWebService_NotifyCachedSuccess', null, ['number']);
123 (<any> window).WasmWebService_NotifySuccess = (<any> window).StoneFrameworkModule.cwrap('WasmWebService_NotifySuccess', null, ['number', 'string', 'array', 'number', 'number']);
124 (<any> window).WasmWebService_NotifyError = (<any> window).StoneFrameworkModule.cwrap('WasmWebService_NotifyError', null, ['number', 'string', 'number', 'number']);
125 (<any> window).WasmDelayedCallExecutor_ExecuteCallback = (<any> window).StoneFrameworkModule.cwrap('WasmDelayedCallExecutor_ExecuteCallback', null, ['number']);
126 // no need to put this into the globals for it's only used in this very module
127 WasmDoAnimation = (<any> window).StoneFrameworkModule.cwrap('WasmDoAnimation', null, []);
128
129 SendSerializedMessageToStoneApplication = (<any> window).StoneFrameworkModule.cwrap('SendSerializedMessageToStoneApplication', 'string', ['string']);
130
131 Logger.defaultLogger.debug("Connecting C++ methods to JS methods - done");
132
133 _InitializeWasmApplication(orthancBaseUrl);
134 });
135 }
136
137
138 // exports.InitializeWasmApplication = InitializeWasmApplication;
139
140
141