public ValueProjectionObservable(StoreBase <TState> store, ValueProjector <TState, T> projector, Action onEmpty)
     : base(onEmpty)
 {
     _store        = store;
     _projector    = projector;
     _currentValue = projector(_store.CurrentState);
 }
示例#2
0
        /// <summary>
        ///  Send a value to all subscribers of a projection, simulating a change
        ///  to the store state.
        /// </summary>
        /// <param name="projector">
        ///  The projection function being simulated. The function is not called.
        /// </param>
        /// <param name="value">
        ///  The value to be sent to the project subscribers.
        /// </param>
        public void Publish <T>(ValueProjector <TState, T> projector, T value)
        {
            if (projector == null)
            {
                throw new ArgumentNullException(nameof(projector));
            }

            var observable = GetObservableForProjection <T>(projector);

            observable?.PublishValue(value);
        }
示例#3
0
        /// <summary>
        ///  Observe a projection of the application state for changes.
        /// </summary>
        /// <typeparam name="T">
        ///  The projector's return type.
        /// </typeparam>
        /// <param name="projector">
        ///  The projection function. Projections are pure static functions which receive
        ///  the current state and return a value based on that state.
        /// </param>
        /// <returns>
        ///  An <see cref="IObservable{T}"/> which can be subscribed for changes.
        /// </returns>
        public IProjectionObservable <T> Observe <T>(ValueProjector <TState, T> projector)
        {
            if (projector == null)
            {
                throw new ArgumentNullException(nameof(projector));
            }

            // if we've seen this projector before, reuse the observable we created for it, else create one
            var observable = GetObservableForProjection <T>(projector);

            if (observable == null)
            {
                observable   = new ValueProjectionObservable <TState, T>(this, projector, () => ForgetProjection(projector));
                _observables = _observables.Add(projector, (IStateListener <TState>)observable);
            }

            return(observable);
        }
示例#4
0
        /// <summary>
        ///  Apply a projection function to the store and return its value immediately.
        /// </summary>
        /// <typeparam name="T">
        ///  The projector's return type.
        /// </typeparam>
        /// <param name="projector">
        ///  The projection function. Projections are pure static functions which receive
        ///  the current state and return a value based on that state.
        /// </param>
        /// <returns>
        ///  The value returned by the projection function.
        /// </returns>
        public T Project <T>(ValueProjector <TState, T> projector)
        {
            var value = projector(CurrentState);

            return(value);
        }