-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
487 additions
and
513 deletions.
There are no files selected for viewing
File renamed without changes.
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 |
---|---|---|
@@ -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 | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -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 }) | ||
} | ||
} |
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 |
---|---|---|
@@ -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) | ||
}) | ||
}) |
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 |
---|---|---|
@@ -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) | ||
}) | ||
}) |
Oops, something went wrong.