Ejemplo n.º 1
0
        public static async Task Run()
        {
            var ae = new AsyncEvent <int>(12);

            await ae.Subscribe(_owner, OnAeChanged1);

            Console.WriteLine("Subscribe 1");

            await ae.Subscribe(_owner, OnAeChanged2);

            Console.WriteLine("Subscribe 2");

            await ae.Subscribe(_owner, OnAeChanged3);

            Console.WriteLine("Subscribe 3");

            await ae.Invoke(13);

            await ae.Unsubscribe(_owner, OnAeChanged1);

            Console.WriteLine("Unubscribe 1");

            await ae.Invoke(14);

            await ae.Unsubscribe(_owner);

            Console.WriteLine("Unubscribe owner");

            await ae.Invoke(15);
        }
Ejemplo n.º 2
0
        public async void RunActionHandler()
        {
            _ev.Subscribe(AddAction);
            await _ev.InvokeAsync(this, new EventArgs()).ConfigureAwait(false);

            await _ev.InvokeAsync(this, new EventArgs()).ConfigureAwait(false);

            Assert.Equal(2, _counter);
            _ev.Unsubscribe(AddAction);
            await _ev.InvokeAsync(this, new EventArgs()).ConfigureAwait(false);

            await _ev.InvokeAsync(this, new EventArgs()).ConfigureAwait(false);

            Assert.Equal(2, _counter);
        }
Ejemplo n.º 3
0
        public async void AsyncEventSyncHandler()
        {
            var counter = 0;

            void MyMethod() => counter++;

            var ev = new AsyncEvent <EventArgs>();

            ev.Subscribe(MyMethod);
            await ev.InvokeAsync(this, new EventArgs());

            Assert.Equal(1, counter);

            ev.Unsubscribe(MyMethod);
            await ev.InvokeAsync(this, new EventArgs());

            Assert.Equal(1, counter);
        }
Ejemplo n.º 4
0
        public async void RunAsyncEvent()
        {
            var x1 = new AsyncEvent();

            x1.Subscribe(AddTask);
            var x2 = new AsyncEvent();

            x2.Subscribe(x1);
            await x2.InvokeAsync(this, new EventArgs()).ConfigureAwait(false);

            await x2.InvokeAsync(this, new EventArgs()).ConfigureAwait(false);

            Assert.Equal(2, _counter);
            x2.Unsubscribe(x1);
            await x2.InvokeAsync(this, new EventArgs()).ConfigureAwait(false);

            await x2.InvokeAsync(this, new EventArgs()).ConfigureAwait(false);

            Assert.Equal(2, _counter);
        }
Ejemplo n.º 5
0
        public async void AsyncEventDispatches()
        {
            var counter = 0;
            var ev      = new AsyncEvent();

            Task MyMethod()
            {
                counter++;
                return(Task.CompletedTask);
            }

            ev.Subscribe(MyMethod);
            await ev.InvokeAsync(this, new EventArgs());

            Assert.Equal(1, counter);

            ev.Unsubscribe(MyMethod);
            await ev.InvokeAsync(this, new EventArgs());

            Assert.Equal(1, counter);
        }
Ejemplo n.º 6
0
        public async void AsyncEventPassesAlong()
        {
            var counter = 0;
            var ev      = new AsyncEvent();

            ev.Subscribe(() =>
            {
                counter++;
                return(Task.CompletedTask);
            });
            var ev2 = new AsyncEvent();

            ev2.Subscribe(ev);
            await ev2.InvokeAsync(this, new EventArgs());

            Assert.Equal(1, counter);

            ev2.Unsubscribe(ev);
            await ev2.InvokeAsync(this, new EventArgs());

            Assert.Equal(1, counter);
        }
Ejemplo n.º 7
0
        public async void AsyncEventSubscribeHandler()
        {
            var counter = 0;

            Task MyMethod(object sender, EventArgs args)
            {
                counter++;
                return(Task.CompletedTask);
            }

            var handler = new AsyncEventHandler <EventArgs>(MyMethod);
            var ev      = new AsyncEvent();

            ev.Subscribe(handler);
            await ev.InvokeAsync(this, new EventArgs());

            Assert.Equal(1, counter);

            ev.Unsubscribe(handler);
            await ev.InvokeAsync(this, new EventArgs());

            Assert.Equal(1, counter);
        }