|
| 1 | +// |
| 2 | +// STNetTaskGroup.m |
| 3 | +// STNetTaskQueue |
| 4 | +// |
| 5 | +// Created by Kevin Lin on 8/5/16. |
| 6 | +// Copyright © 2016 Sth4Me. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import "STNetTaskGroup.h" |
| 10 | +#import "STNetTaskQueue.h" |
| 11 | + |
| 12 | +@interface STNetTaskGroup () |
| 13 | + |
| 14 | +@property (nonatomic, strong) STNetTask *executingTask; |
| 15 | +@property (nonatomic, strong) NSArray<STNetTask *> *tasks; |
| 16 | +@property (nonatomic, assign) BOOL pending; |
| 17 | +@property (nonatomic, assign) BOOL started; |
| 18 | +@property (nonatomic, strong) NSMutableDictionary<NSNumber *, NSMutableArray<STNetTaskGroupSubscriptionBlock> *> *stateToBlock; |
| 19 | +@property (nonatomic, strong) STNetTaskSubscriptionBlock taskSubscriptionBlock; // For serial mode |
| 20 | +@property (nonatomic, strong, readonly) STNetTaskQueue *queue; |
| 21 | + |
| 22 | +@end |
| 23 | + |
| 24 | +@implementation STNetTaskGroup |
| 25 | + |
| 26 | +- (instancetype)initWithTasks:(NSArray<STNetTask *> *)tasks mode:(STNetTaskGroupMode)mode |
| 27 | +{ |
| 28 | + return [self initWithTasks:tasks mode:mode queue:[STNetTaskQueue sharedQueue]]; |
| 29 | +} |
| 30 | + |
| 31 | +- (instancetype)initWithTasks:(NSArray<STNetTask *> *)tasks mode:(STNetTaskGroupMode)mode queue:(STNetTaskQueue *)queue |
| 32 | +{ |
| 33 | + if (self = [super init]) { |
| 34 | + self.tasks = [NSArray arrayWithArray:tasks]; |
| 35 | + _mode = mode; |
| 36 | + _queue = queue; |
| 37 | + } |
| 38 | + return self; |
| 39 | +} |
| 40 | + |
| 41 | +- (void)addTask:(STNetTask *)task |
| 42 | +{ |
| 43 | + NSMutableArray *tasks = [_tasks mutableCopy]; |
| 44 | + [tasks addObject:task]; |
| 45 | + self.tasks = [NSArray arrayWithArray:tasks]; |
| 46 | +} |
| 47 | + |
| 48 | +- (void)subscribeState:(STNetTaskGroupState)state usingBlock:(STNetTaskGroupSubscriptionBlock)block |
| 49 | +{ |
| 50 | + if (!self.stateToBlock) { |
| 51 | + self.stateToBlock = [NSMutableDictionary new]; |
| 52 | + } |
| 53 | + NSMutableArray *blocks = self.stateToBlock[@(state)]; |
| 54 | + if (!blocks) { |
| 55 | + blocks = [NSMutableArray new]; |
| 56 | + self.stateToBlock[@(state)] = blocks; |
| 57 | + } |
| 58 | + [blocks addObject:[block copy]]; |
| 59 | +} |
| 60 | + |
| 61 | +- (void)notifyState:(STNetTaskGroupState)state withError:(NSError *)error |
| 62 | +{ |
| 63 | + NSMutableArray<STNetTaskGroupSubscriptionBlock> *blocks = self.stateToBlock[@(state)]; |
| 64 | + for (STNetTaskSubscriptionBlock block in blocks) { |
| 65 | + block(self, error); |
| 66 | + } |
| 67 | + self.stateToBlock = nil; |
| 68 | + self.taskSubscriptionBlock = nil; |
| 69 | +} |
| 70 | + |
| 71 | +- (void)start |
| 72 | +{ |
| 73 | + NSAssert(!self.started, @"STNetTaskQueue can not be reused, please create a new instance."); |
| 74 | + if (self.pending) { |
| 75 | + return; |
| 76 | + } |
| 77 | + self.pending = YES; |
| 78 | + self.started = YES; |
| 79 | + |
| 80 | + switch (self.mode) { |
| 81 | + case STNetTaskGroupModeSerial: { |
| 82 | + __block NSUInteger executingTaskIndex = 0; |
| 83 | + __weak STNetTaskGroup *weakSelf = self; |
| 84 | + self.taskSubscriptionBlock = ^{ |
| 85 | + if (weakSelf.executingTask.error) { |
| 86 | + [weakSelf notifyState:STNetTaskGroupStateFinished withError:weakSelf.executingTask.error]; |
| 87 | + return; |
| 88 | + } |
| 89 | + executingTaskIndex++; |
| 90 | + if (executingTaskIndex == weakSelf.tasks.count) { |
| 91 | + [weakSelf notifyState:STNetTaskGroupStateFinished withError:nil]; |
| 92 | + } |
| 93 | + else { |
| 94 | + weakSelf.executingTask = weakSelf.tasks[executingTaskIndex]; |
| 95 | + [weakSelf.queue addTask:weakSelf.executingTask]; |
| 96 | + [weakSelf.executingTask subscribeState:STNetTaskStateFinished usingBlock:weakSelf.taskSubscriptionBlock]; |
| 97 | + } |
| 98 | + }; |
| 99 | + self.executingTask = self.tasks[executingTaskIndex]; |
| 100 | + [self.queue addTask:self.executingTask]; |
| 101 | + [self.executingTask subscribeState:STNetTaskStateFinished usingBlock:self.taskSubscriptionBlock]; |
| 102 | + } |
| 103 | + break; |
| 104 | + case STNetTaskGroupModeConcurrent: { |
| 105 | + __block NSUInteger finishedTasksCount = 0; |
| 106 | + for (STNetTask *task in self.tasks) { |
| 107 | + [self.queue addTask:task]; |
| 108 | + [task subscribeState:STNetTaskStateFinished usingBlock:^{ |
| 109 | + if (task.error) { |
| 110 | + [self cancelTasks]; |
| 111 | + [self notifyState:STNetTaskGroupStateFinished withError:task.error]; |
| 112 | + return; |
| 113 | + } |
| 114 | + finishedTasksCount++; |
| 115 | + if (finishedTasksCount == self.tasks.count) { |
| 116 | + [self notifyState:STNetTaskGroupStateFinished withError:nil]; |
| 117 | + } |
| 118 | + }]; |
| 119 | + } |
| 120 | + } |
| 121 | + break; |
| 122 | + default: |
| 123 | + break; |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +- (void)cancel |
| 128 | +{ |
| 129 | + if (!self.pending) { |
| 130 | + return; |
| 131 | + } |
| 132 | + |
| 133 | + switch (self.mode) { |
| 134 | + case STNetTaskGroupModeSerial: { |
| 135 | + [self.queue cancelTask:self.executingTask]; |
| 136 | + self.executingTask = nil; |
| 137 | + } |
| 138 | + break; |
| 139 | + case STNetTaskGroupModeConcurrent: { |
| 140 | + [self cancelTasks]; |
| 141 | + } |
| 142 | + break; |
| 143 | + default: |
| 144 | + break; |
| 145 | + } |
| 146 | + [self notifyState:STNetTaskGroupStateCancelled withError:nil]; |
| 147 | +} |
| 148 | + |
| 149 | +- (void)cancelTasks |
| 150 | +{ |
| 151 | + for (STNetTask *task in self.tasks) { |
| 152 | + if (task.pending) { |
| 153 | + [self.queue cancelTask:task]; |
| 154 | + } |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +@end |
| 159 | + |
| 160 | +@implementation NSArray (STNetTaskGroup) |
| 161 | + |
| 162 | +- (STNetTaskGroup *)serialNetTaskGroup |
| 163 | +{ |
| 164 | + return [[STNetTaskGroup alloc] initWithTasks:self mode:STNetTaskGroupModeSerial]; |
| 165 | +} |
| 166 | + |
| 167 | +- (STNetTaskGroup *)serialNetTaskGroupInQueue:(STNetTaskQueue *)queue |
| 168 | +{ |
| 169 | + return [[STNetTaskGroup alloc] initWithTasks:self mode:STNetTaskGroupModeSerial queue:queue]; |
| 170 | +} |
| 171 | + |
| 172 | +- (STNetTaskGroup *)concurrentNetTaskGroup |
| 173 | +{ |
| 174 | + return [[STNetTaskGroup alloc] initWithTasks:self mode:STNetTaskGroupModeConcurrent]; |
| 175 | +} |
| 176 | + |
| 177 | +- (STNetTaskGroup *)concurrentNetTaskGroupInQueue:(STNetTaskQueue *)queue |
| 178 | +{ |
| 179 | + return [[STNetTaskGroup alloc] initWithTasks:self mode:STNetTaskGroupModeConcurrent queue:queue]; |
| 180 | +} |
| 181 | + |
| 182 | +@end |
0 commit comments