-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #122 from sima-land/38-refactor-for-redirects
Шаг 75 #38 Рефакторинг для редиректов
- Loading branch information
Showing
10 changed files
with
246 additions
and
6 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
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,99 @@ | ||
import { EventEmitter } from 'node:events'; | ||
import { EmitterAsTarget } from '../emitter-as-target'; | ||
|
||
describe('EmitterAsTarget', () => { | ||
it('should works as regular event target', () => { | ||
const emitter = new EventEmitter(); | ||
const target = new EmitterAsTarget(emitter); | ||
|
||
const spy = jest.fn(); | ||
|
||
target.addEventListener('test', spy); | ||
expect(spy).toHaveBeenCalledTimes(0); | ||
|
||
target.dispatchEvent(new Event('test')); | ||
expect(spy).toHaveBeenCalledTimes(1); | ||
|
||
target.dispatchEvent(new Event('test')); | ||
expect(spy).toHaveBeenCalledTimes(2); | ||
|
||
target.removeEventListener('test', spy); | ||
expect(spy).toHaveBeenCalledTimes(2); | ||
|
||
target.dispatchEvent(new Event('test')); | ||
expect(spy).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
it('should callback as object', () => { | ||
const emitter = new EventEmitter(); | ||
const target = new EmitterAsTarget(emitter); | ||
|
||
const spy = jest.fn(); | ||
|
||
target.addEventListener('test', { handleEvent: spy }); | ||
expect(spy).toHaveBeenCalledTimes(0); | ||
|
||
target.dispatchEvent(new Event('test')); | ||
expect(spy).toHaveBeenCalledTimes(1); | ||
|
||
target.dispatchEvent(new Event('test')); | ||
expect(spy).toHaveBeenCalledTimes(2); | ||
|
||
target.removeEventListener('test', { handleEvent: spy }); | ||
expect(spy).toHaveBeenCalledTimes(2); | ||
|
||
target.dispatchEvent(new Event('test')); | ||
expect(spy).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
it('should handle callback as null', () => { | ||
const emitter = new EventEmitter(); | ||
const target = new EmitterAsTarget(emitter); | ||
|
||
expect(() => { | ||
target.addEventListener('foo', null); | ||
}).not.toThrow(); | ||
|
||
expect(() => { | ||
target.removeEventListener('foo', null); | ||
}).not.toThrow(); | ||
}); | ||
|
||
it('should work through emitter', () => { | ||
const emitter = new EventEmitter(); | ||
const target = new EmitterAsTarget(emitter); | ||
|
||
const spy = jest.fn(); | ||
|
||
emitter.on('test', spy); | ||
expect(spy).toHaveBeenCalledTimes(0); | ||
|
||
target.dispatchEvent(new Event('test')); | ||
expect(spy).toHaveBeenCalledTimes(1); | ||
|
||
target.dispatchEvent(new Event('test')); | ||
expect(spy).toHaveBeenCalledTimes(2); | ||
|
||
emitter.removeListener('test', spy); | ||
expect(spy).toHaveBeenCalledTimes(2); | ||
|
||
target.dispatchEvent(new Event('test')); | ||
expect(spy).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
it('should handle options.once', () => { | ||
const emitter = new EventEmitter(); | ||
const target = new EmitterAsTarget(emitter); | ||
|
||
const spy = jest.fn(); | ||
|
||
target.addEventListener('foo', spy, { once: true }); | ||
expect(spy).toHaveBeenCalledTimes(0); | ||
|
||
target.dispatchEvent(new Event('foo')); | ||
expect(spy).toHaveBeenCalledTimes(1); | ||
|
||
target.dispatchEvent(new Event('foo')); | ||
expect(spy).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
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,54 @@ | ||
import type { EventEmitter } from 'node:events'; | ||
|
||
/** | ||
* Наивная реализация обёртки, превращающей EventEmitter в EventTarget. | ||
*/ | ||
export class EmitterAsTarget extends EventTarget { | ||
private emitter: EventEmitter; | ||
|
||
/** @inheritdoc */ | ||
constructor(emitter: EventEmitter) { | ||
super(); | ||
this.emitter = emitter; | ||
} | ||
|
||
/** @inheritdoc */ | ||
addEventListener( | ||
type: string, | ||
callback: EventListenerOrEventListenerObject | null, | ||
options?: AddEventListenerOptions | boolean, | ||
) { | ||
if (!callback) { | ||
return; | ||
} | ||
|
||
const listener = typeof callback === 'function' ? callback : callback.handleEvent; | ||
|
||
switch (true) { | ||
case typeof options === 'object' && options !== null && options.once: { | ||
this.emitter.once(type, listener as (...args: any[]) => void); | ||
break; | ||
} | ||
default: { | ||
this.emitter.on(type, listener as (...args: any[]) => void); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
/** @inheritdoc */ | ||
removeEventListener(type: string, callback: EventListenerOrEventListenerObject | null) { | ||
if (!callback) { | ||
return; | ||
} | ||
|
||
const listener = typeof callback === 'function' ? callback : callback.handleEvent; | ||
|
||
this.emitter.removeListener(type, listener as (...args: any[]) => void); | ||
} | ||
|
||
/** @inheritdoc */ | ||
dispatchEvent(event: Event) { | ||
return this.emitter.emit(event.type, 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
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