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
10 changes: 5 additions & 5 deletions apps/mobile/src/state/issues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ import { formatEnvironmentQueryError } from "./query";

export const issueEnvironment = createIssueEnvironmentAtoms(connectionAtomRuntime);

const mergedIssueLists = createMergedIssueListAtoms(
issueEnvironment.list,
formatEnvironmentQueryError,
);
const mergedIssueLists = createMergedIssueListAtoms(issueEnvironment, formatEnvironmentQueryError);

/** One listing read across every environment asked. */
export function useIssueList(targets: ReadonlyArray<IssueListTarget>) {
const view = useAtomValue(mergedIssueLists.atom(targets));
return { ...view, refresh: () => mergedIssueLists.refresh(appAtomRegistry, targets) };
return {
...view,
refresh: () => mergedIssueLists.refresh(appAtomRegistry, targets, "manual"),
};
}

/** After a write the list cannot see, such as a new issue filed from a sheet. */
Expand Down
1 change: 1 addition & 0 deletions apps/server/src/issue/IssueService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ const make = Effect.gen(function* () {
assignee: input.assignee,
sort: input.sort ?? "updated",
cursor,
refresh: input.refresh,
}),
),
Effect.result,
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/components/issue/IssuesPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ export function IssuesPage() {
// Refreshing a list still loading would cancel it and start over, losing the scopes already read.
useLiveRefresh(
() => {
if (!list.isPending) list.refresh();
if (!list.isPending) list.reload();
},
{ key: JSON.stringify(targets) },
);
Expand Down
16 changes: 8 additions & 8 deletions apps/web/src/state/issues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@ export function useLinkedIssueDetail(
).data;
}

const mergedIssueLists = createMergedIssueListAtoms(
issueEnvironment.list,
formatEnvironmentQueryError,
);
const mergedIssueLists = createMergedIssueListAtoms(issueEnvironment, formatEnvironmentQueryError);

