Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor callbacks #334

Merged
merged 3 commits into from
Aug 30, 2023
Merged

refactor callbacks #334

merged 3 commits into from
Aug 30, 2023

Conversation

Irev-Dev
Copy link
Collaborator

I had two motivations for this.

  1. Some of the sketch on plane work I'm doing Sketch on plane WIP #309 needed another onClick in the 3d scene listener, and at that point, it felt like the engine connection was becoming way too concerned with the implementation details of the the things the app needed to do and listen to. This might become redundant with the xState migration though, not sure.
  2. We need to add websocket support to the typescript client and I think it might be best to make engine connection more and more generic and plop it into that repo instead of this one.

@vercel
Copy link

vercel bot commented Aug 28, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
untitled-lang ✅ Ready (Inspect) Visit Preview Aug 30, 2023 6:54pm

Comment on lines +345 to +350
interface Subscription<T extends ModelTypes> {
event: T
callback: (
data: Extract<Models['OkModelingCmdResponse_type'], { type: T }>
) => void
}
Copy link
Collaborator Author

@Irev-Dev Irev-Dev Aug 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty happy with this typing, making the event narrow down the data arg in the callback, however . . .

Comment on lines +361 to +365
subscriptions: {
[event: string]: {
[localUnsubscribeId: string]: (a: any) => void
}
} = {} as any
Copy link
Collaborator Author

@Irev-Dev Irev-Dev Aug 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

however ... I was unable to get good types for this nested object of callbacks.
I had something like

  subscriptions: {
    [event in ModelTypes]?: {
      [localUnsubscribeId: string]: Subscription<ModelTypes>['callback']
    }
  } = {}

In mind, but that just caused me more headaches. I'm okay with this because it makes the authoring experience of actually adding a new subscription quiet good, (good types in the callback function)

image

But definitely could get better.

Comment on lines +337 to +346
const unSubClick = engineCommandManager.subscribeTo({
event: 'select_with_point',
callback: ({ data }) => {
if (!data?.entity_id) {
setCursor2()
return
}
const sourceRange = sourceRangeMap[data.entity_id]
setCursor2({ range: sourceRange, type: 'default' })
},
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

const sourceRange = sourceRangeMap[id]
setHighlightRange(sourceRange)
}
const unSubHover = engineCommandManager.subscribeToLossy({
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Food for thought, not feedback on this line -- we call this construct (the UDP data channel in the WebRTC stream) a few things, like unreliable or lossy. I have no strong feelings, but it may be worth getting everything to use the same word everywhere to make things a bit more grepable; I think "Lossy" is the one that's won out on the codebase

@@ -331,6 +331,23 @@ export class EngineConnection {
}

export type EngineCommand = Models['WebSocketRequest_type']
type ModelTypes = Models['OkModelingCmdResponse_type']['type']

type LossyResponses = Extract<
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extract is nice, this is cool

result?.data.sequence > this.inSequence &&
result.type === 'highlight_set_entity'
) {
this.inSequence = result.data.sequence
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something I just thought of when working through this review (but not material to this PR since this exists in main), when we do a WebSocket/WebRTC reconnect, we may get a different engine-api, which means the sequence would be reset. I reckon we need to zero out inSequence when we have a EngineConnection open event

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I filed #336 for us to not forget this (although I haven't got reconnect implemented yet -- the new EngineConnection retry stuff is only on connect, not if a WebSocket breaks after connection, so we've never hit this yet, I don't think)

@paultag paultag merged commit f3274e0 into main Aug 30, 2023
7 checks passed
@paultag paultag deleted the refactor-engine-connection-callbacks branch August 30, 2023 19:19
@paultag
Copy link
Collaborator

paultag commented Aug 30, 2023

I landed this since I wound up reaching for something here and saw it wasn't there, it was approved and looks good locally

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants