UniRx icon indicating copy to clipboard operation
UniRx copied to clipboard

await on IMessageBroker.Receive<T> ?

Open slimshader opened this issue 5 years ago • 7 comments

in MonoBehaviour 1 I have a call:

await messageBroker.Receive<Message>();
Debug.Log("Received");

while in the other I have:

messageBroker.Publish<Message>();
Debug.Log("Published");

my problem is that program seems to stop at await stage, no "Publish" or "Received" is printed.

publishing an event is definately happening after await instruction

slimshader avatar Jan 10 '19 00:01 slimshader

It seems that it happens with Subject also meaning awaiting it never continues even the OnNext was called on it elsewhere

slimshader avatar Jan 10 '19 09:01 slimshader

I understand the problem now. 'await' on Observable waits for the last result before completion, not the first one. The solution is to add FirstAsync() operator to the Rx library and await call like so

await messageBroker.Receive<Message>().FirstAsync();

slimshader avatar Jan 10 '19 11:01 slimshader

Yes, Rx's standard await is last. FirstAsync or convert to the UniTask, may works fine.

neuecc avatar Jan 11 '19 03:01 neuecc

Yes, Rx's standard await is last. FirstAsync or convert to the UniTask, may works fine.

I found out that await someSubject.ToUniTask(); works like a charm. But await MessageBroker.Default.Receive<SomeMessage>().ToUniTask() doesn't. @neuecc Can u help me here and bring some light on it?

neitron avatar Feb 01 '22 14:02 neitron

Yes, Rx's standard await is last. FirstAsync or convert to the UniTask, may works fine.

I found out that await someSubject.ToUniTask(); works like a charm. But await MessageBroker.Default.Receive<SomeMessage>().ToUniTask() doesn't. @neuecc Can u help me here and bring some light on it?

ToUniTask() takes bool parameter of whether it should be awaiting First of last element for IObservable().

slimshader avatar Feb 01 '22 14:02 slimshader

await MessageBroker.Default.Receive<SomeMessage>().Take(1).ToUniTask() WORKS!

neitron avatar Feb 01 '22 14:02 neitron

await MessageBroker.Default.Receive<SomeMessage>().Take(1).ToUniTask() WORKS!

Yes, and is effectively the same as Receive().First().ToUniTask()

slimshader avatar Feb 01 '22 14:02 slimshader