Official
files
Built-in files UI (JavaScript UI plugin).
The app opens the listing; nothing installs until an agent in your Plugins workspace has read the files and you enable the plugin. In a terminal: dvg install divergence/files@0.2.6
Permissions in 0.2.6
Files
watch.ts2 KB
// The workspace on screen is watched: a change in it names the shown
// folders it touched, so a file made or removed by anything (an agent, a
// terminal, another app) shows at once.
import * as Effect from "effect/Effect";
import * as Exit from "effect/Exit";
import * as Scope from "effect/Scope";
import { Fs, Host } from "divergence/effect";
import { touchedFolders, type FilesState } from "./panel.ts";
/// `changed` gets the listed folders a change touched, or null when it
/// touched something the tree cannot place and everything lists again.
export const makeWatch = Effect.fn("Files.makeWatch")(function* (
state: FilesState,
changed: (workspaceId: string, touched: readonly string[] | null) => Effect.Effect<void>,
) {
const host = yield* Host;
const fs = yield* Fs;
const scope = yield* Effect.scope;
let watching: Scope.Closeable | undefined;
return Effect.fn("Files.watch")(function* (workspaceId: string) {
if (watching) yield* Scope.close(watching, Exit.void);
const own = yield* Scope.fork(scope);
watching = own;
const root = yield* host.call("host/workspaces.list", {}).pipe(
Effect.map(({ workspaces }) => workspaces.find((workspace) => workspace.id === workspaceId)?.path ?? null),
Effect.catchTag(["HostCallFailed", "NeedsReview", "PermissionNotGranted"], () => Effect.succeed(null)),
);
const onChange = (paths: readonly string[]) => {
if (state.workspaceId !== workspaceId) return;
const touched = state.all ? null : touchedFolders(root, paths, new Set(Object.keys(state.listing)));
Effect.runFork(Effect.forkIn(changed(workspaceId, touched), own, { startImmediately: true }));
};
yield* fs.watch(".", onChange, { workspaceId }).pipe(
Effect.provideService(Scope.Scope, own),
Effect.catchTag(["HostCallFailed", "NeedsReview", "PermissionNotGranted"], (error) =>
Effect.sync(() => console.warn(`files: the workspace is not watched: ${error.message}`)),
),
);
});
});
No comments yet.