Пример #1
0
        public void Bus_AsyncSubscriberException_ErrorHandlerCalled()
        {
            ImportiantData data = new ImportiantData {
                Info = "Valuable information"
            };


            // Second bus subscribes to message after it was dispatched and should receive it
            using (var bus = new MessageBus.Core.RabbitMQBus())
            {
                using (IAsyncSubscriber subscriber = bus.CreateAsyncSubscriber(c => c.SetReceiveSelfPublish().UseErrorSubscriber(this)))
                {
                    subscriber.Subscribe <ImportiantData>(Subscribe);

                    subscriber.Open();

                    using (IPublisher publisher = bus.CreatePublisher())
                    {
                        publisher.Send(data);
                    }

                    bool wait = _ev.WaitOne(TimeSpan.FromSeconds(5));

                    wait.Should().BeTrue();
                }
            }
        }
Пример #2
0
        public FirstAsyncAsyncMessageHandler(IAsyncSubscriber <TMessage> subscriber, CancellationToken cancellationToken, AsyncMessageHandlerFilter <TMessage>[] filters)
        {
            if (cancellationToken.IsCancellationRequested)
            {
                this.core.SetException(new OperationCanceledException(cancellationToken));
                return;
            }

            try
            {
                this.subscription = subscriber.Subscribe(this, filters);
            }
            catch (Exception ex)
            {
                this.core.SetException(ex);
                return;
            }

            if (handleCalled != 0)
            {
                this.subscription?.Dispose();
                return;
            }

            if (cancellationToken.CanBeCanceled)
            {
                this.cancellationToken             = cancellationToken;
                this.cancellationTokenRegistration = cancellationToken.Register(cancelCallback, this, false);
            }
        }
Пример #3
0
        public IAsyncEnumerator <TMessage> GetAsyncEnumerator(CancellationToken cancellationToken = default)
        {
            var disposable = DisposableBag.CreateSingleAssignment();
            var e          = new AsyncMessageHandlerEnumerator <TMessage>(disposable, cancellationToken);

            disposable.Disposable = subscriber.Subscribe(e, filters);
            return(e);
        }
Пример #4
0
        public void Initialize()
        {
            var bag = DisposableBag.CreateBuilder();

            _answerResultSubscriber.Subscribe(GameStateEnum.CheckAnswer, PlayResultAudio).AddTo(bag);

            _subscription = bag.Build();
        }
Пример #5
0
        public void Initialize()
        {
            var bag = DisposableBag.CreateBuilder();

            _correctAnswerSubscriber.Subscribe(GameStateEnum.GenerateLevel, PlayLevelIntroAudio).AddTo(bag);

            _subscription = bag.Build();
        }
Пример #6
0
        public void Initialize()
        {
            var bag = DisposableBag.CreateBuilder();

            _answersSubscriber.Subscribe(GameStateEnum.GenerateLevel, SetupLevel).AddTo(bag);

            _subscription = bag.Build();
        }
Пример #7
0
        public static IDisposable Subscribe <TMessage>(this IAsyncSubscriber <TMessage> subscriber, Func <TMessage, CancellationToken, ValueTask> handler, Func <TMessage, bool> predicate, params AsyncMessageHandlerFilter <TMessage>[] filters)
        {
            var predicateFilter = new AsyncPredicateFilter <TMessage>(predicate);

            filters = (filters.Length == 0)
                ? new[] { predicateFilter }
                : ArrayUtil.ImmutableAdd(filters, predicateFilter);

            return(subscriber.Subscribe(new AnonymousAsyncMessageHandler <TMessage>(handler), filters));
        }
Пример #8
0
        public void Initialize()
        {
            var bag = DisposableBag.CreateBuilder();

            _gameStateSubscriber.Subscribe(async(stateEnum, token) =>
            {
                if (stateEnum != GameStateEnum.MainMenu)
                {
                    return;
                }

                _baseObject.SetActive(true);
                await _startButton.OnClickAsync(token);
                _baseObject.SetActive(false);
            }).AddTo(bag);

            _subscription = bag.Build();
        }
Пример #9
0
            /// <summary>
            /// Subscribe内で待機
            /// </summary>
            public void SubscribeAsync(System.Action onFinished)
            {
                var bag = DisposableBag.CreateBuilder();

                _asyncSubscriber
                .Subscribe(async(x, ctr) =>
                {
                    var time = Time.realtimeSinceStartup;

                    // ここで非同期処理が可能
                    // 大体1秒待機
                    await UniTask.Delay(TimeSpan.FromSeconds(1), cancellationToken: ctr);

                    time = Time.realtimeSinceStartup - time;
                    Debug.Log($"イベント受信完了: {x.Message} 待機秒数: {time}秒");

                    // 終わったらよびだし
                    onFinished?.Invoke();
                }).AddTo(bag);

                _disposable = bag.Build();
            }
Пример #10
0
        public UniTask <IAsyncDisposable> SubscribeAsync(TKey key, IMessageHandler <TMessage> handler, CancellationToken cancellationToken = default)
        {
            var d = subscriber.Subscribe(key, new AsyncMessageHandlerBrdiger <TMessage>(handler));

            return(new UniTask <IAsyncDisposable>(new AsyncDisposableBridge(d)));
        }
Пример #11
0
        // pubsub-keyless-async

        public static IDisposable Subscribe <TMessage>(this IAsyncSubscriber <TMessage> subscriber, Func <TMessage, CancellationToken, ValueTask> handler, params AsyncMessageHandlerFilter <TMessage>[] filters)
        {
            return(subscriber.Subscribe(new AnonymousAsyncMessageHandler <TMessage>(handler), filters));
        }
