Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid memory leakage and circular reference #119

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 33 additions & 8 deletions src/xhr-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,22 @@ Handler[prototype] = Object.create({
triggerListener(xhr, eventReadyStateChange);
triggerListener(xhr, eventLoad);
triggerListener(xhr, eventLoadEnd);
if (xhr.readyState === 4) {
if (xhr.config) xhr.config.xhr = null;
xhr['on' + eventReadyStateChange] = null;
xhr.config = null;
}
},
reject: function reject(error) {
var xhr = this.xhr;
this.xhrProxy.status = 0;
triggerListener(this.xhr, error.type);
triggerListener(this.xhr, eventLoadEnd);
triggerListener(xhr, error.type);
triggerListener(xhr, eventLoadEnd);
if (xhr.readyState === 4) {
if (xhr.config) xhr.config.xhr = null;
xhr['on' + eventReadyStateChange] = null;
xhr.config = null;
}
}
});

Expand Down Expand Up @@ -149,10 +160,13 @@ function proxyAjax(proxy, win) {
}

function stateChangeCallback(xhr, xhrProxy) {
if (xhr.readyState === 4 && xhr.status !== 0) {
handleResponse(xhr, xhrProxy);
} else if (xhr.readyState !== 4) {
triggerListener(xhr, eventReadyStateChange);
var config = xhr ? xhr.config : null;
if (config && xhr && config.xhr === xhr) {
if (xhr.readyState === 4 && xhr.status !== 0) {
handleResponse(xhr, xhrProxy);
} else if (xhr.readyState !== 4) {
triggerListener(xhr, eventReadyStateChange);
}
}
return true;
}
Expand All @@ -175,11 +189,22 @@ function proxyAjax(proxy, win) {
config.async = args[2];
config.user = args[3];
config.password = args[4];
config.xhr = xhr;
Object.defineProperty(config, 'xhr', {
get() {
return xhr; // xhr wil be set to null after xhr.readyState === XMLHttpRequest.DONE (4)
},
set(nv) {
if (nv === null) xhr = null;
return true;
},
enumerable: false,
configurable: true
});
// config.xhr = xhr;
var evName = 'on' + eventReadyStateChange;
if (!xhr[evName]) {
xhr[evName] = function () {
return stateChangeCallback(xhr, _this);
return stateChangeCallback(this, _this);
};
}

Expand Down