/** The same listing read across every connected environment, as the pull-request page does. */
export function useIssueList(
targets: ReadonlyArray<IssueListTarget>,
): MergedIssueList & { readonly refresh: () => void; readonly retry: () => void } {
export function useIssueList(targets: ReadonlyArray<IssueListTarget>): MergedIssueList & {
readonly refresh: () => void;
readonly reload: () => void;
readonly retry: () => void;
} {
const view = useAtomValue(mergedIssueLists.atom(targets));
return {
...view,
refresh: () => mergedIssueLists.refresh(appAtomRegistry, targets),
refresh: () => mergedIssueLists.refresh(appAtomRegistry, targets, "manual"),
reload: () => mergedIssueLists.refresh(appAtomRegistry, targets, "background"),
retry: () => mergedIssueLists.retry(appAtomRegistry, targets),
};
}
Expand Down
12 changes: 8 additions & 4 deletions packages/client-runtime/src/state/issues.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ describe("createMergedIssueListAtoms", () => {
it("refreshes the open listings of the environment written to", () => {
const reads = new Map<string, number>();
const lists = new Map<string, Atom.Atom<AsyncResult.AsyncResult<IssueListResult>>>();
const listings = createMergedIssueListAtoms((target) => {
const list = (target: IssueListTarget) => {
const key = `${target.environmentId}:${target.input.query}`;
const existing = lists.get(key);
if (existing !== undefined) return existing;
Expand All @@ -199,7 +199,11 @@ describe("createMergedIssueListAtoms", () => {
});
lists.set(key, atom);
return atom;
}, String);
};
const listings = createMergedIssueListAtoms(
{ list, refreshList: (registry, target) => registry.refresh(list(target)) },
String,
);
const registry = AtomRegistry.make();
const written = EnvironmentId.make("written");
const other = EnvironmentId.make("other");
Expand Down Expand Up @@ -234,7 +238,7 @@ describe("createMergedIssueListAtoms", () => {
),
);
const listings = createMergedIssueListAtoms(
(target) => lists(target.environmentId),
{ list: (target) => lists(target.environmentId), refreshList: () => {} },
() => "Tracker unavailable",
);
const view = listings.atom(targets);
Expand All @@ -255,7 +259,7 @@ describe("createMergedIssueListAtoms", () => {
NodeV8.setFlagsFromString("--expose-gc");
const gc = NodeVM.runInNewContext("gc") as () => void;
const listings = createMergedIssueListAtoms(
() => Atom.make(AsyncResult.success(answer)),
{ list: () => Atom.make(AsyncResult.success(answer)), refreshList: () => {} },
String,
);
const registry = AtomRegistry.make();
Expand Down
82 changes: 74 additions & 8 deletions packages/client-runtime/src/state/issues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,21 @@ import {
} from "@t3tools/contracts";
import type * as Cause from "effect/Cause";
import * as Effect from "effect/Effect";
import * as Exit from "effect/Exit";
import * as Option from "effect/Option";
import { AsyncResult, Atom, type AtomRegistry } from "effect/reactivity";
import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity";

import {
type AtomCommandResult,
createAtomCommandScheduler,
createEnvironmentRpcCommand,
createEnvironmentRpcQueryAtomFamily,
createEnvironmentRpcSubscriptionAtomFamily,
createEnvironmentQueryAtomFamily,
} from "./runtime.ts";
import type { EnvironmentRegistry } from "../connection/registry.ts";
import * as EnvironmentSupervisor from "../connection/supervisor.ts";
import { request } from "../rpc/client.ts";

/** One row's identity across list pages and detail reads; hosts compare case-insensitively. */
export function issueEntryKey(
Expand Down Expand Up @@ -145,6 +149,8 @@ export function createIssueCommentPager<E>(
};
}

type IssueListRefreshMode = "manual" | "background";

/**
* Every read goes to a tracker API over the environment, so results are reused for a short while
* and refreshed explicitly. Mutations run serially per environment: changes to one issue are
Expand Down Expand Up @@ -179,6 +185,40 @@ export function createIssueEnvironmentAtoms<R, E>(
label: "environment-data:issues:linked-thread-refreshes",
tag: WS_METHODS.pullRequestsSubscribeLinkedThreadRefreshes,
});
const pendingListRefreshes = new WeakMap<
AtomRegistry.AtomRegistry,
WeakMap<Atom.Atom<unknown>, symbol>
>();
type ListRequest = ReturnType<typeof request<typeof WS_METHODS.issuesList>>;
const list = createEnvironmentQueryAtomFamily(runtime, {
label: "environment-data:issues:list",
staleTimeMs: 30_000,
execute: Effect.fnUntraced(function* (
input: IssueListInput,
): Effect.fn.Return<
IssueListResult,
Effect.Error<ListRequest>,
Effect.Services<ListRequest> | AtomRegistry.AtomRegistry
> {
const registry = yield* AtomRegistry.AtomRegistry;
const {
target: { environmentId },
} = yield* EnvironmentSupervisor.EnvironmentSupervisor;
const pending = pendingListRefreshes.get(registry);
if (pending === undefined) return yield* request(WS_METHODS.issuesList, input);
const atom = list({ environmentId, input });
const intent = pending.get(atom);
if (intent === undefined) return yield* request(WS_METHODS.issuesList, input);
return yield* request(WS_METHODS.issuesList, { ...input, refresh: true }).pipe(
// Invalidation interrupts the read; its replacement still owes the explicit refresh.
Effect.onExit((exit) =>
Effect.sync(() => {
if (!Exit.hasInterrupts(exit) && pending.get(atom) === intent) pending.delete(atom);
}),
),
);
}),
});
return {
/** The threads an issue is linked to, reread when the server signals a link changed. */
linkedThreads: createEnvironmentRpcQueryAtomFamily(runtime, {
Expand All @@ -187,11 +227,23 @@ export function createIssueEnvironmentAtoms<R, E>(
staleTimeMs: 0,
refreshTrigger: ({ environmentId }) => linkedThreadSignals({ environmentId, input: {} }),
}),
list: createEnvironmentRpcQueryAtomFamily(runtime, {
label: "environment-data:issues:list",
tag: WS_METHODS.issuesList,
staleTimeMs: 30_000,
}),
list,
refreshList(
registry: AtomRegistry.AtomRegistry,
target: IssueListTarget,
mode: IssueListRefreshMode,
) {
const atom = list(target);
if (mode === "manual") {
let pending = pendingListRefreshes.get(registry);
if (pending === undefined) {
pending = new WeakMap();
pendingListRefreshes.set(registry, pending);
}
pending.set(atom, Symbol());
}
registry.refresh(atom);
},
/**
* A continuation of the listing, run as a command rather than read as a query: a page is
* asked for once and laid over the rows held, and a refresh of the first page forgets it.
Expand Down Expand Up @@ -300,7 +352,19 @@ function refreshFailed<Target, A, E>(
* the environments that failed, so a read still in flight elsewhere is not restarted.
*/
export function createMergedIssueListAtoms<E>(
list: (target: IssueListTarget) => Atom.Atom<AsyncResult.AsyncResult<IssueListResult, E>>,
{
list,
refreshList,
}: {
readonly list: (
target: IssueListTarget,
) => Atom.Atom<AsyncResult.AsyncResult<IssueListResult, E>>;
readonly refreshList: (
registry: AtomRegistry.AtomRegistry,
target: IssueListTarget,
mode: IssueListRefreshMode,
) => void;
},
formatError: (cause: Cause.Cause<E>) => string,
) {
const empty = Atom.make(NO_ISSUE_LISTS).pipe(Atom.withLabel("issues:merged-list:empty"));
Expand All @@ -322,8 +386,9 @@ export function createMergedIssueListAtoms<E>(
const refresh = (
registry: AtomRegistry.AtomRegistry,
targets: ReadonlyArray<IssueListTarget>,
mode: IssueListRefreshMode,
) => {
for (const target of targets) registry.refresh(list(target));
for (const target of targets) refreshList(registry, target, mode);
};
// The targets behind each listing atom, so a write made elsewhere can refresh the ones still
// shown. Weak, so a closed search goes with its atom once the registry drops it.
Expand All @@ -346,6 +411,7 @@ export function createMergedIssueListAtoms<E>(
refresh(
registry,
targets.filter((target) => target.environmentId === environmentId),
"background",
);
}
},
Expand Down
51 changes: 49 additions & 2 deletions packages/client-runtime/src/state/runtime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
DEFAULT_MAX_TERMINAL_BUFFER_BYTES,
EnvironmentId,
type FilesystemBrowseEntry,
type IssueListInput,
type IssueListResult,
ORCHESTRATION_V2_WS_METHODS,
type OrchestrationV2ArchivedShellSnapshot,
ThreadId,
Expand Down Expand Up @@ -52,6 +54,7 @@ import { createOrchestrationEnvironmentAtoms } from "./orchestration.ts";
import { createReviewEnvironmentAtoms } from "./review.ts";
import { createPreviewEnvironmentAtoms } from "./preview.ts";
import { createFilesystemEnvironmentAtoms } from "./filesystem.ts";
import { createIssueEnvironmentAtoms, createMergedIssueListAtoms } from "./issues.ts";
import type { QueryPersistence } from "./queryPersistence.ts";
import type { EnvironmentShellState } from "./shell.ts";
import { v2Now, v2Project, v2ThreadId, v2ThreadShell } from "./orchestrationV2TestFixtures.ts";
Expand Down Expand Up @@ -194,8 +197,8 @@ const readEnvironmentQuery = <A, E>(
Effect.tap(() => Effect.yieldNow),
);

const mountEnvironmentQuery = Effect.fn("TestEnvironmentQuery.mount")(function* <A, E>(
atom: Atom.Atom<AsyncResult.AsyncResult<A, E>>,
const mountEnvironmentQuery = Effect.fn("TestEnvironmentQuery.mount")(function* <A>(
atom: Atom.Atom<A>,
) {
const registry = AtomRegistry.make();
const unmount = registry.mount(atom);
Expand Down Expand Up @@ -263,6 +266,50 @@ it.effect("keeps complete review metadata visible while a refresh is held", () =
}),
);

it.effect("keeps manual Issues refresh intent through an overlapping write", () =>
Effect.gen(function* () {
const requests = yield* Queue.unbounded<IssueListInput>();
const release = yield* Deferred.make<void>();
const answer: IssueListResult = {
viewers: {},
trackers: [],
entries: [],
errors: [],
nextCursors: {},
};
const rpcSession = {
client: {
[WS_METHODS.issuesList]: (input: IssueListInput) =>
Queue.offer(requests, input).pipe(
Effect.andThen(input.refresh ? Deferred.await(release) : Effect.void),
Effect.as(answer),
),
},
} as unknown as RpcSession.RpcSession;
const { runtime } = yield* makeEnvironmentQueryHarness(Effect.void, { rpcSession });
const issues = createIssueEnvironmentAtoms(runtime);
const listings = createMergedIssueListAtoms(issues, String);
const target = {
environmentId: QUERY_ENVIRONMENT.environmentId,
input: { state: "all" },
} as const;
const registry = yield* mountEnvironmentQuery(listings.atom([target]));
expect(yield* Queue.take(requests)).toEqual({ state: "all" });
yield* readEnvironmentQuery(registry, issues.list(target));

listings.refresh(registry, [target], "manual");
expect(yield* Queue.take(requests)).toEqual({ state: "all", refresh: true });

listings.refreshEnvironment(registry, target.environmentId);
expect(yield* Queue.take(requests)).toEqual({ state: "all", refresh: true });

yield* Deferred.succeed(release, undefined);
yield* readEnvironmentQuery(registry, issues.list(target));
listings.refreshEnvironment(registry, target.environmentId);
expect(yield* Queue.take(requests)).toEqual({ state: "all" });
}),
);

describe("preview event subscriptions", () => {
it.effect("delivers every open and close when RPC events arrive in batches", () =>
Effect.gen(function* () {
Expand Down
2 changes: 2 additions & 0 deletions packages/contracts/src/issue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ export const IssueListInput = Schema.Struct({
limit: Schema.optional(Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 200 }))),
/** Carry on from an earlier answer: only the scopes named here are read, one slice each. */
cursors: Schema.optional(IssueListCursors),
/** An explicit refresh of the first page may also refresh tracker metadata. */
refresh: Schema.optional(Schema.Boolean),
/** Free text the trackers themselves are asked to match. */
query: Schema.optional(TrimmedNonEmptyString.check(Schema.isMaxLength(200))),
/** Label names an issue must carry, every one of them. */
Expand Down
Loading
Loading