Skip to content

Commit

Permalink
Merge pull request #37 from algorandfoundation/on-poll
Browse files Browse the repository at this point in the history
feat: Added onBeforePoll and onPoll methods to `AlgorandSubscriber`
  • Loading branch information
robdmoore authored Mar 21, 2024
2 parents a8590e9 + daf506c commit 29d5017
Show file tree
Hide file tree
Showing 14 changed files with 420 additions and 69 deletions.
131 changes: 115 additions & 16 deletions docs/code/classes/index.AlgorandSubscriber.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Handles the logic for subscribing to the Algorand blockchain and emitting events

- [on](index.AlgorandSubscriber.md#on)
- [onBatch](index.AlgorandSubscriber.md#onbatch)
- [onBeforePoll](index.AlgorandSubscriber.md#onbeforepoll)
- [onPoll](index.AlgorandSubscriber.md#onpoll)
- [pollOnce](index.AlgorandSubscriber.md#pollonce)
- [start](index.AlgorandSubscriber.md#start)
- [stop](index.AlgorandSubscriber.md#stop)
Expand All @@ -53,7 +55,7 @@ Create a new `AlgorandSubscriber`.

#### Defined in

[subscriber.ts:34](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/subscriber.ts#L34)
[subscriber.ts:35](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/subscriber.ts#L35)

## Properties

Expand All @@ -63,7 +65,7 @@ Create a new `AlgorandSubscriber`.

#### Defined in

[subscriber.ts:22](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/subscriber.ts#L22)
[subscriber.ts:23](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/subscriber.ts#L23)

___

Expand All @@ -73,7 +75,7 @@ ___

#### Defined in

[subscriber.ts:19](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/subscriber.ts#L19)
[subscriber.ts:20](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/subscriber.ts#L20)

___

Expand All @@ -83,7 +85,7 @@ ___

#### Defined in

[subscriber.ts:21](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/subscriber.ts#L21)
[subscriber.ts:22](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/subscriber.ts#L22)

___

Expand All @@ -93,7 +95,7 @@ ___

#### Defined in

[subscriber.ts:23](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/subscriber.ts#L23)
[subscriber.ts:24](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/subscriber.ts#L24)

___

Expand All @@ -103,7 +105,7 @@ ___

#### Defined in

[subscriber.ts:26](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/subscriber.ts#L26)
[subscriber.ts:27](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/subscriber.ts#L27)

___

Expand All @@ -113,7 +115,7 @@ ___

#### Defined in

[subscriber.ts:20](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/subscriber.ts#L20)
[subscriber.ts:21](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/subscriber.ts#L21)

___

Expand All @@ -123,7 +125,7 @@ ___

#### Defined in

[subscriber.ts:25](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/subscriber.ts#L25)
[subscriber.ts:26](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/subscriber.ts#L26)

___

Expand All @@ -133,7 +135,7 @@ ___

#### Defined in

[subscriber.ts:24](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/subscriber.ts#L24)
[subscriber.ts:25](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/subscriber.ts#L25)

## Methods

Expand Down Expand Up @@ -162,11 +164,24 @@ The listener can be async and it will be awaited if so.

[`AlgorandSubscriber`](index.AlgorandSubscriber.md)

The subscriber so `on`/`onBatch` calls can be chained
The subscriber so `on*` calls can be chained

**`Example`**

```typescript
subscriber.on('my-filter', async (transaction) => { console.log(transaction.id) })
```

**`Example`**

```typescript
new AlgorandSubscriber({filters: [{name: 'my-filter', filter: {...}, mapper: (t) => t.id}], ...}, algod)
.on<string>('my-filter', async (transactionId) => { console.log(transactionId) })
```

#### Defined in

[subscriber.ts:159](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/subscriber.ts#L159)
[subscriber.ts:176](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/subscriber.ts#L176)

___

Expand Down Expand Up @@ -199,11 +214,95 @@ The listener can be async and it will be awaited if so.

[`AlgorandSubscriber`](index.AlgorandSubscriber.md)

The subscriber so `on`/`onBatch` calls can be chained
The subscriber so `on*` calls can be chained

**`Example`**

```typescript
subscriber.onBatch('my-filter', async (transactions) => { console.log(transactions.length) })
```

**`Example`**

```typescript
new AlgorandSubscriber({filters: [{name: 'my-filter', filter: {...}, mapper: (t) => t.id}], ...}, algod)
.onBatch<string>('my-filter', async (transactionIds) => { console.log(transactionIds) })
```

#### Defined in

[subscriber.ts:176](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/subscriber.ts#L176)
[subscriber.ts:202](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/subscriber.ts#L202)

___

### onBeforePoll

**onBeforePoll**(`listener`): [`AlgorandSubscriber`](index.AlgorandSubscriber.md)

Register an event handler to run before every subscription poll.

This is useful when you want to do pre-poll logging or start a transaction etc.

The listener can be async and it will be awaited if so.

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `listener` | [`TypedAsyncEventListener`](../modules/types_subscription.md#typedasynceventlistener)\<[`BeforePollMetadata`](../interfaces/types_subscription.BeforePollMetadata.md)\> | The listener function to invoke with the pre-poll metadata |

#### Returns

[`AlgorandSubscriber`](index.AlgorandSubscriber.md)

The subscriber so `on*` calls can be chained

**`Example`**

```typescript
subscriber.onBeforePoll(async (metadata) => { console.log(metadata.watermark) })
```

#### Defined in

[subscriber.ts:220](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/subscriber.ts#L220)

___

### onPoll

**onPoll**(`listener`): [`AlgorandSubscriber`](index.AlgorandSubscriber.md)

Register an event handler to run after every subscription poll.

This is useful when you want to process all subscribed transactions
in a transactionally consistent manner rather than piecemeal for each
filter, or to have a hook that occurs at the end of each poll to commit
transactions etc.

The listener can be async and it will be awaited if so.

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `listener` | [`TypedAsyncEventListener`](../modules/types_subscription.md#typedasynceventlistener)\<[`TransactionSubscriptionResult`](../interfaces/types_subscription.TransactionSubscriptionResult.md)\> | The listener function to invoke with the poll result |

#### Returns

[`AlgorandSubscriber`](index.AlgorandSubscriber.md)

The subscriber so `on*` calls can be chained

**`Example`**

```typescript
subscriber.onPoll(async (pollResult) => { console.log(pollResult.subscribedTransactions.length, pollResult.syncedRoundRange) })
```

#### Defined in

[subscriber.ts:241](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/subscriber.ts#L241)

___

Expand All @@ -224,7 +323,7 @@ The poll result

#### Defined in

[subscriber.ts:60](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/subscriber.ts#L60)
[subscriber.ts:61](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/subscriber.ts#L61)

___

Expand All @@ -251,7 +350,7 @@ An object that contains a promise you can wait for after calling stop

#### Defined in

[subscriber.ts:98](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/subscriber.ts#L98)
[subscriber.ts:105](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/subscriber.ts#L105)

___

Expand All @@ -275,4 +374,4 @@ A promise that can be awaited to ensure the subscriber has finished stopping

#### Defined in

[subscriber.ts:141](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/subscriber.ts#L141)
[subscriber.ts:149](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/subscriber.ts#L149)
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Any ARC-28 event definitions to process from app call logs

#### Defined in

[types/subscription.ts:69](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L69)
[types/subscription.ts:77](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L77)

___

Expand All @@ -55,7 +55,7 @@ The set of filters to subscribe to / emit events for, along with optional data m

#### Defined in

[types/subscription.ts:175](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L175)
[types/subscription.ts:183](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L183)

___

Expand All @@ -67,7 +67,7 @@ The frequency to poll for new blocks in seconds; defaults to 1s

#### Defined in

[types/subscription.ts:177](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L177)
[types/subscription.ts:185](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L185)

___

Expand All @@ -90,7 +90,7 @@ boundary based on the number of rounds specified here.

#### Defined in

[types/subscription.ts:89](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L89)
[types/subscription.ts:97](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L97)

___

Expand All @@ -112,7 +112,7 @@ your catchup speed when using `sync-oldest`.

#### Defined in

[types/subscription.ts:78](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L78)
[types/subscription.ts:86](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L86)

___

Expand Down Expand Up @@ -143,7 +143,7 @@ past `watermark` then how should that be handled:

#### Defined in

[types/subscription.ts:107](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L107)
[types/subscription.ts:115](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L115)

___

Expand All @@ -155,7 +155,7 @@ Whether to wait via algod `/status/wait-for-block-after` endpoint when at the ti

#### Defined in

[types/subscription.ts:179](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L179)
[types/subscription.ts:187](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L187)

___

Expand All @@ -175,4 +175,4 @@ its position in the chain

#### Defined in

[types/subscription.ts:182](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L182)
[types/subscription.ts:190](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L190)
38 changes: 38 additions & 0 deletions docs/code/interfaces/types_subscription.BeforePollMetadata.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[@algorandfoundation/algokit-subscriber](../README.md) / [types/subscription](../modules/types_subscription.md) / BeforePollMetadata

# Interface: BeforePollMetadata

[types/subscription](../modules/types_subscription.md).BeforePollMetadata

Metadata about an impending subscription poll.

## Table of contents

### Properties

- [currentRound](types_subscription.BeforePollMetadata.md#currentround)
- [watermark](types_subscription.BeforePollMetadata.md#watermark)

## Properties

### currentRound

**currentRound**: `number`

The current round of algod

#### Defined in

[types/subscription.ts:49](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L49)

___

### watermark

**watermark**: `number`

The current watermark of the subscriber

#### Defined in

[types/subscription.ts:47](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L47)
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Any ARC-28 event definitions to process from app call logs

#### Defined in

[types/subscription.ts:69](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L69)
[types/subscription.ts:77](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L77)

___

Expand Down Expand Up @@ -65,7 +65,7 @@ A list of filters with corresponding names.

#### Defined in

[types/subscription.ts:67](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L67)
[types/subscription.ts:75](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L75)

___

Expand All @@ -84,7 +84,7 @@ boundary based on the number of rounds specified here.

#### Defined in

[types/subscription.ts:89](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L89)
[types/subscription.ts:97](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L97)

___

Expand All @@ -102,7 +102,7 @@ your catchup speed when using `sync-oldest`.

#### Defined in

[types/subscription.ts:78](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L78)
[types/subscription.ts:86](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L86)

___

Expand All @@ -129,4 +129,4 @@ past `watermark` then how should that be handled:

#### Defined in

[types/subscription.ts:107](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L107)
[types/subscription.ts:115](https://github.com/algorandfoundation/algokit-subscriber-ts/blob/main/src/types/subscription.ts#L115)
Loading

0 comments on commit 29d5017

Please sign in to comment.