예제 #1
0
        public void CompositeDisposable_CopyTo_Index_Out_Of_Range()
        {
            var d1 = new BooleanDisposable();
            var d2 = new BooleanDisposable();

            var composite = new CompositeDisposable(d1, d2);

            var array = new IDisposable[2];

            try
            {
                composite.CopyTo(array, 1);
                Assert.False(true, "Should have thrown!");
            }
            catch (ArgumentOutOfRangeException)
            {
                // expected
            }
        }
예제 #2
0
        public void ScheduledDisposable()
        {
            var d = new BooleanDisposable();
            var s = new ScheduledDisposable(Scheduler.Immediate, d);

            Assert.False(d.IsDisposed);

            Assert.Same(Scheduler.Immediate, s.Scheduler);
            Assert.Same(d, s.Disposable);

            s.Dispose();

            Assert.True(d.IsDisposed);
            Assert.True(s.IsDisposed);

            Assert.Same(Scheduler.Immediate, s.Scheduler);
            //Assert.Same(d, s.Disposable); // BREAKING CHANGE v2 > v1.x - Undefined behavior after disposal.
            s.Disposable.Dispose();            // This should be a nop.
        }
예제 #3
0
        public static IObservable <int> Sync()
        {
            return(Observable.Create <int>(observer =>
            {
                var d = new BooleanDisposable();
                for (int i = 0; i < 10; i++)
                {
                    if (d.IsDisposed)
                    {
                        return Disposable.Empty;
                    }

                    observer.OnNext(i);
                    Thread.Sleep(100 * i);
                }
                observer.OnCompleted();
                observer.OnNext(100); // wrong implementation
                return d;
            }));
        }
예제 #4
0
        public void CompositeDisposable_GetEnumerator_Disposed_Entries()
        {
            var d1 = new BooleanDisposable();
            var d2 = new BooleanDisposable();
            var d3 = new BooleanDisposable();

            var composite = new CompositeDisposable(d1, d2, d3);

            composite.Remove(d2);

            var enumerator = composite.GetEnumerator();

            Assert.True(enumerator.MoveNext());
            Assert.Equal(d1, enumerator.Current);

            Assert.True(enumerator.MoveNext());
            Assert.Equal(d3, enumerator.Current);

            Assert.False(enumerator.MoveNext());
        }
        public void RefCount()
        {
            var ps = new PublishSubject <int>(true);

            var d = new BooleanDisposable();

            ps.OnSubscribe(d);

            Assert.False(d.IsDisposed());

            var to = ps.Test();

            Assert.True(ps.HasObservers);

            to.Dispose();

            Assert.False(ps.HasObservers);

            Assert.True(d.IsDisposed());
        }
예제 #6
0
        public async ValueTask SubscribeAsync(IAsyncObserver <T> observer)
        {
            var disposable = new BooleanDisposable();

            try
            {
                await observer.OnSubscribeAsync(disposable).ConfigureAwait(false);

                var items = await _enumerable.ConfigureAwait(false);

                foreach (var item in items)
                {
                    if (disposable.IsDisposed)
                    {
                        break;
                    }

                    var t = observer.OnNextAsync(item);

                    try
                    {
                        await t.ConfigureAwait(false);
                    }
                    catch (Exception ex)
                    {
                        await observer.OnErrorAsync(ex).ConfigureAwait(false);

                        break;
                    }
                }

                if (!disposable.IsDisposed)
                {
                    await observer.OnCompletedAsync().ConfigureAwait(false);
                }
            }
            finally
            {
                await observer.OnFinallyAsync().ConfigureAwait(false);
            }
        }
예제 #7
0
        public void RefCountDisposable_PrimaryDisposesFirst()
        {
            var d = new BooleanDisposable();
            var r = new RefCountDisposable(d);

            Assert.False(d.IsDisposed);

            var d1 = r.GetDisposable();
            var d2 = r.GetDisposable();

            Assert.False(d.IsDisposed);

            d1.Dispose();
            Assert.False(d.IsDisposed);

            r.Dispose();
            Assert.False(d.IsDisposed);

            d2.Dispose();
            Assert.True(d.IsDisposed);
        }
