Skip to content

Latest commit

 

History

History
93 lines (67 loc) · 2.2 KB

Scheduled.md

File metadata and controls

93 lines (67 loc) · 2.2 KB

Scheduled

1. 继承ScheduledAction或者直接使用ScheduledAction
@interface ScheduledCounterAction ()

@property (nonatomic, strong) NSString *operation;

@end

@implementation ScheduledCounterAction

- (instancetype)initWithOperation:(NSString *)operation  {
    self = [super initWithStart:1 interval:2 repeats:YES];
    if (self) {
        self.operation = operation;
    }
    
    return self;
}

- (NSString *)description {
    return [NSString stringWithFormat:@"Scheduled(%@)", self.operation];
}

@end
2. Reducer
Reducer ScheduleReducer = ^CounterState * (ScheduledCounterAction *action, CounterState *state) {

    if (state == nil) {
        CounterState *nextState = [CounterState new];
        nextState.number = 0;
        nextState.operation = @"init operation";
        return nextState;
    }

    if([action isKindOfClass:[ScheduledCounterAction class]]) {

        if ([action.operation isEqualToString:@"-"]) {
            state.number -= 1;
        }

        if ([action.operation isEqualToString:@"+"]) {
            state.number += 1;
        }

        state.operation = action.description;
    }

    return state;
};
3. ScheduleMiddleware
self.store = [[Store alloc] initWithReducer:ScheduleReducer
                                              state:nil
                                        middlewares:@[ActionLogger, StateLogger, ScheduleMiddleware]];
4. dispatch scheduledAction
- (void)buttonClick:(UIButton *)sender {
    NSString *type = sender.currentTitle;
    if (self.scheduledAction) {
        [self.scheduledAction cancelAction];
    }
    ScheduledCounterAction *action = [[ScheduledCounterAction alloc] initWithOperation:type];
    self.scheduledAction = [self.store dispatch:action];
}

这里需要注意,如果之前的Action是repeats的,如果不cancel之前的Action,则之前action还会继续repeats

4. cancel scheduledAction
- (void)cancelButtontClick:(UIButton *)sender {
    [self.scheduledAction cancelAction];
    self.scheduledAction = nil;
}

tip

当然这里也可以自定义store或者Middleware来实现自己的scheduleAction