From 78528a0c87f078923b32e9c1831658579ba851c3 Mon Sep 17 00:00:00 2001 From: "Frank A. Krueger" Date: Fri, 9 Mar 2018 23:12:07 -0800 Subject: [PATCH] BoxViewClock Forms sample working in wasm --- Ooui.Wasm/README.md | 2 +- Ooui.Wasm/ooui-sample.cs | 30 ++++++++++++++++++-------- Ooui.Wasm/ooui-sample.html | 2 ++ Ooui/Session.cs | 2 +- Ooui/WebAssemblySession.cs | 43 +++++++++++++++++++++----------------- 5 files changed, 49 insertions(+), 30 deletions(-) diff --git a/Ooui.Wasm/README.md b/Ooui.Wasm/README.md index d9af42a..d48b9fb 100644 --- a/Ooui.Wasm/README.md +++ b/Ooui.Wasm/README.md @@ -13,7 +13,7 @@ Expand that into this directory. ## Build ```bash -csc /nostdlib /target:library /r:managed/mscorlib.dll /r:managed/System.Runtime.dll /r:managed/Ooui.dll /out:managed/Ooui.Sample.dll ooui-sample.cs +csc /nostdlib /target:library /r:managed/mscorlib.dll /r:managed/System.Runtime.dll /r:managed/Xamarin.Forms.Core.dll /r:managed/Ooui.dll /r:managed/Ooui.Forms.dll /out:managed/Ooui.Sample.dll ../Samples/ISample.cs ../Samples/BoxViewClockSample.cs ooui-sample.cs ``` diff --git a/Ooui.Wasm/ooui-sample.cs b/Ooui.Wasm/ooui-sample.cs index ac57e88..896bd1c 100644 --- a/Ooui.Wasm/ooui-sample.cs +++ b/Ooui.Wasm/ooui-sample.cs @@ -3,18 +3,30 @@ using Ooui; public class Program { + static Element GetButtonElement () + { + var l = new Label { Text = "Hello" }; + var b = new Button ("Click Me"); + var e = new Div (new Div (l), b); + int count = 0; + b.Click += (s, ee) => { + count++; + b.Text = $"Clicked {count} times"; + }; + return e; + } + + static Element GetBoxViewClockElement () + { + var s = new Samples.BoxViewClockSample (); + return s.CreateElement (); + } + public static string Main (string a0, string a1) { + Xamarin.Forms.Forms.Init (); try { - var l = new Label { Text = "Hello" }; - var b = new Button ("Click Me"); - var e = new Div (new Div (l), b); - int count = 0; - b.Click += (s, ee) => { - count++; - b.Text = $"Clicked {count} times"; - }; - + var e = GetBoxViewClockElement (); UI.SetGlobalElement ("main", e); return e.ToString (); } diff --git a/Ooui.Wasm/ooui-sample.html b/Ooui.Wasm/ooui-sample.html index f47470c..1a610a9 100644 --- a/Ooui.Wasm/ooui-sample.html +++ b/Ooui.Wasm/ooui-sample.html @@ -37,6 +37,7 @@ "System.Threading.Tasks.dll", "System.Reflection.dll", "System.Reflection.Extensions.dll", + "System.Resources.ResourceManager.dll", "System.Runtime.dll", "System.Runtime.Extensions.dll", "System.Runtime.Serialization.dll", @@ -48,6 +49,7 @@ "Xamarin.Forms.Platform.dll", "Xamarin.Forms.Xaml.dll", "Ooui.dll", + "Ooui.Forms.dll", mainAsmName + ".dll" ]; oouiWasm (mainAsmName, "", "Program", "Main", assemblies); diff --git a/Ooui/Session.cs b/Ooui/Session.cs index 029c530..2b8570e 100644 --- a/Ooui/Session.cs +++ b/Ooui/Session.cs @@ -44,7 +44,7 @@ namespace Ooui } } - void QueueMessageLocked (Message message) + protected void QueueMessageLocked (Message message) { // // Make sure all the referenced objects have been created diff --git a/Ooui/WebAssemblySession.cs b/Ooui/WebAssemblySession.cs index df1b3e9..b7d53f4 100644 --- a/Ooui/WebAssemblySession.cs +++ b/Ooui/WebAssemblySession.cs @@ -18,38 +18,43 @@ namespace Ooui protected override void QueueMessage (Message message) { - base.QueueMessage (message); - TransmitQueuedMessages (); - } - - void TransmitQueuedMessages () - { - // - // Dequeue as many messages as we can - // - var messagesToSend = new List (); lock (queuedMessages) { - messagesToSend.AddRange (queuedMessages); + QueueMessageLocked (message); + var max = 1; + var i = 0; + while (i < queuedMessages.Count) { + TransmitQueuedMessagesLocked (queuedMessages, i, max); + i += max; + } + // WebAssembly.Runtime.InvokeJS("console.log('TRANSMITTED'," + queuedMessages.Count + ")"); queuedMessages.Clear (); } + } + void TransmitQueuedMessagesLocked (List messagesToSend, int startIndex, int max) + { if (messagesToSend.Count == 0) return; // // Now actually send the messages // - var sb = new StringBuilder (); + var sb = new System.IO.StringWriter (); + sb.Write ("__oouiReceiveMessages(\""); + sb.Write (id); + sb.Write ("\","); + sb.Write ("["); var head = ""; - sb.Append ("["); - foreach (var m in messagesToSend) { - sb.Append (head); - sb.Append (m.ToJson ()); + int n = 0; + for (var i = startIndex; i < messagesToSend.Count && n < max; i++, n++) { + sb.Write (head); + messagesToSend[i].WriteJson (sb); head = ","; } - sb.Append ("]"); - var json = sb.ToString (); - WebAssembly.Runtime.InvokeJS ("__oouiReceiveMessages(\"" + id + "\", " + json + ")"); + sb.Write ("])"); + var jsonp = sb.ToString (); + // WebAssembly.Runtime.InvokeJS("console.log('TRANSMIT',"+n+")"); + WebAssembly.Runtime.InvokeJS (jsonp); } public void ReceiveMessageJson (string json)