예제 #8
0
        public IDisposable Schedule <TState>(TState state, DateTimeOffset dueTime, Func <IScheduler, TState, IDisposable> action)
        {
            var disposable = new BooleanDisposable();

            var due = dueTime - DateTimeOffset.Now;

            if (due.TotalMilliseconds < 0)
            {
                due = TimeSpan.Zero;
            }

            _handler.PostDelayed(() =>
            {
                if (!disposable.IsDisposed)
                {
                    action(this, state);
                }
            }, (long)due.TotalMilliseconds);

            return(disposable);
        }
예제 #9
0
        /// <summary>
        /// Schedules action to be executed.
        /// </summary>
        public IDisposable Schedule(Action action)
        {
            if (action == null)
            {
                throw new ArgumentNullException("action");
            }

            var d = new BooleanDisposable();

            var thread = new Thread(() =>
            {
                if (!d.IsDisposed)
                {
                    action();
                }
            });

            thread.Start();

            return(d);
        }
예제 #10
0
        public async Task MultipleAssignmentDisposable_Test()
        {
            // arrange
            var d1 = new BooleanDisposable();
            var d2 = new BooleanDisposable();
            var d3 = new BooleanDisposable();
            // arrange
            var disp = new MultipleAssignmentDisposable();

            disp.Disposable = d1;

            // act & verify
            Assert.IsFalse(disp.IsDisposed);
            Assert.IsFalse(d1.IsDisposed);
            Assert.IsFalse(d2.IsDisposed);
            Assert.IsFalse(d3.IsDisposed);

            disp.Disposable = d2;
            disp.Dispose();

            Assert.IsTrue(disp.IsDisposed);
            Assert.IsFalse(d1.IsDisposed);
            Assert.IsTrue(d2.IsDisposed);
            Assert.IsFalse(d3.IsDisposed);

            var  gate = new ManualResetEventSlim(false);
            Task t    = Task.Run(() =>
            {
                gate.Wait();
                disp.Disposable = d3;
            });

            gate.Set();
            await t;

            Assert.IsTrue(disp.IsDisposed);
            Assert.IsFalse(d1.IsDisposed);
            Assert.IsTrue(d2.IsDisposed);
            Assert.IsTrue(d3.IsDisposed);
        }
예제 #11
0
        public async ValueTask SubscribeAsync(IAsyncObserver <int> observer)
        {
            var disposable = new BooleanDisposable();

            await observer.OnSubscribeAsync(disposable).ConfigureAwait(false);

            try
            {
                for (int i = _start; i < _end; i++)
                {
                    if (disposable.IsDisposed)
                    {
                        break;
                    }

                    var t = observer.OnNextAsync(i);

                    try
                    {
                        await t.ConfigureAwait(false);
                    }
                    catch (Exception ex)
                    {
                        await observer.OnErrorAsync(ex).ConfigureAwait(false);

                        break;
                    }
                }

                if (!disposable.IsDisposed)
                {
                    await observer.OnCompletedAsync().ConfigureAwait(false);
                }
            }
            finally
            {
                await observer.OnFinallyAsync().ConfigureAwait(false);
            }
        }
예제 #12
0
        public void Error()
        {
            var cs = new CompletableSubject();

            Assert.False(cs.HasObserver());
            Assert.False(cs.HasCompleted());
            Assert.False(cs.HasException());
            Assert.IsNull(cs.GetException());

            var to = cs.Test();

            Assert.True(cs.HasObserver());

            to.AssertEmpty();

            var bd = new BooleanDisposable();

            cs.OnSubscribe(bd);
            cs.OnError(new InvalidOperationException());
            cs.OnError(new IndexOutOfRangeException());

            Assert.True(bd.IsDisposed());

            to.AssertFailure(typeof(InvalidOperationException));

            Assert.False(cs.HasObserver());
            Assert.False(cs.HasCompleted());
            Assert.True(cs.HasException());
            Assert.IsNotNull(cs.GetException());
            Assert.True(typeof(InvalidOperationException).IsAssignableFrom(cs.GetException()));

            cs.Test().AssertFailure(typeof(InvalidOperationException));

            Assert.False(cs.HasObserver());
            Assert.False(cs.HasCompleted());
            Assert.True(cs.HasException());
            Assert.IsNotNull(cs.GetException());
            Assert.True(typeof(InvalidOperationException).IsAssignableFrom(cs.GetException()));
        }
