Skip to content

Commit

Permalink
Merge pull request #40 from massalabs/fix/poller
Browse files Browse the repository at this point in the history
improved events polling, updated documentation
  • Loading branch information
Adrien LF authored Sep 20, 2022
2 parents 7d6cebc + f68b39c commit 5757539
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 20 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ const eventsFilter = {
is_final: null,
} as IEventFilter;
const eventPoller = EventPoller.startEventPoller(
const eventPoller = EventPoller.startEventsPolling(
eventsFilter,
1000,
web3Client
Expand All @@ -394,13 +394,13 @@ eventPoller.on(ON_MASSA_EVENT_ERROR, onEventDataError);
eventPoller.stopPolling();
```

Alternatively, one could make direct use of an async promise for doing the latter by providing callback functions which would fire on event data received or generated errors:
Alternatively, one could make direct use of callback functions as function arguments which would fire on event data received or generated errors:

```ts
const onEventData = (events: Array<IEvent>) => {console.log("Event Data Received:" , events);}
const onEventDataError = (error: Error) => {console.log("Event Data Error:" , error);}
const eventPoller: EventPoller = await EventPoller.startEventsPollingAsync(
const eventPoller: EventPoller = EventPoller.startEventsPolling(
eventsFilter,
1000,
web3Client,
Expand All @@ -423,7 +423,7 @@ export function main(_args: string): i32 {
const sc_address = createContract();
call(sc_address, "initialize", "", 0);
print("Initialized, address:" + sc_address);
generate_event(`Address:${sc_address}`); //emit an event with the address
generateEvent(`Address:${sc_address}`); //emit an event with the address
...
}
```
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@massalabs/massa-web3",
"version": "1.10.7",
"version": "1.10.8",
"description": "massa's web3 sdk client",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
23 changes: 8 additions & 15 deletions src/web3/EventPoller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,31 +93,24 @@ export class EventPoller extends EventEmitter {
this.timeoutId = new Timeout(this.pollIntervalMillis, () => that.callback());
}

public static async startEventsPollingAsync(eventsFilter: IEventFilter | IEventRegexFilter,
public static startEventsPolling(eventsFilter: IEventFilter | IEventRegexFilter,
pollIntervalMillis: number,
web3Client: Client,
onData: (data: Array<IEvent>) => void,
onError: (err: Error) => void): Promise<EventPoller> {
onData?: (data: Array<IEvent>) => void,
onError?: (err: Error) => void): EventPoller {
const eventPoller = new EventPoller(eventsFilter, pollIntervalMillis, web3Client);
eventPoller.startPolling();
return new Promise((resolve, reject) => {
if (onData) {
eventPoller.on(ON_MASSA_EVENT_DATA, (data: [IEvent]) => {
onData(data);
});
}
if (onError) {
eventPoller.on(ON_MASSA_EVENT_ERROR, (e) => {
onError(e);
});

return resolve(eventPoller);
});
}

public static startEventPoller(eventsFilter: IEventFilter | IEventRegexFilter,
pollIntervalMillis: number,
web3Client: Client): EventPoller {
const eventPoller = new EventPoller(eventsFilter, pollIntervalMillis, web3Client);
eventPoller.startPolling();
return eventPoller;
}
return eventPoller;
}

public static getEventsOnce(eventsFilter: IEventFilter | IEventRegexFilter,
Expand Down

0 comments on commit 5757539

Please sign in to comment.