feat(core): reload commands from config change feed (#39160)

This commit is contained in:
Kit Langton 2026-07-27 15:02:40 -04:00 committed by GitHub
commit 92807d0bb9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 254 additions and 8 deletions

View file

@ -27,7 +27,19 @@ export const Plugin = define({
)
}).pipe(Effect.map((documents) => documents.flat()))
})
const loaded = { documents: yield* load() }
const loaded: { documents: { commands: Config.Info["commands"] }[] } = { documents: [] }
const reload = load().pipe(
Effect.tap((documents) => Effect.sync(() => (loaded.documents = documents))),
Effect.andThen(ctx.command.reload()),
)
// Subscribe before the initial scan so updates racing the scan still trigger a rebuild.
yield* config.changes().pipe(
Stream.filterEffect((update) => Effect.map(config.entries(), (entries) => isCommandSource(entries, update.path))),
Stream.debounce("100 millis"),
Stream.runForEach(() => reload),
Effect.forkScoped({ startImmediately: true }),
)
loaded.documents = yield* load()
yield* ctx.command.transform((draft) => {
for (const document of loaded.documents) {
for (const [name, command] of Object.entries(document.commands ?? {})) {
@ -48,17 +60,25 @@ export const Plugin = define({
})
yield* ctx.event.subscribe().pipe(
Stream.filter((event) => event.type === "config.updated"),
Stream.runForEach(() =>
load().pipe(
Effect.tap((documents) => Effect.sync(() => (loaded.documents = documents))),
Effect.andThen(ctx.command.reload()),
),
),
Stream.runForEach(() => reload),
Effect.forkScoped({ startImmediately: true }),
)
}),
})
// Matches anything at or under <root>/{command,commands}. No file-suffix check:
// directory-level events such as renames carry no per-file paths.
function isCommandSource(entries: Config.Entry[], file: string) {
return entries.some(
(entry) =>
entry.type === "directory" &&
["command", "commands"].some((name) => {
const directory = path.join(entry.path, name)
return file === directory || file.startsWith(directory + path.sep)
}),
)
}
function loadDirectory(fs: FSUtil.Interface, directory: string) {
return Effect.gen(function* () {
const files = yield* fs