Skip to content

Commit

Permalink
add event polyfill
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben-Rey committed Aug 26, 2024
1 parent 5216708 commit 2c48909
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)

Check warning on line 56 in src/events/eventsPoller.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
this.lastSlot = events[events.length - 1].context.slot
}

Check warning on line 58 in src/events/eventsPoller.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 58 in src/events/eventsPoller.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
} catch (ex) {
this.dispatchEvent(
new CustomEvent(ON_MASSA_EVENT_ERROR, {
detail: ex,
})
)
this.emit(ON_MASSA_EVENT_ERROR, ex)

Check warning on line 60 in src/events/eventsPoller.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 61 in src/events/eventsPoller.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 62 in src/events/eventsPoller.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

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)

Check warning on line 102 in src/events/eventsPoller.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 103 in src/events/eventsPoller.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 103 in src/events/eventsPoller.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
if (onError) {
eventPoller.addEventListener(
ON_MASSA_EVENT_ERROR,
(e: CustomEvent<Error>) => {
onError(e.detail)
}
)
eventPoller.on(ON_MASSA_EVENT_ERROR, onError)

Check warning on line 105 in src/events/eventsPoller.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 106 in src/events/eventsPoller.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 106 in src/events/eventsPoller.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

eventPoller.start()
Expand Down

0 comments on commit 2c48909

Please sign in to comment.