Skip to content

A platform for community discussion. Free, open, simple.

FIX: Prevent `PreloadStore` from calling the finder when value is falsy (#14899)

Jarek Radosz97aa56bdc326
diff --git a/app/assets/javascripts/discourse/app/lib/preload-store.js b/app/assets/javascripts/discourse/app/lib/preload-store.js
index 9fe7ca37888..3562b1da6ef 100644
--- a/app/assets/javascripts/discourse/app/lib/preload-store.js
+++ b/app/assets/javascripts/discourse/app/lib/preload-store.js
@@ -3,10 +3,10 @@
import { Promise } from "rsvp";
export default {
- data: {},
+ data: new Map(),
store(key, value) {
- this.data[key] = value;
+ this.data.set(key, value);
},
/**
@@ -16,9 +16,9 @@ export default {
So, for example, you can't load a preloaded topic more than once.
**/
getAndRemove(key, finder) {
- if (this.data[key]) {
- let promise = Promise.resolve(this.data[key]);
- delete this.data[key];
+ if (this.data.has(key)) {
+ let promise = Promise.resolve(this.data.get(key));
+ this.data.delete(key);
return promise;
}
@@ -41,16 +41,14 @@ export default {
},
get(key) {
- return this.data[key];
+ return this.data.get(key);
},
remove(key) {
- if (this.data[key]) {
- delete this.data[key];
- }
+ this.data.delete(key);
},
reset() {
- this.data = {};
+ this.data = new Map();
},
};
diff --git a/app/assets/javascripts/discourse/tests/unit/lib/preload-store-test.js b/app/assets/javascripts/discourse/tests/unit/lib/preload-store-test.js
index 089ef194d66..ca060af841a 100644
--- a/app/assets/javascripts/discourse/tests/unit/lib/preload-store-test.js
+++ b/app/assets/javascripts/discourse/tests/unit/lib/preload-store-test.js
@@ -54,4 +54,12 @@ module("Unit | Utility | preload-store", function (hooks) {
const result = await PreloadStore.getAndRemove("bane");
assert.strictEqual(result, "evil");
});
+
+ test("returns falsy values without calling finder", async function (assert) {
+ PreloadStore.store("falsy", false);
+ const result = await PreloadStore.getAndRemove("falsy", () =>
+ assert.ok(false)
+ );
+ assert.strictEqual(result, false);
+ });
});