FIX: Memory Leaks w/ Container (#7750)
Gives instance initializers the ability to add a `teardown` method that will be called between tests to clean up after themselves.
This commit is contained in:
parent
47095a7fa1
commit
3d7c26c15e
|
@ -1,21 +1,28 @@
|
||||||
// Append our CSRF token to AJAX requests when necessary.
|
// Append our CSRF token to AJAX requests when necessary.
|
||||||
|
|
||||||
let installedFilter = false;
|
let installed = false;
|
||||||
|
let callbacks = $.Callbacks();
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: "csrf-token",
|
name: "csrf-token",
|
||||||
initialize: function(container) {
|
initialize(container) {
|
||||||
// Add a CSRF token to all AJAX requests
|
// Add a CSRF token to all AJAX requests
|
||||||
let session = container.lookup("session:main");
|
let session = container.lookup("session:main");
|
||||||
session.set("csrfToken", $("meta[name=csrf-token]").attr("content"));
|
session.set("csrfToken", $("meta[name=csrf-token]").attr("content"));
|
||||||
|
|
||||||
if (!installedFilter) {
|
if (!installed) {
|
||||||
$.ajaxPrefilter(function(options, originalOptions, xhr) {
|
$.ajaxPrefilter(callbacks.fire);
|
||||||
if (!options.crossDomain) {
|
installed = true;
|
||||||
xhr.setRequestHeader("X-CSRF-Token", session.get("csrfToken"));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
installedFilter = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
callbacks.add(function(options, originalOptions, xhr) {
|
||||||
|
if (!options.crossDomain) {
|
||||||
|
xhr.setRequestHeader("X-CSRF-Token", session.get("csrfToken"));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
teardown() {
|
||||||
|
callbacks.empty();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -169,6 +169,15 @@ QUnit.testDone(function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Discourse._runInitializer("instanceInitializers", function(
|
||||||
|
name,
|
||||||
|
initializer
|
||||||
|
) {
|
||||||
|
if (initializer && initializer.teardown) {
|
||||||
|
initializer.teardown();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// attempts to remove any subscribed message bus callback
|
// attempts to remove any subscribed message bus callback
|
||||||
window.MessageBus.callbacks.forEach(function(callback) {
|
window.MessageBus.callbacks.forEach(function(callback) {
|
||||||
window.MessageBus.unsubscribe(callback.channel, callback.func);
|
window.MessageBus.unsubscribe(callback.channel, callback.func);
|
||||||
|
|
Loading…
Reference in New Issue