From b16028bc798266f2362b4796709a37e931da36ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Saquetim?= <1108771+megothss@users.noreply.github.com> Date: Tue, 2 Aug 2022 16:22:17 -0300 Subject: [PATCH] FIX: Inject appEvents in ScreenTrack (#17751) This commit reverts partially https://github.com/discourse/discourse/pull/17543. Service appEvents was not being injected in ScreenTrack. This causes `this.appEvents.trigger("topic:timings-sent", data);` to fail and the error is swallowed by the `catch` on the promise. This caused a regression on plugins that rely on this event to implement other behaviors. --- .../discourse/app/services/screen-track.js | 4 ++- .../tests/unit/services/screen-track-test.js | 32 ++++++++++++++++--- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/app/assets/javascripts/discourse/app/services/screen-track.js b/app/assets/javascripts/discourse/app/services/screen-track.js index edc9f9d3363..07fea19d3ae 100644 --- a/app/assets/javascripts/discourse/app/services/screen-track.js +++ b/app/assets/javascripts/discourse/app/services/screen-track.js @@ -1,4 +1,4 @@ -import Service from "@ember/service"; +import Service, { inject as service } from "@ember/service"; import { ajax } from "discourse/lib/ajax"; import { bind } from "discourse-common/utils/decorators"; import { isTesting } from "discourse-common/config/environment"; @@ -18,6 +18,8 @@ const AJAX_FAILURE_DELAYS = [5000, 10000, 20000, 40000]; const ALLOWED_AJAX_FAILURES = [405, 429, 500, 501, 502, 503, 504]; export default class ScreenTrack extends Service { + @service appEvents; + _consolidatedTimings = []; _lastTick = null; _lastScrolled = null; diff --git a/app/assets/javascripts/discourse/tests/unit/services/screen-track-test.js b/app/assets/javascripts/discourse/tests/unit/services/screen-track-test.js index b3580a066ba..d586cb6e7f0 100644 --- a/app/assets/javascripts/discourse/tests/unit/services/screen-track-test.js +++ b/app/assets/javascripts/discourse/tests/unit/services/screen-track-test.js @@ -1,9 +1,11 @@ -import { discourseModule } from "discourse/tests/helpers/qunit-helpers"; -import { test } from "qunit"; +import { module, test } from "qunit"; +import { setupTest } from "ember-qunit"; + +module("Unit | Service | screen-track", function (hooks) { + setupTest(hooks); -discourseModule("Unit | Service | screen-track", function () { test("consolidateTimings", async function (assert) { - const tracker = this.container.lookup("service:screen-track"); + const tracker = this.owner.lookup("service:screen-track"); tracker.consolidateTimings({ 1: 10, 2: 5 }, 10, 1); tracker.consolidateTimings({ 1: 5, 3: 1 }, 3, 1); @@ -26,4 +28,26 @@ discourseModule("Unit | Service | screen-track", function () { "caches highest read post number for second topic" ); }); + + test("ScreenTrack has appEvents", async function (assert) { + const tracker = this.owner.lookup("service:screen-track"); + assert.ok(tracker.appEvents); + }); + + test("appEvent topic:timings-sent is triggered after posting consolidated timings", async function (assert) { + assert.timeout(1000); + + const tracker = this.owner.lookup("service:screen-track"); + const appEvents = this.owner.lookup("service:app-events"); + + const done = assert.async(); + + appEvents.on("topic:timings-sent", () => { + assert.ok(true); + done(); + }); + + tracker.consolidateTimings({ 1: 10, 2: 5 }, 10, 1); + await tracker.sendNextConsolidatedTiming(); + }); });