Skip to content

Commit

Permalink
Standardise usage of internal and local events (#298)
Browse files Browse the repository at this point in the history
* unify usage of internal events for everything

* fix usage of old event

* apply transform after execute.

* remove logger

* extend validateEventsToSubscribe to cleanup the event namespace

* add some tests to validate new logic

* change how namespace is defined for internal events, make clear where a function is waiting for an internal event

* add util to check and convert to local events

* make use of the new utils

* add logic to be able to apply local and remote transforms

* add changesets

* fix tests

* pr comment, add code comments

* remove unneeded changeset
  • Loading branch information
framini authored Sep 20, 2021
1 parent 72eb91b commit 49b4aa9
Show file tree
Hide file tree
Showing 14 changed files with 291 additions and 154 deletions.
5 changes: 5 additions & 0 deletions .changeset/unlucky-walls-train.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@signalwire/core': patch
---

Normalize usage of events to always use our "internal" version for registering, transforms, caching, etc.
28 changes: 12 additions & 16 deletions packages/core/src/BaseComponent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,29 +51,27 @@ describe('BaseComponent', () => {
})

it('should keep track of the original events with the _eventsPrefix', () => {
const firstInstance = new JestComponent('first')
const firstInstance = new JestComponent('first_namespace')
firstInstance.on('first.event_one', () => {})
firstInstance.once('video.first.eventOne', () => {})
firstInstance.on('first.event_two', () => {})
firstInstance.once('video.first.eventTwo', () => {})
firstInstance.once('video.first.eventTwoExtra', () => {})

const secondInstance = new JestComponent('second')
const secondInstance = new JestComponent('second_namespace')
secondInstance.on('second.event_one', () => {})
secondInstance.once('video.second.eventOne', () => {})
secondInstance.on('second.event_two', () => {})
secondInstance.once('video.second.eventTwo', () => {})

expect(firstInstance.eventNames()).toStrictEqual([
'video.first.event_one',
'video.first.eventOne',
'video.first.event_two',
'video.first.eventTwo',
'first_namespace:video.first.event_one',
'first_namespace:video.first.event_two',
'first_namespace:video.first.event_two_extra',
])
expect(secondInstance.eventNames()).toStrictEqual([
'video.second.event_one',
'video.second.eventOne',
'video.second.event_two',
'video.second.eventTwo',
'second_namespace:video.second.event_one',
'second_namespace:video.second.event_two',
])
})

Expand All @@ -89,10 +87,9 @@ describe('BaseComponent', () => {
expect(instance.listenerCount('custom:video.test.event_one')).toEqual(2)
expect(instance.listenerCount('custom:video.test.event_two')).toEqual(2)

// _trackedEvents contains raw-prefixed event names
expect(instance.eventNames()).toStrictEqual([
'video.test.eventOne',
'video.test.eventTwo',
'custom:video.test.event_one',
'custom:video.test.event_two',
])

// No-op
Expand Down Expand Up @@ -120,10 +117,9 @@ describe('BaseComponent', () => {
expect(instance.listenerCount('custom:video.test.event_one')).toEqual(2)
expect(instance.listenerCount('custom:video.test.event_two')).toEqual(2)

// _trackedEvents contains raw-prefixed event names
expect(instance.eventNames()).toStrictEqual([
'video.test.eventOne',
'video.test.eventTwo',
'custom:video.test.event_one',
'custom:video.test.event_two',
])

instance.removeAllListeners()
Expand Down
Loading

0 comments on commit 49b4aa9

Please sign in to comment.