Skip to content

Commit

Permalink
Convert EventHandler to class
Browse files Browse the repository at this point in the history
  • Loading branch information
EvandroLG committed Dec 25, 2024
1 parent 02a29fa commit 036b46b
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 33 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ module.exports = {
},
rules: {
'prefer-const': 'error',
'@typescript-eslint/explicit-module-boundary-types': 'off',
},
};
67 changes: 44 additions & 23 deletions src/EventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,55 @@ import { EventEmitterType } from './EventEmitter';

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

type EventHandlerType = {
ready: (callback: callbackType) => void;
start: (callback: callbackType) => void;
end: (callback: callbackType) => void;
state: (callback: callbackType) => void;
};

const EventHandler = (
emitter: EventEmitterType,
audioCtx?: AudioContext
): EventHandlerType => ({
/**
* EventHandler class to manage event listeners for an audio context.
*/
export class EventHandler {
private emitter: EventEmitterType;
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: EventEmitterType, audioCtx?: AudioContext) {
this.emitter = emitter;
this.audioCtx = audioCtx;
}

/**
* Registers a callback for the 'decoded' event.
* @param callback - The callback to be invoked when the event occurs.
*/
ready(callback: callbackType) {
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.
*/
start(callback: callbackType) {
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.
*/
end(callback: callbackType) {
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.
*/
state(callback: callbackType) {
if (audioCtx) {
audioCtx.onstatechange = () => callback({ data: audioCtx.state });
}
},
});
if (!this.audioCtx) return;

export default EventHandler;
this.audioCtx.onstatechange = () =>
callback({ data: this.audioCtx?.state });
}
}
17 changes: 12 additions & 5 deletions src/__tests__/EventHanlder.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import EventHandler from '../EventHandler';
import { EventHandler } from '../EventHandler';
import { EventEmitterType } from '../EventEmitter';

const EventEmitterMock = () =>
Expand All @@ -7,21 +7,28 @@ const EventEmitterMock = () =>
emit: jest.fn(),
} as EventEmitterType);

describe('event listener', () => {
describe('EventHandler', () => {
const emitter = EventEmitterMock();
const eventHandler = EventHandler(emitter);
const eventHandler = new EventHandler(emitter);

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

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

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

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

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

expect(emitter.listener).toBeCalledWith('end', callback);
});
});
6 changes: 3 additions & 3 deletions src/audio/Audio.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import AudioCtx from './AudioCtx';
import globalStates from './states';
import EventEmitter from '../EventEmitter';
import EventHandler from '../EventHandler';
import { EventHandler } from '../EventHandler';
import initializeSource from './initializeSource';
import decodeAudioData from './decodeAudioData';
import { AudioPropType, AudioEventType, AudioType } from './types';
Expand All @@ -25,12 +25,12 @@ const Audio = ({
const audioCtx = AudioCtx();
const states = { ...globalStates };
const emitter = EventEmitter();
const eventHandler = EventHandler(emitter, audioCtx);
const eventHandler = new EventHandler(emitter, audioCtx);
const curryGetBuffer = (source: AudioBufferSourceNode) => {
states.isDecoded = false;

getBuffer(file)
.then(buffer =>
.then((buffer) =>
decodeAudioData(
audioCtx,
source,
Expand Down
11 changes: 9 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"include": ["src/**/*"],
"exclude": ["node_modules", "dist", "src/**/**/*.test.ts"],
"exclude": ["node_modules", "dist"],
"compilerOptions": {
"outDir": "dist",
"target": "ESNext",
Expand All @@ -16,6 +16,13 @@
"noFallthroughCasesInSwitch": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"esModuleInterop": true
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"noImplicitAny": true,
"strictNullChecks": true,
"exactOptionalPropertyTypes": true,
"useUnknownInCatchVariables": true,
"noPropertyAccessFromIndexSignature": true
}
}

0 comments on commit 036b46b

Please sign in to comment.