Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion apps/mobile/src/connection/background-activity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down
54 changes: 53 additions & 1 deletion apps/web/src/lib/backgroundActivityReporter.test.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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),
);
});
4 changes: 3 additions & 1 deletion apps/web/src/lib/backgroundActivityReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down
Loading