Skip to content

Commit

Permalink
Refactor: Update ts and prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
EvandroLG committed Dec 26, 2024
1 parent b857331 commit 67830c7
Show file tree
Hide file tree
Showing 27 changed files with 491 additions and 517 deletions.
8 changes: 4 additions & 4 deletions .eslintrc.js → .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ module.exports = {
'error',
{
groups: [
['type'], // Type imports always at the top
['builtin', 'external'], // Node.js built-ins and external packages
['internal'], // Internal imports
['parent', 'sibling', 'index'], // Parent, sibling, and index files
['type'],
['builtin', 'external'],
['internal'],
['parent', 'sibling', 'index'],
],
pathGroups: [
{
Expand Down
10 changes: 7 additions & 3 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
{
"printWidth": 80,
"semi": true,
"printWidth": 100,
"semi": false,
"singleQuote": true,
"trailingComma": "es5"
"trailingComma": "all",
"arrowParens": "always",
"endOfLine": "lf",
"tabWidth": 2,
"bracketSpacing": true
}
35 changes: 27 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@
"jest": "^29.3.1",
"jest-environment-jsdom": "^29.3.1",
"microbundle": "^0.15.1",
"prettier": "2.8.0",
"prettier": "3.4.2",
"size-limit": "^8.1.2",
"ts-jest": "^29.0.3",
"ts-node": "^10.9.1",
"typescript": "^4.8.4"
"ts-jest": "^29.0.5",
"ts-node": "^10.9.2",
"typescript": "^5.7.2"
},
"keywords": [
"audio",
Expand Down
12 changes: 6 additions & 6 deletions src/EventEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ export type Event = {
/**
* The data associated with the event. The type of data is unknown.
*/
data: unknown;
};
data: unknown
}

/**
* Event emitter class that allows registering event listeners and emitting events.
Expand All @@ -16,13 +16,13 @@ export class EventEmitter {
* A map of event keys to their respective callback functions.
* @private
*/
private events: { [key: string]: (param: Event) => void };
private events: { [key: string]: (param: Event) => void }

/**
* Initializes a new instance of the EventEmitter class.
*/
constructor() {
this.events = {};
this.events = {}
}

/**
Expand All @@ -32,7 +32,7 @@ export class EventEmitter {
* @param {(param: Event) => void} callback - The callback function to be invoked when the event is emitted.
*/
public listener(keyEvent: string, callback: (param: Event) => void): void {
this.events[keyEvent] = callback;
this.events[keyEvent] = callback
}

/**
Expand All @@ -43,7 +43,7 @@ export class EventEmitter {
*/
public emit(keyEvent: string, param: Event): void {
if (this.events[keyEvent]) {
this.events[keyEvent](param);
this.events[keyEvent](param)
}
}
}
23 changes: 11 additions & 12 deletions src/EventHandler.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,55 @@
import type { EventEmitter } from './EventEmitter';
import type { EventEmitter } from './EventEmitter'

type callbackType = <T>(param: { [data: string]: T }) => void;
type callbackType = <T>(param: { [data: string]: T }) => void

/**
* EventHandler class to manage event listeners for an audio context.
*/
export class EventHandler {
private emitter: EventEmitter;
private audioCtx: AudioContext | undefined;
private emitter: EventEmitter
private audioCtx: AudioContext | undefined

/**
* Creates an instance of EventHandler.
* @param emitter - The event emitter instance to manage event listeners.
* @param audioCtx - AudioContext instance to monitor state changes. Optional to facilitate testing.
*/
constructor(emitter: EventEmitter, audioCtx?: AudioContext) {
this.emitter = emitter;
this.audioCtx = audioCtx;
this.emitter = emitter
this.audioCtx = audioCtx
}

/**
* Registers a callback for the 'decoded' event.
* @param callback - The callback to be invoked when the event occurs.
*/
public ready(callback: callbackType) {
this.emitter.listener('decoded', callback);
this.emitter.listener('decoded', callback)
}

/**
* Registers a callback for the 'start' event.
* @param callback - The callback to be invoked when the event occurs.
*/
public start(callback: callbackType) {
this.emitter.listener('start', callback);
this.emitter.listener('start', callback)
}

/**
* Registers a callback for the 'end' event.
* @param callback - The callback to be invoked when the event occurs.
*/
public end(callback: callbackType) {
this.emitter.listener('end', callback);
this.emitter.listener('end', callback)
}

/**
* Monitors the state changes of the AudioContext and invokes the callback.
* @param callback - The callback to be invoked when the AudioContext state changes.
*/
public state(callback: callbackType) {
if (!this.audioCtx) return;
if (!this.audioCtx) return

this.audioCtx.onstatechange = () =>
callback({ data: this.audioCtx?.state });
this.audioCtx.onstatechange = () => callback({ data: this.audioCtx?.state })
}
}
20 changes: 10 additions & 10 deletions src/__tests__/EventEmitter.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { EventEmitter } from '../EventEmitter';
import { EventEmitter } from '../EventEmitter'

describe('EventEmitter', () => {
test('emit', () => {
const emitter = new EventEmitter();
const keyEvent = 'decoded';
const callback = jest.fn();
const param = { data: true };
const emitter = new EventEmitter()
const keyEvent = 'decoded'
const callback = jest.fn()
const param = { data: true }

emitter.listener(keyEvent, callback);
emitter.emit(keyEvent, { data: true });
emitter.listener(keyEvent, callback)
emitter.emit(keyEvent, { data: true })

expect(callback).toBeCalledWith(param);
});
});
expect(callback).toBeCalledWith(param)
})
})
44 changes: 22 additions & 22 deletions src/__tests__/EventHanlder.test.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
import { EventEmitter } from '../EventEmitter';
import { EventHandler } from '../EventHandler';
import { EventEmitter } from '../EventEmitter'
import { EventHandler } from '../EventHandler'

jest.mock('../EventEmitter');
jest.mock('../EventEmitter')

describe('EventHandler', () => {
let mockEmitter: jest.Mocked<EventEmitter>;
let eventHandler: EventHandler;
let mockEmitter: jest.Mocked<EventEmitter>
let eventHandler: EventHandler

beforeEach(() => {
mockEmitter = new EventEmitter() as jest.Mocked<EventEmitter>;
mockEmitter.listener = jest.fn();
eventHandler = new EventHandler(mockEmitter);
});
mockEmitter = new EventEmitter() as jest.Mocked<EventEmitter>
mockEmitter.listener = jest.fn()
eventHandler = new EventHandler(mockEmitter)
})

test('registers a callback for the "decoded" event', () => {
const callback = jest.fn();
eventHandler.ready(callback);
const callback = jest.fn()
eventHandler.ready(callback)

expect(mockEmitter.listener).toBeCalledWith('decoded', callback);
});
expect(mockEmitter.listener).toBeCalledWith('decoded', callback)
})

test('registers a callback for the "start" event', () => {
const callback = jest.fn();
eventHandler.start(callback);
const callback = jest.fn()
eventHandler.start(callback)

expect(mockEmitter.listener).toBeCalledWith('start', callback);
});
expect(mockEmitter.listener).toBeCalledWith('start', callback)
})

test('registers a callback for the "end" event', () => {
const callback = jest.fn();
eventHandler.end(callback);
const callback = jest.fn()
eventHandler.end(callback)

expect(mockEmitter.listener).toBeCalledWith('end', callback);
});
});
expect(mockEmitter.listener).toBeCalledWith('end', callback)
})
})
Loading

0 comments on commit 67830c7

Please sign in to comment.