diff --git a/package-lock.json b/package-lock.json index 12c03261..99196e73 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17,6 +17,7 @@ "bs58check": "^4.0.0", "decimal.js": "^10.4.3", "dotenv": "^16.0.3", + "eventemitter3": "^5.0.1", "lodash.isequal": "^4.5.0", "secure-random": "^1.1.2", "string_decoder": "^1.3.0", @@ -5791,8 +5792,7 @@ "node_modules/eventemitter3": { "version": "5.0.1", "resolved": "https://registry.npmjs.org/eventemitter3/-/eventemitter3-5.0.1.tgz", - "integrity": "sha512-GWkBvjiSZK87ELrYOSESUYeVIc9mvLLf/nXalMOS5dYrgZq9o5OVkbZAVM06CVxYsCwH9BDZFPlQTlPA1j4ahA==", - "dev": true + "integrity": "sha512-GWkBvjiSZK87ELrYOSESUYeVIc9mvLLf/nXalMOS5dYrgZq9o5OVkbZAVM06CVxYsCwH9BDZFPlQTlPA1j4ahA==" }, "node_modules/execa": { "version": "5.1.1", diff --git a/package.json b/package.json index 07d5c313..00728899 100644 --- a/package.json +++ b/package.json @@ -67,6 +67,7 @@ "bs58check": "^4.0.0", "decimal.js": "^10.4.3", "dotenv": "^16.0.3", + "eventemitter3": "^5.0.1", "lodash.isequal": "^4.5.0", "secure-random": "^1.1.2", "string_decoder": "^1.3.0", diff --git a/src/events/eventsPoller.ts b/src/events/eventsPoller.ts index a1e5b8bd..f6285ee8 100644 --- a/src/events/eventsPoller.ts +++ b/src/events/eventsPoller.ts @@ -1,5 +1,6 @@ import { Provider } from '../provider' import { EventFilter, NB_THREADS, SCEvent, Slot } from '../client' +import EventEmitter from 'eventemitter3' /** Smart Contracts Event Poller */ export const ON_MASSA_EVENT_DATA = 'ON_MASSA_EVENT' @@ -22,7 +23,7 @@ function nextSlot(prevSlot: Slot): Slot { /** * The EventPoller class provides a convenient way to poll events from the Massa network. */ -export class EventPoller extends EventTarget { +export class EventPoller extends EventEmitter { private intervalId: NodeJS.Timeout private lastSlot: Slot @@ -52,19 +53,11 @@ export class EventPoller extends EventTarget { const events = await this.provider.getEvents(this.eventsFilter) if (events.length) { - this.dispatchEvent( - new CustomEvent(ON_MASSA_EVENT_DATA, { - detail: events, - }) - ) + this.emit(ON_MASSA_EVENT_DATA, events) this.lastSlot = events[events.length - 1].context.slot } } catch (ex) { - this.dispatchEvent( - new CustomEvent(ON_MASSA_EVENT_ERROR, { - detail: ex, - }) - ) + this.emit(ON_MASSA_EVENT_ERROR, ex) } } @@ -106,20 +99,10 @@ export class EventPoller extends EventTarget { ): { stopPolling: () => void } { const eventPoller = new EventPoller(provider, eventsFilter, pollIntervalMs) if (onData) { - eventPoller.addEventListener( - ON_MASSA_EVENT_DATA, - (e: CustomEvent) => { - onData(e.detail) - } - ) + eventPoller.on(ON_MASSA_EVENT_DATA, onData) } if (onError) { - eventPoller.addEventListener( - ON_MASSA_EVENT_ERROR, - (e: CustomEvent) => { - onError(e.detail) - } - ) + eventPoller.on(ON_MASSA_EVENT_ERROR, onError) } eventPoller.start()