Skip to content

Commit e974daf

Browse files
authored
Support query.cancel() on Node (#1030)
Moves abort event to prevent xhr.open from calling it. Ensure requestTask is set after the request sends.
1 parent bdbd385 commit e974daf

File tree

4 files changed

+34
-16
lines changed

4 files changed

+34
-16
lines changed

integration/test/ParseQueryTest.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1998,4 +1998,17 @@ describe('Parse Query', () => {
19981998
}, 0);
19991999
assert.equal(result, 6);
20002000
});
2001+
2002+
it('can cancel query', async () => {
2003+
const obj1 = new TestObject({ number: 1 });
2004+
const obj2 = new TestObject({ number: 2 });
2005+
const obj3 = new TestObject({ number: 3 });
2006+
await Parse.Object.saveAll([obj1, obj2, obj3]);
2007+
2008+
const query = new Parse.Query(TestObject);
2009+
query.find().then((results) => {
2010+
assert.equal(results.length, 0);
2011+
});
2012+
query.cancel();
2013+
});
20012014
});

src/ParseQuery.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1837,10 +1837,13 @@ class ParseQuery {
18371837
*/
18381838
cancel(): ParseQuery {
18391839
if (this._xhrRequest.task && typeof this._xhrRequest.task.abort === 'function') {
1840+
this._xhrRequest.task._aborted = true;
18401841
this._xhrRequest.task.abort();
1842+
this._xhrRequest.task = null;
1843+
this._xhrRequest.onchange = () => {};
1844+
return this;
18411845
}
1842-
this._xhrRequest.task = null;
1843-
return this;
1846+
return this._xhrRequest.onchange = () => this.cancel();
18441847
}
18451848

18461849
_setRequestTask(options) {

src/RESTController.js

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,6 @@ if (typeof XDomainRequest !== 'undefined' &&
5151
function ajaxIE9(method: string, url: string, data: any, headers?: any, options?: FullOptions) {
5252
return new Promise((resolve, reject) => {
5353
const xdr = new XDomainRequest();
54-
if (options && typeof options.requestTask === 'function') {
55-
options.requestTask(xdr);
56-
}
5754
xdr.onload = function() {
5855
let response;
5956
try {
@@ -82,6 +79,9 @@ function ajaxIE9(method: string, url: string, data: any, headers?: any, options?
8279
};
8380
xdr.open(method, url);
8481
xdr.send(data);
82+
if (options && typeof options.requestTask === 'function') {
83+
options.requestTask(xdr);
84+
}
8585
});
8686
}
8787

@@ -104,14 +104,10 @@ const RESTController = {
104104
);
105105
}
106106
let handled = false;
107-
let aborted = false;
108107

109108
const xhr = new XHR();
110-
if (options && typeof options.requestTask === 'function') {
111-
options.requestTask(xhr);
112-
}
113109
xhr.onreadystatechange = function() {
114-
if (xhr.readyState !== 4 || handled) {
110+
if (xhr.readyState !== 4 || handled || xhr._aborted) {
115111
return;
116112
}
117113
handled = true;
@@ -132,8 +128,6 @@ const RESTController = {
132128
if (response) {
133129
promise.resolve({ response, status: xhr.status, xhr });
134130
}
135-
} else if (aborted && xhr.status === 0) {
136-
promise.resolve({ response: {}, status: 0, xhr });
137131
} else if (xhr.status >= 500 || xhr.status === 0) { // retry on 5XX or node-xmlhttprequest error
138132
if (++attempts < CoreManager.get('REQUEST_ATTEMPT_LIMIT')) {
139133
// Exponentially-growing random delay
@@ -167,9 +161,6 @@ const RESTController = {
167161
for (const key in customHeaders) {
168162
headers[key] = customHeaders[key];
169163
}
170-
xhr.onabort = () => {
171-
aborted = true;
172-
};
173164
xhr.onprogress = (event) => {
174165
if(options && typeof options.progress === 'function') {
175166
if (event.lengthComputable) {
@@ -184,7 +175,18 @@ const RESTController = {
184175
for (const h in headers) {
185176
xhr.setRequestHeader(h, headers[h]);
186177
}
178+
xhr.onabort = function () {
179+
promise.resolve({
180+
response: { results: [] },
181+
status: 0,
182+
xhr,
183+
});
184+
};
187185
xhr.send(data);
186+
187+
if (options && typeof options.requestTask === 'function') {
188+
options.requestTask(xhr);
189+
}
188190
}
189191
dispatch();
190192

src/__tests__/ParseQuery-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2124,7 +2124,7 @@ describe('ParseQuery', () => {
21242124

21252125
await query.find();
21262126

2127-
expect(query._xhrRequest.task).toEqual(mockRequestTask);
2127+
expect(query._xhrRequest.task).toEqual(null);
21282128
query.cancel();
21292129
expect(mockRequestTask.abort).toHaveBeenCalledTimes(1);
21302130
});

0 commit comments

Comments
 (0)