예제 #13
0
            public void Run(ToObservable <TSource> parent)
            {
                var e = default(IEnumerator <TSource>);

                try
                {
                    e = parent._source.GetEnumerator();
                }
                catch (Exception exception)
                {
                    ForwardOnError(exception);

                    return;
                }

                var longRunning = parent._scheduler.AsLongRunning();

                if (longRunning != null)
                {
                    //
                    // Long-running schedulers have the contract they should *never* prevent
                    // the work from starting, such that the scheduled work has the chance
                    // to observe the cancellation and perform proper clean-up. In this case,
                    // we're sure Loop will be entered, allowing us to dispose the enumerator.
                    //
                    SetUpstream(longRunning.ScheduleLongRunning((@this: this, e), (tuple, cancelable) => [email protected](tuple.e, cancelable)));
                }
                else
                {
                    //
                    // We never allow the scheduled work to be cancelled. Instead, the flag
                    // is used to have LoopRec bail out and perform proper clean-up of the
                    // enumerator.
                    //
                    var flag = new BooleanDisposable();
                    parent._scheduler.Schedule(new State(this, flag, e), (state, action) => state.sink.LoopRec(state, action));
                    SetUpstream(flag);
                }
            }
예제 #14
0
            public IDisposable Run()
            {
                var e = default(IEnumerator <TSource>);

                try
                {
                    e = _parent._source.GetEnumerator();
                }
                catch (Exception exception)
                {
                    base._observer.OnError(exception);
                    base.Dispose();
                    return(Disposable.Empty);
                }

                var longRunning = _parent._scheduler.AsLongRunning();

                if (longRunning != null)
                {
                    //
                    // Long-running schedulers have the contract they should *never* prevent
                    // the work from starting, such that the scheduled work has the chance
                    // to observe the cancellation and perform proper clean-up. In this case,
                    // we're sure Loop will be entered, allowing us to dispose the enumerator.
                    //
                    return(longRunning.ScheduleLongRunning(e, Loop));
                }
                else
                {
                    //
                    // We never allow the scheduled work to be cancelled. Instead, the flag
                    // is used to have LoopRec bail out and perform proper clean-up of the
                    // enumerator.
                    //
                    var flag = new BooleanDisposable();
                    _parent._scheduler.Schedule(new State(flag, e), LoopRec);
                    return(flag);
                }
            }
예제 #15
0
파일: Producer.cs 프로젝트: bnayae/rx-cs
        public IDisposable Subscribe(IObserver <int> observer)
        {
            var d = new BooleanDisposable();

            Task.Run(() =>
            {
                for (int i = 0; i < 10; i++)
                {
                    if (d.IsDisposed)
                    {
                        return;
                    }

                    observer.OnNext(i);
                    Thread.Sleep(100 * i);
                }
                observer.OnCompleted();
                //observer.OnNext(100); // wrong implementation
            });

            return(d);
        }
예제 #16
0
        public async ValueTask SubscribeAsync(IAsyncObserver <T> observer)
        {
            var disposable = new BooleanDisposable();

            await observer.OnSubscribeAsync(disposable).ConfigureAwait(false);

            try
            {
                if (disposable.IsDisposed)
                {
                    return;
                }

                var t = observer.OnNextAsync(_value);

                try
                {
                    await t.ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    if (!disposable.IsDisposed)
                    {
                        await observer.OnErrorAsync(ex).ConfigureAwait(false);
                    }
                    return;
                }

                if (!disposable.IsDisposed)
                {
                    await observer.OnCompletedAsync().ConfigureAwait(false);
                }
            }
            finally
            {
                await observer.OnFinallyAsync().ConfigureAwait(false);
            }
        }
