-
Notifications
You must be signed in to change notification settings - Fork 1
/
RNConcurrentBlockOperation.m
115 lines (94 loc) · 3.68 KB
/
RNConcurrentBlockOperation.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
//
// RNConcurrentBlockOperation.h
//
// Copyright (c) 2013 Rafael Nobre
//
// https://github.com/nobre84/RNConcurrentBlockOperation
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import "RNConcurrentBlockOperation.h"
NSString * const RNOperationStatusKey = @"RNOperationStatusKey";
NSString * const RNOperationStatusCanceled = @"RNOperationStatusCanceled";
NSString * const RNOperationStatusFinished = @"RNOperationStatusFinished";
NSString * const RNOperationResultKey = @"RNOperationResultKey";
NSString * const RNOperationErrorKey = @"RNOperationErrorKey";
@interface RNConcurrentBlockOperation()
@property (nonatomic, copy) RNOperationBlock operationBlock;
@end
@implementation RNConcurrentBlockOperation {
BOOL _isFinished, _isExecuting;
}
-(instancetype)initWithBlock:(RNOperationBlock)operationBlock {
if (self = [super init]) {
_isExecuting = NO;
_isFinished = NO;
self.operationBlock = operationBlock;
}
return self;
}
+(instancetype)operationWithBlock:(RNOperationBlock)operationBlock {
id operation = [[self alloc]initWithBlock:operationBlock];
return operation;
}
-(BOOL)isConcurrent {
return YES;
}
-(void)start {
@autoreleasepool {
//some RunLoop based asynchronous work must be started from the main thread so we ensure we start there.
if (![NSThread isMainThread]) {
[self performSelectorOnMainThread:@selector(start) withObject:nil waitUntilDone:NO];
return;
}
//if the operation has been cancelled before starting, finish right away.
if ([self isCancelled]) {
[self finish];
return;
}
//start execution
[self willChangeValueForKey:@"isExecuting"];
_isExecuting = YES;
[self didChangeValueForKey:@"isExecuting"];
if (self.operationBlock) {
//call the operation block and finish the operation when it signals completion
self.operationBlock(^(NSDictionary *userInfo){
//if the user signalled cancellation, do it before finishing.
if ([userInfo[RNOperationStatusKey] isEqualToString:RNOperationStatusCanceled]) {
[self cancel];
}
self.userInfo = userInfo;
[self finish];
});
}
}
}
-(BOOL)isFinished {
return _isFinished;
}
-(BOOL)isExecuting {
return _isExecuting;
}
-(void)finish {
[self willChangeValueForKey:@"isExecuting"];
[self willChangeValueForKey:@"isFinished"];
_isExecuting = NO;
_isFinished = YES;
[self didChangeValueForKey:@"isExecuting"];
[self didChangeValueForKey:@"isFinished"];
}
@end