# SharedDoc

## Content

# Class: SharedDoc<S, T\>

Represents a shared document for requesting snapshots and operations.

## Type parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `S` | `unknown` | The type of snapshot data. |
| `T` | `unknown` | The type of operation data. |

## Table of contents

### Constructors

- [constructor](SharedDoc#constructor)

### Accessors

- [connection](SharedDoc#connection)
- [data](SharedDoc#data)
- [id](SharedDoc#id)
- [type](SharedDoc#type)
- [version](SharedDoc#version)

### Methods

- [create](SharedDoc#create)
- [del](SharedDoc#del)
- [destroy](SharedDoc#destroy)
- [fetch](SharedDoc#fetch)
- [fetchHistorySnapshot](SharedDoc#fetchhistorysnapshot)
- [hardRollback](SharedDoc#hardrollback)
- [off](SharedDoc#off)
- [on](SharedDoc#on)
- [once](SharedDoc#once)
- [restore](SharedDoc#restore)
- [submitOp](SharedDoc#submitop)
- [subscribe](SharedDoc#subscribe)

## Constructors

### <a id="constructor" name="constructor"></a> constructor

• **new SharedDoc**<`S`, `T`\>(`conn`)

Creates a new shared document instance.

#### Type parameters

| Name | Type |
| :------ | :------ |
| `S` | `unknown` |
| `T` | `unknown` |

#### Parameters

| Name | Type |
| :------ | :------ |
| `conn` | `Connection` |

## Accessors

### <a id="connection" name="connection"></a> connection

• `get` **connection**(): `Connection`

Retrieves the connection associated with the shared document.

#### Returns

`Connection`

The connection instance.

___

### <a id="data" name="data"></a> data

• `get` **data**(): `undefined` \| `S`

Retrieves the data of the current snapshot.

#### Returns

`undefined` \| `S`

___

### <a id="id" name="id"></a> id

• `get` **id**(): `string`

Retrieves the ID of the shared document.

#### Returns

`string`

The document ID.

___

### <a id="type" name="type"></a> type

• `get` **type**(): `undefined` \| [`OT_Type`](../interfaces/OT_Type)<`S`, `T`\>

Retrieves the OT type of the snapshot.

#### Returns

`undefined` \| [`OT_Type`](../interfaces/OT_Type)<`S`, `T`\>

___

### <a id="version" name="version"></a> version

• `get` **version**(): `undefined` \| `number`

Retrieves the version of the current snapshot.

#### Returns

`undefined` \| `number`

## Methods

### <a id="create" name="create"></a> create

▸ **create**(`data`, `type`, `options`): `Promise`<`void`\>

Create a new document.

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `data` | `S` | The data used to initialize the snapshot. |
| `type` | `string` | The URI of the OT type. |
| `options` | [`IOptions`](../interfaces/IOptions) | Options for the operation. |

#### Returns

`Promise`<`void`\>

A promise that resolves after the document is created.

___

### <a id="del" name="del"></a> del

▸ **del**(`options`): `Promise`<`void`\>

Delete the document.

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `options` | [`IOptions`](../interfaces/IOptions) | The options of the op. |

#### Returns

`Promise`<`void`\>

A promise that resolves after the document is deleted.

___

### <a id="destroy" name="destroy"></a> destroy

▸ **destroy**(): `void`

Destroys the observable and cleans up resources.

#### Returns

`void`

___

### <a id="fetch" name="fetch"></a> fetch

▸ **fetch**(): `Promise`<`void`\>

Fetches the snapshot data.

#### Returns

`Promise`<`void`\>

A promise that resolves after the snapshot is fetched, the fetched snapshot data is 'doc.data'.

___

### <a id="fetchhistorysnapshot" name="fetchhistorysnapshot"></a> fetchHistorySnapshot

▸ **fetchHistorySnapshot**(`options`): `Promise`<[`ISnapshot`](../interfaces/ISnapshot)<`S`\>\>

Fetches the history snapshot data.

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `options` | [`IHistorySnapshotRequestOptions`](../interfaces/IHistorySnapshotRequestOptions) | Options for the history snapshot request. |

#### Returns

`Promise`<[`ISnapshot`](../interfaces/ISnapshot)<`S`\>\>

A promise that resolves after the history snapshot is fetched.

___

### <a id="hardrollback" name="hardrollback"></a> hardRollback

▸ **hardRollback**(): `Promise`<`void`\>

Hard rolls back the snapshot data to the latest version.

#### Returns

`Promise`<`void`\>

A promise that resolves when the rollback is complete.

___

### <a id="off" name="off"></a> off

▸ **off**<`NAME`\>(`name`, `f`): `void`

Removes a listener for a specific event.

#### Type parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `NAME` | extends keyof [`ISharedDocEvents`](../interfaces/ISharedDocEvents)<`S`, `T`\> | The type of the event name, extending keyof ISharedDocEvents<S, T>. |

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `name` | `NAME` | The name of the event. |
| `f` | [`ISharedDocEvents`](../interfaces/ISharedDocEvents)<`S`, `T`\>[`NAME`] | The event handler function to remove. |

#### Returns

`void`

___

### <a id="on" name="on"></a> on

▸ **on**<`NAME`\>(`name`, `f`): [`ISharedDocEvents`](../interfaces/ISharedDocEvents)<`S`, `T`\>[`NAME`]

Registers a listener for a specific event.

#### Type parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `NAME` | extends keyof [`ISharedDocEvents`](../interfaces/ISharedDocEvents)<`S`, `T`\> | The type of the event name, extending keyof ISharedDocEvents<S, T>. |

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `name` | `NAME` | The name of the event. |
| `f` | [`ISharedDocEvents`](../interfaces/ISharedDocEvents)<`S`, `T`\>[`NAME`] | The event handler function. |

#### Returns

[`ISharedDocEvents`](../interfaces/ISharedDocEvents)<`S`, `T`\>[`NAME`]

The registered event handler.

___

### <a id="once" name="once"></a> once

▸ **once**<`NAME`\>(`name`, `f`): `void`

Registers a one-time listener for a specific event.

#### Type parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `NAME` | extends keyof [`ISharedDocEvents`](../interfaces/ISharedDocEvents)<`S`, `T`\> | The type of the event name, extending keyof ISharedDocEvents<S, T>. |

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `name` | `NAME` | The name of the event. |
| `f` | [`ISharedDocEvents`](../interfaces/ISharedDocEvents)<`S`, `T`\>[`NAME`] | The event handler function to call once. |

#### Returns

`void`

___

### <a id="restore" name="restore"></a> restore

▸ **restore**(`targetVersion`, `options?`): `Promise`<`void`\>

Restore the document to a specific version.

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `targetVersion` | `number` | The target version number to restore to. |
| `options?` | [`IOptions`](../interfaces/IOptions) | The options of the op. |

#### Returns

`Promise`<`void`\>

A promise that resolves after the document is restored.

___

### <a id="submitop" name="submitop"></a> submitOp

▸ **submitOp**(`component`, `options?`): `Promise`<`void`\>

Submits an op to the document.

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `component` | `T` | The op component. |
| `options?` | [`IOptions`](../interfaces/IOptions) | - |

#### Returns

`Promise`<`void`\>

A promise that resolves after the op is submitted.

___

### <a id="subscribe" name="subscribe"></a> subscribe

▸ **subscribe**(): `Promise`<`void`\>

Subscribes the snapshot data.

#### Returns

`Promise`<`void`\>

A promise that resolves after the snapshot is subscribed, the subscribed snapshot data is 'doc.data'.
