desktop/source/app/appinit.cxx          |    2 +
 static/README.wasm.md                   |   49 ++++++++++++++++----------------
 static/emscripten/uno.js                |   11 +++----
 unotest/source/embindtest/embindtest.js |    3 -
 4 files changed, 35 insertions(+), 30 deletions(-)

New commits:
commit 91bedcab61424cdfb2f3ba9e48481406fe141ceb
Author:     Stephan Bergmann <stephan.bergm...@allotropia.de>
AuthorDate: Thu Jul 18 11:44:45 2024 +0200
Commit:     Stephan Bergmann <stephan.bergm...@allotropia.de>
CommitDate: Thu Jul 18 15:55:12 2024 +0200

    Change from Module.intiUno() to Module.uno_init promise
    
    ...that is resolved from within C++ Desktop::InitApplicationServiceManager 
once
    UNO is fully initialized, so client code can trigger on
    Module.uno_init.then(...)
    
    Change-Id: I2d4c542d9729d09f434502e3f966e9ee474e926c
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/170683
    Tested-by: Jenkins
    Reviewed-by: Stephan Bergmann <stephan.bergm...@allotropia.de>

diff --git a/desktop/source/app/appinit.cxx b/desktop/source/app/appinit.cxx
index 633ebde2d967..f2b7edba1c8e 100644
--- a/desktop/source/app/appinit.cxx
+++ b/desktop/source/app/appinit.cxx
@@ -45,6 +45,7 @@
 #include <map>
 
 #if defined EMSCRIPTEN
+#include <emscripten.h>
 #include <bindings_uno.hxx>
 #endif
 
@@ -86,6 +87,7 @@ void Desktop::InitApplicationServiceManager()
     comphelper::setProcessServiceFactory(sm);
 #if defined EMSCRIPTEN
     init_unoembind_uno();
+    EM_ASM(Module.uno_init$resolve(););
 #endif
 }
 
diff --git a/static/README.wasm.md b/static/README.wasm.md
index 7d69716e7173..523658868e58 100644
--- a/static/README.wasm.md
+++ b/static/README.wasm.md
@@ -210,33 +210,36 @@ improvement! ;)
 Some usage examples through javascript of the current implementation:
 ```js
 // inserts a string at the start of the Writer document.
-Module.initUno();
-const css = Module.uno.com.sun.star;
-const xModel = Module.getCurrentModelFromViewSh();
-const xTextDocument = css.text.XTextDocument.query(xModel);
-const xText = xTextDocument.getText();
-const xTextCursor = xText.createTextCursor();
-xTextCursor.setString("string here!");
+Module.uno_init.then(function() {
+    const css = Module.uno.com.sun.star;
+    const xModel = Module.getCurrentModelFromViewSh();
+    const xTextDocument = css.text.XTextDocument.query(xModel);
+    const xText = xTextDocument.getText();
+    const xTextCursor = xText.createTextCursor();
+    xTextCursor.setString("string here!");
+});
 ```
 
 ```js
 // changes each paragraph of the Writer document to a random color.
-Module.initUno();
-const css = Module.uno.com.sun.star;
-const xModel = Module.getCurrentModelFromViewSh();
-const xTextDocument = css.text.XTextDocument.query(xModel);
-const xText = xTextDocument.getText();
-const xEnumAccess = css.container.XEnumerationAccess.query(xText);
-const xParaEnumeration = xEnumAccess.createEnumeration();
-while (xParaEnumeration.hasMoreElements()) {
-    const next = xParaEnumeration.nextElement();
-    const xParagraph = css.text.XTextRange.query(next.get());
-    const xParaProps = css.beans.XPropertySet.query(xParagraph);
-    const color = new Module.uno_Any(Module.uno_Type.Long(), 
Math.floor(Math.random() * 0xFFFFFF));
-    xParaProps.setPropertyValue("CharColor", color);
-    next.delete();
-    color.delete();
-}
+Module.uno_init.then(function() {
+    const css = Module.uno.com.sun.star;
+    const xModel = Module.getCurrentModelFromViewSh();
+    const xTextDocument = css.text.XTextDocument.query(xModel);
+    const xText = xTextDocument.getText();
+    const xEnumAccess = css.container.XEnumerationAccess.query(xText);
+    const xParaEnumeration = xEnumAccess.createEnumeration();
+    while (xParaEnumeration.hasMoreElements()) {
+        const next = xParaEnumeration.nextElement();
+        const xParagraph = css.text.XTextRange.query(next.get());
+        const xParaProps = css.beans.XPropertySet.query(xParagraph);
+        const color = new Module.uno_Any(
+            Module.uno_Type.Long(), Math.floor(Math.random() * 0xFFFFFF));
+        xParaProps.setPropertyValue("CharColor", color);
+        next.delete();
+        color.delete();
+    }
+});
 ```
 
 
diff --git a/static/emscripten/uno.js b/static/emscripten/uno.js
index 6a9c4cd5cb6e..7d051a24a84b 100644
--- a/static/emscripten/uno.js
+++ b/static/emscripten/uno.js
@@ -11,11 +11,13 @@
 
 Module.unoTagSymbol = Symbol('unoTag');
 
-Module.initUno = function() {
-    if (Module.uno === undefined) {
+Module.uno_init = new Promise(function (resolve, reject) {
+    Module.uno_init$resolve = function() {
         Module.uno = init_unoembind_uno(Module, Module.unoTagSymbol);
-    }
-};
+        resolve();
+    };
+    Module.uno_init$reject = reject;
+});
 
 Module.catchUnoException = function(exception) {
     // Rethrow non-C++ exceptions (non-UNO C++ exceptions are mapped to 
css.uno.RuntimeException in
@@ -30,7 +32,6 @@ Module.catchUnoException = function(exception) {
 }
 
 Module.unoObject = function(interfaces, obj) {
-    Module.initUno();
     interfaces = ['com.sun.star.lang.XTypeProvider'].concat(interfaces);
     obj.impl_refcount = 0;
     obj.queryInterface = function(type) {
diff --git a/unotest/source/embindtest/embindtest.js 
b/unotest/source/embindtest/embindtest.js
index bb4d2716ee5d..a81937deed87 100644
--- a/unotest/source/embindtest/embindtest.js
+++ b/unotest/source/embindtest/embindtest.js
@@ -9,9 +9,8 @@
 
 'use strict';
 
-Module.addOnPostRun(function() {
+Module.uno_init.then(function() {
     console.log('Running embindtest');
-    Module.initUno();
     let css = Module.uno.com.sun.star;
     let test = 
Module.uno.org.libreoffice.embindtest.Test.create(Module.getUnoComponentContext());
     console.assert(typeof test === 'object');

Reply via email to