-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathSTNetTaskQueue.h
More file actions
141 lines (106 loc) · 4.69 KB
/
STNetTaskQueue.h
File metadata and controls
141 lines (106 loc) · 4.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
//
// STNetTaskQueue.h
// STNetTaskQueue
//
// Created by Kevin Lin on 29/11/14.
// Copyright (c) 2014 Sth4Me. All rights reserved.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@class STNetTask;
@protocol STNetTaskDelegate;
//! Project version number for STNetTaskQueue.
FOUNDATION_EXPORT double STNetTaskQueueVersionNumber;
//! Project version string for STNetTaskQueue.
FOUNDATION_EXPORT const unsigned char STNetTaskQueueVersionString[];
@class STNetTaskQueue;
@protocol STNetTaskQueueHandler <NSObject>
/**
STNetTaskQueue will call this method when a net task is added to queue and become ready to be excecuted.
@param netTaskQueue STNetTaskQueue The net task queue which is holding the net task.
@param task STNetTask The net task which is ready to be executed.
*/
- (void)netTaskQueue:(STNetTaskQueue *)netTaskQueue handleTask:(STNetTask *)task;
/**
STNetTaskQueue will call this method when a net task is cancelled and removed from net task queue.
Giving a chance to the handler to do a clean up for the cancelled net task.
@param netTaskQueue STNetTaskQueue The net task queue which is holding the cancelled net task.
@param task STNetTask The net task which is cancelled and removed from net task queue.
*/
- (void)netTaskQueue:(STNetTaskQueue *)netTaskQueue didCancelTask:(STNetTask *)task;
/**
STNetTaskQueue will call this method when the net task queue is deallocated.
@param netTaskQueue STNetTaskQueue The net task queue which is deallocated.
*/
- (void)netTaskQueueDidBecomeInactive:(STNetTaskQueue *)netTaskQueue;
@end
@interface STNetTaskQueue : NSObject
/**
The STNetTaskQueueHandler which is used for handling the net tasks in queue.
*/
@property (nullable, nonatomic, strong) id<STNetTaskQueueHandler> handler;
/**
Count of Max concurrent task in a queue.
If the number of unfinished tasks in queue hits the max count, upcoming task will be processed till one of the unfinished task is done.
*/
@property (nonatomic, assign) NSUInteger maxConcurrentTasksCount;
/**
A shared STNetTaskQueue instance.
*/
+ (instancetype)sharedQueue;
/**
Add a net task into the net task queue.
The net task may not be executed immediately depends on the "maxConcurrentTasksCount",
but the net task will be marked as "pending" anyway.
@param task STNetTask The net task to be added into the queue.
*/
- (void)addTask:(STNetTask *)task;
/**
Cancel and remove the net task from queue.
If the net task is executing, it will be cancelled and remove from the queue without calling the "netTaskDidEnd" delegate method.
@param task STNetTask The net task to be cancelled and removed from the queue.
*/
- (void)cancelTask:(STNetTask *)task;
/**
This method should be called when the "handler" finish handling the net task successfully.
After "handler" called this method, the net task will be marked as "finished", set "pending" as "NO", and removed from the queue.
@param task STNetTask The net task which is handled by "handler".
@param response id The response object.
*/
- (void)task:(STNetTask *)task didResponse:(id)response;
/**
This method should be caled when the "handler" finish handling the net task with error.
After "hadnler" called this method, the net task will be marked as "finished", set "pending" as "NO", and removed from the queue.
@param task STNetTask The net task which is handled by "handler".
@param error NSError Error object.
*/
- (void)task:(STNetTask *)task didFailWithError:(NSError *)error;
/**
Add a net task delegate to "STNetTaskQueue" with uri of the net task,
it's a weak reference and duplicated delegate with same uri will be ignored.
@param delegate id<STNetTaskDelegate>
@param uri NSString A unique string returned by STNetTask.
*/
- (void)addTaskDelegate:(id<STNetTaskDelegate>)delegate uri:(NSString *)uri;
/**
Add a net task delegate to "STNetTaskQueue" with class of net task,
it's a weak reference and duplicated delegate with same class will be ignored.
@param delegate id<STNetTaskDelegate>
@param class Class Class which extends STNetTask.
*/
- (void)addTaskDelegate:(id<STNetTaskDelegate>)delegate class:(Class)clazz;
/**
Most of the times you don't need to remove net task delegate explicitly,
because "STNetTaskQueue" holds weak reference of each delegate.
@param delegate id<STNetTaskDelegate>
@param uri NSString
*/
- (void)removeTaskDelegate:(id<STNetTaskDelegate>)delegate uri:(NSString *)uri;
- (void)removeTaskDelegate:(id<STNetTaskDelegate>)delegate class:(Class)clazz;
- (void)removeTaskDelegate:(id<STNetTaskDelegate>)delegate;
@end
NS_ASSUME_NONNULL_END
#import <STNetTaskQueue/STNetTaskChain.h>
#import <STNetTaskQueue/STNetTaskGroup.h>
#import <STNetTaskQueue/STHTTPNetTask.h>
#import <STNetTaskQueue/STHTTPNetTaskQueueHandler.h>