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
53 changes: 53 additions & 0 deletions packages/client-runtime/src/state/pullRequests.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,59 @@ it.effect("keeps hover previews fresh after edits and turns", () =>
),
);

it.effect("shares one refresh stream between independently created pull request readers", () =>
Effect.scoped(
Effect.gen(function* () {
const revision = yield* SubscriptionRef.make(0);
let subscriptions = 0;
let title = "Original title";
const client = {
[WS_METHODS.pullRequestsSubscribeRefreshes]: () =>
Stream.unwrap(
Effect.sync(() => {
subscriptions++;
return SubscriptionRef.changes(revision);
}),
),
[WS_METHODS.pullRequestsSummary]: () => Effect.sync(() => ({ title, state: "open" })),
[WS_METHODS.pullRequestsDetail]: () => Effect.sync(() => ({ title, state: "open" })),
} as unknown as WsRpcProtocolClient;
const { runtime, atoms, registry } = yield* makeTestRuntime(client);
const target = {
environmentId: TARGET.environmentId,
input: { projectId: ProjectId.make("project-1"), repository: "acme/web", number: 1 },
};
const summary = createLinkedPullRequestSummaryAtomFamily(runtime)(target);
const detail = atoms.detail(target);
for (const atom of [summary, detail]) {
const unmount = registry.mount(atom);
yield* Effect.addFinalizer(() => Effect.sync(unmount));
expect((yield* AtomRegistry.getResult(registry, atom)).title).toBe(title);
}
yield* AtomRegistry.getResult(
registry,
atoms.refreshes({ environmentId: target.environmentId, input: {} }),
);
expect(subscriptions).toBe(1);

const changed = [summary, detail].map((atom) => {
const ready = Latch.makeUnsafe();
const stop = registry.subscribe(atom, (result) => {
if (AsyncResult.isSuccess(result) && result.value.title === "Updated title") {
ready.openUnsafe();
}
});
return { ready, stop };
});
yield* Effect.addFinalizer(() => Effect.sync(() => changed.forEach(({ stop }) => stop())));
title = "Updated title";
yield* SubscriptionRef.update(revision, (value) => value + 1);
for (const { ready } of changed) yield* ready.await;
expect(subscriptions).toBe(1);
}),
),
);

it.effect("shares close, reopen, and merge with an untouched client's mounted PR readers", () =>
Effect.scoped(
Effect.gen(function* () {
Expand Down
17 changes: 16 additions & 1 deletion packages/client-runtime/src/state/pullRequests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function updateCached<A, E>(
registry.update(atom, AsyncResult.map(update));
}

function createPullRequestRefreshAtomFamily<R, E>(
function makePullRequestRefreshAtomFamily<R, E>(
runtime: Atom.AtomRuntime<EnvironmentRegistry | R, E>,
) {
return createEnvironmentRpcSubscriptionAtomFamily(runtime, {
Expand All @@ -115,6 +115,21 @@ function createPullRequestRefreshAtomFamily<R, E>(
});
}

const refreshFamiliesByRuntime = new WeakMap<
object,
ReturnType<typeof makePullRequestRefreshAtomFamily>
>();

function createPullRequestRefreshAtomFamily<R, E>(
runtime: Atom.AtomRuntime<EnvironmentRegistry | R, E>,
): ReturnType<typeof makePullRequestRefreshAtomFamily<R, E>> {
const existing = refreshFamiliesByRuntime.get(runtime);
if (existing) return existing as ReturnType<typeof makePullRequestRefreshAtomFamily<R, E>>;
const refreshes = makePullRequestRefreshAtomFamily(runtime);
refreshFamiliesByRuntime.set(runtime, refreshes);
return refreshes;
}

export function createLinkedPullRequestSummaryAtomFamily<R, E>(
runtime: Atom.AtomRuntime<EnvironmentRegistry | R, E>,
refreshes = createPullRequestRefreshAtomFamily(runtime),
Expand Down
Loading