Skip to content

Commit

Permalink
add event polyfill (#674)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben-Rey authored Aug 26, 2024
1 parent 5216708 commit 6e9c168
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 25 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
29 changes: 6 additions & 23 deletions src/events/eventsPoller.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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

Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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<SCEvent[]>) => {
onData(e.detail)
}
)
eventPoller.on(ON_MASSA_EVENT_DATA, onData)
}
if (onError) {
eventPoller.addEventListener(
ON_MASSA_EVENT_ERROR,
(e: CustomEvent<Error>) => {
onError(e.detail)
}
)
eventPoller.on(ON_MASSA_EVENT_ERROR, onError)
}

eventPoller.start()
Expand Down

1 comment on commit 6e9c168

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage report for experimental massa-web3

St.
Category Percentage Covered / Total
🟡 Statements 63.12% 1109/1757
🔴 Branches 43.49% 187/430
🔴 Functions 46.4% 206/444
🟡 Lines 63.43% 1103/1739

Test suite run success

129 tests passing in 13 suites.

Report generated by 🧪jest coverage report action from 6e9c168

Please sign in to comment.