예제 #17
0
        public void Basic()
        {
            var cs = new CompletableSubject();

            Assert.False(cs.HasObservers);
            Assert.False(cs.HasCompleted());
            Assert.False(cs.HasException());
            Assert.IsNull(cs.GetException());

            var to = cs.Test();

            Assert.True(cs.HasObserver());

            to.AssertEmpty();

            var bd = new BooleanDisposable();

            cs.OnSubscribe(bd);

            cs.OnCompleted();
            cs.OnError(new IndexOutOfRangeException());

            Assert.True(bd.IsDisposed());

            to.AssertResult();

            Assert.False(cs.HasObservers);
            Assert.True(cs.HasCompleted());
            Assert.False(cs.HasException());
            Assert.IsNull(cs.GetException());

            cs.Test().AssertResult();

            Assert.False(cs.HasObserver());
            Assert.True(cs.HasCompleted());
            Assert.False(cs.HasException());
            Assert.IsNull(cs.GetException());
        }
예제 #18
0
        protected override IObservable <int> OnQuery()
        {
            var xs = Observable.Create <int>(async observer =>
            {
                var disp = new BooleanDisposable();
                for (int i = 1; i <= 20; i++)
                {
                    if (disp.IsDisposed)
                    {
                        break;
                    }
                    await Task.Delay(150 * i);
                    observer.OnNext(i);
                }
                observer.OnCompleted();
                return(disp);
            });

            xs = xs.Monitor("Source", Order)
                 .Sample(TimeSpan.FromSeconds(1.5))
                 .Monitor("Sample", Order + 0.1);
            return(xs);
        }
예제 #19
0
        /// <summary>
        /// Schedules a long-running task by creating a new thread. Cancellation happens through polling.
        /// </summary>
        /// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam>
        /// <param name="state">State passed to the action to be executed.</param>
        /// <param name="action">Action to be executed.</param>
        /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
        /// <exception cref="ArgumentNullException"><paramref name="action"/> is null.</exception>
        public IDisposable ScheduleLongRunning <TState>(TState state, Action <TState, ICancelable> action)
        {
            if (action == null)
            {
                throw new ArgumentNullException("action");
            }

            var d = new BooleanDisposable();

            var thread = _threadFactory(() =>
            {
                //
                // Notice we don't check d.IsDisposed. The contract for ISchedulerLongRunning
                // requires us to ensure the scheduled work gets an opportunity to observe
                // the cancellation request.
                //
                action(state, d);
            });

            thread.Start();

            return(d);
        }
예제 #20
0
        public static void SingleAssignmentDisposable_WillOnlyAllowSettingTheUnderlyingDisposableOnce_FurtherModificationsWillMakeItThrow()
        {
            var singleAssignmentDisposable = new SingleAssignmentDisposable();
            var notDisposedDisposable      = Disposable.Create(() => Console.WriteLine("I am gonna be disposed because I was first."));
            var replacementDisposable      = new BooleanDisposable();//Disposable.Create(() => Console.WriteLine("I can't replace the first assigned disposable. Estupido!"));

            singleAssignmentDisposable.Disposable = notDisposedDisposable;

            try
            {
                singleAssignmentDisposable.Disposable = replacementDisposable;
            }
            catch
            {
                Console.WriteLine("Told you you can't replace it once it has been set.");
            }

            Console.WriteLine("Disposing of the single assignement disposable.");
            singleAssignmentDisposable.Dispose();

            Console.WriteLine($"Single assignement disposable is disposed: {singleAssignmentDisposable.IsDisposed}.");
            Console.WriteLine($"Replacement disposable is disposed: {replacementDisposable.IsDisposed}.");
        }
예제 #21
0
        public IDisposable Schedule(Action action, ushort dueTime)
        {
            if (dueTime == 0)
            {
                dueTime = 1;
            }

            var disposable = new BooleanDisposable();

            var runAt = (ushort)Math.Min(Ticks + dueTime, ushort.MaxValue);

            var run = new Action(() =>
            {
                if (!disposable.IsDisposed)
                {
                    action();
                }
            });

            queue.Enqueue(new Item(runAt, scheduled++, run));

            return(disposable);
        }
