This repository was archived by the owner on Jun 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMCQueuedRequest.j
More file actions
141 lines (117 loc) · 3.69 KB
/
MCQueuedRequest.j
File metadata and controls
141 lines (117 loc) · 3.69 KB
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
// This is basically a queable wrapper for MCHTTPRequest
@implementation MCQueuedRequest : CPObject
{
MCHTTPRequest HTTPRequest @accessors;
MCQueue queue @accessors;
CPSet childRequests @accessors;
BOOL blocking @accessors;
float progress @accessors(readonly);
CPSet finishedChildRequests;
id originalDelegate;
}
// Constructor method for clean code (TM)
+ (MCQueuedRequest)queuedRequestWithRequest:(MCHTTPRequest)aRequest
{
var queuedRequest = [[MCQueuedRequest alloc] initWithRequest:aRequest];
return queuedRequest;
}
// Designated initializer
- (id)initWithRequest:(MCHTTPRequest)aRequest
{
if(self = [super init])
{
childRequests = [CPSet set];
finishedChildRequests = [CPSet set];
[self setHTTPRequest:aRequest];
}
return self;
}
// Pass-thru methods to start/stop requests
- (void)start
{
[HTTPRequest start];
}
- (void)cancel
{
[HTTPRequest cancel];
}
// Wrap a request in this request
- (void)setHTTPRequest:(MCHTTPRequest)aRequest
{
HTTPRequest = aRequest;
// Hijack the request's delegate and register as observer
// so we can insert all child requests into the queue upon
// completion of the original request
originalDelegate = [aRequest delegate];
[aRequest setDelegate:self];
[[CPNotificationCenter defaultCenter] addObserver:self selector:@selector(requestDidChangeProgress:) name:MCHTTPRequestDidChangeProgressNotificationName object:aRequest];
}
// Add a child request to be executed upon completion of this request.
- (void)addChildRequest:(MCQueuedRequest)childRequest
{
[childRequests addObject:childRequest];
}
- (void)addChildRequests:(CPArray)theRequests
{
[childRequests addObjectsFromArray:theRequests]
}
- (void)queue
{
return [MCQueue sharedQueue];
}
#pragma mark -
#pragma mark Notification handlers
// Insert child requests if there were any
- (void)requestDidFinish:(MCQueuedRequest)aRequest
{
// If there were no child requests, notify the original delegate immediately
if([childRequests count] == 0)
{
[originalDelegate requestDidFinish:self];
}
else
{
// Otherwise, insert the child requests into the queue and register as an observer
// to be notified upon their completion
var childRequest,
requestEnumerator = [childRequests objectEnumerator];
while(childRequest = [requestEnumerator nextObject])
{
[[CPNotificationCenter defaultCenter] addObserver:self selector:@selector(childRequestDidFinish:) name:MCHTTPRequestDidFinishNotificationName object:[childRequest HTTPRequest]];
[[self queue] appendRequest:childRequest];
}
}
}
- (void)childRequestDidFinish:(CPNotification)aNotification
{
// Note that this child request did finish
[finishedChildRequests addObject:[aNotification object]];
// Check whether all child requests have finished
if([childRequests count] == [finishedChildRequests count])
{
// and notify the original delegate
[originalDelegate requestDidFinish:self];
}
}
// Update the request progress
- (void)requestDidChangeProgress:(CPNotification)aNotification
{
_progress = [[aNotification userInfo] valueForKey:@"progress"];
}
- (void)requestDidFail:(MCHTTPRequest)aRequest
{
[originalDelegate requestDidFail:self];
}
#pragma mark -
#pragma mark Useful overrides
- (CPString)description
{
var description = @"<MCQueuedRequest 0x" + [CPString stringWithHash:[self UID]] + ": " + [HTTPRequest HTTPMethod] + " to " + [HTTPRequest URL] + ">",
childRequestArray = [childRequests allObjects];
for(var i = 0; i < [childRequestArray count]; i++)
{
description += @"\n\tChild request: " + [[childRequestArray objectAtIndex:i] description];
}
return description;
}
@end