Skip to content

feat: dispatcher extensions #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions PubNubUnity/Assets/PubNubUnity/Extensions/SdkExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using PubnubApi;
using PubnubApi.EndPoint;
using PubNubUnity.Internal;

namespace PubNubUnity {
public static class SdkExtensions {
/// <summary>
/// Execute the publish operation and run the callback upon completion. The callback is dispatched to Unity main thread
/// </summary>
/// <param name="operation">Publish operation</param>
/// <param name="callback">Callback to run upon operation completion</param>
public static void Execute(this PublishOperation operation, System.Action<PNPublishResult, PNStatus> callback) {
operation.Execute(new PNPublishResultExt((a, b) => PNDispatcher.Dispatch(() => callback?.Invoke(a, b))));
}
}
}
11 changes: 11 additions & 0 deletions PubNubUnity/Assets/PubNubUnity/Extensions/SdkExtensions.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions PubNubUnity/Assets/PubNubUnity/Utils/Editor.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using UnityEditor;

namespace PubNubUnity.Internal {
[CustomEditor(typeof(PNDispatcher))]
public class PNDispatcherEditor : Editor {
public class DispatcherEditor : Editor {
public override void OnInspectorGUI() {
EditorGUILayout.HelpBox("This script allows dispatching to the main Unity render thread", MessageType.Info);
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

81 changes: 81 additions & 0 deletions PubNubUnity/Assets/PubNubUnity/Utils/PNDispatcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;

namespace PubNubUnity.Internal {
public class PNDispatcher : MonoBehaviour {
static PNDispatcher instance;
static object lockObject;

static volatile Queue<System.Action> dispatchQueue = new Queue<System.Action>();

void FixedUpdate() {
HandleDispatch();
}

static void HandleDispatch() {
lock (lockObject) {
var c = dispatchQueue.Count;
for (int i = 0; i < c; i++) {
try {
dispatchQueue.Dequeue()();
} catch (System.Exception e) {
// TODO investigate if we need more error handling mechanisms
Debug.LogError($"{e.Message} ::\n{e.StackTrace}");
}
}
}
}

/// <summary>
/// Dispatches the callback to Unity's main thread. Facilitates working on Unity's objects within the callback.
/// </summary>
/// <param name="action">Action to dispatch to main thread</param>
public static void Dispatch(System.Action action) {
if (action is null) {
return;
}

lock (lockObject) {
dispatchQueue.Enqueue(action);
}
}

/// <summary>
/// Dispatch an async operation's result to the main thread. Facilitates working on Unity's objects within the callback.
/// </summary>
/// <param name="task">Async task to dispatch</param>
/// <param name="callback">Callback function which accepts task result as the argument</param>
/// <typeparam name="T">Task return type</typeparam>
public static async void DispatchTask<T>(Task<T> task, System.Action<T> callback) {
if (callback is null) {
return;
}

T res;
if (task.IsCompleted) {
res = task.Result;
} else {
res = await task;
}

Dispatch(() => callback(res));
}

[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
static void Initialize() {
if (!instance) {
instance = new GameObject("[PubNub Dispatcher]").AddComponent<PNDispatcher>();
}
instance.gameObject.hideFlags = HideFlags.NotEditable | HideFlags.DontSave;
instance.transform.hideFlags = HideFlags.HideInInspector;

// For future potential edit-mode dispatching
if (Application.isPlaying) {
DontDestroyOnLoad(instance.gameObject);
}

lockObject ??= new object();
}
}
}
11 changes: 11 additions & 0 deletions PubNubUnity/Assets/PubNubUnity/Utils/PNDispatcher.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.