feat: advance FreeCAD parity and native OCCT history
This commit is contained in:
49
src/facade/nativeHistoryWorkerEntry.ts
Normal file
49
src/facade/nativeHistoryWorkerEntry.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
/// <reference lib="webworker" />
|
||||
|
||||
import type { NativeOcctHistoryStepProvider } from './nativeHistoryProvider'
|
||||
import type { NativeOcctHistoryCapabilities, NativeOcctHistoryRequest, NativeOcctHistoryProtocolResponse } from './nativeHistoryProtocol'
|
||||
|
||||
type WorkerRequest = { type: 'initialize'; moduleUrl: string } | { type: 'capture'; request: NativeOcctHistoryRequest } | { type: 'cancel'; requestId: string } | { type: 'dispose' }
|
||||
type WorkerResponse = { type: 'ready'; capabilities: NativeOcctHistoryCapabilities } | { type: 'response'; response: NativeOcctHistoryProtocolResponse } | { type: 'error'; requestId?: string; error: string }
|
||||
|
||||
const scope = self as DedicatedWorkerGlobalScope
|
||||
let provider: NativeOcctHistoryStepProvider | null = null
|
||||
const cancelled = new Set<string>()
|
||||
|
||||
const send = (message: WorkerResponse) => scope.postMessage(message)
|
||||
|
||||
const initialize = async (moduleUrl: string) => {
|
||||
const imported = await import(/* @vite-ignore */ moduleUrl) as { default?: () => Promise<NativeOcctHistoryStepProvider> }
|
||||
if (typeof imported.default !== 'function') throw new Error('Native OCCT history module has no default Emscripten factory export.')
|
||||
provider = await imported.default()
|
||||
send({
|
||||
type: 'ready',
|
||||
capabilities: {
|
||||
providerId: 'occt-native.history-step',
|
||||
providerVersion: '8.0.0-embind',
|
||||
occtVersion: provider.occtVersion(),
|
||||
availability: 'available',
|
||||
operations: ['fuse', 'cut', 'common'],
|
||||
transport: 'step-text',
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
scope.onmessage = ({ data }: MessageEvent<WorkerRequest>) => {
|
||||
void (async () => {
|
||||
try {
|
||||
if (data.type === 'initialize') return await initialize(data.moduleUrl)
|
||||
if (data.type === 'dispose') { provider = null; scope.close(); return }
|
||||
if (data.type === 'cancel') { cancelled.add(data.requestId); return }
|
||||
if (!provider) throw new Error('Native OCCT history Worker is not initialized.')
|
||||
if (cancelled.delete(data.request.requestId)) return
|
||||
const history = provider.booleanHistoryFromStep(data.request.objectStep, data.request.toolStep, data.request.operation)
|
||||
if (cancelled.delete(data.request.requestId)) return
|
||||
send({ type: 'response', response: { protocolVersion: data.request.protocolVersion, requestId: data.request.requestId, documentId: data.request.documentId, documentVersion: data.request.documentVersion, operationId: data.request.operationId, provider: { providerId: 'occt-native.history-step', providerVersion: '8.0.0-embind', occtVersion: history.occtVersion, availability: 'available', operations: ['fuse', 'cut', 'common'], transport: 'step-text' }, history } })
|
||||
} catch (error) {
|
||||
send({ type: 'error', requestId: data.type === 'capture' ? data.request.requestId : undefined, error: error instanceof Error ? error.message : String(error) })
|
||||
}
|
||||
})()
|
||||
}
|
||||
|
||||
export {}
|
||||
Reference in New Issue
Block a user