feat: advance FreeCAD parity and native OCCT history

This commit is contained in:
2026-08-03 08:04:10 -04:00
parent 2ce261b982
commit e5a5d74dbc
32 changed files with 1883 additions and 30 deletions

View File

@@ -0,0 +1,111 @@
import type { NativeOcctHistoryResponse, NativeOcctHistoryStepProvider } from './nativeHistoryProvider'
import { NATIVE_OCCT_HISTORY_PROTOCOL_VERSION, type NativeOcctHistoryCapabilities, type NativeOcctHistoryProvider, type NativeOcctHistoryProtocolResponse, type NativeOcctHistoryRequest } from './nativeHistoryProtocol'
type WorkerLike = Pick<Worker, 'postMessage' | 'terminate'> & {
addEventListener(type: 'message', listener: (event: MessageEvent) => void): void
addEventListener(type: 'error', listener: (event: ErrorEvent) => void): void
removeEventListener(type: 'message', listener: (event: MessageEvent) => void): void
removeEventListener(type: 'error', listener: (event: ErrorEvent) => void): void
}
type WorkerResponse = { type: 'ready'; capabilities: NativeOcctHistoryCapabilities } | { type: 'response'; response: NativeOcctHistoryProtocolResponse } | { type: 'error'; requestId?: string; error: string }
const abortError = () => new DOMException('Native OCCT history Worker request cancelled.', 'AbortError')
export type NativeOcctHistoryWorkerOptions = {
moduleUrl?: string
initializationTimeoutMs?: number
workerFactory?: () => WorkerLike
}
const defaultWorkerFactory = () => new Worker(new URL('./nativeHistoryWorkerEntry.ts', import.meta.url), { type: 'module', name: 'occt-native-history' })
export class NativeOcctHistoryWorkerProvider implements NativeOcctHistoryProvider {
private readonly worker: WorkerLike
private readonly options: Required<Pick<NativeOcctHistoryWorkerOptions, 'moduleUrl' | 'initializationTimeoutMs'>>
private readonly pending = new Map<string, { resolve: (response: NativeOcctHistoryProtocolResponse) => void; reject: (error: Error) => void }>()
private initialization: Promise<NativeOcctHistoryCapabilities> | null = null
private initializationReject: ((error: Error) => void) | null = null
private current: NativeOcctHistoryCapabilities = { providerId: 'occt-native.history-step', providerVersion: 'unavailable', occtVersion: 'unknown', availability: 'unavailable', operations: [], transport: 'step-text', reason: 'Native OCCT history Worker has not been initialized.' }
private readonly onMessage = (event: MessageEvent<WorkerResponse>) => {
if (event.data.type === 'ready') {
this.current = event.data.capabilities
return
}
if (event.data.type === 'error') {
const requestId = event.data.requestId
const pending = requestId ? this.pending.get(requestId) : null
if (pending && requestId) { this.pending.delete(requestId); pending.reject(new Error(event.data.error)) }
if (!requestId) this.initializationReject?.(new Error(event.data.error))
return
}
const pending = this.pending.get(event.data.response.requestId)
if (!pending) return
this.pending.delete(event.data.response.requestId)
pending.resolve(event.data.response)
}
private readonly onError = (event: ErrorEvent) => {
const error = new Error(event.message || 'Native OCCT history Worker failed.')
for (const pending of this.pending.values()) pending.reject(error)
this.pending.clear()
this.initialization = null
this.initializationReject?.(error)
this.initializationReject = null
this.current = { ...this.current, availability: 'unavailable', reason: error.message }
}
constructor(options: NativeOcctHistoryWorkerOptions = {}) {
this.worker = (options.workerFactory || defaultWorkerFactory)()
this.options = {
moduleUrl: options.moduleUrl || '/native/occt-history/bitbybit-occt-history.js',
initializationTimeoutMs: options.initializationTimeoutMs ?? 120_000,
}
this.worker.addEventListener('message', this.onMessage)
this.worker.addEventListener('error', this.onError)
}
capabilities(): NativeOcctHistoryCapabilities { return { ...this.current, operations: [...this.current.operations] } }
initialize(): Promise<NativeOcctHistoryCapabilities> {
if (this.current.availability === 'available') return Promise.resolve(this.capabilities())
if (this.initialization) return this.initialization
this.initialization = new Promise<NativeOcctHistoryCapabilities>((resolve, reject) => {
let settled = false
const timer = setTimeout(() => fail(new Error('Native OCCT history Worker initialization timed out.')), this.options.initializationTimeoutMs)
const fail = (error: Error) => { if (settled) return; settled = true; clearTimeout(timer); this.worker.removeEventListener('message', waitForReady); this.initialization = null; this.initializationReject = null; this.current = { ...this.current, reason: error.message }; reject(error) }
this.initializationReject = fail
const waitForReady = (event: MessageEvent<WorkerResponse>) => { if (event.data.type === 'error') return fail(new Error(event.data.error)); if (event.data.type !== 'ready') return; settled = true; clearTimeout(timer); this.worker.removeEventListener('message', waitForReady); this.initializationReject = null; resolve(this.capabilities()) }
this.worker.addEventListener('message', waitForReady)
this.worker.postMessage({ type: 'initialize', moduleUrl: this.options.moduleUrl })
})
return this.initialization
}
async capture(request: NativeOcctHistoryRequest, signal: AbortSignal): Promise<NativeOcctHistoryProtocolResponse> {
if (request.protocolVersion !== NATIVE_OCCT_HISTORY_PROTOCOL_VERSION) throw new RangeError(`Unsupported native OCCT history protocol version: ${request.protocolVersion}.`)
await this.initialize()
if (signal.aborted) throw abortError()
const response = new Promise<NativeOcctHistoryProtocolResponse>((resolve, reject) => {
this.pending.set(request.requestId, { resolve, reject })
this.worker.postMessage({ type: 'capture', request })
})
const cancel = () => { this.worker.postMessage({ type: 'cancel', requestId: request.requestId }); this.pending.get(request.requestId)?.reject(abortError()); this.pending.delete(request.requestId) }
signal.addEventListener('abort', cancel, { once: true })
try { return await response } finally { signal.removeEventListener('abort', cancel) }
}
dispose() {
this.worker.postMessage({ type: 'dispose' })
this.worker.removeEventListener('message', this.onMessage)
this.worker.removeEventListener('error', this.onError)
this.worker.terminate()
this.initializationReject?.(new Error('Native OCCT history Worker disposed.'))
this.initializationReject = null
for (const pending of this.pending.values()) pending.reject(new Error('Native OCCT history Worker disposed.'))
this.pending.clear()
this.initialization = null
}
}
export type NativeOcctHistoryWorkerModule = NativeOcctHistoryStepProvider & { default?: never }
export type NativeOcctHistoryWorkerResponse = NativeOcctHistoryResponse