Skip to content

Commit 8bbe5aa

Browse files
only21milcodex
andauthored
fix(mobile): share pull request refresh subscriptions (#23464)
Co-authored-by: GPT-6.1 Sol <[email protected]>
1 parent 9d42473 commit 8bbe5aa

2 files changed

Lines changed: 69 additions & 1 deletion

File tree

‎packages/client-runtime/src/state/pullRequests.test.ts‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,6 +1177,59 @@ it.effect("keeps hover previews fresh after edits and turns", () =>
11771177
),
11781178
);
11791179

1180+
it.effect("shares one refresh stream between independently created pull request readers", () =>
1181+
Effect.scoped(
1182+
Effect.gen(function* () {
1183+
const revision = yield* SubscriptionRef.make(0);
1184+
let subscriptions = 0;
1185+
let title = "Original title";
1186+
const client = {
1187+
[WS_METHODS.pullRequestsSubscribeRefreshes]: () =>
1188+
Stream.unwrap(
1189+
Effect.sync(() => {
1190+
subscriptions++;
1191+
return SubscriptionRef.changes(revision);
1192+
}),
1193+
),
1194+
[WS_METHODS.pullRequestsSummary]: () => Effect.sync(() => ({ title, state: "open" })),
1195+
[WS_METHODS.pullRequestsDetail]: () => Effect.sync(() => ({ title, state: "open" })),
1196+
} as unknown as WsRpcProtocolClient;
1197+
const { runtime, atoms, registry } = yield* makeTestRuntime(client);
1198+
const target = {
1199+
environmentId: TARGET.environmentId,
1200+
input: { projectId: ProjectId.make("project-1"), repository: "acme/web", number: 1 },
1201+
};
1202+
const summary = createLinkedPullRequestSummaryAtomFamily(runtime)(target);
1203+
const detail = atoms.detail(target);
1204+
for (const atom of [summary, detail]) {
1205+
const unmount = registry.mount(atom);
1206+
yield* Effect.addFinalizer(() => Effect.sync(unmount));
1207+
expect((yield* AtomRegistry.getResult(registry, atom)).title).toBe(title);
1208+
}
1209+
yield* AtomRegistry.getResult(
1210+
registry,
1211+
atoms.refreshes({ environmentId: target.environmentId, input: {} }),
1212+
);
1213+
expect(subscriptions).toBe(1);
1214+
1215+
const changed = [summary, detail].map((atom) => {
1216+
const ready = Latch.makeUnsafe();
1217+
const stop = registry.subscribe(atom, (result) => {
1218+
if (AsyncResult.isSuccess(result) && result.value.title === "Updated title") {
1219+
ready.openUnsafe();
1220+
}
1221+
});
1222+
return { ready, stop };
1223+
});
1224+
yield* Effect.addFinalizer(() => Effect.sync(() => changed.forEach(({ stop }) => stop())));
1225+
title = "Updated title";
1226+
yield* SubscriptionRef.update(revision, (value) => value + 1);
1227+
for (const { ready } of changed) yield* ready.await;
1228+
expect(subscriptions).toBe(1);
1229+
}),
1230+
),
1231+
);
1232+
11801233
it.effect("shares close, reopen, and merge with an untouched client's mounted PR readers", () =>
11811234
Effect.scoped(
11821235
Effect.gen(function* () {

‎packages/client-runtime/src/state/pullRequests.ts‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ function updateCached<A, E>(
115115
registry.update(atom, AsyncResult.map(update));
116116
}
117117

118-
function createPullRequestRefreshAtomFamily<R, E>(
118+
function makePullRequestRefreshAtomFamily<R, E>(
119119
runtime: Atom.AtomRuntime<EnvironmentRegistry | R, E>,
120120
) {
121121
return createEnvironmentRpcSubscriptionAtomFamily(runtime, {
@@ -124,6 +124,21 @@ function createPullRequestRefreshAtomFamily<R, E>(
124124
});
125125
}
126126

127+
const refreshFamiliesByRuntime = new WeakMap<
128+
object,
129+
ReturnType<typeof makePullRequestRefreshAtomFamily>
130+
>();
131+
132+
function createPullRequestRefreshAtomFamily<R, E>(
133+
runtime: Atom.AtomRuntime<EnvironmentRegistry | R, E>,
134+
): ReturnType<typeof makePullRequestRefreshAtomFamily<R, E>> {
135+
const existing = refreshFamiliesByRuntime.get(runtime);
136+
if (existing) return existing as ReturnType<typeof makePullRequestRefreshAtomFamily<R, E>>;
137+
const refreshes = makePullRequestRefreshAtomFamily(runtime);
138+
refreshFamiliesByRuntime.set(runtime, refreshes);
139+
return refreshes;
140+
}
141+
127142
export function createLinkedPullRequestSummaryAtomFamily<R, E>(
128143
runtime: Atom.AtomRuntime<EnvironmentRegistry | R, E>,
129144
refreshes = createPullRequestRefreshAtomFamily(runtime),

0 commit comments

Comments
 (0)