-
-
Notifications
You must be signed in to change notification settings - Fork 11.5k
Cleaned up tier repository initializer #26766
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -10,9 +10,9 @@ const nql = require('@tryghost/nql'); | |||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||
| module.exports = class TierRepository { | ||||||||||||||||||||||||||||||||
| /** @type {import('./tier')[]} */ | ||||||||||||||||||||||||||||||||
| #store = []; | ||||||||||||||||||||||||||||||||
| /** @type {Object.<string, true>} */ | ||||||||||||||||||||||||||||||||
| #ids = {}; | ||||||||||||||||||||||||||||||||
| #store; | ||||||||||||||||||||||||||||||||
| /** @type {Set<string>} */ | ||||||||||||||||||||||||||||||||
| #ids = new Set(); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| /** @type {Object} */ | ||||||||||||||||||||||||||||||||
| #ProductModel; | ||||||||||||||||||||||||||||||||
|
|
@@ -31,16 +31,15 @@ module.exports = class TierRepository { | |||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| async init() { | ||||||||||||||||||||||||||||||||
| this.#store = []; | ||||||||||||||||||||||||||||||||
| this.#ids = {}; | ||||||||||||||||||||||||||||||||
| const models = await this.#ProductModel.findAll({ | ||||||||||||||||||||||||||||||||
| withRelated: ['benefits'] | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
| for (const model of models) { | ||||||||||||||||||||||||||||||||
| this.#ids.clear(); | ||||||||||||||||||||||||||||||||
| this.#store = await Promise.all(models.map(async (model) => { | ||||||||||||||||||||||||||||||||
| const tier = await Tier.create(this.mapToTier(model)); | ||||||||||||||||||||||||||||||||
| this.#store.push(tier); | ||||||||||||||||||||||||||||||||
| this.#ids[tier.id.toHexString()] = true; | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| this.#ids.add(tier.id.toHexString()); | ||||||||||||||||||||||||||||||||
| return tier; | ||||||||||||||||||||||||||||||||
| })); | ||||||||||||||||||||||||||||||||
|
Comment on lines
+37
to
+42
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Populate the caches atomically after
💡 Suggested change async init() {
const models = await this.#ProductModel.findAll({
withRelated: ['benefits']
});
- this.#ids.clear();
- this.#store = await Promise.all(models.map(async (model) => {
- const tier = await Tier.create(this.mapToTier(model));
- this.#ids.add(tier.id.toHexString());
- return tier;
- }));
+ const store = await Promise.all(models.map((model) => {
+ return Tier.create(this.mapToTier(model));
+ }));
+
+ this.#store = store;
+ this.#ids = new Set(store.map(tier => tier.id.toHexString()));
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||
|
|
@@ -136,7 +135,7 @@ module.exports = class TierRepository { | |||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| const toSave = await Tier.create(tier); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| if (this.#ids[tier.id.toHexString()]) { | ||||||||||||||||||||||||||||||||
| if (this.#ids.has(tier.id.toHexString())) { | ||||||||||||||||||||||||||||||||
| const existing = this.#store.findIndex((item) => { | ||||||||||||||||||||||||||||||||
| return item.id.equals(tier.id); | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
|
|
@@ -147,7 +146,7 @@ module.exports = class TierRepository { | |||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||
| await this.#ProductModel.add(data); | ||||||||||||||||||||||||||||||||
| this.#store.push(toSave); | ||||||||||||||||||||||||||||||||
| this.#ids[tier.id.toHexString()] = true; | ||||||||||||||||||||||||||||||||
| this.#ids.add(tier.id.toHexString()); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| for (const event of tier.events) { | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: if
init()is called twice (as it is in test fixture utils here), this could leave stale ids in the Set since we're no longer explicitly clearingthis.#ids.Unlikely to cause any real problems I think, but a slight unintended behavioral change.
Adding a
this.#ids.clear()at the beginning ofinit()would solve this, but I'm wouldn't consider this blockingThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. Addressed in d8bad23.