Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Fix memory leak in tests (#105)
Browse files Browse the repository at this point in the history
* Fix memory leak in tests

Closes #104

* Make the number of pending requests global

* Avoid unnecessary fat-arrow function

* Switch to one, global waiter
  • Loading branch information
alexlafroscia committed May 5, 2016
1 parent def4f57 commit 06141d9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 18 deletions.
25 changes: 10 additions & 15 deletions addon/ajax-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,14 @@ function isJSONAPIContentType(header) {
return header.indexOf(JSONAPIContentType) === 0;
}

export default class AjaxRequest {

constructor() {
this.init();
}
let pendingRequestCount = 0;
if (testing) {
Test.registerWaiter(function() {
return pendingRequestCount === 0;
});
}

init() {
this.pendingRequestCount = 0;
if (testing) {
Test.registerWaiter(() => this.pendingRequestCount === 0);
}
}
export default class AjaxRequest {

request(url, options) {
const hash = this.options(url, options);
Expand Down Expand Up @@ -90,7 +86,7 @@ export default class AjaxRequest {
requestData
);

this.pendingRequestCount--;
pendingRequestCount--;

if (isAjaxError(response)) {
run.join(null, reject, { payload, textStatus, jqXHR, response });
Expand Down Expand Up @@ -118,12 +114,12 @@ export default class AjaxRequest {
);
}

this.pendingRequestCount--;
pendingRequestCount--;

run.join(null, reject, { payload, textStatus, jqXHR, errorThrown, response });
};

this.pendingRequestCount++;
pendingRequestCount++;

ajax(hash);
}, `ember-ajax: ${hash.type} ${hash.url}`);
Expand Down Expand Up @@ -216,7 +212,6 @@ export default class AjaxRequest {
options.url = this._buildURL(url, options);
options.type = options.type || 'GET';
options.dataType = options.dataType || 'json';
options.context = this;

if (this._shouldSendHeaders(options)) {
options.headers = this._getFullHeadersHash(options.headers);
Expand Down
3 changes: 0 additions & 3 deletions tests/unit/ajax-request-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ test('options() sets raw data', function(assert) {
const ajaxOptions = service.options(url, { type, data: { key: 'value' } });

assert.deepEqual(ajaxOptions, {
context: service,
data: {
key: 'value'
},
Expand All @@ -195,7 +194,6 @@ test('options() sets options correctly', function(assert) {

assert.deepEqual(ajaxOptions, {
contentType: 'application/json; charset=utf-8',
context: service,
data: '{"key":"value"}',
dataType: 'json',
headers: {},
Expand All @@ -211,7 +209,6 @@ test('options() empty data', function(assert) {
const ajaxOptions = service.options(url, { type });

assert.deepEqual(ajaxOptions, {
context: service,
dataType: 'json',
headers: {},
type: 'POST',
Expand Down

0 comments on commit 06141d9

Please sign in to comment.