예제 #22
0
        /// <summary>
        /// Schedules action to be executed.
        /// </summary>
        public IDisposable Schedule(Action action)
        {
            if (action == null)
            {
                throw new ArgumentNullException("action");
            }

            var cancelable = new BooleanDisposable();

            if (queue == null)
            {
                try
                {
                    queue = new Queue <Action>();
                    action();
                    while (queue.Count > 0)
                    {
                        queue.Dequeue()();
                    }
                }
                finally
                {
                    queue = null;
                }
            }
            else
            {
                queue.Enqueue(() =>
                {
                    if (!cancelable.IsDisposed)
                    {
                        action();
                    }
                });
            }
            return(cancelable);
        }
예제 #23
0
        public void CompositeDisposable_DisposeAll_Test()
        {
            // arrange
            var d1   = new BooleanDisposable();
            var d2   = new BooleanDisposable();
            var d3   = new BooleanDisposable();
            var disp = new CompositeDisposable(d1, d2);

            disp.Add(d3);

            // act & verify

            Assert.IsFalse(d1.IsDisposed);
            Assert.IsFalse(d2.IsDisposed);
            Assert.IsFalse(d3.IsDisposed);
            Assert.IsFalse(disp.IsDisposed);

            disp.Dispose(); // dispose all

            Assert.IsTrue(d1.IsDisposed);
            Assert.IsTrue(d2.IsDisposed);
            Assert.IsTrue(d3.IsDisposed);
            Assert.IsTrue(disp.IsDisposed);
        }
예제 #24
0
        public void When_commit_and_reject_actions_are_specified_in_the_constructor_then_reject_is_only_called_when_the_outer_unit_of_work_is_committed()
        {
            var disposable  = new BooleanDisposable();
            var rejectCount = 0;
            var commitCount = 0;
            Func <UnitOfWork <BooleanDisposable> > create = () =>
                                                            new UnitOfWork <BooleanDisposable>(() => disposable,
                                                                                               reject: d => rejectCount++,
                                                                                               commit: d => commitCount++);

            using (var outer = create())
            {
                using (create())
                {
                }
                disposable.IsDisposed.Should().BeFalse();
                rejectCount.Should().Be(1);
                outer.VoteCommit();
            }

            commitCount.Should().Be(0);
            rejectCount.Should().Be(1);
            disposable.IsDisposed.Should().BeTrue();
        }
        public IDisposable ScheduleLongRunning <TState>(TState state, Action <TState, ICancelable> action)
        {
            var d = new BooleanDisposable();

            var eb = new ManualResetEvent(false);

            _setStart(eb);

            var ee = new ManualResetEvent(false);

            _setEnd(ee);

            Task.Run(() =>
            {
                eb.Set();
                try
                {
                    action(state, d);
                }
                catch (Exception ex)
                {
                    if (_setException == null)
                    {
                        throw;
                    }

                    _setException(ex);
                }
                finally
                {
                    ee.Set();
                }
            });

            return(d);
        }
예제 #26
0
파일: UnitTests.cs 프로젝트: ragcat/UniRx
 public void Boolean()
 {
     var bd = new BooleanDisposable();
     bd.IsDisposed.IsFalse();
     bd.Dispose();
     bd.IsDisposed.IsTrue();
 }
