From 06141d9fbc37e9616ccb124f051d84d3d7b5e8e4 Mon Sep 17 00:00:00 2001 From: Alex LaFroscia Date: Thu, 5 May 2016 18:17:53 -0400 Subject: [PATCH] Fix memory leak in tests (#105) * Fix memory leak in tests Closes #104 * Make the number of pending requests global * Avoid unnecessary fat-arrow function * Switch to one, global waiter --- addon/ajax-request.js | 25 ++++++++++--------------- tests/unit/ajax-request-test.js | 3 --- 2 files changed, 10 insertions(+), 18 deletions(-) diff --git a/addon/ajax-request.js b/addon/ajax-request.js index 2478f6ba..5ff21ac6 100644 --- a/addon/ajax-request.js +++ b/addon/ajax-request.js @@ -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); @@ -90,7 +86,7 @@ export default class AjaxRequest { requestData ); - this.pendingRequestCount--; + pendingRequestCount--; if (isAjaxError(response)) { run.join(null, reject, { payload, textStatus, jqXHR, response }); @@ -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}`); @@ -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); diff --git a/tests/unit/ajax-request-test.js b/tests/unit/ajax-request-test.js index f5b42f7d..419ef5a9 100644 --- a/tests/unit/ajax-request-test.js +++ b/tests/unit/ajax-request-test.js @@ -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' }, @@ -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: {}, @@ -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',