annotate Platforms/Wasm/wasm-application-runner.ts @ 303:ed1a4302154f am-callable-and-promise

new messages in wasm too
author am@osimis.io
date Tue, 18 Sep 2018 18:17:26 +0200
parents 3897f9f28cfa
children 8bdc6112bc2e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
287
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
1 ///<reference path='stone-framework-loader.ts'/>
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
2 ///<reference path='wasm-viewport.ts'/>
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
3
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
4 if (!('WebAssembly' in window)) {
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
5 alert('Sorry, your browser does not support WebAssembly :(');
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
6 }
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
7
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
8 declare var StoneFrameworkModule : Stone.Framework;
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
9
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
10 // global functions
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
11 var WasmWebService_NotifyError: Function = null;
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
12 var WasmWebService_NotifySuccess: Function = null;
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
13 var WasmWebService_SetBaseUri: Function = null;
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
14 var NotifyUpdateContent: Function = null;
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
15 var SetStartupParameter: Function = null;
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
16 var CreateWasmApplication: Function = null;
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
17 var CreateCppViewport: Function = null;
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
18 var ReleaseCppViewport: Function = null;
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
19 var StartWasmApplication: Function = null;
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
20 var SendMessageToStoneApplication: Function = null;
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
21
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
22
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
23 function UpdateContentThread() {
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
24 if (NotifyUpdateContent != null) {
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
25 NotifyUpdateContent();
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
26 }
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
27
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
28 setTimeout(UpdateContentThread, 100); // Update the viewport content every 100ms if need be
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
29 }
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
30
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
31
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
32 function GetUriParameters() {
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
33 var parameters = window.location.search.substr(1);
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
34
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
35 if (parameters != null &&
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
36 parameters != '') {
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
37 var result = {};
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
38 var tokens = parameters.split('&');
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
39
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
40 for (var i = 0; i < tokens.length; i++) {
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
41 var tmp = tokens[i].split('=');
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
42 if (tmp.length == 2) {
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
43 result[tmp[0]] = decodeURIComponent(tmp[1]);
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
44 }
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
45 }
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
46
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
47 return result;
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
48 }
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
49 else {
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
50 return {};
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
51 }
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
52 }
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
53
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
54 // function UpdateWebApplication(statusUpdateMessage: string) {
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
55 // console.log(statusUpdateMessage);
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
56 // }
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
57
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
58 function _InitializeWasmApplication(canvasId: string, orthancBaseUrl: string): void {
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
59
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
60 /************************************** */
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
61 CreateWasmApplication();
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
62 WasmWebService_SetBaseUri(orthancBaseUrl);
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
63
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
64
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
65 // parse uri and transmit the parameters to the app before initializing it
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
66 var parameters = GetUriParameters();
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
67
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
68 for (var key in parameters) {
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
69 if (parameters.hasOwnProperty(key)) {
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
70 SetStartupParameter(key, parameters[key]);
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
71 }
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
72 }
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
73
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
74 StartWasmApplication();
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
75 /************************************** */
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
76
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
77 UpdateContentThread();
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
78 }
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
79
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
80 function InitializeWasmApplication(wasmModuleName: string, orthancBaseUrl: string) {
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
81
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
82 Stone.Framework.Configure(wasmModuleName);
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
83
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
84 // Wait for the Orthanc Framework to be initialized (this initializes
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
85 // the WebAssembly environment) and then, create and initialize the Wasm application
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
86 Stone.Framework.Initialize(true, function () {
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
87
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
88 console.log("Connecting C++ methods to JS methods");
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
89
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
90 SetStartupParameter = StoneFrameworkModule.cwrap('SetStartupParameter', null, ['string', 'string']);
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
91 CreateWasmApplication = StoneFrameworkModule.cwrap('CreateWasmApplication', null, ['number']);
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
92 CreateCppViewport = StoneFrameworkModule.cwrap('CreateCppViewport', 'number', []);
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
93 ReleaseCppViewport = StoneFrameworkModule.cwrap('ReleaseCppViewport', null, ['number']);
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
94 StartWasmApplication = StoneFrameworkModule.cwrap('StartWasmApplication', null, ['number']);
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
95
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
96 WasmWebService_NotifySuccess = StoneFrameworkModule.cwrap('WasmWebService_NotifySuccess', null, ['number', 'string', 'array', 'number', 'number']);
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
97 WasmWebService_NotifyError = StoneFrameworkModule.cwrap('WasmWebService_NotifyError', null, ['number', 'string', 'number']);
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
98 WasmWebService_SetBaseUri = StoneFrameworkModule.cwrap('WasmWebService_SetBaseUri', null, ['string']);
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
99 NotifyUpdateContent = StoneFrameworkModule.cwrap('NotifyUpdateContent', null, []);
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
100
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
101 SendMessageToStoneApplication = StoneFrameworkModule.cwrap('SendMessageToStoneApplication', 'string', ['string']);
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
102
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
103 console.log("Connecting C++ methods to JS methods - done");
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
104
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
105 // Prevent scrolling
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
106 document.body.addEventListener('touchmove', function (event) {
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
107 event.preventDefault();
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
108 }, false);
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
109
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
110 _InitializeWasmApplication("canvas", orthancBaseUrl);
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
111 });
2038d76bf13f interaction with HTML/JS
am@osimis.io
parents:
diff changeset
112 }