Add demo CAD kernel Controller, and typing to suit

We might be adding JSCAD soon and adding some guidance on the happy
path with good typing for the CadKernels is a good idea

related to #411
This commit is contained in:
Kurt Hutten
2021-07-18 08:23:20 +10:00
parent 62ec8159b8
commit 21608b740a
3 changed files with 64 additions and 5 deletions

View File

@@ -5,9 +5,10 @@ import {
createUnhealthyResponse, createUnhealthyResponse,
timeoutErrorMessage, timeoutErrorMessage,
RenderArgs, RenderArgs,
DefaultKernelExport,
} from './common' } from './common'
export const render = async ({ export const render: DefaultKernelExport['render'] = async ({
code, code,
settings: { quality = 'low' }, settings: { quality = 'low' },
}: RenderArgs) => { }: RenderArgs) => {
@@ -52,7 +53,7 @@ export const render = async ({
} }
} }
const openscad = { const openscad: DefaultKernelExport = {
render, render,
// more functions to come // more functions to come
} }

View File

@@ -19,7 +19,20 @@ export interface RenderArgs {
} }
} }
export function createHealthyResponse({ date, data, consoleMessage, type }) { export interface HealthyResponse {
status: 'healthy'
message: {
type: 'message'
message: string
time: Date
}
objectData: {
data: any
type: 'stl' | 'png' | 'geometry'
}
}
export function createHealthyResponse({ date, data, consoleMessage, type }: {date: Date; data: any; consoleMessage: string, type: HealthyResponse['objectData']['type'] }): HealthyResponse {
return { return {
status: 'healthy', status: 'healthy',
objectData: { objectData: {
@@ -34,7 +47,16 @@ export function createHealthyResponse({ date, data, consoleMessage, type }) {
} }
} }
export function createUnhealthyResponse(date, message = 'network issue') { export interface ErrorResponse {
status: 'error'
message: {
type: 'error'
message: string
time: Date
}
}
export function createUnhealthyResponse(date: Date, message = 'network issue'): ErrorResponse {
// TODO handle errors better // TODO handle errors better
// I think we should display something overlayed on the viewer window something like "network issue try again" // I think we should display something overlayed on the viewer window something like "network issue try again"
// and in future I think we need timeouts differently as they maybe from a user trying to render something too complex // and in future I think we need timeouts differently as they maybe from a user trying to render something too complex
@@ -49,4 +71,8 @@ export function createUnhealthyResponse(date, message = 'network issue') {
} }
} }
export const timeoutErrorMessage = `timeout: We're currently limited a 30s execution time. You can try again, sometimes it works the second time` export const timeoutErrorMessage = `timeout: We're currently limited to a 30s execution time. You can try again, sometimes it works the second time`
export interface DefaultKernelExport {
render: (arg: RenderArgs) => Promise<HealthyResponse | ErrorResponse>
}

View File

@@ -0,0 +1,32 @@
// not an actual and there fore not imported into index.ts in this folder,
// this is boiler plate code for adding new integrations.
import {
RenderArgs,
DefaultKernelExport,
} from './common'
export const render: DefaultKernelExport['render'] = async ({
code,
settings,
}: RenderArgs) => {
// do your magic
return {
status: 'healthy',
message: {
type: 'message',
message: 'demo',
time: new Date()
},
objectData: {
data: 'any',
type: 'geometry'
}
}
}
const demoController: DefaultKernelExport = {
render,
}
export default demoController