From f68b39cc1b6643b115cef21dcc3e4a28586acaf8 Mon Sep 17 00:00:00 2001 From: EvgeniPirianov Date: Tue, 20 Sep 2022 11:02:29 +0300 Subject: [PATCH] improved events polling, updated documentation --- README.md | 8 ++++---- package.json | 2 +- src/web3/EventPoller.ts | 23 ++++++++--------------- 3 files changed, 13 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 2d946a89..5d7f32aa 100644 --- a/README.md +++ b/README.md @@ -380,7 +380,7 @@ const eventsFilter = { is_final: null, } as IEventFilter; -const eventPoller = EventPoller.startEventPoller( +const eventPoller = EventPoller.startEventsPolling( eventsFilter, 1000, web3Client @@ -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) => {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, @@ -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 ... } ``` diff --git a/package.json b/package.json index 22e71a5c..28af8200 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/web3/EventPoller.ts b/src/web3/EventPoller.ts index eb79358b..6079a2b7 100644 --- a/src/web3/EventPoller.ts +++ b/src/web3/EventPoller.ts @@ -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) => void, - onError: (err: Error) => void): Promise { + onData?: (data: Array) => 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,