Пример #12
0
 static IDisposable Ret(IAsyncSubscriber <int, int> subscriber)
 {
     return(subscriber.Subscribe(0, async(_, _) => Console.WriteLine("OK RETURN")));
 }
Пример #13
0
        static void AsyncSubscriber2(IAsyncSubscriber <string, int> subscriber)
        {
            subscriber.Subscribe("a", async(_, _) => Console.WriteLine("NG NOT HANDLED"));

            var d = subscriber.Subscribe("a", async(_, _) => Console.WriteLine("OK ASSIGN"));

            IDisposable d1;

            d1 = subscriber.Subscribe("a", async(_, _) => Console.WriteLine("OK ASSIGN2"));

            _ = subscriber.Subscribe("a", async(_, _) => Console.WriteLine("OK ASSIGN3"));

            DisposableBag.Create(subscriber.Subscribe("a", async(_, _) => Console.WriteLine("OK METHOD ARG1")));

            var bag = DisposableBag.CreateBuilder();

            subscriber.Subscribe("a", async(_, _) => Console.WriteLine("OK METHOD CHAIN")).AddTo(bag);

            Enumerable.Range(1, 10).Select(async(_, _) => subscriber.Subscribe("a", async(_, _) => Console.WriteLine("OK IN LAMBDA")));

            new MyClass(subscriber.Subscribe("a", async(_, _) => Console.WriteLine("OK CTOR")));

            using (subscriber.Subscribe("a", async(_, _) => Console.WriteLine("OK USING")))
            {
            }

            using (subscriber.Subscribe("a", async(_, _) => Console.WriteLine("OK USING2")))
            {
            }

            using var u = subscriber.Subscribe("a", async(_, _) => Console.WriteLine("OK USING 3"));
        }
Пример #14
0
        public unsafe PublishOps()
        {
            var provider = new ServiceCollection().AddMessagePipe().BuildServiceProvider();

            prism       = new Prism.Events.EventAggregator().GetEvent <Message>();
            prismStrong = new Prism.Events.EventAggregator().GetEvent <Message>();

            var mdiatr = new ServiceCollection().AddMediatR(typeof(PublishOps).Assembly).BuildServiceProvider();

            medi = mdiatr.GetRequiredService <IMediator>();


            p = provider.GetRequiredService <IPublisher <Message> >();
            var s = provider.GetRequiredService <ISubscriber <Message> >();

            keyed = provider.GetRequiredService <IPublisher <Guid, Message> >();
            var keyedS = provider.GetRequiredService <ISubscriber <Guid, Message> >();

            hub = Hub.Default;



            var px = new ServiceCollection().AddMessagePipe().BuildServiceProvider();

            filter1 = px.GetRequiredService <IPublisher <Message> >();
            var filter1Sub = px.GetRequiredService <ISubscriber <Message> >();

            var px2 = new ServiceCollection().AddMessagePipe().BuildServiceProvider();

            filter2 = px2.GetRequiredService <IPublisher <Message> >();
            var filter2Sub = px2.GetRequiredService <ISubscriber <Message> >();

            m       = new Message();
            subject = new Subject <Message>();

            signalBus  = SetupZenject();
            easyMsgHub = new MessageHub();

            asyncP = provider.GetRequiredService <IAsyncPublisher <Message> >();
            asyncS = provider.GetRequiredService <IAsyncSubscriber <Message> >();


#if WinBenchmark
            mvvmLight       = new Messenger();
            mvvmLightStrong = new Messenger();
#endif

            toolkitStrong = new Microsoft.Toolkit.Mvvm.Messaging.StrongReferenceMessenger();
            toolkitWeak   = new Microsoft.Toolkit.Mvvm.Messaging.WeakReferenceMessenger();



            simpleArray    = new PlainAction[8];
            actionDelegate = new Action[8];
            interfaceArray = new IInvoke[8];


            for (int i = 0; i < 8; i++)
            {
                s.Subscribe(new EmptyMessageHandler());
                prism.Subscribe(lambdaRef.Delegate);
                prismStrong.Subscribe(_ => { }, true);
                ev += _ => { };
                subject.Subscribe(_ => { });
                hub.Subscribe <Message>(_ => { });

#if WinBenchmark
                UniRx.MessageBroker.Default.Receive <Message>().Subscribe(new NopObserver());
                mvvmLight.Register <Message>(this, _ => { }, false);
                // mvvmLightStrong.Register<Message>(this, _ => { }, true);
#endif

                keyedS.Subscribe(key, _ => { });

                filter1Sub.Subscribe(new EmptyMessageHandler(), new EmptyMessageHandlerFilter());
                filter2Sub.Subscribe(new EmptyMessageHandler(), new EmptyMessageHandlerFilter(), new EmptyMessageHandlerFilter());

                easyMsgHub.Subscribe <Message>(_ => { });

                simpleArray[i]    = new PlainAction();
                actionDelegate[i] = new PlainAction().DelegateAction;
                interfaceArray[i] = new PlainAction();

                asyncS.Subscribe((_, _) => default(ValueTask));

                toolkitStrong.Register <Message>(new object(), lambdaRef2.Delegate);
                toolkitWeak.Register <Message>(new object(), lambdaRef2.Delegate);
            }

            signalBus.Subscribe <Message>(_ => { });
            signalBus.Subscribe <Message>(_ => { });
            signalBus.Subscribe <Message>(_ => { });
            signalBus.Subscribe <Message>(_ => { });
            signalBus.Subscribe <Message>(_ => { });
            signalBus.Subscribe <Message>(_ => { });
            signalBus.Subscribe <Message>(_ => { });
            signalBus.Subscribe <Message>(_ => { });
        }