@@ -25,7 +25,7 @@ function onError(session, nsTask, error) {
25
25
const fileManager = utils . ios . getter ( NSFileManager , NSFileManager . defaultManager ) ;
26
26
fileManager . removeItemAtPathError ( task . _fileToCleanup ) ;
27
27
}
28
- let response = nsTask && nsTask . response ? < NSHTTPURLResponse > nsTask . response : null ;
28
+ const response = nsTask && < NSHTTPURLResponse > nsTask . performSelector ( " response" ) ;
29
29
if ( error ) {
30
30
task . notifyPropertyChange ( "status" , task . status ) ;
31
31
task . notify ( < common . ErrorEventData > {
@@ -61,20 +61,15 @@ class BackgroundUploadDelegate extends NSObject implements NSURLSessionDelegate,
61
61
62
62
// NSURLSessionDelegate
63
63
URLSessionDidBecomeInvalidWithError ( session , error ) {
64
- // console.log("URLSessionDidBecomeInvalidWithError:");
65
- // console.log(" - session: " + session);
66
- // console.log(" - error: " + error);
67
64
}
68
65
69
66
URLSessionDidReceiveChallengeCompletionHandler ( session , challenge , comlpetionHandler ) {
70
- // console.log("URLSessionDidFinishEventsForBackgroundURLSession: " + session + " " + challenge);
71
67
const disposition = null ;
72
68
const credential = null ;
73
69
comlpetionHandler ( disposition , credential ) ;
74
70
}
75
71
76
72
URLSessionDidFinishEventsForBackgroundURLSession ( session ) {
77
- // console.log("URLSessionDidFinishEventsForBackgroundURLSession: " + session);
78
73
}
79
74
80
75
// NSURLSessionTaskDelegate
@@ -85,7 +80,6 @@ class BackgroundUploadDelegate extends NSObject implements NSURLSessionDelegate,
85
80
}
86
81
87
82
URLSessionTaskDidReceiveChallengeCompletionHandler ( session , task , challenge , completionHandler ) {
88
- // console.log("URLSessionTaskDidReceiveChallengeCompletionHandler: " + session + " " + task + " " + challenge);
89
83
const disposition = null ;
90
84
const credential = null ;
91
85
completionHandler ( disposition , credential ) ;
@@ -98,28 +92,23 @@ class BackgroundUploadDelegate extends NSObject implements NSURLSessionDelegate,
98
92
}
99
93
100
94
URLSessionTaskNeedNewBodyStream ( session , task , need ) {
101
- // console.log("URLSessionTaskNeedNewBodyStream");
102
95
}
103
96
104
97
URLSessionTaskWillPerformHTTPRedirectionNewRequestCompletionHandler ( session , task , redirect , request , completionHandler ) {
105
- // console.log("URLSessionTaskWillPerformHTTPRedirectionNewRequestCompletionHandler");
106
98
completionHandler ( request ) ;
107
99
}
108
100
109
101
// NSURLSessionDataDelegate
110
102
URLSessionDataTaskDidReceiveResponseCompletionHandler ( session , dataTask , response , completionHandler ) {
111
- // console.log("URLSessionDataTaskDidReceiveResponseCompletionHandler");
112
103
const disposition = null ;
113
104
completionHandler ( disposition ) ;
114
105
}
115
106
116
107
URLSessionDataTaskDidBecomeDownloadTask ( session , dataTask , downloadTask ) {
117
- // console.log("URLSessionDataTaskDidBecomeDownloadTask");
118
108
}
119
109
120
110
URLSessionDataTaskDidReceiveData ( session : NSURLSession , dataTask : NSURLSessionDataTask , data : NSData ) {
121
111
dispatch_async ( main_queue , ( ) => {
122
- // console.log("URLSessionDataTaskDidReceiveData");
123
112
// we have a response in the data...
124
113
const jsTask = Task . getTask ( session , dataTask ) ;
125
114
const jsonString = NSString . alloc ( ) . initWithDataEncoding ( data , NSUTF8StringEncoding ) ;
@@ -134,20 +123,16 @@ class BackgroundUploadDelegate extends NSObject implements NSURLSessionDelegate,
134
123
}
135
124
136
125
URLSessionDataTaskWillCacheResponseCompletionHandler ( ) {
137
- // console.log("URLSessionDataTaskWillCacheResponseCompletionHandler");
138
126
}
139
127
140
128
// NSURLSessionDownloadDelegate
141
129
URLSessionDownloadTaskDidResumeAtOffsetExpectedTotalBytes ( session , task , offset , expects ) {
142
- // console.log("URLSessionDownloadTaskDidResumeAtOffsetExpectedTotalBytes");
143
130
}
144
131
145
132
URLSessionDownloadTaskDidWriteDataTotalBytesWrittenTotalBytesExpectedToWrite ( session , task , data , written , expected ) {
146
- // console.log("URLSessionDownloadTaskDidWriteDataTotalBytesWrittenTotalBytesExpectedToWrite");
147
133
}
148
134
149
135
URLSessionDownloadTaskDidFinishDownloadingToURL ( session , task , url ) {
150
- // console.log("URLSessionDownloadTaskDidFinishDownloadingToURL");
151
136
}
152
137
}
153
138
@@ -247,8 +232,38 @@ class Session implements common.Session {
247
232
}
248
233
}
249
234
235
+ class NativePropertyReader {
236
+ private _invocationCache = new Map < string , NSInvocation > ( ) ;
237
+
238
+ private getInvocationObject ( object : NSObject , selector : string ) : NSInvocation {
239
+ let invocation = this . _invocationCache . get ( selector ) ;
240
+ if ( ! invocation ) {
241
+ const sig = object . methodSignatureForSelector ( selector ) ;
242
+ invocation = NSInvocation . invocationWithMethodSignature ( sig ) ;
243
+ invocation . selector = selector ;
244
+
245
+ this . _invocationCache [ selector ] = invocation ;
246
+ }
247
+
248
+ return invocation ;
249
+ }
250
+
251
+ public readProp < T > ( object : NSObject , prop : string , type : interop . Type < T > ) : T {
252
+ const invocation = this . getInvocationObject ( object , prop ) ;
253
+ invocation . invokeWithTarget ( object ) ;
254
+
255
+ const ret = new interop . Reference < T > ( type , new interop . Pointer ( ) ) ;
256
+ invocation . getReturnValue ( ret ) ;
257
+
258
+ return ret . value ;
259
+ }
260
+ }
261
+
250
262
class Task extends Observable {
251
263
public static _tasks = new Map < NSURLSessionTask , Task > ( ) ;
264
+ public static tasksReader = new NativePropertyReader ( ) ;
265
+ private static is64BitArchitecture = interop . sizeof ( interop . types . id ) === 8 ;
266
+ public static NSIntegerType = Task . is64BitArchitecture ? interop . types . int64 : interop . types . int32 ;
252
267
253
268
public _fileToCleanup : string ;
254
269
private _task : NSURLSessionTask ;
@@ -269,18 +284,19 @@ class Task extends Observable {
269
284
}
270
285
271
286
get upload ( ) : number {
272
- return this . _task . countOfBytesSent ;
287
+ return Task . tasksReader . readProp ( this . _task , " countOfBytesSent" , interop . types . int64 ) ;
273
288
}
274
289
275
290
get totalUpload ( ) : number {
276
- return this . _task . countOfBytesExpectedToSend ;
291
+ return Task . tasksReader . readProp ( this . _task , " countOfBytesExpectedToSend" , interop . types . int64 ) ;
277
292
}
278
293
279
294
get status ( ) : string {
280
- if ( this . _task . error ) {
295
+ if ( Task . tasksReader . readProp ( this . _task , " error" , Task . NSIntegerType ) ) {
281
296
return "error" ;
282
297
}
283
- switch ( this . _task . state ) {
298
+ // NSURLSessionTaskState : NSInteger, so we should pass number format here
299
+ switch ( Task . tasksReader . readProp ( this . _task , "state" , Task . NSIntegerType ) as NSURLSessionTaskState ) {
284
300
case NSURLSessionTaskState . Running : return "uploading" ;
285
301
case NSURLSessionTaskState . Completed : return "complete" ;
286
302
case NSURLSessionTaskState . Canceling : return "error" ;
@@ -299,6 +315,7 @@ class Task extends Observable {
299
315
300
316
return task ;
301
317
}
318
+
302
319
public cancel ( ) : void {
303
320
this . _task . cancel ( ) ;
304
321
}
0 commit comments