예제 #27
0
        public void OnGUI()
        {
            var xpos = 0;
            var ypos = 0;

            if (GUI.Button(new Rect(xpos, ypos, 100, 100), "Clear"))
            {
                logtext.Length = 0;
                disposables.Clear();
            }
            ypos += 100;
            if (GUI.Button(new Rect(xpos, ypos, 100, 100), "CurrentThreadScheduler"))
            {
                try
                {
                    Scheduler.CurrentThread.Schedule(() =>
                    {
                        try
                        {
                            logtext.AppendLine("test threadscheduler");
                        }
                        catch (Exception ex)
                        {
                            logtext.AppendLine("innner ex" + ex.ToString());
                        }
                    });
                }
                catch (Exception ex)
                {
                    logtext.AppendLine("outer ex" + ex.ToString());
                }
            }
            ypos += 100;
            if (GUI.Button(new Rect(xpos, ypos, 100, 100), "EveryUpdate"))
            {
                Observable.EveryUpdate()
                .Subscribe(x => logtext.AppendLine(x.ToString()), ex => logtext.AppendLine("ex:" + ex.ToString()))
                .AddTo(disposables);
            }
            ypos += 100;
            if (GUI.Button(new Rect(xpos, ypos, 100, 100), "FromCoroutinePure"))
            {
                Observable.Create <Unit>(observer =>
                {
                    var cancel = new BooleanDisposable();

                    MainThreadDispatcher.StartCoroutine(Hoge(observer));

                    return(cancel);
                })
                .Subscribe(x => logtext.AppendLine(x.ToString()), ex => logtext.AppendLine("ex:" + ex.ToString()));
            }
            ypos += 100;
            if (GUI.Button(new Rect(xpos, ypos, 100, 100), "FromCoroutine"))
            {
                Observable.FromCoroutine <Unit>(Hoge)
                .Subscribe(x => logtext.AppendLine(x.ToString()), ex => logtext.AppendLine("ex:" + ex.ToString()));
            }


            /*
             *
             * ypos += 100;
             * if (GUI.Button(new Rect(xpos, ypos, 100, 100), "TimeScale-1"))
             * {
             *  Time.timeScale -= 1f;
             * }
             *
             * ypos += 100;
             * if (GUI.Button(new Rect(xpos, ypos, 100, 100), "TimeScale+1"))
             * {
             *  Time.timeScale += 1f;
             * }
             *
             * ypos += 100;
             * if (GUI.Button(new Rect(xpos, ypos, 100, 100), "TimeScale=0"))
             * {
             *  Time.timeScale = 0;
             * }
             *
             * ypos += 100;
             * if (GUI.Button(new Rect(xpos, ypos, 100, 100), "TimeScale=100"))
             * {
             *  Time.timeScale = 100;
             * }
             *
             * ypos += 100;
             * if (GUI.Button(new Rect(xpos, ypos, 100, 100), "Scheduler0"))
             * {
             *  logger.Debug("run");
             *  Scheduler.MainThread.Schedule(TimeSpan.FromMilliseconds(5000), () =>
             *  {
             *      logger.Debug(DateTime.Now);
             *  });
             * }
             *
             * xpos += 100;
             * ypos = 0;
             * if (GUI.Button(new Rect(xpos, ypos, 100, 100), "Scheduler1"))
             * {
             *  logger.Debug("Before Start");
             *  Scheduler.MainThread.Schedule(() => logger.Debug("immediate"));
             *  Scheduler.MainThread.Schedule(TimeSpan.Zero, () => logger.Debug("zero span"));
             *  Scheduler.MainThread.Schedule(TimeSpan.FromMilliseconds(1), () => logger.Debug("0.1 span"));
             *  logger.Debug("After Start");
             * }
             *
             * ypos += 100;
             * if (GUI.Button(new Rect(xpos, ypos, 100, 100), "Scheduler2"))
             * {
             *  logger.Debug("M:Before Start");
             *  Scheduler.MainThread.Schedule(TimeSpan.FromSeconds(5), () => logger.Debug("M:after 5 minutes"));
             *  Scheduler.MainThread.Schedule(TimeSpan.FromMilliseconds(5500), () => logger.Debug("M:after 5.5 minutes"));
             * }
             *
             * ypos += 100;
             * if (GUI.Button(new Rect(xpos, ypos, 100, 100), "Realtime"))
             * {
             *  logger.Debug("R:Before Start");
             *  Scheduler.MainThreadIgnoreTimeScale.Schedule(TimeSpan.FromSeconds(5), () => logger.Debug("R:after 5 minutes"));
             *  Scheduler.MainThreadIgnoreTimeScale.Schedule(TimeSpan.FromMilliseconds(5500), () => logger.Debug("R:after 5.5 minutes"));
             * }
             *
             #if !UNITY_METRO
             *
             * ypos += 100;
             * if (GUI.Button(new Rect(xpos, ypos, 100, 100), "ManagedThreadId"))
             * {
             *  logger.Debug("Current:" + Thread.CurrentThread.ManagedThreadId);
             *  new Thread(_ => logger.Debug("NewThread:" + Thread.CurrentThread.ManagedThreadId)).Start();
             *  ThreadPool.QueueUserWorkItem(_ =>
             *  {
             *      logger.Debug("ThraedPool:" + Thread.CurrentThread.ManagedThreadId);
             *      this.transform.position = new Vector3(0, 0, 0); // exception
             *  });
             * }
             *
             * ypos += 100;
             * if (GUI.Button(new Rect(xpos, ypos, 100, 100), "ThreadStatic"))
             * {
             *  logger.Debug(threadstaticobj != null);
             *  new Thread(_ => logger.Debug(threadstaticobj != null)).Start();
             *  ThreadPool.QueueUserWorkItem(_ => logger.Debug(threadstaticobj != null));
             * }
             *
             * ypos += 100;
             * if (GUI.Button(new Rect(xpos, ypos, 100, 100), "Log"))
             * {
             *  logger.Debug("test", this);
             *  ThreadPool.QueueUserWorkItem(_ => logger.Debug("test2", this));
             * }
             *
             #endif
             *
             * ypos += 100;
             * if (GUI.Button(new Rect(xpos, ypos, 100, 100), "POST"))
             * {
             *  var form = new WWWForm();
             *  form.AddField("test", "abcdefg");
             *  ObservableWWW.PostWWW("http://localhost:53395/Handler1.ashx", form, new Hash
             * {
             *  {"aaaa", "bbb"},
             *  {"User-Agent", "HugaHuga"}
             * })
             *  .Subscribe(x => logger.Debug(x.text));
             * }
             *
             * xpos += 100;
             * ypos = 0;
             * if (GUI.Button(new Rect(xpos, ypos, 100, 100), "Yield"))
             * {
             *  yieldCancel = Observable.FromCoroutineValue<string>(StringYield, false)
             *      .Subscribe(x => logger.Debug(x), ex => logger.Debug("E-x:" + ex));
             * }
             *
             * ypos += 100;
             * if (GUI.Button(new Rect(xpos, ypos, 100, 100), "YieldCancel"))
             * {
             *  yieldCancel.Dispose();
             * }
             *
             * ypos += 100;
             * if (GUI.Button(new Rect(xpos, ypos, 100, 100), "ThreadPool"))
             * {
             *  Observable.Timer(TimeSpan.FromMilliseconds(400), Scheduler.ThreadPool)
             *      .ObserveOnMainThread()
             *      .Subscribe(x => logger.Debug(x));
             * }
             *
             * ypos += 100;
             * if (GUI.Button(new Rect(xpos, ypos, 100, 100), "Subscribe"))
             * {
             *  subscriber.InitSubscriptions();
             *  logger.Debug("Subscribe++ : " + subscriber.SubscriptionCount);
             * }
             *
             * ypos += 100;
             * if (GUI.Button(new Rect(xpos, ypos, 100, 100), "Push"))
             * {
             *  Publisher.foo();
             * }
             *
             * ypos += 100;
             * if (GUI.Button(new Rect(xpos, ypos, 100, 100), "Unsubscriber"))
             * {
             *  subscriber.RemoveSubscriptions();
             *  logger.Debug("UnsubscribeAll : " + subscriber.SubscriptionCount);
             * }
             *
             * ypos += 100;
             * if (GUI.Button(new Rect(xpos, ypos, 100, 100), "DistinctUntilChanged"))
             * {
             *  new[] { "hoge", null, null, "huga", "huga", "hoge" }
             *      .ToObservable()
             *      .DistinctUntilChanged()
             *      .Subscribe(x => logger.Debug(x));
             * }
             * */

            // Time

            var sb = new StringBuilder();

            sb.AppendLine("CaptureFramerate:" + Time.captureFramerate);
            sb.AppendLine("deltaTime:" + Time.deltaTime);
            sb.AppendLine("fixedDeltaTime:" + Time.fixedDeltaTime);
            sb.AppendLine("fixedTime:" + Time.fixedTime);
            sb.AppendLine("frameCount:" + Time.frameCount);
            sb.AppendLine("maximumDeltaTime:" + Time.maximumDeltaTime);
            sb.AppendLine("realtimeSinceStartup:" + Time.realtimeSinceStartup);
            sb.AppendLine("renderedFrameCount:" + Time.renderedFrameCount);
            sb.AppendLine("smoothDeltaTime:" + Time.smoothDeltaTime);
            sb.AppendLine("time:" + Time.time);
            sb.AppendLine("timeScale:" + Time.timeScale);
            sb.AppendLine("timeSinceLevelLoad:" + Time.timeSinceLevelLoad);
            sb.AppendLine("unscaledDeltaTime:" + Time.unscaledDeltaTime);
            sb.AppendLine("unscaledTime:" + Time.unscaledTime);

            //GUI.Box(new Rect(Screen.width - 300, Screen.height - 300, 300, 300), "Time");
            //GUI.Label(new Rect(Screen.width - 290, Screen.height - 290, 290, 290), sb.ToString());

            // logtext only
            GUI.Box(new Rect(Screen.width - 300, Screen.height - 300, 300, 300), "logtext");
            GUI.Label(new Rect(Screen.width - 290, Screen.height - 290, 290, 290), logtext.ToString());

            // Log
            //GUI.Box(new Rect(Screen.width - 300, 0, 300, 300), "Log");
            //GUI.Label(new Rect(Screen.width - 290, 10, 290, 290), logtext.ToString());
        }
