-
Notifications
You must be signed in to change notification settings - Fork 3
/
DumbOperation.m
134 lines (129 loc) · 2.9 KB
/
DumbOperation.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
//
// DumbOperations.m
// spires
//
// Created by Yuji on 09/02/07.
// Copyright 2009 Y. Tachikawa. All rights reserved.
//
#import "DumbOperation.h"
#import "NetworkOperationQueue.h"
static NSOperationQueue*_queue=nil;
static NSOperationQueue*_Iqueue=nil;
static NSOperationQueue*_Squeue=nil;
static NSOperationQueue*_Aqueue=nil;
@interface UniqueOperationQueue:NSOperationQueue{
}
@end
@implementation UniqueOperationQueue
-(void)addOperation:(NSOperation*)op
{
for(NSOperation*o in self.operations){
if([op isEqual:o]){
return;
}
}
[super addOperation:op];
}
@end
@implementation OperationQueues
+(NSOperationQueue*)sharedQueue;
{
if(!_queue){
_queue=[[UniqueOperationQueue alloc] init];
// [_queue setMaxConcurrentOperationCount:1];
}
return _queue;
}
+(NSOperationQueue*)importQueue;
{
if(!_Iqueue){
_Iqueue=[[UniqueOperationQueue alloc] init];
[_Iqueue setMaxConcurrentOperationCount:1];
}
return _Iqueue;
}
+(NSOperationQueue*)spiresQueue;
{
if(!_Squeue){
_Squeue=[[NetworkOperationQueue alloc] initWithHost:@"inspirehep.net" andWaitBetweenOperations:
[[NSUserDefaults standardUserDefaults] integerForKey:@"inspireWaitInSeconds"]];
[_Squeue setMaxConcurrentOperationCount:1];
}
return _Squeue;
}
+(NSOperationQueue*)arxivQueue;
{
if(!_Aqueue){
_Aqueue=[[NetworkOperationQueue alloc] initWithHost:@"arxiv.org" andWaitBetweenOperations:
[[NSUserDefaults standardUserDefaults] integerForKey:@"arXivWaitInSeconds"]];
[_Aqueue setMaxConcurrentOperationCount:1];
}
return _Aqueue;
}
+(void)cancelOperationsInQueue:(NSOperationQueue*)q
{
for(NSOperation*op in [q operations]){
[op cancel];
}
}
+(void)cancelCurrentOperations
{
[self cancelOperationsInQueue:[self sharedQueue]];
[self cancelOperationsInQueue:[self arxivQueue]];
[self cancelOperationsInQueue:[self spiresQueue]];
}
@end
@implementation ConcurrentOperation
-(void)start
{
[self performSelectorOnMainThread:@selector(run) withObject:nil waitUntilDone:YES];
}
-(void)run
{
NSLog(@"should not be called at all!");
}
-(BOOL)isConcurrent
{
return YES;
}
-(BOOL)isExecuting
{
return isExecuting;
}
-(BOOL)isFinished
{
return isFinished;
}
-(void)checkIfCancelled:(id)userInfo
{
if([self isCancelled]){
[self cleanupToCancel];
[self finish];
}
}
-(void)setIsExecuting:(BOOL)b
{
[self willChangeValueForKey:@"isExecuting"];
isExecuting=b;
if(b){
cancelTimer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(checkIfCancelled:) userInfo:nil repeats:YES];
}else{
[cancelTimer invalidate];
}
[self didChangeValueForKey:@"isExecuting"];
}
-(void)setIsFinished:(BOOL)b
{
[self willChangeValueForKey:@"isFinished"];
isFinished=b;
[self didChangeValueForKey:@"isFinished"];
}
-(void)finish
{
self.isExecuting=NO;
self.isFinished=YES;
}
-(void)cleanupToCancel
{
}
@end