Skip to content

Commit

Permalink
Track task count
Browse files Browse the repository at this point in the history
  • Loading branch information
brianquinlan committed Oct 2, 2024
1 parent 715d3fe commit 8569676
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions pkgs/cupertino_http/lib/src/cupertino_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,25 @@ class URLSession extends _ObjectHolder<ncb.NSURLSession> {
// Indicates if the session is a background session. Copied from the
// [URLSessionConfiguration._isBackground] associated with this [URLSession].
final bool _isBackground;
static var _taskCount = 0;
static ReceivePort? _port = null;

static void _incrementTaskCount() {
if (_port == null) {
_port = ReceivePort();
}
++_taskCount;
}

static void _decrementTaskCount() {
assert(_taskCount > 0);
--_taskCount;
if (_taskCount == 0 && _port != null) {
_port?.close();
_port = null;
_taskCount = 0;
}
}

static objc.ObjCObjectBase delegate(
bool isBackground, {
Expand Down Expand Up @@ -858,7 +877,6 @@ class URLSession extends _ObjectHolder<ncb.NSURLSession> {
protoBuilder,
URLSession_dataTask_didReceiveResponse_completionHandler_:
(session, dataTask, response, completionHandler) {
// print('URLSession_dataTask_didReceiveResponse_completionHandler_:');
var disposition =
ncb.NSURLSessionResponseDisposition.NSURLSessionResponseAllow;

Expand All @@ -872,7 +890,6 @@ class URLSession extends _ObjectHolder<ncb.NSURLSession> {
completionHandler.call(disposition);
},
URLSession_dataTask_didReceiveData_: (session, dataTask, data) {
// print('URLSession_dataTask_didReceiveData_:');
if (onData != null) {
onData(URLSession._(session, isBackground: isBackground),
URLSessionTask._(dataTask), data);
Expand Down Expand Up @@ -903,6 +920,7 @@ class URLSession extends _ObjectHolder<ncb.NSURLSession> {
nsRequestCompleter.call(redirectRequest?._nsObject);
},
URLSession_task_didCompleteWithError_: (nsSession, nsTask, nsError) {
_decrementTaskCount();
if (onComplete != null) {
onComplete(URLSession._(nsSession, isBackground: isBackground),
URLSessionTask._(nsTask), nsError);
Expand Down Expand Up @@ -1039,6 +1057,7 @@ class URLSession extends _ObjectHolder<ncb.NSURLSession> {
URLSessionTask dataTaskWithRequest(URLRequest request) {
final task =
URLSessionTask._(_nsObject.dataTaskWithRequest_(request._nsObject));
_incrementTaskCount();
return task;
}

Expand All @@ -1062,6 +1081,7 @@ class URLSession extends _ObjectHolder<ncb.NSURLSession> {
// 2. use a delegate to send information about the task to the port
// 3. call the user-provided completion function when we receive the
// `CompletedMessage` message type.
_incrementTaskCount();
throw UnsupportedError('dataTaskWithCompletionHandler');
/*
final task =
Expand Down Expand Up @@ -1096,6 +1116,7 @@ class URLSession extends _ObjectHolder<ncb.NSURLSession> {
URLSessionDownloadTask downloadTaskWithRequest(URLRequest request) {
final task = URLSessionDownloadTask._(
_nsObject.downloadTaskWithRequest_(request._nsObject));
_incrementTaskCount();
return task;
}

Expand All @@ -1113,6 +1134,7 @@ class URLSession extends _ObjectHolder<ncb.NSURLSession> {
}
final task = URLSessionWebSocketTask._(
_nsObject.webSocketTaskWithRequest_(request._nsObject));
_incrementTaskCount();
return task;
}

Expand All @@ -1136,6 +1158,7 @@ class URLSession extends _ObjectHolder<ncb.NSURLSession> {
_nsObject.webSocketTaskWithURL_protocols_(
uriToNSURL(uri), stringIterableToNSArray(protocols)));
}
_incrementTaskCount();
return task;
}

Expand Down

0 comments on commit 8569676

Please sign in to comment.