Skip to content
TypeScript

fix(web,mobile): keep reporting client activity after an interrupted report (#15738)

Sky Hong7c367e88d39a
diff --git a/apps/mobile/src/connection/background-activity.ts b/apps/mobile/src/connection/background-activity.ts
index 80e185b9e05..d6cd2f0ca2d 100644
--- a/apps/mobile/src/connection/background-activity.ts
+++ b/apps/mobile/src/connection/background-activity.ts
@@ -81,7 +81,9 @@ export const layerReporter = Layer.effectDiscard(
observedAt: DateTime.makeUnsafe(observedAtMs),
}),
)
- .pipe(Effect.ignore),
+ // Not `ignore`: tearing down an environment mid-request interrupts its report,
+ // and an interrupt leaking out here would end the reporting loop for good.
+ .pipe(Effect.ignoreCause),
{ concurrency: "unbounded", discard: true },
);
}).pipe(Effect.withSpan("mobile.backgroundActivity.report"));
diff --git a/apps/web/src/lib/backgroundActivityReporter.test.ts b/apps/web/src/lib/backgroundActivityReporter.test.ts
index 23e90448332..3dee04ec171 100644
--- a/apps/web/src/lib/backgroundActivityReporter.test.ts
+++ b/apps/web/src/lib/backgroundActivityReporter.test.ts
@@ -1,8 +1,15 @@
+import { EnvironmentRegistry } from "@t3tools/client-runtime/connection";
import { EnvironmentId, WS_METHODS } from "@t3tools/contracts";
-import { describe, expect, it } from "@effect/vitest";
+import { afterEach, beforeEach, describe, expect, it } from "@effect/vitest";
+import * as Cause from "effect/Cause";
import * as Effect from "effect/Effect";
+import * as Layer from "effect/Layer";
+import * as SubscriptionRef from "effect/SubscriptionRef";
+import * as TestClock from "effect/testing/TestClock";
+import { vi } from "vite-plus/test";
import {
+ layer,
observeBackgroundActivitySubscription,
retainedBackgroundScopes,
wasRecentlyInteracted,
@@ -70,3 +77,48 @@ describe("wasRecentlyInteracted", () => {
}),
);
});
+
+describe("layer", () => {
+ beforeEach(() => {
+ const target = new EventTarget();
+ const listeners = {
+ addEventListener: target.addEventListener.bind(target),
+ removeEventListener: target.removeEventListener.bind(target),
+ };
+ vi.stubGlobal("window", {
+ ...listeners,
+ localStorage: { getItem: () => "client", setItem: () => undefined },
+ });
+ vi.stubGlobal("document", { ...listeners, visibilityState: "visible", hasFocus: () => true });
+ });
+
+ afterEach(() => {
+ vi.unstubAllGlobals();
+ });
+
+ // As when an environment's connection is torn down while its report is in flight.
+ it.effect("keeps reporting after a report is interrupted", () =>
+ Effect.gen(function* () {
+ const entries = yield* SubscriptionRef.make(
+ new Map([[EnvironmentId.make("environment-reporter-test"), {}]]),
+ );
+ let calls = 0;
+ const registry = EnvironmentRegistry.EnvironmentRegistry.of({
+ entries,
+ run: () => {
+ calls += 1;
+ return calls === 1 ? Effect.failCause(Cause.interrupt()) : Effect.void;
+ },
+ } as unknown as EnvironmentRegistry.EnvironmentRegistry["Service"]);
+
+ yield* Layer.build(
+ layer.pipe(Layer.provide(Layer.succeed(EnvironmentRegistry.EnvironmentRegistry, registry))),
+ );
+ yield* TestClock.adjust("1 second");
+ expect(calls).toBe(1);
+
+ yield* TestClock.adjust("25 seconds");
+ expect(calls).toBe(2);
+ }).pipe(Effect.scoped),
+ );
+});
diff --git a/apps/web/src/lib/backgroundActivityReporter.ts b/apps/web/src/lib/backgroundActivityReporter.ts
index 21c09f7600c..3ae10997c55 100644
--- a/apps/web/src/lib/backgroundActivityReporter.ts
+++ b/apps/web/src/lib/backgroundActivityReporter.ts
@@ -205,7 +205,9 @@ export const layer = Layer.effectDiscard(
createActivityReport(environmentId, lastInteractionAtMs, observedAtMs),
),
)
- .pipe(Effect.ignore),
+ // Not `ignore`: tearing down an environment mid-request interrupts its report,
+ // and an interrupt leaking out here would end the reporting loop for good.
+ .pipe(Effect.ignoreCause),
{ concurrency: "unbounded", discard: true },
);
}).pipe(Effect.withSpan("web.backgroundActivity.report"));