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

This commit is contained in:
Jarek Radosz 2021-11-12 17:45:06 +01:00 committed by GitHub
parent 79f49dfb7a
commit 97aa56bdc3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 10 deletions

View File

@ -3,10 +3,10 @@
import { Promise } from "rsvp"; import { Promise } from "rsvp";
export default { export default {
data: {}, data: new Map(),
store(key, value) { 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. So, for example, you can't load a preloaded topic more than once.
**/ **/
getAndRemove(key, finder) { getAndRemove(key, finder) {
if (this.data[key]) { if (this.data.has(key)) {
let promise = Promise.resolve(this.data[key]); let promise = Promise.resolve(this.data.get(key));
delete this.data[key]; this.data.delete(key);
return promise; return promise;
} }
@ -41,16 +41,14 @@ export default {
}, },
get(key) { get(key) {
return this.data[key]; return this.data.get(key);
}, },
remove(key) { remove(key) {
if (this.data[key]) { this.data.delete(key);
delete this.data[key];
}
}, },
reset() { reset() {
this.data = {}; this.data = new Map();
}, },
}; };

View File

@ -54,4 +54,12 @@ module("Unit | Utility | preload-store", function (hooks) {
const result = await PreloadStore.getAndRemove("bane"); const result = await PreloadStore.getAndRemove("bane");
assert.strictEqual(result, "evil"); 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);
});
}); });