-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modernization with TypeScript and Event Target (#220)
* More typing * Update PR number
- Loading branch information
Showing
18 changed files
with
437 additions
and
231 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
27 changes: 27 additions & 0 deletions
27
packages/web-speech-cognitive-services/src/SpeechServices/SpeechToText/EventListenerMap.ts
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,27 @@ | ||
type EventListener<T extends Event> = (event: T) => void; | ||
|
||
export default class EventListenerMap<T extends string, EventMap extends Record<T, Event>> { | ||
constructor(eventTarget: EventTarget) { | ||
this.#eventTarget = eventTarget; | ||
this.#propertyMap = {}; | ||
} | ||
|
||
#eventTarget: EventTarget; | ||
#propertyMap: { [Name in keyof EventMap]?: EventListener<EventMap[Name]> | undefined }; | ||
|
||
getProperty(name: T): ((event: EventMap[typeof name]) => void) | undefined { | ||
return this.#propertyMap[name]; | ||
} | ||
|
||
setProperty(name: T, value: ((event: EventMap[typeof name]) => void) | undefined) { | ||
const existing = this.#propertyMap[name]; | ||
|
||
existing && this.#eventTarget.removeEventListener(name, existing as EventListener<Event>); | ||
|
||
if (value) { | ||
this.#eventTarget.addEventListener(name, value as EventListener<Event>); | ||
} | ||
|
||
this.#propertyMap[name] = value; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
packages/web-speech-cognitive-services/src/SpeechServices/SpeechToText/FakeArray.spec.ts
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,13 @@ | ||
import FakeArray from './FakeArray'; | ||
|
||
test('should return indexed item', () => { | ||
expect(new FakeArray([1, 2, 3])).toHaveProperty([1], 2); | ||
}); | ||
|
||
test('should return indexed item via Reflect.get', () => { | ||
expect(Reflect.get(new FakeArray([1, 2, 3]), 1)).toBe(2); | ||
}); | ||
|
||
test('should return length', () => { | ||
expect(new FakeArray([1, 2, 3])).toHaveProperty('length', 3); | ||
}); |
33 changes: 33 additions & 0 deletions
33
packages/web-speech-cognitive-services/src/SpeechServices/SpeechToText/FakeArray.ts
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,33 @@ | ||
interface FakeArrayInterface<T> { | ||
[index: number]: T | undefined; | ||
get length(): number; | ||
} | ||
|
||
export default class FakeArray<T> implements FakeArrayInterface<T> { | ||
constructor(array: readonly T[]) { | ||
if (!array) { | ||
throw new Error('array must be set.'); | ||
} | ||
|
||
this.#array = array; | ||
|
||
for (const key in array) { | ||
Object.defineProperty(this, key, { | ||
enumerable: true, | ||
get() { | ||
return array[key]; | ||
} | ||
}); | ||
} | ||
} | ||
|
||
#array: readonly T[]; | ||
[index: number]: T | undefined; | ||
[Symbol.iterator]() { | ||
return this.#array[Symbol.iterator](); | ||
} | ||
|
||
get length(): number { | ||
return this.#array.length; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...speech-cognitive-services/src/SpeechServices/SpeechToText/SpeechRecognitionAlternative.ts
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,22 @@ | ||
export type SpeechRecognitionAlternativeInit = { | ||
confidence: number; | ||
transcript: string; | ||
}; | ||
|
||
export default class SpeechRecognitionAlternative { | ||
constructor({ confidence, transcript }: SpeechRecognitionAlternativeInit) { | ||
this.#confidence = confidence; | ||
this.#transcript = transcript; | ||
} | ||
|
||
#confidence: number; | ||
#transcript: string; | ||
|
||
get confidence() { | ||
return this.#confidence; | ||
} | ||
|
||
get transcript() { | ||
return this.#transcript; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...-speech-cognitive-services/src/SpeechServices/SpeechToText/SpeechRecognitionErrorEvent.ts
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,34 @@ | ||
export type SpeechRecognitionErrorType = | ||
| 'aborted' | ||
| 'audio-capture' | ||
| 'bad-grammar' | ||
| 'language-not-supported' | ||
| 'network' | ||
| 'no-speech' | ||
| 'not-allowed' | ||
| 'service-not-allowed'; | ||
|
||
export type SpeechRecognitionErrorEventInit = { | ||
error: SpeechRecognitionErrorType; | ||
message?: string | undefined; | ||
}; | ||
|
||
export default class SpeechRecognitionErrorEvent extends Event { | ||
constructor(type: 'error', { error, message }: SpeechRecognitionErrorEventInit) { | ||
super(type); | ||
|
||
this.#error = error; | ||
this.#message = message; | ||
} | ||
|
||
#error: SpeechRecognitionErrorType; | ||
#message: string | undefined; | ||
|
||
get error(): SpeechRecognitionErrorType { | ||
return this.#error; | ||
} | ||
|
||
get message(): string | undefined { | ||
return this.#message; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
...s/web-speech-cognitive-services/src/SpeechServices/SpeechToText/SpeechRecognitionEvent.ts
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,57 @@ | ||
import SpeechRecognitionResultList from './SpeechRecognitionResultList'; | ||
|
||
export type SpeechRecognitionEventInit = { | ||
data?: undefined | unknown; | ||
resultIndex?: number | undefined; | ||
results?: SpeechRecognitionResultList | undefined; | ||
}; | ||
|
||
export default class SpeechRecognitionEvent< | ||
T extends | ||
| 'audioend' | ||
| 'audiostart' | ||
| 'cognitiveservices' | ||
| 'end' | ||
| 'result' | ||
| 'soundend' | ||
| 'soundstart' | ||
| 'speechend' | ||
| 'speechstart' | ||
| 'start' | ||
> extends Event { | ||
constructor(type: 'cognitiveservices', init: SpeechRecognitionEventInit & { data: { type: string } }); | ||
constructor(type: 'audioend'); | ||
constructor(type: 'audiostart'); | ||
constructor(type: 'end'); | ||
constructor(type: 'result', init: SpeechRecognitionEventInit); | ||
constructor(type: 'soundend'); | ||
constructor(type: 'soundstart'); | ||
constructor(type: 'speechend'); | ||
constructor(type: 'speechstart'); | ||
constructor(type: 'start'); | ||
|
||
constructor(type: T, { data, resultIndex, results }: SpeechRecognitionEventInit = {}) { | ||
super(type); | ||
|
||
this.#data = data; | ||
this.#resultIndex = resultIndex; | ||
this.#results = results || new SpeechRecognitionResultList([]); | ||
} | ||
|
||
#data: undefined | unknown; | ||
// TODO: "resultIndex" should be set. | ||
#resultIndex: number | undefined; | ||
#results: SpeechRecognitionResultList; | ||
|
||
get data(): unknown { | ||
return this.#data; | ||
} | ||
|
||
get resultIndex(): number | undefined { | ||
return this.#resultIndex; | ||
} | ||
|
||
get results(): SpeechRecognitionResultList { | ||
return this.#results; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...h-cognitive-services/src/SpeechServices/SpeechToText/SpeechRecognitionEventListenerMap.ts
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,30 @@ | ||
import EventListenerMap from './EventListenerMap'; | ||
import type SpeechRecognitionErrorEvent from './SpeechRecognitionErrorEvent'; | ||
import type SpeechRecognitionEvent from './SpeechRecognitionEvent'; | ||
|
||
export type SpeechRecognitionEventListenerMap = EventListenerMap< | ||
| 'audioend' | ||
| 'audiostart' | ||
| 'cognitiveservices' | ||
| 'end' | ||
| 'error' | ||
| 'result' | ||
| 'soundend' | ||
| 'soundstart' | ||
| 'speechend' | ||
| 'speechstart' | ||
| 'start', | ||
{ | ||
audioend: SpeechRecognitionEvent<'audioend'>; | ||
audiostart: SpeechRecognitionEvent<'audiostart'>; | ||
cognitiveservices: SpeechRecognitionEvent<'cognitiveservices'>; | ||
end: SpeechRecognitionEvent<'end'>; | ||
error: SpeechRecognitionErrorEvent; | ||
result: SpeechRecognitionEvent<'result'>; | ||
soundend: SpeechRecognitionEvent<'soundend'>; | ||
soundstart: SpeechRecognitionEvent<'soundstart'>; | ||
speechend: SpeechRecognitionEvent<'speechend'>; | ||
speechstart: SpeechRecognitionEvent<'speechstart'>; | ||
start: SpeechRecognitionEvent<'start'>; | ||
} | ||
>; |
20 changes: 20 additions & 0 deletions
20
.../web-speech-cognitive-services/src/SpeechServices/SpeechToText/SpeechRecognitionResult.ts
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,20 @@ | ||
import FakeArray from './FakeArray'; | ||
|
||
export type SpeechRecognitionResultInit = { | ||
isFinal: boolean; | ||
results: readonly SpeechRecognitionAlternative[]; | ||
}; | ||
|
||
export default class SpeechRecognitionResult extends FakeArray<SpeechRecognitionAlternative> { | ||
constructor(init: SpeechRecognitionResultInit) { | ||
super(init.results); | ||
|
||
this.#isFinal = init.isFinal; | ||
} | ||
|
||
#isFinal: boolean; | ||
|
||
get isFinal(): boolean { | ||
return this.#isFinal; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...-speech-cognitive-services/src/SpeechServices/SpeechToText/SpeechRecognitionResultList.ts
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,8 @@ | ||
import FakeArray from './FakeArray'; | ||
import type SpeechRecognitionResult from './SpeechRecognitionResult'; | ||
|
||
export default class SpeechRecognitionResultList extends FakeArray<SpeechRecognitionResult> { | ||
constructor(result: readonly SpeechRecognitionResult[]) { | ||
super(result); | ||
} | ||
} |
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
Oops, something went wrong.