Skip to content

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

DEV: Improve reactivity of user-tips and remove runloop workarounds (#23897)

David Taylor597ef1119536
diff --git a/app/assets/javascripts/discourse/app/components/user-tip.gjs b/app/assets/javascripts/discourse/app/components/user-tip.gjs
index dae57f0e06b..369e047e907 100644
--- a/app/assets/javascripts/discourse/app/components/user-tip.gjs
+++ b/app/assets/javascripts/discourse/app/components/user-tip.gjs
@@ -15,13 +15,15 @@ export default class UserTip extends Component {
@service tooltip;
registerTip = modifier(() => {
- this.userTips.addAvailableTip({
+ const tip = {
id: this.args.id,
priority: this.args.priority ?? 0,
- });
+ };
+
+ this.userTips.addAvailableTip(tip);
return () => {
- this.userTips.removeAvailableTip({ id: this.args.id });
+ this.userTips.removeAvailableTip(tip);
};
});
@@ -69,7 +71,7 @@ export default class UserTip extends Component {
});
get shouldRenderTip() {
- return this.userTips.renderedId === this.args.id;
+ return this.userTips.shouldRender(this.args.id);
}
<template>
diff --git a/app/assets/javascripts/discourse/app/services/user-tips.js b/app/assets/javascripts/discourse/app/services/user-tips.js
index bb266681209..751ab86b5c7 100644
--- a/app/assets/javascripts/discourse/app/services/user-tips.js
+++ b/app/assets/javascripts/discourse/app/services/user-tips.js
@@ -1,6 +1,5 @@
-import { tracked } from "@glimmer/tracking";
-import { next } from "@ember/runloop";
import Service, { inject as service } from "@ember/service";
+import { TrackedMap } 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";
@@ -10,15 +9,17 @@ export default class UserTips extends Service {
@service site;
@service currentUser;
- @tracked availableTips = [];
- @tracked renderedId;
+ #availableTips = new Set();
+ #renderedId;
+ #shouldRenderMap = new TrackedMap();
- computeRenderedId() {
- if (this.availableTips.find((tip) => tip.id === this.renderedId)) {
- return this.renderedId;
+ #updateRenderedId() {
+ const tipsArray = [...this.#availableTips];
+ if (tipsArray.find((tip) => tip.id === this.#renderedId)) {
+ return;
}
- return this.availableTips
+ const newId = tipsArray
.sortBy("priority")
.reverse()
.find((tip) => {
@@ -26,23 +27,26 @@ export default class UserTips extends Service {
return tip.id;
}
})?.id;
+
+ if (this.#renderedId !== newId) {
+ this.#shouldRenderMap.delete(this.#renderedId);
+ this.#shouldRenderMap.set(newId, true);
+ this.#renderedId = newId;
+ }
+ }
+
+ shouldRender(id) {
+ return this.#shouldRenderMap.get(id);
}
addAvailableTip(tip) {
- next(() => {
- this.availableTips = [...this.availableTips, tip];
- this.renderedId = this.computeRenderedId();
- });
+ this.#availableTips.add(tip);
+ this.#updateRenderedId();
}
removeAvailableTip(tip) {
- next(() => {
- this.availableTips = this.availableTips.filter((availableTip) => {
- return tip.id !== availableTip.id;
- });
-
- this.renderedId = this.computeRenderedId();
- });
+ this.#availableTips.delete(tip);
+ this.#updateRenderedId();
}
canSeeUserTip(tipId) {
@@ -89,7 +93,8 @@ export default class UserTips extends Service {
return;
}
- this.removeAvailableTip({ id: tipId });
+ const tipObj = [...this.#availableTips].find((t) => t.id === tipId);
+ this.removeAvailableTip(tipObj);
// Update list of seen user tips.
let seenUserTips = this.currentUser.user_option?.seen_popups || [];