diff --git a/packages/core/src/plugin/host.ts b/packages/core/src/plugin/host.ts index 004fce68d8f..f9cf0b76235 100644 --- a/packages/core/src/plugin/host.ts +++ b/packages/core/src/plugin/host.ts @@ -397,6 +397,7 @@ export const make = Effect.fn("PluginHost.make")(function* ( add: (name, source) => editor.add(name, Schema.decodeUnknownSync(Reference.Source)(source)), remove: editor.remove, list: editor.list, + get: editor.get, }) }), }, diff --git a/packages/core/src/reference.ts b/packages/core/src/reference.ts index ba7f2980bc4..5e0b965b004 100644 --- a/packages/core/src/reference.ts +++ b/packages/core/src/reference.ts @@ -32,6 +32,7 @@ type Editor = { add(name: string, source: Source): void remove(name: string): void list(): readonly [string, Source][] + get(name: string): Source | undefined } export interface Interface extends State.Transformable { @@ -98,6 +99,7 @@ const layer = Layer.effect( add: (name, source) => editor.sources.set(name, source), remove: (name) => editor.sources.delete(name), list: () => Array.from(editor.sources), + get: (name) => editor.sources.get(name), }), notify: () => Effect.gen(function* () { diff --git a/packages/core/test/reference.test.ts b/packages/core/test/reference.test.ts index 40934c8e698..1e1d14020c8 100644 --- a/packages/core/test/reference.test.ts +++ b/packages/core/test/reference.test.ts @@ -19,6 +19,26 @@ const referenceLayer = AppNodeBuilder.build(LayerNode.group([Reference.node, Bus ]) describe("Reference", () => { + it.effect("reads the current editor source by name", () => + Effect.gen(function* () { + const references = yield* Reference.Service + const source = Reference.LocalSource.make({ type: "local", path: AbsolutePath.make("/docs") }) + yield* references.transform((editor) => editor.add("docs", source)) + yield* references.transform((editor) => { + expect(editor.get("docs")).toBe(editor.list()[0]?.[1]) + expect(editor.get("docs")).toEqual(source) + expect(editor.get("missing")).toBeUndefined() + const replacement = Reference.GitSource.make({ type: "git", repository: "owner/repo" }) + editor.add("docs", replacement) + expect(editor.get("docs")).toBe(replacement) + editor.remove("docs") + expect(editor.get("docs")).toBeUndefined() + }) + + expect(yield* references.list()).toEqual([]) + }).pipe(Effect.provide(referenceLayer)), + ) + it.effect("reads batched references before cache work and update events", () => { const operations: RepositoryCache.EnsureInput[] = [] const started = Deferred.makeUnsafe() diff --git a/packages/plugin/src/effect/reference.ts b/packages/plugin/src/effect/reference.ts index 970e55d8230..bdb84f02a34 100644 --- a/packages/plugin/src/effect/reference.ts +++ b/packages/plugin/src/effect/reference.ts @@ -7,6 +7,7 @@ export interface ReferenceEditor { add(name: string, source: ReferenceLocalSource | ReferenceGitSource): void remove(name: string): void list(): readonly (readonly [string, ReferenceLocalSource | ReferenceGitSource])[] + get(name: string): ReferenceLocalSource | ReferenceGitSource | undefined } export interface ReferenceDomain extends ReferenceApi { diff --git a/packages/plugin/src/promise/reference.ts b/packages/plugin/src/promise/reference.ts index f61f2bb3219..fc07c308903 100644 --- a/packages/plugin/src/promise/reference.ts +++ b/packages/plugin/src/promise/reference.ts @@ -6,6 +6,7 @@ export interface ReferenceEditor { add(name: string, source: ReferenceLocalSource | ReferenceGitSource): void remove(name: string): void list(): readonly (readonly [string, ReferenceLocalSource | ReferenceGitSource])[] + get(name: string): ReferenceLocalSource | ReferenceGitSource | undefined } export interface ReferenceDomain extends ReferenceApi { diff --git a/packages/www/src/docs/content/build/plugins/effect.mdx b/packages/www/src/docs/content/build/plugins/effect.mdx index fac913007df..d9fd97a6b9d 100644 --- a/packages/www/src/docs/content/build/plugins/effect.mdx +++ b/packages/www/src/docs/content/build/plugins/effect.mdx @@ -644,7 +644,8 @@ effect: (ctx) => }), ``` -Add or remove local and Git references, then reload after external state changes. +Add or remove local and Git references, then reload after external state changes. `get(name)` returns the current configured +source, or `undefined` when the name is absent. ```ts effect: (ctx) => @@ -653,6 +654,7 @@ effect: (ctx) => yield* reference.transform((editor) => { editor.add("handbook", { type: "local", path: "/workspace/docs/handbook" }) editor.add("standards", { type: "git", repository: "https://github.com/acme/standards", branch: "main" }) + const handbook = editor.get("handbook") editor.remove("legacy") }) yield* reference.reload() @@ -667,6 +669,7 @@ interface ReferenceEditor { add(name: string, source: ReferenceLocalSource | ReferenceGitSource): void remove(name: string): void list(): readonly (readonly [string, ReferenceLocalSource | ReferenceGitSource])[] + get(name: string): ReferenceLocalSource | ReferenceGitSource | undefined } interface ReferenceDomain extends ReferenceApi { diff --git a/packages/www/src/docs/content/build/plugins/index.mdx b/packages/www/src/docs/content/build/plugins/index.mdx index 580378d040c..72ea1a230ac 100644 --- a/packages/www/src/docs/content/build/plugins/index.mdx +++ b/packages/www/src/docs/content/build/plugins/index.mdx @@ -596,13 +596,15 @@ Read the references available at a location. const references = await ctx.reference.list() ``` -Register a transform to inspect, add, or remove local and Git references. +Register a transform to inspect, add, or remove local and Git references. `get(name)` returns the current configured source, +or `undefined` when the name is absent. ```ts await ctx.reference.transform((editor) => { const references = editor.list() editor.add("handbook", { type: "local", path: "/workspace/docs/handbook" }) editor.add("standards", { type: "git", repository: "https://github.com/acme/standards", branch: "main" }) + const handbook = editor.get("handbook") editor.remove("legacy") }) ``` @@ -627,6 +629,7 @@ interface ReferenceContext { interface ReferenceEditor { list(): readonly (readonly [string, ReferenceLocalSource | ReferenceGitSource])[] + get(name: string): ReferenceLocalSource | ReferenceGitSource | undefined add(name: string, source: ReferenceLocalSource | ReferenceGitSource): void remove(name: string): void }