public void ImplicitRelease()
        {
            var supplier = new OnDemandEvent <int>();
            int reciever = 0;
            WeakReference <IDisposable> weak = null;

            void exec()
            {
                var d = supplier.WeakSubscribe <OnDemandEvent <int> >(
                    (sup, hdr) => sup.TestEvent += hdr,
                    (sup, hdr) => sup.TestEvent -= hdr,
                    (s, e) => reciever          += 1);

                supplier.Fire();
                weak = new WeakReference <IDisposable>(d, true);
            };

            // execute local method.
            exec();
            reciever.Is(1);

            supplier.Fire();
            reciever.Is(2);

            // Force GC.
            GC.Collect(0, GCCollectionMode.Forced);
            GC.WaitForPendingFinalizers();
            GC.Collect(0, GCCollectionMode.Forced); // just to be sure
            System.Threading.Tasks.Task.Delay(100).Wait();

            supplier.Fire();

            // 'reciever' should stay '2', because subscription should be automatically released.
            reciever.Is(2);
        }
 public void NullChecks(
     OnDemandEvent <int> supplier,
     Action <OnDemandEvent <int>, EventHandler <int> > addHandler,
     Action <OnDemandEvent <int>, EventHandler <int> > removeHandler,
     EventHandler <int> handler,
     string expect)
 => Assert.Throws <ArgumentNullException>(
     () => supplier.WeakSubscribe(addHandler, removeHandler, handler))
 .ParamName.Is(expect);
 public void NullChecks(
     OnDemandEvent <int> supplier,
     Func <Action <string>, EventHandler> converter,
     Action <OnDemandEvent <int>, EventHandler> addHandler,
     Action <OnDemandEvent <int>, EventHandler> removeHandler,
     Action <string> handler,
     string expect)
 => Assert.Throws <ArgumentNullException>(
     () => supplier.WeakSubscribe <OnDemandEvent <int>, Action <string> >(
         converter,
         addHandler,
         removeHandler,
         handler))
 .ParamName.Is(expect);
        public void NormalRelease()
        {
            var supplier = new OnDemandEvent <int>();
            int reciever = 0;
            var dispo    = supplier.WeakSubscribe <OnDemandEvent <int> >(
                (sup, hdr) => sup.TestEvent += hdr,
                (sup, hdr) => sup.TestEvent -= hdr,
                (s, e) => { reciever += 1; });

            supplier.Fire();
            reciever.Is(1);

            dispo.Dispose();
            supplier.Fire();
            reciever.Is(1);
        }
        public void NormalRelease()
        {
            var    supplier = new OnDemandEvent <int>();
            int    reciever = int.MinValue;
            string sender   = null;
            var    dispo    = supplier.WeakSubscribe <OnDemandEvent <int>, int>(
                (sup, hdr) => sup.GenericTestEvent += hdr,
                (sup, hdr) => sup.GenericTestEvent -= hdr,
                (s, e) => { reciever = e; sender = s.GetType().Name; });

            supplier.Fire(1);
            reciever.Is(1);

            dispo.Dispose();
            supplier.Fire(3);
            reciever.Is(1);
        }
        public void NormalRelease()
        {
            var    supplier = new OnDemandEvent <int>();
            string reciever = null;

            var dispo = supplier.WeakSubscribe <OnDemandEvent <int>, int, Action <string> >(
                f => (s, e) => f(e.ToString()),
                (sup, hdr) => sup.GenericTestEvent += hdr,
                (sup, hdr) => sup.GenericTestEvent -= hdr,
                e => { reciever = e; });

            supplier.Fire(1);
            reciever.Is(1.ToString());

            dispo.Dispose();
            supplier.Fire(3);
            reciever.Is(1.ToString());
        }
        public void NoConvert()
        {
            var supplier = new OnDemandEvent <int>();
            int reciever = int.MinValue;

            var dispo = supplier.WeakSubscribe <OnDemandEvent <int>, int, EventHandler <int> >(
                f => (s, e) => f(s, e),
                (sup, hdr) => sup.GenericTestEvent += hdr,
                (sup, hdr) => sup.GenericTestEvent -= hdr,
                (s, e) => { reciever = e; });

            supplier.Fire(1);
            reciever.Is(1);

            dispo.Dispose();
            supplier.Fire(3);
            reciever.Is(1);
        }
        public void NoConvert()
        {
            var supplier = new OnDemandEvent <int>();
            int reciever = 0;

            var dispo = supplier.WeakSubscribe <OnDemandEvent <int>, EventHandler>(
                f => (s, e) => f(s, e),
                (sup, hdr) => sup.TestEvent += hdr,
                (sup, hdr) => sup.TestEvent -= hdr,
                (s, e) => { reciever += 1; });

            supplier.Fire();
            reciever.Is(1);

            dispo.Dispose();
            supplier.Fire();
            reciever.Is(1);
        }
        public void NormalRelease()
        {
            var    supplier = new OnDemandEvent <int>();
            string reciever = null;
            int    count    = 0;

            var dispo = supplier.WeakSubscribe <OnDemandEvent <int>, Action <string> >(
                f => (s, e) => f($"{++count}"),
                (sup, hdr) => sup.TestEvent += hdr,
                (sup, hdr) => sup.TestEvent -= hdr,
                e => { reciever = e; });

            supplier.Fire();
            reciever.Is(1.ToString());

            dispo.Dispose();
            supplier.Fire();
            reciever.Is(1.ToString());
        }