Skip to content

Commit

Permalink
[evgeny, carlos] fixed listener leak in util.request
Browse files Browse the repository at this point in the history
  • Loading branch information
Evgeny Poberezkin committed Aug 14, 2014
1 parent 984267f commit efb85fd
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 46 deletions.
49 changes: 26 additions & 23 deletions lib/util/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,6 @@ var _ = require('mol-proto')
module.exports = request;


function onReady(req, callback, promise) {
if (req.readyState == 4) {
if (req.statusText.toUpperCase() == 'OK' ) {
callback && callback(null, req.responseText, req);
promise.setData(null, req.responseText);
postMessage('success');
}
else if(req.status != 0) { // not canceled eg. with abort() method
callback && callback(req.status, req.responseText, req);
promise.setData(req.status, req.responseText);
postMessage('error');
postMessage('error' + req.status);
}
}

function postMessage(msg) {
if (_messenger) request.postMessageSync(msg,
{ status: req.status, response: req.responseText });
}
}


function request(url, opts, callback) {
opts.url = url;
opts.contentType = opts.contentType || 'application/json;charset=UTF-8';
Expand All @@ -68,10 +46,35 @@ function request(url, opts, callback) {

var promise = new Promise(req);

req.onreadystatechange = _.partial(onReady, req, callback, promise);
req.addEventListener('readystatechange', onReady);
req.send(JSON.stringify(opts.data));

return promise;


function onReady() {
if (req.readyState == 4) {
if (req.statusText.toUpperCase() == 'OK' ) {
callback && callback(null, req.responseText, req);
promise.setData(null, req.responseText);
postMessage('success');
}
else if(req.status != 0) { // not canceled eg. with abort() method
callback && callback(req.status, req.responseText, req);
promise.setData(req.status, req.responseText);
postMessage('error');
postMessage('error' + req.status);
}

// not removing subscription creates memory leak
req.removeEventListener('readystatechange', onReady);
}

function postMessage(msg) {
if (_messenger) request.postMessageSync(msg,
{ status: req.status, response: req.responseText });
}
}
}


Expand Down
49 changes: 26 additions & 23 deletions milo.bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -13925,28 +13925,6 @@ var _ = require('mol-proto')
module.exports = request;


function onReady(req, callback, promise) {
if (req.readyState == 4) {
if (req.statusText.toUpperCase() == 'OK' ) {
callback && callback(null, req.responseText, req);
promise.setData(null, req.responseText);
postMessage('success');
}
else if(req.status != 0) { // not canceled eg. with abort() method
callback && callback(req.status, req.responseText, req);
promise.setData(req.status, req.responseText);
postMessage('error');
postMessage('error' + req.status);
}
}

function postMessage(msg) {
if (_messenger) request.postMessageSync(msg,
{ status: req.status, response: req.responseText });
}
}


function request(url, opts, callback) {
opts.url = url;
opts.contentType = opts.contentType || 'application/json;charset=UTF-8';
Expand All @@ -13962,10 +13940,35 @@ function request(url, opts, callback) {

var promise = new Promise(req);

req.onreadystatechange = _.partial(onReady, req, callback, promise);
req.addEventListener('readystatechange', onReady);
req.send(JSON.stringify(opts.data));

return promise;


function onReady() {
if (req.readyState == 4) {
if (req.statusText.toUpperCase() == 'OK' ) {
callback && callback(null, req.responseText, req);
promise.setData(null, req.responseText);
postMessage('success');
}
else if(req.status != 0) { // not canceled eg. with abort() method
callback && callback(req.status, req.responseText, req);
promise.setData(req.status, req.responseText);
postMessage('error');
postMessage('error' + req.status);
}

// not removing subscription creates memory leak
req.removeEventListener('readystatechange', onReady);
}

function postMessage(msg) {
if (_messenger) request.postMessageSync(msg,
{ status: req.status, response: req.responseText });
}
}
}


Expand Down

0 comments on commit efb85fd

Please sign in to comment.