Skip to content

Instantly share code, notes, and snippets.

@zerkalica
Created December 27, 2023 22:56
Show Gist options
  • Save zerkalica/261812b8c521dd41b89e638c3920cb91 to your computer and use it in GitHub Desktop.
Save zerkalica/261812b8c521dd41b89e638c3920cb91 to your computer and use it in GitHub Desktop.
wait.ts
namespace $ {
export type $gd_speech_ws_command =
| {
cmd: 'init'
data: {
app_ref: string
audio_guid: string
final_sep: string
samplerate: number
source: string
profile: Record<string, string | Record<string, string>>
}
}
| {
cmd: 'stop'
}
| 'ping'
export class $gd_speech_ws extends $gd_kit_ws<$gd_speech_ws_command> {
override on_object( raw: Object ) {
let obj: typeof $gd_speech_ws_message.Value | undefined
try {
obj = this.$.$gd_speech_ws_message(raw as any)
} catch (e) {
if ($mol_promise_like(e)) $mol_fail_hidden(e)
this.error({ val: e as Error })
return
}
if (! obj || typeof obj === 'string') return
if (obj.cmd === 'init_success') return this.init_success().value(obj.data)
if (obj.cmd === 'log_info') return this.log_info(obj.data)
if (obj.cmd === 'rec') return this.rec(obj.data)
if (obj.cmd === 'end_rec') return this.end_rec().value(obj.data)
if (obj.cmd === 'error') {
this.error({ val: new Error(obj.data ?? 'unknown error', { cause: obj }) })
}
}
protected ping_off() {
return true
}
ping_send() {
this.send('ping')
}
rec(data?: typeof $gd_speech_ws_message_rec.Value) {
return data ?? null
}
log_info(data: typeof $gd_speech_ws_message_log_info.Value) {
return data ?? null
}
@ $mol_action
parsed_text(data: typeof $gd_speech_ws_message_end_rec.Value) {}
@ $mol_mem
protected end_rec( next?: typeof $gd_speech_ws_message_end_rec.Value ) {
return new this.$.$gd_kit_sync_wait<typeof $gd_speech_ws_message_end_rec.Value>()
}
@ $mol_mem
stop() {
this.send_object({ cmd: 'stop' })
return this.end_rec().value()
}
init_args() {
return { } as Extract<$gd_speech_ws_command, { cmd: 'init' }>['data']
}
@ $mol_mem
protected init_success() {
return new this.$.$gd_kit_sync_wait<typeof $gd_speech_ws_message_init_success.Value>()
}
@ $mol_mem
init() {
this.status()
this.send_object({
cmd: 'init',
data: this.init_args()
})
const res = this.init_success().value()
return res
}
}
}
namespace $ {
export class $gd_kit_sync_wait<Value> extends $mol_object {
constructor(protected deadline = 5000) {
super()
}
protected promise_value = null as null | ReturnType<typeof $mol_promise<Value>>
protected timer = null as null | $mol_after_timeout
promise() {
if (this.promise_value) return this.promise_value
this.timer = new this.$.$mol_after_timeout(
this.deadline,
() => this.promise().fail( new Error(`Deadline ${this.deadline} ms exceeded`) ),
)
return this.promise_value = $mol_promise()
}
destructor() {
this.timer?.destructor()
}
@ $mol_action
value(next?: Value) {
if (next !== undefined) {
this.promise().done(next)
this.timer?.destructor()
return next
}
const val = $mol_wire_sync(this).promise() as Value
this.promise_value = null
this.timer = null
return val
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment