A platform for community discussion. Free, open, simple.
DEV: Convert discourseLater to TypeScript (#42456)
Sérgio Saquetimb554114938f4
diff --git a/frontend/discourse/app/lib/later.js b/frontend/discourse/app/lib/later.jsdeleted file mode 100644index b14c517ad74..00000000000--- a/frontend/discourse/app/lib/later.js+++ /dev/null@@ -1,14 +0,0 @@-import { later } from "@ember/runloop";-import { isTesting } from "discourse/lib/environment";--export default function () {- if (isTesting() && typeof [...arguments].at(-1) === "number") {- // Replace the `wait` argument with 10ms- let args = [].slice.call(arguments, 0, -1);- args.push(10);-- return later.apply(undefined, args);- } else {- return later(...arguments);- }-}diff --git a/frontend/discourse/app/lib/later.ts b/frontend/discourse/app/lib/later.tsnew file mode 100644index 00000000000..0eb2970eaa6--- /dev/null+++ b/frontend/discourse/app/lib/later.ts@@ -0,0 +1,40 @@+import { DEBUG } from "@glimmer/env";+import { later, type Timer } from "@ember/runloop";+import { isTesting } from "discourse/lib/environment";++/**+ * The wait that every scheduled call collapses to while the test suite is running.+ * Short enough not to slow a test down, but still a real turn of the run loop rather+ * than an immediate call, so ordering that depends on the delay is preserved.+ */+const TESTING_WAIT_MS = 10;++/**+ * Schedules a function on the run loop, exactly like Ember's `later`, except that+ * the wait collapses to {@link TESTING_WAIT_MS} while the test suite is running. A+ * timer scheduled for several seconds would otherwise make every test that waits on+ * it that slow.+ *+ * The shortening applies only when the final argument is a number, which is the+ * position `later` reads the wait from. Any other call shape is forwarded untouched,+ * as are all calls outside of tests.+ *+ * Accepts each of `later`'s call forms and, like it, returns a timer that can be+ * handed to `cancel`.+ */+function discourseLater(...args: unknown[]): Timer {+ // `later`'s overloads describe its individual call forms, none of which a+ // spread of unknowns can satisfy; this wrapper only relays the arguments.+ const relay = later as (...args: unknown[]) => Timer;++ if (DEBUG && isTesting() && typeof [...args].at(-1) === "number") {+ const shortenedArgs = args.slice(0, -1);+ shortenedArgs.push(TESTING_WAIT_MS);++ return relay(...shortenedArgs);+ } else {+ return relay(...args);+ }+}++export default discourseLater as typeof later;diff --git a/frontend/discourse/type-tests/lib/later-test.ts b/frontend/discourse/type-tests/lib/later-test.tsnew file mode 100644index 00000000000..36ff1e65e04--- /dev/null+++ b/frontend/discourse/type-tests/lib/later-test.ts@@ -0,0 +1,28 @@+import type { Timer } from "@ember/runloop";+import { expectTypeOf } from "expect-type";+import discourseLater from "discourse/lib/later";++const target = {+ method(count: number) {+ return count;+ },+};++// The timer is what callers hold on to in order to `cancel()` later, so it has to+// survive the wrapper. Before this module was typed it inferred as `any`, which+// silently degraded every `ReturnType<typeof discourseLater>` field into `any`.+expectTypeOf(discourseLater(() => {}, 100)).toEqualTypeOf<Timer>();+expectTypeOf(discourseLater(() => {}, 100)).not.toBeAny();++// `later`'s three call forms all have to survive the wrapper: a bare method, a+// target plus a function, and a target plus a method name.+expectTypeOf(+ discourseLater(target, function () {}, 100)+).toEqualTypeOf<Timer>();+expectTypeOf(discourseLater(target, "method", 5, 100)).toEqualTypeOf<Timer>();++// Arguments destined for the scheduled function are still checked against it,+// rather than being swallowed by a variadic `any[]`.+expectTypeOf(discourseLater)+ .parameter(0)+ .not.toEqualTypeOf<ReadonlyArray<unknown>>();