示例#1
0
        /// <summary>
        /// Loads the current state and history states from a persisted state (<see cref="Save"/>).
        /// The loader should return exactly the data that was passed to the saver.
        /// </summary>
        /// <param name="stateMachineLoader">Loader providing persisted data.</param>
        /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
        public async Task Load(IAsyncStateMachineLoader <TState> stateMachineLoader)
        {
            Guard.AgainstNullArgument("stateMachineLoader", stateMachineLoader);

            this.CheckThatNotAlreadyInitialized();

            this.initialized = await this.stateMachine.Load(stateMachineLoader).ConfigureAwait(false);
        }
示例#2
0
        public async Task Load(IAsyncStateMachineLoader <TState> stateMachineLoader)
        {
            Guard.AgainstNullArgument("stateMachineLoader", stateMachineLoader);
            this.CheckThatStateMachineIsNotAlreadyInitialized();

            await this.LoadCurrentState(stateMachineLoader).ConfigureAwait(false);

            await this.LoadHistoryStates(stateMachineLoader).ConfigureAwait(false);
        }
示例#3
0
        public async Task <bool> Load(IAsyncStateMachineLoader <TState> stateMachineLoader)
        {
            Guard.AgainstNullArgument(nameof(stateMachineLoader), stateMachineLoader);
            this.CheckThatStateMachineIsNotAlreadyInitialized();

            Initializable <TState> loadedCurrentState = await stateMachineLoader.LoadCurrentState().ConfigureAwait(false);

            IDictionary <TState, TState> historyStates = await stateMachineLoader.LoadHistoryStates().ConfigureAwait(false);

            var initialized = SetCurrentState();

            LoadHistoryStates();
            NotifyExtensions();

            return(initialized);

            bool SetCurrentState()
            {
                if (loadedCurrentState.IsInitialized)
                {
                    this.currentState = this.states[loadedCurrentState.Value];
                    return(true);
                }

                this.currentState = null;
                return(false);
            }

            void LoadHistoryStates()
            {
                foreach (KeyValuePair <TState, TState> historyState in historyStates)
                {
                    IState <TState, TEvent> superState      = this.states[historyState.Key];
                    IState <TState, TEvent> lastActiveState = this.states[historyState.Value];

                    if (!superState.SubStates.Contains(lastActiveState))
                    {
                        throw new InvalidOperationException(ExceptionMessages.CannotSetALastActiveStateThatIsNotASubState);
                    }

                    superState.LastActiveState = lastActiveState;
                }
            }

            void NotifyExtensions()
            {
                this.extensions.ForEach(
                    extension => extension.Loaded(
                        this,
                        loadedCurrentState,
                        historyStates));
            }
        }
示例#4
0
        private async Task LoadHistoryStates(IAsyncStateMachineLoader <TState> stateMachineLoader)
        {
            IDictionary <TState, TState> historyStates = await stateMachineLoader.LoadHistoryStates().ConfigureAwait(false);

            foreach (KeyValuePair <TState, TState> historyState in historyStates)
            {
                IState <TState, TEvent> superState      = this.states[historyState.Key];
                IState <TState, TEvent> lastActiveState = this.states[historyState.Value];

                if (!superState.SubStates.Contains(lastActiveState))
                {
                    throw new InvalidOperationException(ExceptionMessages.CannotSetALastActiveStateThatIsNotASubState);
                }

                superState.LastActiveState = lastActiveState;
            }
        }
示例#5
0
        private async Task LoadCurrentState(IAsyncStateMachineLoader <TState> stateMachineLoader)
        {
            Initializable <TState> loadedCurrentState = await stateMachineLoader.LoadCurrentState().ConfigureAwait(false);

            this.currentState = loadedCurrentState.IsInitialized ? this.states[loadedCurrentState.Value] : null;
        }