Created
December 29, 2017 05:30
-
-
Save kyubuns/466bcf9ba042551c82e971d60e1d19f5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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