# SwitchableSubscription

URL: https://www.caprid.com/docs/reference/typescript/caprid/relay/index/interfaces/switchablesubscription/

Defined in: [relay-sdks/ts/src/switchable.ts:107](https://github.com/caprids/relay-sdks/blob/main/ts/src/switchable.ts#L107)

The handle `RelaySubscriber.subscribeSwitchable()` returns.

## Properties

### ended

```ts
readonly ended: Promise<SubscriptionEnd>;
```

Defined in: [relay-sdks/ts/src/switchable.ts:122](https://github.com/caprids/relay-sdks/blob/main/ts/src/switchable.ts#L122)

Settles once, with the active subscription's ending, when it ends other than by a cutover, or
when `cancel()` runs. Any pending switch is cancelled with it. Never rejects.

***

### pending

```ts
readonly pending: string | undefined;
```

Defined in: [relay-sdks/ts/src/switchable.ts:111](https://github.com/caprids/relay-sdks/blob/main/ts/src/switchable.ts#L111)

The pending subscription's track name, while a switch is pending.

***

### subscribed

```ts
readonly subscribed: Promise<SubscribeOk>;
```

Defined in: [relay-sdks/ts/src/switchable.ts:117](https://github.com/caprids/relay-sdks/blob/main/ts/src/switchable.ts#L117)

Resolves with the acceptance info of the first subscription to be active and accepted: the first
one, or the one a switch made before the first one was answered. Rejects as
`RelaySubscription.subscribed` does if the switchable ends before that.

***

### track

```ts
readonly track: string;
```

Defined in: [relay-sdks/ts/src/switchable.ts:109](https://github.com/caprids/relay-sdks/blob/main/ts/src/switchable.ts#L109)

The active subscription's track name.

## Methods

### cancel()

```ts
cancel(reason?): void;
```

Defined in: [relay-sdks/ts/src/switchable.ts:134](https://github.com/caprids/relay-sdks/blob/main/ts/src/switchable.ts#L134)

Cancel the active subscription and any pending one. Idempotent.

#### Parameters

| Parameter | Type |
| ------ | ------ |
| `reason?` | `unknown` |

#### Returns

`void`

***

### switchTo()

```ts
switchTo(track, options?): Promise<SwitchedInfo>;
```

Defined in: [relay-sdks/ts/src/switchable.ts:132](https://github.com/caprids/relay-sdks/blob/main/ts/src/switchable.ts#L132)

Subscribe to `track` in the same namespace, keep delivering the active subscription until the new
one's object passes `until`, then cancel the old one. The track may be the active one, to change
parameters. A newer `switchTo` cancels a pending one, which rejects with `CLOSED`. Rejects as
`RelaySubscription.subscribed` does if the new subscription ends before its cutover, with the
signal's reason when the signal aborts it, or with the thrown error when `until` throws; the
active subscription carries on. Throws at once for an invalid track or options, a closed
connection, or an ended switchable.

#### Parameters

| Parameter | Type |
| ------ | ------ |
| `track` | `string` |
| `options?` | [`SwitchOptions`](https://www.caprid.com/docs/reference/typescript/caprid/relay/index/interfaces/switchoptions/) |

#### Returns

`Promise`\<[`SwitchedInfo`](https://www.caprid.com/docs/reference/typescript/caprid/relay/index/interfaces/switchedinfo/)\>
