-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement event emmiter to use it for selected change handling
- Loading branch information
1 parent
c51131d
commit b260d13
Showing
13 changed files
with
151 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const EVENT_EMMITER_EVENTS = { | ||
FIND_RECORDS_SELECTED_CHANGED: 'FIND_RECORDS_SELECTED_CHANGED', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { useEventEmitter } from './useEventEmitter'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { EventEmitter } from '../../utils'; | ||
|
||
export const useEventEmitter = ({ singleton = true } = {}) => { | ||
return new EventEmitter({ singleton }); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { renderHook } from '@folio/jest-config-stripes/testing-library/react'; | ||
|
||
import { EventEmitter } from '../../utils'; | ||
import { useEventEmitter } from './useEventEmitter'; | ||
|
||
describe('useEventEmitter', () => { | ||
it('should return event emitter instance', async () => { | ||
const { result } = renderHook(() => useEventEmitter()); | ||
|
||
expect(result.current).toBeInstanceOf(EventEmitter); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
export class EventEmitter { | ||
constructor(config = {}) { | ||
if (config.singleton && EventEmitter.sharedEventTarget) { | ||
// If `singleton` is set and `sharedEventTarget` already exists, use it for all instances | ||
this.eventTarget = EventEmitter.sharedEventTarget; | ||
} else { | ||
// Otherwise, create a new `EventTarget` | ||
this.eventTarget = new EventTarget(); | ||
|
||
// If `singleton` is set, save the created `EventTarget` in `sharedEventTarget` | ||
if (config.singleton) { | ||
EventEmitter.sharedEventTarget = this.eventTarget; | ||
} | ||
} | ||
} | ||
|
||
on(eventName, callback) { | ||
this.eventTarget.addEventListener(eventName, callback); | ||
} | ||
|
||
off(eventName, callback) { | ||
this.eventTarget.removeEventListener(eventName, callback); | ||
} | ||
|
||
emit(eventName, data) { | ||
const event = new CustomEvent(eventName, { detail: data }); | ||
|
||
this.eventTarget.dispatchEvent(event); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { EventEmitter } from './EventEmitter'; | ||
|
||
const EVENT_TYPE = 'test-event-type'; | ||
const callback = jest.fn(); | ||
const payload = 'Test payload'; | ||
|
||
describe('EventEmitter', () => { | ||
let emitter; | ||
|
||
beforeEach(() => { | ||
emitter = new EventEmitter(); | ||
callback.mockClear(); | ||
}); | ||
|
||
it('should add and invoke event listeners', () => { | ||
emitter.on(EVENT_TYPE, callback); | ||
emitter.emit(EVENT_TYPE, payload); | ||
|
||
expect(callback).toHaveBeenCalledWith(expect.objectContaining({ detail: payload })); | ||
}); | ||
|
||
it('should remove event listeners', () => { | ||
emitter.on(EVENT_TYPE, callback); | ||
emitter.off(EVENT_TYPE, callback); | ||
emitter.emit(EVENT_TYPE, payload); | ||
|
||
expect(callback).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should emit events with the correct data', () => { | ||
emitter.on(EVENT_TYPE, callback); | ||
emitter.emit(EVENT_TYPE, payload); | ||
|
||
expect(callback).toHaveBeenCalledWith(expect.objectContaining({ detail: payload })); | ||
}); | ||
|
||
describe('with configuration', () => { | ||
it('should create a shared EventTarget for instances with singleton set to true', () => { | ||
const emitter1 = new EventEmitter({ singleton: true }); | ||
const emitter2 = new EventEmitter({ singleton: true }); | ||
|
||
expect(emitter1.eventTarget).toBe(emitter2.eventTarget); | ||
}); | ||
|
||
it('should create separate EventTargets for instances with singleton set to false', () => { | ||
const emitter3 = new EventEmitter(); | ||
const emitter4 = new EventEmitter(); | ||
|
||
expect(emitter3.eventTarget).not.toBe(emitter4.eventTarget); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { EventEmitter } from './EventEmitter'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters