Background git status refresh after successful mutations
Co-authored-by: codex <[email protected]>
This commit is contained in:
1 parent
f3bc116544
commit
564e94dccc
4 files changed
+165
-18
No files matched your search
@@ -7,6 +7,7 @@ import type {
|
||||
GitStatusRemoteResult,
|
||||
GitStatusStreamEvent,
|
||||
} from "@t3tools/contracts";
|
||||
import { makeKeyedCoalescingWorker } from "@t3tools/shared/KeyedCoalescingWorker";
|
||||
import { mergeGitStatusParts } from "@t3tools/shared/git";
|
||||
|
||||
import {
|
||||
@@ -186,6 +187,23 @@ export const GitStatusBroadcasterLive = Layer.effect(
|
||||
},
|
||||
);
|
||||
|
||||
const refreshWorker = yield* makeKeyedCoalescingWorker<string, void, never, never>({
|
||||
merge: () => undefined,
|
||||
process: (cwd) =>
|
||||
refreshStatus(cwd).pipe(
|
||||
Effect.catchCause((cause) =>
|
||||
Effect.logWarning("git status refresh failed", {
|
||||
cwd,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
Effect.asVoid,
|
||||
),
|
||||
});
|
||||
|
||||
const enqueueRefreshStatus: GitStatusBroadcasterShape["enqueueRefreshStatus"] = (cwd) =>
|
||||
refreshWorker.enqueue(normalizeCwd(cwd), undefined);
|
||||
|
||||
const ensureRemotePoller = Effect.fn("ensureRemotePoller")(function* (cwd: string) {
|
||||
const normalizedCwd = normalizeCwd(cwd);
|
||||
const shouldStart = yield* Ref.modify(pollersRef, (activePollers) => {
|
||||
@@ -248,6 +266,7 @@ export const GitStatusBroadcasterLive = Layer.effect(
|
||||
|
||||
return {
|
||||
getStatus,
|
||||
enqueueRefreshStatus,
|
||||
refreshStatus,
|
||||
streamStatus,
|
||||
} satisfies GitStatusBroadcasterShape;
|
||||
|
||||
@@ -11,6 +11,7 @@ export interface GitStatusBroadcasterShape {
|
||||
readonly getStatus: (
|
||||
input: GitStatusInput,
|
||||
) => Effect.Effect<GitStatusResult, GitManagerServiceError>;
|
||||
readonly enqueueRefreshStatus: (cwd: string) => Effect.Effect<void>;
|
||||
readonly refreshStatus: (cwd: string) => Effect.Effect<GitStatusResult, GitManagerServiceError>;
|
||||
readonly streamStatus: (
|
||||
input: GitStatusInput,
|
||||
|
||||
@@ -22,7 +22,7 @@ import {
|
||||
} from "@t3tools/contracts";
|
||||
import { assert, it } from "@effect/vitest";
|
||||
import { assertFailure, assertInclude, assertTrue } from "@effect/vitest/utils";
|
||||
import { Effect, FileSystem, Layer, ManagedRuntime, Path, Stream } from "effect";
|
||||
import { Duration, Effect, FileSystem, Layer, ManagedRuntime, Path, Stream } from "effect";
|
||||
import {
|
||||
FetchHttpClient,
|
||||
HttpBody,
|
||||
@@ -1584,8 +1584,8 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
|
||||
);
|
||||
|
||||
assertFailure(result, gitError);
|
||||
assert.equal(invalidationCalls, 2);
|
||||
assert.equal(statusCalls, 1);
|
||||
assert.equal(invalidationCalls, 0);
|
||||
assert.equal(statusCalls, 0);
|
||||
}).pipe(Effect.provide(NodeHttpServer.layerTest)),
|
||||
);
|
||||
|
||||
@@ -1666,11 +1666,131 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
|
||||
);
|
||||
|
||||
assertFailure(result, gitError);
|
||||
assert.equal(invalidationCalls, 2);
|
||||
assert.equal(statusCalls, 1);
|
||||
assert.equal(invalidationCalls, 0);
|
||||
assert.equal(statusCalls, 0);
|
||||
}).pipe(Effect.provide(NodeHttpServer.layerTest)),
|
||||
);
|
||||
|
||||
it.effect("completes websocket rpc git.pull before background git status refresh finishes", () =>
|
||||
Effect.gen(function* () {
|
||||
yield* buildAppUnderTest({
|
||||
layers: {
|
||||
gitCore: {
|
||||
pullCurrentBranch: () =>
|
||||
Effect.succeed({
|
||||
status: "pulled" as const,
|
||||
branch: "main",
|
||||
upstreamBranch: "origin/main",
|
||||
}),
|
||||
},
|
||||
gitManager: {
|
||||
invalidateLocalStatus: () => Effect.void,
|
||||
invalidateRemoteStatus: () => Effect.void,
|
||||
localStatus: () =>
|
||||
Effect.succeed({
|
||||
isRepo: true,
|
||||
hasOriginRemote: true,
|
||||
isDefaultBranch: true,
|
||||
branch: "main",
|
||||
hasWorkingTreeChanges: false,
|
||||
workingTree: { files: [], insertions: 0, deletions: 0 },
|
||||
}),
|
||||
remoteStatus: () =>
|
||||
Effect.sleep(Duration.seconds(2)).pipe(
|
||||
Effect.as({
|
||||
hasUpstream: true,
|
||||
aheadCount: 0,
|
||||
behindCount: 0,
|
||||
pr: null,
|
||||
}),
|
||||
),
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const wsUrl = yield* getWsServerUrl("/ws");
|
||||
const startedAt = Date.now();
|
||||
const result = yield* Effect.scoped(
|
||||
withWsRpcClient(wsUrl, (client) => client[WS_METHODS.gitPull]({ cwd: "/tmp/repo" })),
|
||||
);
|
||||
const elapsedMs = Date.now() - startedAt;
|
||||
|
||||
assert.equal(result.status, "pulled");
|
||||
assertTrue(elapsedMs < 1_000);
|
||||
}).pipe(Effect.provide(NodeHttpServer.layerTest)),
|
||||
);
|
||||
|
||||
it.effect(
|
||||
"completes websocket rpc git.runStackedAction before background git status refresh finishes",
|
||||
() =>
|
||||
Effect.gen(function* () {
|
||||
yield* buildAppUnderTest({
|
||||
layers: {
|
||||
gitManager: {
|
||||
invalidateLocalStatus: () => Effect.void,
|
||||
invalidateRemoteStatus: () => Effect.void,
|
||||
localStatus: () =>
|
||||
Effect.succeed({
|
||||
isRepo: true,
|
||||
hasOriginRemote: true,
|
||||
isDefaultBranch: false,
|
||||
branch: "feature/demo",
|
||||
hasWorkingTreeChanges: false,
|
||||
workingTree: { files: [], insertions: 0, deletions: 0 },
|
||||
}),
|
||||
remoteStatus: () =>
|
||||
Effect.sleep(Duration.seconds(2)).pipe(
|
||||
Effect.as({
|
||||
hasUpstream: true,
|
||||
aheadCount: 0,
|
||||
behindCount: 0,
|
||||
pr: null,
|
||||
}),
|
||||
),
|
||||
runStackedAction: () =>
|
||||
Effect.succeed({
|
||||
action: "commit" as const,
|
||||
branch: { status: "skipped_not_requested" as const },
|
||||
commit: {
|
||||
status: "created" as const,
|
||||
commitSha: "abc123",
|
||||
subject: "feat: demo",
|
||||
},
|
||||
push: { status: "skipped_not_requested" as const },
|
||||
pr: { status: "skipped_not_requested" as const },
|
||||
toast: {
|
||||
title: "Committed abc123",
|
||||
description: "feat: demo",
|
||||
cta: {
|
||||
kind: "run_action" as const,
|
||||
label: "Push",
|
||||
action: {
|
||||
kind: "push" as const,
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const wsUrl = yield* getWsServerUrl("/ws");
|
||||
const startedAt = Date.now();
|
||||
yield* Effect.scoped(
|
||||
withWsRpcClient(wsUrl, (client) =>
|
||||
client[WS_METHODS.gitRunStackedAction]({
|
||||
actionId: "action-1",
|
||||
cwd: "/tmp/repo",
|
||||
action: "commit",
|
||||
}).pipe(Stream.runCollect),
|
||||
),
|
||||
);
|
||||
const elapsedMs = Date.now() - startedAt;
|
||||
|
||||
assertTrue(elapsedMs < 1_000);
|
||||
}).pipe(Effect.provide(NodeHttpServer.layerTest)),
|
||||
);
|
||||
|
||||
it.effect("routes websocket rpc orchestration methods", () =>
|
||||
Effect.gen(function* () {
|
||||
const now = new Date().toISOString();
|
||||
|
||||
+20
-13
@@ -67,6 +67,8 @@ const WsRpcLayer = WsRpcGroup.toLayer(
|
||||
const workspaceEntries = yield* WorkspaceEntries;
|
||||
const workspaceFileSystem = yield* WorkspaceFileSystem;
|
||||
const projectSetupScriptRunner = yield* ProjectSetupScriptRunner;
|
||||
const services = yield* Effect.services();
|
||||
const runPromise = Effect.runPromiseWith(services);
|
||||
|
||||
const serverCommandId = (tag: string) =>
|
||||
CommandId.makeUnsafe(`server:${tag}:${crypto.randomUUID()}`);
|
||||
@@ -351,9 +353,11 @@ const WsRpcLayer = WsRpcGroup.toLayer(
|
||||
});
|
||||
|
||||
const refreshGitStatus = (cwd: string) =>
|
||||
gitStatusBroadcaster
|
||||
.refreshStatus(cwd)
|
||||
.pipe(Effect.ignoreCause({ log: true }), Effect.asVoid);
|
||||
Effect.sync(() => {
|
||||
setTimeout(() => {
|
||||
void runPromise(gitStatusBroadcaster.enqueueRefreshStatus(cwd));
|
||||
}, 0);
|
||||
});
|
||||
|
||||
return WsRpcGroup.of({
|
||||
[ORCHESTRATION_WS_METHODS.getSnapshot]: (_input) =>
|
||||
@@ -581,9 +585,13 @@ const WsRpcLayer = WsRpcGroup.toLayer(
|
||||
[WS_METHODS.gitPull]: (input) =>
|
||||
observeRpcEffect(
|
||||
WS_METHODS.gitPull,
|
||||
git
|
||||
.pullCurrentBranch(input.cwd)
|
||||
.pipe(Effect.ensuring(refreshGitStatus(input.cwd).pipe(Effect.ignore({ log: true })))),
|
||||
git.pullCurrentBranch(input.cwd).pipe(
|
||||
Effect.matchCauseEffect({
|
||||
onFailure: (cause) => Effect.failCause(cause),
|
||||
onSuccess: (result) =>
|
||||
refreshGitStatus(input.cwd).pipe(Effect.ignore({ log: true }), Effect.as(result)),
|
||||
}),
|
||||
),
|
||||
{ "rpc.aggregate": "git" },
|
||||
),
|
||||
[WS_METHODS.gitRunStackedAction]: (input) =>
|
||||
@@ -599,14 +607,13 @@ const WsRpcLayer = WsRpcGroup.toLayer(
|
||||
})
|
||||
.pipe(
|
||||
Effect.matchCauseEffect({
|
||||
onFailure: (cause) =>
|
||||
refreshGitStatus(input.cwd).pipe(
|
||||
Effect.ignore({ log: true }),
|
||||
Effect.andThen(Queue.failCause(queue, cause)),
|
||||
),
|
||||
onFailure: (cause) => Queue.failCause(queue, cause),
|
||||
onSuccess: () =>
|
||||
refreshGitStatus(input.cwd).pipe(
|
||||
Effect.andThen(Queue.end(queue).pipe(Effect.asVoid)),
|
||||
Queue.end(queue).pipe(
|
||||
Effect.asVoid,
|
||||
Effect.andThen(
|
||||
refreshGitStatus(input.cwd).pipe(Effect.ignore({ log: true })),
|
||||
),
|
||||
),
|
||||
}),
|
||||
),
|
||||
|
||||
Reference in new issue
Block a user