Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to deactive the state machine #31

Closed
nanatlantica opened this issue Sep 23, 2015 · 2 comments
Closed

How to deactive the state machine #31

nanatlantica opened this issue Sep 23, 2015 · 2 comments

Comments

@nanatlantica
Copy link

I only found the activate method, can't found the deactivate method

@lolgear
Copy link

lolgear commented Dec 14, 2016

@nanatlantica
You should determine terminate state.

After that you could throw event "switchToTerminateState".
And cleanup pointer to this state machine.

- (TKStateMachine *)stateMachine {
    return  _stateMachine ?: (_stateMachine = [TKStateMachine new], _stateMachine);
}

- (void)activate {
   if (self.stateMachine.isActive) {
       NSLog(@"%@ %@ stateMachine is active? (%@)", self.debugDescription, NSStringFromSelector(_cmd), self.stateMachine.isActive ? @"YES" : @"NO");
   }
   else {
           // setup machine
           [self setupStateMachine:self.stateMachine];
           [self.stateMachine activate];
   }
}

- (TKStateMachine *)setupStateMachine:(TKStateMachine *)machine {
    // for example, you could setup machine here or choose special method for that.
    // you need special event that will switch machine to terminate state from ANY state. (except terminate state, of course, it is useless :) )
    // you determine states
    NSArray *states = @[InitialState, TerminateState, CustomStateOne, CustomStateTwo];
    NSArray *statesExceptTerminateState = @[...];
    TKEvent *terminatedEvent = [TKEvent eventWithName:[TKStateMachine eventNameTerminated] transitioningFromStates:statesExceptTerminateState toState:terminatedState];
    NSArray *events = @[...];
    [machine addStates:states];
    [machine addEvents:events];

    // initial state
    machine.initialState = states[0]; // InitialState is first.
    return machine;
}

// suppose that you have terminate state.
// event name is eventNameTerminated
- (void)deactivate {
   [self.stateMachine fireEvent:[self.stateMachine eventNamed:[TKStateMachine eventNameTerminated]] userInfo:nil error:nil];
   self.stateMachine = nil;
}

@interface TKStateMachine (CustomEvents)
+ (NSString *)eventNameTerminated;
@end

@nanatlantica
Copy link
Author

nanatlantica commented Mar 10, 2017

@lolgear ,Thank you for your answer! And the state machine had worked correctly in my project more than 2 years

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants