Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -287,25 +287,40 @@ The preceding code:
</TabItem>
<TabItem id="typescript" label="TypeScript">

<Aside type="caution">
The TypeScript `UpdateCommandStateContext` currently exposes only `serviceProvider` — it does **not** expose a `resourceSnapshot` property. The C# pattern of gating a command on `ResourceSnapshot.HealthStatus` therefore has no direct TypeScript equivalent today. TypeScript callbacks can still resolve services from the provider, but they cannot inspect the live resource snapshot.
</Aside>
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).

Comment on lines +290 to 294
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",
executeClearCache,
{
commandOptions: {
iconName: "AnimalRabbitOff",
updateState: async (ctx) => {
const snapshot = await ctx.resourceSnapshot();

return snapshot.healthStatus === HealthStatus.Healthy
? ResourceCommandState.Enabled
: ResourceCommandState.Disabled;
Comment on lines 297 to +312
},
},
});
```

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.

</TabItem>
</Tabs>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,25 +286,40 @@ private static ResourceCommandState OnUpdateResourceState(
</TabItem>
<TabItem id="typescript" label="TypeScript">

<Aside type="caution">
TypeScript の `UpdateCommandStateContext` は現在 `serviceProvider` のみを公開し、`resourceSnapshot` プロパティは **公開していません**。そのため、`ResourceSnapshot.HealthStatus` でコマンドをゲートする C# パターンに、現時点で直接対応する TypeScript 等価表現はありません。TypeScript コールバックでもプロバイダーからサービス解決はできますが、ライブのリソース スナップショットは検査できません。
</Aside>
TypeScript では `CommandOptions` の `updateState` フィールドは非同期コールバックで、`UpdateCommandStateContext` を受け取ります。コンテキストは以下を公開します。

TypeScript では `CommandOptions` の `updateState` フィールドが緩い型(`any`)であり、SDK はまだ `ResourceCommandState` 列挙体をエクスポートしていません。現時点の TypeScript AppHost で最も単純な方法は、`updateState` を完全に省略することです。コマンドは既定で有効になり、リソース到達後にのみコマンド登録します。
- `serviceProvider` — サービスを解決するために使用します。
- `resourceSnapshot()` — リソース インスタンスのスナップショット(`healthStatus` とライフサイクル状態の `state` を含む)を返す非同期関数。
Comment on lines +289 to +292

次の例では、C# パターンと同様に、リソースが健全な状態のときのみ `clear-cache` コマンドを有効にします。

```typescript title="apphost.ts"
import { HealthStatus, ResourceCommandState } from './.modules/aspire.js';

await cache.withCommand(
"clear-cache",
"Clear Cache",
executeClearCache,
{
commandOptions: {
iconName: "AnimalRabbitOff",
updateState: async (ctx) => {
const snapshot = await ctx.resourceSnapshot();

return snapshot.healthStatus === HealthStatus.Healthy
? ResourceCommandState.Enabled
: ResourceCommandState.Disabled;
},
Comment on lines 296 to +312
},
});
```

TypeScript AppHost でライフサイクル対応のゲーティングが必要な場合は、コールバック内ではなく外側で制御します。たとえば、接続文字列またはエンドポイント イベントを購読した後に、コマンドが依存するデータがリソースから公開されてからコマンドを登録してください。
上記のコードは以下を行います。

- `ctx.resourceSnapshot()` を呼び出して現在のリソース スナップショットを取得します。
- リソースが健全であれば `ResourceCommandState.Enabled` を返し、そうでなければ `ResourceCommandState.Disabled` を返します。

`HealthStatus` と `ResourceCommandState` 列挙体は生成された SDK(`aspire.ts`)からエクスポートされており、直接インポートして使用できます。

</TabItem>
</Tabs>
Expand Down
Loading