add client lib @kittycad/lib (#214)

* add client lib

* tsc after build

* update after spec update

* remove uneeded check

* updates

Signed-off-by: Jess Frazelle <github@jessfraz.com>

* fixes

Signed-off-by: Jess Frazelle <github@jessfraz.com>

* fix camera drag

* fix throttle typing

* comment with link to issue

---------

Signed-off-by: Jess Frazelle <github@jessfraz.com>
Co-authored-by: Jess Frazelle <github@jessfraz.com>
This commit is contained in:
Kurt Hutten
2023-08-02 15:41:59 +10:00
committed by GitHub
parent aabb88ee45
commit 5419039fae
12 changed files with 140 additions and 126 deletions

View File

@ -1,6 +1,7 @@
import { SourceRange } from '../executor'
import { Selections } from '../../useStore'
import { VITE_KC_API_WS_MODELING_URL } from '../../env'
import { Models } from '@kittycad/lib'
interface ResultCommand {
type: 'result'
@ -48,43 +49,13 @@ interface XYZ {
y: number
z: number
}
export interface EngineCommand {
type: 'ModelingCmdReq'
cmd: {
StartPath?: {}
MovePathPen?: {
path: uuid
to: XYZ
}
ExtendPath?: {
path: uuid
segment: {
Line: {
end: XYZ
}
}
}
ClosePath?: {
path_id: uuid
}
Extrude?: {
target: uuid
distance: number
cap: boolean
}
CameraDragMove?: MouseDrag
CameraDragStart?: MouseStuff
CameraDragEnd?: MouseStuff
DefaultCameraEnableSketchMode?: {
origin: XYZ
x_axis: XYZ
y_axis: XYZ
distance_to_plane: number
ortho: boolean
}
}
cmd_id: uuid
file_id: uuid
export type _EngineCommand = Models['ModelingCmdReq_type']
// TODO extending this type to add the type property is a work around
// see https://github.com/KittyCAD/api-deux/issues/1096
export interface EngineCommand extends _EngineCommand {
type: 'modeling_cmd_req'
}
export class EngineCommandManager {
@ -152,12 +123,12 @@ export class EngineCommandManager {
console.warn('something went wrong: ', event.data)
} else {
const message = JSON.parse(event.data)
if (message.type === 'SDPAnswer') {
if (message.type === 's_d_p_answer') {
this.pc?.setRemoteDescription(
new RTCSessionDescription(message.answer)
)
} else if (message.type === 'IceServerInfo' && this.pc) {
console.log('received IceServerInfo')
} else if (message.type === 'ice_server_info' && this.pc) {
console.log('received ice_server_info')
this.pc?.setConfiguration({
iceServers: message.ice_servers,
})
@ -173,10 +144,10 @@ export class EngineCommandManager {
this.pc.addEventListener('icecandidate', (event) => {
if (!this.pc || !this.socket) return
if (event.candidate === null) {
console.log('sent SDPOffer')
console.log('sent s_d_p_offer')
this.socket.send(
JSON.stringify({
type: 'SDPOffer',
type: 's_d_p_offer',
offer: this.pc.localDescription,
})
)
@ -191,9 +162,9 @@ export class EngineCommandManager {
.createOffer()
.then(async (descriptionInit) => {
await this?.pc?.setLocalDescription(descriptionInit)
console.log('sent SDPOffer begin')
console.log('sent s_d_p_offer begin')
const msg = JSON.stringify({
type: 'SDPOffer',
type: 's_d_p_offer',
offer: this.pc?.localDescription,
})
this.socket?.send(msg)
@ -297,9 +268,10 @@ export class EngineCommandManager {
console.log('socket not ready')
return
}
if (command.cmd.CameraDragMove && this.lossyDataChannel) {
const cmd = command.cmd
if (cmd.type === 'camera_drag_move' && this.lossyDataChannel) {
console.log('sending lossy command', command, this.lossyDataChannel)
command.cmd.CameraDragMove.sequence = this.sequence
cmd.sequence = this.sequence
this.sequence++
this.lossyDataChannel.send(JSON.stringify(command))
return