Skip to content

Commit b9d3fed

Browse files
committed
Converts Emitter to typescript
1 parent 2eccefe commit b9d3fed

File tree

4 files changed

+15
-11
lines changed

4 files changed

+15
-11
lines changed

src/Draggable/Emitter/Emitter.js renamed to src/Draggable/Emitter/Emitter.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
1+
import AbstractEvent from 'shared/AbstractEvent';
2+
3+
type CallbackFunction = (event: AbstractEvent<unknown>) => void;
4+
5+
interface Callback {
6+
[key: string]: CallbackFunction[];
7+
}
8+
19
/**
210
* The Emitter is a simple emitter class that provides you with `on()`, `off()` and `trigger()` methods
311
* @class Emitter
412
* @module Emitter
513
*/
614
export default class Emitter {
7-
constructor() {
8-
this.callbacks = {};
9-
}
15+
public callbacks: Callback = {};
1016

1117
/**
1218
* Registers callbacks by event name
1319
* @param {String} type
1420
* @param {...Function} callbacks
1521
*/
16-
on(type, ...callbacks) {
22+
on(type: string, ...callbacks: CallbackFunction[]) {
1723
if (!this.callbacks[type]) {
1824
this.callbacks[type] = [];
1925
}
@@ -28,7 +34,7 @@ export default class Emitter {
2834
* @param {String} type
2935
* @param {Function} callback
3036
*/
31-
off(type, callback) {
37+
off(type: string, callback: CallbackFunction) {
3238
if (!this.callbacks[type]) {
3339
return null;
3440
}
@@ -48,7 +54,7 @@ export default class Emitter {
4854
* Triggers event callbacks by event object
4955
* @param {AbstractEvent} event
5056
*/
51-
trigger(event) {
57+
trigger(event: AbstractEvent<unknown>) {
5258
if (!this.callbacks[event.type]) {
5359
return null;
5460
}

src/Draggable/Emitter/index.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/Draggable/Emitter/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {default} from './Emitter';

src/Draggable/Emitter/tests/Emitter.test.js renamed to src/Draggable/Emitter/tests/Emitter.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import AbstractEvent from 'shared/AbstractEvent';
22

33
import Emitter from '../Emitter';
44

5-
class TestEvent extends AbstractEvent {}
5+
class TestEvent extends AbstractEvent<unknown> {}
66

77
describe('Emitter', () => {
8-
let emitter;
8+
let emitter: Emitter;
99

1010
beforeEach(() => {
1111
emitter = new Emitter();

0 commit comments

Comments
 (0)