Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

list-scroll-activity.test.ts

53 lines1.6 KB

1import { describe, expect, it } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import * as Fiber from "effect/Fiber";
4import * as Ref from "effect/Ref";
5​
6import { isListScrolling, setListScrolling, waitUntilScrollIdle } from "./list-scroll-activity";
7​
8describe("list-scroll-activity", () => {
9 it.effect("waits until all active scrollers settle", () =>
10 Effect.gen(function* () {
11 const ownerA = {};
12 const ownerB = {};
13 const settled = yield* Ref.make(false);
14​
15 expect(isListScrolling()).toBe(false);
16 yield* waitUntilScrollIdle;
17​
18 setListScrolling(true, ownerA);
19 setListScrolling(true, ownerB);
20 expect(isListScrolling()).toBe(true);
21​
22 const waiter = yield* waitUntilScrollIdle.pipe(
23 Effect.andThen(Ref.set(settled, true)),
24 Effect.forkChild({ startImmediately: true }),
25 );
26​
27 setListScrolling(false, ownerA);
28 expect(isListScrolling()).toBe(true);
29 expect(yield* Ref.get(settled)).toBe(false);
30​
31 setListScrolling(false, ownerB);
32 expect(isListScrolling()).toBe(false);
33 yield* Fiber.join(waiter);
34 expect(yield* Ref.get(settled)).toBe(true);
35 }),
36 );
37​
38 it.effect("cleans up interrupted waiters without leaking", () =>
39 Effect.gen(function* () {
40 const owner = {};
41 setListScrolling(true, owner);
42 try {
43 const waiter = yield* waitUntilScrollIdle.pipe(
44 Effect.forkChild({ startImmediately: true }),
45 );
46 yield* Fiber.interrupt(waiter);
47 } finally {
48 setListScrolling(false, owner);
49 }
50 expect(isListScrolling()).toBe(false);
51 }),
52 );
53});