From fcaa65e7ae5315cc81731e4057c6cb00ddc117a9 Mon Sep 17 00:00:00 2001 From: Matteo Cominetti Date: Wed, 10 Feb 2021 20:17:07 +0000 Subject: [PATCH] feat(receiver): makes receiver properly async and adds progress reporting --- Assets/ScriptableObjectExample.cs | 5 +- Assets/Speckle Connector/Receiver.cs | 89 ++++++++++++++++------------ Assets/Speckle Connector/Sender.cs | 1 - Assets/SpeckleExamples.cs | 56 ++++++++++------- 4 files changed, 87 insertions(+), 64 deletions(-) diff --git a/Assets/ScriptableObjectExample.cs b/Assets/ScriptableObjectExample.cs index d925535..bae0cb8 100644 --- a/Assets/ScriptableObjectExample.cs +++ b/Assets/ScriptableObjectExample.cs @@ -14,9 +14,10 @@ public class ScriptableObjectExample : MonoBehaviour void Start() { + return; receiver.Init(); - receiver.Receive().ConfigureAwait(false); - + receiver.Receive(); + //... do more stuff eg set materials, subscribe to changes etc } diff --git a/Assets/Speckle Connector/Receiver.cs b/Assets/Speckle Connector/Receiver.cs index 09b74b2..ba478a9 100644 --- a/Assets/Speckle Connector/Receiver.cs +++ b/Assets/Speckle Connector/Receiver.cs @@ -7,6 +7,7 @@ using Speckle.Core.Models; using Speckle.Core.Transports; using System; using System.Collections; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; @@ -22,10 +23,16 @@ namespace Speckle.ConnectorUnity public class Receiver : ScriptableObject { public string StreamId; + private bool AutoReceive; + private Action> OnProgressAction; + private Action OnErrorAction; + private Action OnTotalChildrenCountKnown; + private Action OnDataReceivedAction; - public delegate void DataReceivedHandler(GameObject data); - public event DataReceivedHandler OnNewData; + // public delegate void DataReceivedHandler(GameObject data); + // + // public event DataReceivedHandler OnNewData; private ConverterUnity _converter = new ConverterUnity(); private Client Client { get; set; } @@ -34,19 +41,30 @@ namespace Speckle.ConnectorUnity public Receiver() { } - + /// /// Initializes the Receiver manually, with StreamId and Account /// /// /// - public void Init(string streamId, Account account = null) + public void Init(string streamId, bool autoReceive = false, Account account = null, + Action onDataReceivedAction = null, Action> onProgressAction = null, + Action onErrorAction = null, Action onTotalChildrenCountKnown = null) { StreamId = streamId; + AutoReceive = autoReceive; + OnDataReceivedAction = onDataReceivedAction; + OnErrorAction = onErrorAction; + OnProgressAction = onProgressAction; + OnTotalChildrenCountKnown = onTotalChildrenCountKnown; Client = new Client(account ?? AccountManager.GetDefaultAccount()); - Client.SubscribeCommitCreated(StreamId); - Client.OnCommitCreated += Client_OnCommitCreated; + + if (AutoReceive) + { + Client.SubscribeCommitCreated(StreamId); + Client.OnCommitCreated += Client_OnCommitCreated; + } } /// @@ -56,30 +74,31 @@ namespace Speckle.ConnectorUnity public void Init() { Client = new Client(AccountManager.GetDefaultAccount()); - Client.SubscribeCommitCreated(StreamId); - Client.OnCommitCreated += Client_OnCommitCreated; } /// /// Gets and converts the data of the last commit on the Stream /// /// - public async Task Receive() + public void Receive() { - try + Task.Run(async () => { - var branches = await Client.StreamGetBranches(StreamId); - var mainBranch = branches.FirstOrDefault(b => b.name == "main"); - var commit = mainBranch.commits.items[0]; - return await GetAndConvertObject(commit.referencedObject, commit.id); - } - catch (Exception e) - { - Log.CaptureAndThrow(e); - } - - return null; + try + { + var branches = await Client.StreamGetBranches(StreamId); + var mainBranch = branches.FirstOrDefault(b => b.name == "main"); + var commit = mainBranch.commits.items[0]; + GetAndConvertObject(commit.referencedObject, commit.id); + } + catch (Exception e) + { + Log.CaptureAndThrow(e); + } + }); } + + #region private methods @@ -92,38 +111,32 @@ namespace Speckle.ConnectorUnity protected virtual void Client_OnCommitCreated(object sender, CommitInfo e) { Debug.Log("Commit created"); - - if (OnNewData == null) - return; - - //Run on a dispatcher as GOs can only be created on the main thread - Dispatcher.Instance().EnqueueAsync(async () => - { - var go = await GetAndConvertObject(e.objectId, e.id); - OnNewData?.Invoke(go); - }); + GetAndConvertObject(e.objectId, e.id); } - private async Task GetAndConvertObject(string objectId, string commitId) + private async void GetAndConvertObject(string objectId, string commitId) { try { - Debug.Log("test"); var transport = new ServerTransport(Client.Account, StreamId); var @base = await Operations.Receive( objectId, - remoteTransport: transport + remoteTransport: transport, + onErrorAction: OnErrorAction, + onProgressAction: OnProgressAction, + onTotalChildrenCountKnown: OnTotalChildrenCountKnown ); - - return ConvertRecursivelyToNative(@base, commitId); + Dispatcher.Instance().Enqueue( () => + { + var go = ConvertRecursivelyToNative(@base, commitId); + OnDataReceivedAction?.Invoke(go); + }); } catch (Exception e) { Log.CaptureAndThrow(e); } - - return null; } diff --git a/Assets/Speckle Connector/Sender.cs b/Assets/Speckle Connector/Sender.cs index 95d23e7..00db03b 100644 --- a/Assets/Speckle Connector/Sender.cs +++ b/Assets/Speckle Connector/Sender.cs @@ -21,7 +21,6 @@ namespace Speckle.ConnectorUnity public static class Sender { - /// /// Converts and sends the data of the last commit on the Stream /// diff --git a/Assets/SpeckleExamples.cs b/Assets/SpeckleExamples.cs index 5e978d7..584a61d 100644 --- a/Assets/SpeckleExamples.cs +++ b/Assets/SpeckleExamples.cs @@ -6,6 +6,7 @@ using Speckle.Core.Models; using Speckle.Core.Api; using Speckle.Core.Credentials; using System; +using System.Collections.Concurrent; using System.Threading.Tasks; using System.Linq; using Speckle.Core.Transports; @@ -32,63 +33,72 @@ namespace Speckle.ConnectorUnity //hardcoded cuz I'm lazy, replace with what you need ReceiveText.text = "4ad65b572e"; SendText.text = "cd83745025"; - + if (ReceiveBtn == null || ReceiveText == null || SendBtn == null || SendText == null) { Debug.Log("Please set Send/Receive buttons and input fields"); return; } - + Button btn = ReceiveBtn.GetComponent