예제 #28
0
        //----- method -----

        public YieldCancell()
        {
            cancelDisposable  = new BooleanDisposable();
            cancellationToken = new CancellationToken(cancelDisposable);
        }
예제 #29
0
            public IDisposable Schedule(TimeSpan dueTime, Action action)
            {
                var d = new BooleanDisposable();
                var time = Scheduler.Normalize(dueTime);

                MainThreadDispatcher.SendStartCoroutine(DelayAction(time, () =>
                {
                    if (!d.IsDisposed)
                    {
                        action();
                    }
                }, d));

                return d;
            }
예제 #30
0
        /// <summary>
        /// Subscribes an observer to an enumerable sequence.  Returns an object that can be used to unsubscribe the observer from the enumerable.
        /// </summary>
        public static IDisposable Subscribe <TSource>(this IEnumerable <TSource> source, IObserver <TSource> observer, IScheduler scheduler)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            if (observer == null)
            {
                throw new ArgumentNullException("observer");
            }
            if (scheduler == null)
            {
                throw new ArgumentNullException("scheduler");
            }

            var e    = source.GetEnumerator();
            var flag = new BooleanDisposable();

            scheduler.Schedule(self =>
            {
                var hasNext = false;
                var ex      = default(Exception);
                var current = default(TSource);

                if (flag.IsDisposed)
                {
                    e.Dispose();
                    return;
                }

                try
                {
                    hasNext = e.MoveNext();
                    if (hasNext)
                    {
                        current = e.Current;
                    }
                }
                catch (Exception exception)
                {
                    ex = exception;
                }

                if (!hasNext || ex != null)
                {
                    e.Dispose();
                }

                if (ex != null)
                {
                    observer.OnError(ex);
                    return;
                }

                if (!hasNext)
                {
                    observer.OnCompleted();
                    return;
                }

                observer.OnNext(current);
                self();
            });

            return(flag);
        }
예제 #31
0
 public IDisposable Run()
 {
     cancelationToken = new BooleanDisposable();
     ((SingleAssignmentDisposable)(sourceSubscription = new SingleAssignmentDisposable())).Disposable = parent.source.Subscribe(this);
     return(StableCompositeDisposable.Create(cancelationToken, sourceSubscription));
 }
예제 #32
0
 public IDisposable Schedule(Action action)
 {
     var d = new BooleanDisposable();
     MainThreadDispatcher.Post(() =>
     {
         if (!d.IsDisposed)
         {
             action();
         }
     });
     return d;
 }