Skip to content

Instantly share code, notes, and snippets.

@kyubuns
Created December 29, 2017 05:30
Show Gist options
  • Save kyubuns/466bcf9ba042551c82e971d60e1d19f5 to your computer and use it in GitHub Desktop.
Save kyubuns/466bcf9ba042551c82e971d60e1d19f5 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using UniRx;
namespace Misc
{
public class QueueAsyncMessageBroker
{
private bool Running;
private AsyncMessageBroker Broker { get; } = new AsyncMessageBroker();
private List<Action> Queue { get; } = new List<Action>();
public void Publish<T>(T message)
{
Queue.Add(() =>
{
Running = true;
Broker.PublishAsync(message).Subscribe(_ =>
{
Running = false;
Next();
});
});
Next();
}
private void Next()
{
if (Running || Queue.Count == 0) return;
var e = Queue.First();
Queue.RemoveAt(0);
e();
}
public IDisposable Subscribe<T>(Func<T, UniRx.IObservable<Unit>> asyncMessageReceiver)
{
return Broker.Subscribe(asyncMessageReceiver);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment