Exemple #1
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));
            }
        }
Exemple #2
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;
            }
        }