Skip to content

Commit

Permalink
FIX: Prevent PreloadStore from calling the finder when value is fal…
Browse files Browse the repository at this point in the history
…sy (#14899)
  • Loading branch information
CvX committed Nov 12, 2021
1 parent 79f49df commit 97aa56b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
18 changes: 8 additions & 10 deletions app/assets/javascripts/discourse/app/lib/preload-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},

/**
Expand All @@ -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;
}

Expand All @@ -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();
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

0 comments on commit 97aa56b

Please sign in to comment.