# API

**Kind**: interface | **Module**: [Shared Contexts](https://docs.interop.io/desktop/reference/javascript/shared%20contexts/index.md) | **Access**: `io.contexts`

**Source**: https://docs.interop.io/desktop/reference/javascript/shared contexts/api/index.html

## Methods

### all

Returns all existing context names. Using the context name you can subscribe for context changes, updates or set context values.

```ts
() => string[]
```

**Returns**: `string[]`

### destroy

Destroys a context and all the data associated with it

```ts
(name: string) => Promise<any>
```

**Parameters**

- **`name`** (`string`, required)
  Name of the context to be removed

**Returns**: `Promise<any>`

### get

Return the context data immediately or asynchronously as soon as any data becomes available.

```ts
(name: string) => Promise<any>
```

**Parameters**

- **`name`** (`string`, required)
  Name of the context from which you want to get data.

**Returns**: `Promise<any>`

### set

Replaces a context. All properties of the specified context object will be removed and replaced with the ones supplied in the `data` parameter.

```ts
(name: string, data: any) => Promise<void>
```

**Parameters**

- **`name`** (`string`, required)
  Name of the context to be replaced.
- **`data`** (`any`, required)
  The object that will be applied to the context.

**Returns**: `Promise<void>`

### setPath

Sets a path in the context to some value. Use this to update values that are not on top level in the context.

```ts
(name: string, path: string, data: any) => Promise<void>
```

**Parameters**

- **`name`** (`string`, required)
  Name of the context to be updated
- **`path`** (`string`, required)
  Path to be updated. Path should be in the format "prop1.prop2"
- **`data`** (`any`, required)
  The object that will be applied to the path

**Returns**: `Promise<void>`

### setPaths

Sets multiple paths in the context to some values in a single command.

```ts
(name: string, paths: PathValue[]) => Promise<void>
```

**Parameters**

- **`name`** (`string`, required)
  Name of the context to be updated
- **`paths`** (`PathValue[]`, required)
  Array of paths and their values to be updated. Path should be in the format "prop1.prop2"

**Returns**: `Promise<void>`

### subscribe

Subscribes for context events. Returns an unsubscribe function which you can use to stop receiving context updates.

```ts
(name: string, callback: (data: any, delta: any, removed: string[], unsubscribe: () => void, extraData?: any) => void) => Promise<() => void>
```

**Parameters**

- **`name`** (`string`, required)
  Name of the context to which you want to subscribe.
- **`callback`** (`(data: any, delta: any, removed: string[], unsubscribe: () => void, extraData?: any) => void`, required)
  Function that will handle the updates.

**Returns**: `Promise<() => void>`

### update

Updates a context with the supplied object. This method updates only the specified context properties. Any other existing context properties will remain intact.
If the context does not exist, the `update()` method will create it.

```ts
(name: string, data: any) => Promise<void>
```

**Parameters**

- **`name`** (`string`, required)
  Name of the context to be updated.
- **`data`** (`any`, required)
  The object that will be applied to the context.

**Returns**: `Promise<void>`

**Example**

```ts
```javascript
io.contexts.update("app-styling",
   {
       backgroundColor: "red",
       alternativeColor: "green"
   });
```
```

## Related types

- [PathValue](https://docs.interop.io/desktop/reference/javascript/shared%20contexts/pathvalue/index.md)
