rename lossy to unreliable (#357)

* rename lossy to unreliable

* fmt

* missed a rename
This commit is contained in:
Kurt Hutten
2023-08-31 07:39:03 +10:00
committed by GitHub
parent f3274e03ff
commit eb48d51309
2 changed files with 42 additions and 33 deletions

View File

@ -325,7 +325,7 @@ export function App() {
await engineCommandManager.waitForAllCommands() await engineCommandManager.waitForAllCommands()
setArtifactMap({ artifactMap, sourceRangeMap }) setArtifactMap({ artifactMap, sourceRangeMap })
const unSubHover = engineCommandManager.subscribeToLossy({ const unSubHover = engineCommandManager.subscribeToUnreliable({
event: 'highlight_set_entity', event: 'highlight_set_entity',
callback: ({ data }) => { callback: ({ data }) => {
if (!data?.entity_id) { if (!data?.entity_id) {

View File

@ -50,7 +50,7 @@ type WebSocketResponse = Models['OkWebSocketResponseData_type']
export class EngineConnection { export class EngineConnection {
websocket?: WebSocket websocket?: WebSocket
pc?: RTCPeerConnection pc?: RTCPeerConnection
lossyDataChannel?: RTCDataChannel unreliableDataChannel?: RTCDataChannel
private ready: boolean private ready: boolean
@ -462,11 +462,11 @@ export class EngineConnection {
let connectionStarted = new Date() let connectionStarted = new Date()
this.pc.addEventListener('datachannel', (event) => { this.pc.addEventListener('datachannel', (event) => {
this.lossyDataChannel = event.channel this.unreliableDataChannel = event.channel
console.log('accepted lossy data channel', event.channel.label) console.log('accepted unreliable data channel', event.channel.label)
this.lossyDataChannel.addEventListener('open', (event) => { this.unreliableDataChannel.addEventListener('open', (event) => {
console.log('lossy data channel opened', event) console.log('unreliable data channel opened', event)
if (this.shouldTrace()) { if (this.shouldTrace()) {
dataChannelSpan.finish() dataChannelSpan.finish()
} }
@ -477,13 +477,13 @@ export class EngineConnection {
this.ready = true this.ready = true
}) })
this.lossyDataChannel.addEventListener('close', (event) => { this.unreliableDataChannel.addEventListener('close', (event) => {
console.log('lossy data channel closed') console.log('unreliable data channel closed')
this.close() this.close()
}) })
this.lossyDataChannel.addEventListener('error', (event) => { this.unreliableDataChannel.addEventListener('error', (event) => {
console.log('lossy data channel error') console.log('unreliable data channel error')
this.close() this.close()
}) })
}) })
@ -498,10 +498,10 @@ export class EngineConnection {
close() { close() {
this.websocket?.close() this.websocket?.close()
this.pc?.close() this.pc?.close()
this.lossyDataChannel?.close() this.unreliableDataChannel?.close()
this.websocket = undefined this.websocket = undefined
this.pc = undefined this.pc = undefined
this.lossyDataChannel = undefined this.unreliableDataChannel = undefined
this.onClose(this) this.onClose(this)
this.ready = false this.ready = false
@ -511,13 +511,13 @@ export class EngineConnection {
export type EngineCommand = Models['WebSocketRequest_type'] export type EngineCommand = Models['WebSocketRequest_type']
type ModelTypes = Models['OkModelingCmdResponse_type']['type'] type ModelTypes = Models['OkModelingCmdResponse_type']['type']
type LossyResponses = Extract< type UnreliableResponses = Extract<
Models['OkModelingCmdResponse_type'], Models['OkModelingCmdResponse_type'],
{ type: 'highlight_set_entity' } { type: 'highlight_set_entity' }
> >
interface LossySubscription<T extends LossyResponses['type']> { interface UnreliableSubscription<T extends UnreliableResponses['type']> {
event: T event: T
callback: (data: Extract<LossyResponses, { type: T }>) => void callback: (data: Extract<UnreliableResponses, { type: T }>) => void
} }
interface Subscription<T extends ModelTypes> { interface Subscription<T extends ModelTypes> {
@ -541,7 +541,7 @@ export class EngineCommandManager {
[localUnsubscribeId: string]: (a: any) => void [localUnsubscribeId: string]: (a: any) => void
} }
} = {} as any } = {} as any
lossySubscriptions: { unreliableSubscriptions: {
[event: string]: { [event: string]: {
[localUnsubscribeId: string]: (a: any) => void [localUnsubscribeId: string]: (a: any) => void
} }
@ -575,15 +575,17 @@ export class EngineCommandManager {
}, },
onConnectionStarted: (engineConnection) => { onConnectionStarted: (engineConnection) => {
engineConnection?.pc?.addEventListener('datachannel', (event) => { engineConnection?.pc?.addEventListener('datachannel', (event) => {
let lossyDataChannel = event.channel let unreliableDataChannel = event.channel
lossyDataChannel.addEventListener('message', (event) => { unreliableDataChannel.addEventListener('message', (event) => {
const result: LossyResponses = JSON.parse(event.data) const result: UnreliableResponses = JSON.parse(event.data)
Object.values(this.lossySubscriptions[result.type] || {}).forEach( Object.values(
// TODO: There is only one response that uses the lossy channel atm, this.unreliableSubscriptions[result.type] || {}
).forEach(
// TODO: There is only one response that uses the unreliable channel atm,
// highlight_set_entity, if there are more it's likely they will all have the same // highlight_set_entity, if there are more it's likely they will all have the same
// sequence logic, but I'm not sure if we use a single global sequence or a sequence // sequence logic, but I'm not sure if we use a single global sequence or a sequence
// per lossy subscription. // per unreliable subscription.
(callback) => { (callback) => {
if ( if (
result?.data?.sequence && result?.data?.sequence &&
@ -688,23 +690,26 @@ export class EngineCommandManager {
private unSubscribeTo(event: ModelTypes, id: string) { private unSubscribeTo(event: ModelTypes, id: string) {
delete this.subscriptions[event][id] delete this.subscriptions[event][id]
} }
subscribeToLossy<T extends LossyResponses['type']>({ subscribeToUnreliable<T extends UnreliableResponses['type']>({
event, event,
callback, callback,
}: LossySubscription<T>): () => void { }: UnreliableSubscription<T>): () => void {
const localUnsubscribeId = uuidv4() const localUnsubscribeId = uuidv4()
const otherEventCallbacks = this.lossySubscriptions[event] const otherEventCallbacks = this.unreliableSubscriptions[event]
if (otherEventCallbacks) { if (otherEventCallbacks) {
otherEventCallbacks[localUnsubscribeId] = callback otherEventCallbacks[localUnsubscribeId] = callback
} else { } else {
this.lossySubscriptions[event] = { this.unreliableSubscriptions[event] = {
[localUnsubscribeId]: callback, [localUnsubscribeId]: callback,
} }
} }
return () => this.unSubscribeToLossy(event, localUnsubscribeId) return () => this.unSubscribeToUnreliable(event, localUnsubscribeId)
} }
private unSubscribeToLossy(event: LossyResponses['type'], id: string) { private unSubscribeToUnreliable(
delete this.lossySubscriptions[event][id] event: UnreliableResponses['type'],
id: string
) {
delete this.unreliableSubscriptions[event][id]
} }
endSession() { endSession() {
// this.websocket?.close() // this.websocket?.close()
@ -743,19 +748,23 @@ export class EngineCommandManager {
const cmd = command.cmd const cmd = command.cmd
if ( if (
cmd.type === 'camera_drag_move' && cmd.type === 'camera_drag_move' &&
this.engineConnection?.lossyDataChannel this.engineConnection?.unreliableDataChannel
) { ) {
cmd.sequence = this.outSequence cmd.sequence = this.outSequence
this.outSequence++ this.outSequence++
this.engineConnection?.lossyDataChannel?.send(JSON.stringify(command)) this.engineConnection?.unreliableDataChannel?.send(
JSON.stringify(command)
)
return Promise.resolve() return Promise.resolve()
} else if ( } else if (
cmd.type === 'highlight_set_entity' && cmd.type === 'highlight_set_entity' &&
this.engineConnection?.lossyDataChannel this.engineConnection?.unreliableDataChannel
) { ) {
cmd.sequence = this.outSequence cmd.sequence = this.outSequence
this.outSequence++ this.outSequence++
this.engineConnection?.lossyDataChannel?.send(JSON.stringify(command)) this.engineConnection?.unreliableDataChannel?.send(
JSON.stringify(command)
)
return Promise.resolve() return Promise.resolve()
} }
console.log('sending command', command) console.log('sending command', command)