Skip to content

Commit

Permalink
feat: implement disable method (#14)
Browse files Browse the repository at this point in the history
* feat: add disable types

* feat: add disable method to contollers

* docs: add disable method tutorials
  • Loading branch information
kieranroneill committed Apr 8, 2024
1 parent ffd752d commit fd37807
Show file tree
Hide file tree
Showing 18 changed files with 705 additions and 8 deletions.
27 changes: 27 additions & 0 deletions docs/api-reference/avm-web-client.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,22 @@ import TOCInline from '@theme/TOCInline';

## Methods

### `disable([params])`

> Sends a request to remove the client from providers.
#### Parameters

| Name | Type | Required | Default | Description |
|--------|------------------------------------------|----------|---------|-------------------------------------------------------------------------|
| params | [`IDisableParams`](types#idisableparams) | no | - | Params that specify which provider,network and/or specific session IDs. |

#### Returns

| Type | Description |
|--------|-------------|
| `void` | - |

### `discover([params])`

> Sends a request to get information relating to available providers. This should be called before interacting with any providers to ensure networks and methods are supported.
Expand Down Expand Up @@ -70,6 +86,17 @@ import TOCInline from '@theme/TOCInline';
| [`IAVMWebClientConfig`](types#iavmwebclientconfig) | The client's configuration. |


### `onDisable(listener)`

> Listens to `disable` messages sent from providers. This will replace any previous set listeners. If null is supplied, the listener will be removed.
#### Parameters

| Name | Type | Required | Default | Description |
|----------|----------------------------------------------------------------------------------------------------------------------------------------------------------------|----------|---------|---------------------------------------------------------------------------------------------------------------------------------------------------------------|
| listener | (`result`?: [`IDisableResult`](types#idisableresult) \| `null`, `error`?: [`IBaseError`](types#ibaseerror) \| `null`) => (`void` \| `Promise<void>`) \| `null` | yes | - | The callback is called when a response message is received, or null to remove the listener. If the result was successful, the `error` parameter will be null. |


### `onDiscover(listener)`

> Listens to `discover` messages sent from providers. This will replace any previous set listeners. If null is supplied, the listener will be removed.
Expand Down
20 changes: 18 additions & 2 deletions docs/api-reference/avm-web-provider.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,25 @@ import TOCInline from '@theme/TOCInline';
|--------------------------------------------------------|-------------------------------|
| [`IAVMWebProviderConfig`](types#iavmwebproviderconfig) | The provider's configuration. |

### `onDisable(listener)`

> Listens to `disable` messages sent from clients. This will replace any previous set listeners. If null is supplied, the listener will be removed.
#### Parameters

| Name | Type | Required | Default | Description |
|----------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------|---------|----------------------------------------------------------------------------------------|
| listener | (`params`?: [`IDisableParams`](types#idisableparams)) => ([`IDisableResult`](types#idisableresult) \| [`Promise<IDiscoverResult>`](types#idiscoverresult)) \| `null` | yes | - | The listener to call when the request message is sent, or null to remove the listener. |

#### Returns

| Type | Description |
|--------|-------------|
| `void` | - |

### `onDiscover(listener)`

> Listens to discover messages sent from clients. This will replace any previous set listeners. If null is supplied, the listener will be removed.
> Listens to `discover` messages sent from clients. This will replace any previous set listeners. If null is supplied, the listener will be removed.
#### Parameters

Expand All @@ -56,7 +72,7 @@ import TOCInline from '@theme/TOCInline';

### `onEnable(listener)`

> Listens to enable messages sent from clients. This will replace any previous set listeners. If null is supplied, the listener will be removed.
> Listens to `enable` messages sent from clients. This will replace any previous set listeners. If null is supplied, the listener will be removed.
#### Parameters

Expand Down
25 changes: 21 additions & 4 deletions docs/api-reference/types.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,27 @@ This is the basic type for an error. For specific details on errors check out th

### `IAccount`

| Name | Type | Required | Default | Description |
|--------|----------|----------|---------|-----------------------------------------|
| addess | `string` | yes | - | The address of the account. |
| name | `string` | no | - | A human-readable name for this account. |
| Name | Type | Required | Default | Description |
|---------|----------|----------|---------|-----------------------------------------|
| address | `string` | yes | - | The address of the account. |
| name | `string` | no | - | A human-readable name for this account. |

### `IDisableParams`

| Name | Type | Required | Default | Description |
|-------------|------------|----------|---------|------------------------------------------------------------------------------|
| genesisHash | `string` | no | - | The unique identifier for the network that is the hash of the genesis block. |
| providerId | `string` | no | - | The ID of the provider. |
| sessionIds | `string[]` | no | - | A list of specific session IDs to remove. |

### `IDisableResult`

| Name | Type | Required | Default | Description |
|-------------|------------|----------|---------|------------------------------------------------------------------------------|
| genesisHash | `string` | yes | - | The unique identifier for the network that is the hash of the genesis block. |
| genesisId | `string` | yes | - | A human-readable identifier for the network. |
| providerId | `string` | yes | - | The ID of the provider. |
| sessionIds | `string[]` | no | - | A list of removed session IDs. |

### `IDiscoverParams`

Expand Down
239 changes: 239 additions & 0 deletions docs/clients/disabling-a-client.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
import TOCInline from '@theme/TOCInline';

# Disabling A Client

<TOCInline
maxHeadingLevel={4}
toc={toc}
/>

## Overview

The disable method is not required for providers to implement. However, it is useful for providers that have a sessioning system in place when a client has previously called [`enable()`](../../api-reference/avm-web-client#enableparams).

The aim of the disable method is for clients to request that these sessions be removed from the provider.

## Disabling the client with all providers

Assuming a client has been [enabled](enabling-a-client) with one or all providers, you can remove the client from all available providers by:

<Tabs
defaultValue="javascript"
values={[
{ label: 'Javascript', value: 'javascript' },
{ label: 'TypeScript', value: 'typescript' },
]}>
<TabItem value="javascript">

```js
// initialized client
client.onDisable((result, error) => {
if (error) {
console.error('error:', error);

return;
}

console.log(result);
/*
{
genesisHash: 'SGO1GKSzyE7IEPItTxCByw9x8FmnrCDexi9/cOUJOiI=',
genesisId: 'testnet-v1.0',
providerId: '02657eaf-be17-4efc-b0a4-19d654b2448e',
}
*/
});

// broadcast a disable request
client.disable();
```

</TabItem>
<TabItem value="typescript">

```typescript
import type { BaseARC0027Error, IDisableResult } from '@agoralabs-sh/avm-web-provider';

// initialized client
client.onDisable((result: IDisableResult: null, error: BaseARC0027Error | null) => {
if (error) {
console.error('error:', error);

return;
}

console.log(result);
/*
{
genesisHash: 'SGO1GKSzyE7IEPItTxCByw9x8FmnrCDexi9/cOUJOiI=',
genesisId: 'testnet-v1.0',
providerId: '02657eaf-be17-4efc-b0a4-19d654b2448e',
}
*/
});

// broadcast a disable request
client.disable();
```

</TabItem>
</Tabs>

:::caution

If any providers do not support the `disable` method, then a [`MethodNotSupportedError`](../../api-reference/errors#methodnotsupportederror) will be returned.

:::

## Disabling the client with a specific provider and network

If you want to target a specific provider and network, you can simply pass the ID of the provider and the genesis hash of the network in the params:

<Tabs
defaultValue="javascript"
values={[
{ label: 'Javascript', value: 'javascript' },
{ label: 'TypeScript', value: 'typescript' },
]}>
<TabItem value="javascript">

```js
// initialized client
client.onDisable((result, error) => {
if (error) {
console.error('error:', error);

return;
}

console.log(result);
/*
genesisHash: 'SGO1GKSzyE7IEPItTxCByw9x8FmnrCDexi9/cOUJOiI=',
genesisId: 'testnet-v1.0',
providerId: '02657eaf-be17-4efc-b0a4-19d654b2448e',
}
*/
});

// broadcast an disable request
client.disable({
genesisHash: 'SGO1GKSzyE7IEPItTxCByw9x8FmnrCDexi9/cOUJOiI=',
providerId: '02657eaf-be17-4efc-b0a4-19d654b2448e',
});
```

</TabItem>
<TabItem value="typescript">

```typescript
import type { BaseARC0027Error, IDisableResult } from '@agoralabs-sh/avm-web-provider';
const providerId: string = '02657eaf-be17-4efc-b0a4-19d654b2448e';
// initialized client
client.onEnable((result: IDisableResult: null, error: BaseARC0027Error | null) => {
if (error) {
console.error('error:', error);
return;
}
console.log(result);
/*
{
genesisHash: 'SGO1GKSzyE7IEPItTxCByw9x8FmnrCDexi9/cOUJOiI=',
genesisId: 'testnet-v1.0',
providerId: '02657eaf-be17-4efc-b0a4-19d654b2448e',
}
*/
});
// broadcast an disable request
client.disable({
genesisHash: 'SGO1GKSzyE7IEPItTxCByw9x8FmnrCDexi9/cOUJOiI=',
providerId: '02657eaf-be17-4efc-b0a4-19d654b2448e',
});
```

</TabItem>
</Tabs>

:::caution

If the network and the provider ID is specified, and the provider does not support the network, then a [`NetworkNotSupportedError`](../../api-reference/errors#networknotsupportederror) will be thrown.

:::

## Disabling a client for a specific session

If you want to remove a specific session, you can provide the session ID(s) in the params:

<Tabs
defaultValue="javascript"
values={[
{ label: 'Javascript', value: 'javascript' },
{ label: 'TypeScript', value: 'typescript' },
]}>
<TabItem value="javascript">

```js
// initialized client
client.onDisable((result, error) => {
if (error) {
console.error('error:', error);
return;
}
console.log(result);
/*
{
genesisHash: 'SGO1GKSzyE7IEPItTxCByw9x8FmnrCDexi9/cOUJOiI=',
genesisId: 'testnet-v1.0',
providerId: '02657eaf-be17-4efc-b0a4-19d654b2448e',
sessionIds: ['ab192498-0c63-4028-80fd-f148710611d8'],
}
*/
});
// broadcast an disable request
client.disable({
sessionIds: ['ab192498-0c63-4028-80fd-f148710611d8'],
});
```

</TabItem>
<TabItem value="typescript">

```typescript
import type { BaseARC0027Error, IDisableResult } from '@agoralabs-sh/avm-web-provider';
// initialized client
client.onEnable((result: IDisableResult: null, error: BaseARC0027Error | null) => {
if (error) {
console.error('error:', error);
return;
}
console.log(result);
/*
{
genesisHash: 'SGO1GKSzyE7IEPItTxCByw9x8FmnrCDexi9/cOUJOiI=',
genesisId: 'testnet-v1.0',
providerId: '02657eaf-be17-4efc-b0a4-19d654b2448e',
sessionIds: ['ab192498-0c63-4028-80fd-f148710611d8'],
}
*/
});
// broadcast an disable request
client.disable({
sessionIds: ['ab192498-0c63-4028-80fd-f148710611d8'],
});
```

</TabItem>
</Tabs>
2 changes: 1 addition & 1 deletion docs/clients/enabling-a-client.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Before we can start interacting with a provider, we need ask the provider to ena

:::note

It should be safe to call [`enable()`](../../api-reference/avm-web-client#enable) as many times as your client needs; the provider should assume this.
It should be safe to call [`enable()`](../../api-reference/avm-web-client#enableparams). as many times as your client needs; the provider should assume this.

:::

Expand Down
2 changes: 2 additions & 0 deletions docs/clients/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
* [Getting Started](clients/getting-started)
* [Discover Providers](clients/discover-providers)
* [Enabling A Client](clients/enabling-a-client)
* [Posting Transactions](clients/posting-transactions)
* [Disabling A Client](clients/disabling-a-client)
2 changes: 2 additions & 0 deletions docs/providers/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
* [Getting Started](providers/getting-started)
* [Responding To Discover Requests](providers/responding-to-discover-requests)
* [Responding To Enable Requests](providers/responding-to-enable-requests)
* [Responding To Disable Requests](providers/responding-to-disable-requests)
* [Responding To Enable Requests](providers/responding-to-post-transactions-requests)
Loading

0 comments on commit fd37807

Please sign in to comment.