Beispiel #1
0
        public void Cache()
        {
            var        input = new ReactiveVariable <int>(2); //We define a reactive variable.
            Func <int> f     = () =>                          //f is the calculation we want to cache.
            {
                Console.WriteLine("f was evaluated");
                return(input.Value * input.Value);     //f depends on our reactive variable input.
            };
            var cache = new ReactiveCache <int>(f);    //We base our cache on f.

            Console.WriteLine("f() = " + cache.Get()); //Cache was not set so we evaluate f.
            Console.WriteLine("f() = " + cache.Get()); //Cache is set so we don't evaluate f.

            input.Value = 3;                           //We change our input variable, causing our cache to become stale.
            Console.WriteLine("f() = " + cache.Get()); //Cache is stale, so we must evaluate f.
        }
        public void CanCache()
        {
            var        counter   = 0;
            var        source    = new DebugReactiveVariable <int>(0, "source");
            Func <int> cacheFunc = () =>
            {
                counter++;
                return(source.Value);
            };
            var cache = new ReactiveCache <int>(cacheFunc);

            Assert.AreEqual(0, counter);
            Assert.AreEqual(0, cache.Get());
            Assert.AreEqual(1, counter);
            Assert.AreEqual(0, cache.Get());
            Assert.AreEqual(1, counter);
        }
        public void TestInvalidate()
        {
            var        counter   = 0;
            var        source    = new DebugReactiveVariable <int>(0, "source");
            Func <int> cacheFunc = () =>
            {
                counter++;
                return(source.Value);
            };
            var cache       = new ReactiveCache <int>(cacheFunc);
            var expectation = 0;

            Assert.AreEqual(0, cache.Get());
            Assert.AreEqual(++expectation, counter);
            cache.Invalidate();
            Assert.AreEqual(0, cache.Get());
            Assert.AreEqual(++expectation, counter);
        }
Beispiel #4
0
        public void TestDebugMethodAndProperties()
        {
            var dependency = new WeakDependency(0, null);

            Assert.AreEqual("empty reference", dependency.ToString());

            var variable = new DebugReactiveVariable <bool>(false, "yo");

            Assert.AreEqual("yo", variable.ToString());

            var expression = new DebugReactiveExpression <bool>(() => variable.Value);

            expression.Evaluate();
            Assert.AreEqual("unnamed", expression.ToString());
            Assert.True(variable.Dependents.Any());
            Assert.False(expression.Dependents.Any());


            var cache = new ReactiveCache <bool>(() => false);

            Assert.False(cache.Dependents.Any());
        }
Beispiel #5
0
 public sealed override void OnGetValue(LocationInterceptionArgs args)
 {
     cache      = cache ?? new ReactiveCache <object>(args.GetCurrentValue);
     args.Value = cache.Get();
 }