Skip to content

Commit 1fb44fd

Browse files
committed
fix(android): only create OkHttpResponseCloseCallback when necessary
This will improve app because the creation of the first one takes quite some time.
1 parent 705dd42 commit 1fb44fd

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

src/https.android.ts

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,23 @@ class HttpsResponse implements Https.HttpsResponseLegacy {
4747
private callback?: com.nativescript.https.OkHttpResponse.OkHttpResponseAsyncCallback;
4848
constructor(
4949
private response: com.nativescript.https.OkHttpResponse,
50+
private tag: string,
5051
private url: string
5152
) {}
53+
54+
getOrCreateCloseCallback() {
55+
if (!notClosedResponses[this.tag]) {
56+
// we need to store handling request to be able to cancel them
57+
notClosedResponses[this.tag] = this.response;
58+
this.response.closeCallback = new OkHttpResponse.OkHttpResponseCloseCallback(
59+
{
60+
onClose() {
61+
delete notClosedResponses[this.tag];
62+
},
63+
}
64+
);
65+
}
66+
}
5267
getCallback(resolve, reject) {
5368
return new com.nativescript.https.OkHttpResponse.OkHttpResponseAsyncCallback(
5469
{
@@ -88,6 +103,7 @@ class HttpsResponse implements Https.HttpsResponseLegacy {
88103
return Promise.resolve(this.arrayBuffer);
89104
}
90105
return new Promise((resolve, reject) => {
106+
this.getOrCreateCloseCallback();
91107
this.response.toByteArrayAsync(this.getCallback(resolve, reject));
92108
}).then((r: ArrayBuffer) => {
93109
this.arrayBuffer = r;
@@ -113,6 +129,7 @@ class HttpsResponse implements Https.HttpsResponseLegacy {
113129
}
114130
// TODO: handle arraybuffer already stored
115131
return new Promise<string>((resolve, reject) => {
132+
this.getOrCreateCloseCallback();
116133
this.response.asStringAsync(this.getCallback(resolve, reject));
117134
}).then((r) => {
118135
this.stringResponse = r;
@@ -172,6 +189,7 @@ class HttpsResponse implements Https.HttpsResponseLegacy {
172189
return Promise.resolve(this.imageSource);
173190
}
174191
return new Promise<ImageSource>((resolve, reject) => {
192+
this.getOrCreateCloseCallback();
175193
this.response
176194
.toBitmapAsync(this.getCallback(resolve, reject))
177195
.then((r) => {
@@ -197,6 +215,7 @@ class HttpsResponse implements Https.HttpsResponseLegacy {
197215
destinationFilePath = Https.getFilenameFromUrl(this.url);
198216
}
199217
return new Promise((resolve, reject) => {
218+
this.getOrCreateCloseCallback();
200219
this.response.toFileAsync(
201220
destinationFilePath,
202221
this.getCallback(resolve, reject)
@@ -554,15 +573,7 @@ export function createRequest(
554573
}
555574

556575
const nResponse = new OkHttpResponse(responseBody);
557-
// we need to store handling request to be able to cancel them
558-
notClosedResponses[tag] = nResponse;
559-
nResponse.closeCallback = new OkHttpResponse.OkHttpResponseCloseCallback(
560-
{
561-
onClose() {
562-
delete notClosedResponses[tag];
563-
},
564-
}
565-
);
576+
566577
if (opts.onProgress) {
567578
nResponse.progressCallback = new OkHttpResponse.OkHttpResponseProgressCallback(
568579
{
@@ -573,18 +584,26 @@ export function createRequest(
573584

574585
resolve({
575586
response,
576-
content: new HttpsResponse(nResponse, opts.url),
587+
content: new HttpsResponse(
588+
nResponse,
589+
tag,
590+
opts.url
591+
),
577592
statusCode,
578593
reason: message,
579-
get headers(){return getHeaders()},
594+
get headers() {
595+
return getHeaders();
596+
},
580597
});
581598
} else {
582599
resolve({
583600
response,
584601
content: responseBody.string(),
585602
reason: message,
586603
statusCode,
587-
get headers(){return getHeaders()},
604+
get headers() {
605+
return getHeaders();
606+
},
588607
});
589608
}
590609
},

0 commit comments

Comments
 (0)