From 40355139018a3119d24430e230397c3fece01e90 Mon Sep 17 00:00:00 2001 From: Jarek Radosz Date: Mon, 22 Jan 2024 12:13:02 +0100 Subject: [PATCH] DEV: Clean up user-tips service (#25361) 1. TrackedSet instead of TrackedMap of `true`s 2. Use Set#has for early exit 3. Use the site service instead of Site import --- .../discourse/app/services/user-tips.js | 21 ++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/app/assets/javascripts/discourse/app/services/user-tips.js b/app/assets/javascripts/discourse/app/services/user-tips.js index 751ab86b5c7..f45e89ba3fe 100644 --- a/app/assets/javascripts/discourse/app/services/user-tips.js +++ b/app/assets/javascripts/discourse/app/services/user-tips.js @@ -1,7 +1,6 @@ import Service, { inject as service } from "@ember/service"; -import { TrackedMap } from "@ember-compat/tracked-built-ins"; +import { TrackedSet } from "@ember-compat/tracked-built-ins"; import { disableImplicitInjections } from "discourse/lib/implicit-injections"; -import Site from "discourse/models/site"; import { isTesting } from "discourse-common/config/environment"; @disableImplicitInjections @@ -11,15 +10,14 @@ export default class UserTips extends Service { #availableTips = new Set(); #renderedId; - #shouldRenderMap = new TrackedMap(); + #shouldRenderSet = new TrackedSet(); #updateRenderedId() { - const tipsArray = [...this.#availableTips]; - if (tipsArray.find((tip) => tip.id === this.#renderedId)) { + if (this.#availableTips.has(this.#renderedId)) { return; } - const newId = tipsArray + const newId = [...this.#availableTips] .sortBy("priority") .reverse() .find((tip) => { @@ -29,14 +27,14 @@ export default class UserTips extends Service { })?.id; if (this.#renderedId !== newId) { - this.#shouldRenderMap.delete(this.#renderedId); - this.#shouldRenderMap.set(newId, true); + this.#shouldRenderSet.delete(this.#renderedId); + this.#shouldRenderSet.add(newId); this.#renderedId = newId; } } shouldRender(id) { - return this.#shouldRenderMap.get(id); + return this.#shouldRenderSet.has(id); } addAvailableTip(tip) { @@ -54,8 +52,7 @@ export default class UserTips extends Service { return false; } - const userTips = Site.currentProp("user_tips"); - + const userTips = this.site.user_tips; if (!userTips || this.currentUser.user_option?.skip_new_user_tips) { return false; } @@ -81,7 +78,7 @@ export default class UserTips extends Service { return; } - const userTips = Site.currentProp("user_tips"); + const userTips = this.site.user_tips; if (!userTips || this.currentUser.user_option?.skip_new_user_tips) { return; }