-
Notifications
You must be signed in to change notification settings - Fork 1
/
JPHAsyncOperation.m
163 lines (133 loc) · 4.3 KB
/
JPHAsyncOperation.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//
// JPHAsyncOperation.m
// JPHAsyncOperation
//
// Created by Pierre Houston on 2015-06-19.
// Copyright © 2015 Pierre Houston. All rights reserved.
//
// Inspired by AFURLConnectionOperation in AFNetworking, though no code was used as-is from it.
//
#import "JPHAsyncOperation.h"
typedef NS_ENUM(NSInteger, JPHAsyncOperationState) {
JPHAsyncOperationReadyState = 1,
JPHAsyncOperationExecutingState = 2,
JPHAsyncOperationFinishedState = 3,
//JPHAsyncOperationPausedState = -1, maybe a paused state, also like AFURLConnectionOperation?
};
@interface JPHAsyncOperation ()
@property (nonatomic, assign) JPHAsyncOperationState state;
@property (nonatomic, strong) JPHAsyncOperationBlock startBlock;
@property (nonatomic, strong) JPHAsyncOperationBlock cancelBlock;
@end
@implementation JPHAsyncOperation
+ (instancetype)operationWithBlock:(JPHAsyncOperationBlock)block
{
return [[self alloc] initWithStartBlock:block cancelBlock:nil];
}
+ (instancetype)operationWithStartBlock:(JPHAsyncOperationBlock)startBlock cancelBlock:(JPHAsyncOperationBlock)cancelBlock
{
return [[self alloc] initWithStartBlock:startBlock cancelBlock:cancelBlock];
}
- (instancetype)initWithBlock:(JPHAsyncOperationBlock)block
{
return [self initWithStartBlock:block cancelBlock:nil];
}
- (instancetype)initWithStartBlock:(JPHAsyncOperationBlock)startBlock cancelBlock:(JPHAsyncOperationBlock)cancelBlock
{
if (startBlock == nil) {
[NSException raise:NSInvalidArgumentException format:@"%s startBlock must not be nil", __PRETTY_FUNCTION__];
}
self = [super init];
if (self) {
_startBlock = startBlock;
_cancelBlock = cancelBlock;
_state = JPHAsyncOperationReadyState;
}
return self;
}
static inline BOOL validStateTransition(JPHAsyncOperationState fromState, JPHAsyncOperationState toState, BOOL isCancelled)
{
if ((int)toState == (int)fromState + 1)
return YES;
if (isCancelled && fromState == JPHAsyncOperationReadyState && toState == JPHAsyncOperationFinishedState)
return YES;
return NO;
}
static inline NSString *keypathForState(JPHAsyncOperationState state)
{
switch (state) {
case JPHAsyncOperationReadyState:
return @"isReady";
case JPHAsyncOperationExecutingState:
return @"isExecuting";
case JPHAsyncOperationFinishedState:
return @"isFinished";
//case JPHAsyncOperationPausedState:
// return @"isPaused";
default:
return @"state"; // returning nil would surely crash in will/didChangeValueForKey:
}
}
- (void)setState:(JPHAsyncOperationState)newState
{
if (validStateTransition(self.state, newState, [self isCancelled])) {
NSString *oldStateKey = keypathForState(self.state);
NSString *newStateKey = keypathForState(newState);
[self willChangeValueForKey:newStateKey];
[self willChangeValueForKey:oldStateKey];
_state = newState;
[self didChangeValueForKey:oldStateKey];
[self didChangeValueForKey:newStateKey];
}
}
- (void)finish
{
self.state = JPHAsyncOperationFinishedState;
}
- (void)performStart
{
if (self.startBlock) self.startBlock(self);
}
- (void)performCancel
{
if (self.cancelBlock) self.cancelBlock(self);
}
#pragma mark -
// required overrides for an async NSOperation subclass
- (BOOL)isAsynchronous // perhaps implement isConcurrent instead to support older os versions
{
return YES;
}
- (BOOL)isReady {
return self.state == JPHAsyncOperationReadyState && [super isReady];
}
- (BOOL)isExecuting
{
return self.state == JPHAsyncOperationExecutingState;
}
- (BOOL)isFinished
{
return self.state == JPHAsyncOperationFinishedState;
}
- (void)start
{
if ([self isCancelled]) {
self.state = JPHAsyncOperationFinishedState;
}
else if ([self isReady]) {
self.state = JPHAsyncOperationExecutingState;
}
if (self.state == JPHAsyncOperationExecutingState) {
[self performStart];
}
}
- (void)cancel
{
BOOL alreadyCancelled = [self isCancelled];
// multiple calls to this method should avoid running cancelBlock on all but the first
[super cancel];
if (!alreadyCancelled && [self isExecuting] && self.cancelBlock != nil) {
[self performCancel];
}
}
@end