diff --git a/src/frontend/src/content/docs/fundamentals/custom-resource-commands.mdx b/src/frontend/src/content/docs/fundamentals/custom-resource-commands.mdx index f1122a1d9..8b9ea5560 100644 --- a/src/frontend/src/content/docs/fundamentals/custom-resource-commands.mdx +++ b/src/frontend/src/content/docs/fundamentals/custom-resource-commands.mdx @@ -287,13 +287,16 @@ The preceding code: - +In TypeScript the `updateState` field on `CommandOptions` is an async callback that receives an `UpdateCommandStateContext`. The context exposes: + +- `serviceProvider` — used to resolve services. +- `resourceSnapshot()` — an async function that returns a curated snapshot of the resource instance, including `healthStatus` and `state` (lifecycle state). -In TypeScript the `updateState` field on `CommandOptions` is typed loosely (`any`), and the SDK does not yet export a `ResourceCommandState` enum. The simplest path for TypeScript AppHosts today is to omit `updateState` entirely — commands default to enabled — and only register the command after the resource is reachable: +The following example gates the `clear-cache` command on the resource being healthy, mirroring the C# pattern: ```typescript title="apphost.ts" +import { HealthStatus, ResourceCommandState } from './.modules/aspire.js'; + await cache.withCommand( "clear-cache", "Clear Cache", @@ -301,11 +304,23 @@ await cache.withCommand( { commandOptions: { iconName: "AnimalRabbitOff", + updateState: async (ctx) => { + const snapshot = await ctx.resourceSnapshot(); + + return snapshot.healthStatus === HealthStatus.Healthy + ? ResourceCommandState.Enabled + : ResourceCommandState.Disabled; + }, }, }); ``` -When you need lifecycle-aware gating in a TypeScript AppHost, drive it from outside the callback by registering the command only after the resource exposes the data your command depends on (for example, after subscribing to a connection-string or endpoint event). +The preceding code: + +- Calls `ctx.resourceSnapshot()` to retrieve the current resource snapshot. +- Returns `ResourceCommandState.Enabled` if the resource is healthy; otherwise, it returns `ResourceCommandState.Disabled`. + +The `HealthStatus` and `ResourceCommandState` enums are exported from the generated SDK (`aspire.ts`) and are available to import directly. diff --git a/src/frontend/src/content/docs/ja/fundamentals/custom-resource-commands.mdx b/src/frontend/src/content/docs/ja/fundamentals/custom-resource-commands.mdx index b3049b99d..dae635c10 100644 --- a/src/frontend/src/content/docs/ja/fundamentals/custom-resource-commands.mdx +++ b/src/frontend/src/content/docs/ja/fundamentals/custom-resource-commands.mdx @@ -286,13 +286,16 @@ private static ResourceCommandState OnUpdateResourceState( - +TypeScript では `CommandOptions` の `updateState` フィールドは非同期コールバックで、`UpdateCommandStateContext` を受け取ります。コンテキストは以下を公開します。 -TypeScript では `CommandOptions` の `updateState` フィールドが緩い型(`any`)であり、SDK はまだ `ResourceCommandState` 列挙体をエクスポートしていません。現時点の TypeScript AppHost で最も単純な方法は、`updateState` を完全に省略することです。コマンドは既定で有効になり、リソース到達後にのみコマンド登録します。 +- `serviceProvider` — サービスを解決するために使用します。 +- `resourceSnapshot()` — リソース インスタンスのスナップショット(`healthStatus` とライフサイクル状態の `state` を含む)を返す非同期関数。 + +次の例では、C# パターンと同様に、リソースが健全な状態のときのみ `clear-cache` コマンドを有効にします。 ```typescript title="apphost.ts" +import { HealthStatus, ResourceCommandState } from './.modules/aspire.js'; + await cache.withCommand( "clear-cache", "Clear Cache", @@ -300,11 +303,23 @@ await cache.withCommand( { commandOptions: { iconName: "AnimalRabbitOff", + updateState: async (ctx) => { + const snapshot = await ctx.resourceSnapshot(); + + return snapshot.healthStatus === HealthStatus.Healthy + ? ResourceCommandState.Enabled + : ResourceCommandState.Disabled; + }, }, }); ``` -TypeScript AppHost でライフサイクル対応のゲーティングが必要な場合は、コールバック内ではなく外側で制御します。たとえば、接続文字列またはエンドポイント イベントを購読した後に、コマンドが依存するデータがリソースから公開されてからコマンドを登録してください。 +上記のコードは以下を行います。 + +- `ctx.resourceSnapshot()` を呼び出して現在のリソース スナップショットを取得します。 +- リソースが健全であれば `ResourceCommandState.Enabled` を返し、そうでなければ `ResourceCommandState.Disabled` を返します。 + +`HealthStatus` と `ResourceCommandState` 列挙体は生成された SDK(`aspire.ts`)からエクスポートされており、直接インポートして使用できます。