From 2a5e8cadd446fb0e6357b490752edb779070a7df Mon Sep 17 00:00:00 2001 From: Paul Elliott Date: Mon, 19 Oct 2026 11:36:51 -0400 Subject: [PATCH] refactor(macros)!: remove deprecated typed-array registry exports BREAKING CHANGE: TYPED_ARRAYS is no longer a named macros export or a property of the default macros object. Use native typed-array constructors, newTypedArray, or newTypedArrayFrom instead. --- BREAKING_CHANGES.md | 1 + Sources/Common/Core/DataArray/index.d.ts | 2 +- Sources/Common/DataModel/Planes/index.js | 8 +++----- Sources/Common/DataModel/Triangle/index.js | 4 ++-- Sources/Testing/testMacro.js | 20 ++++++++++++++++++++ Sources/macros.d.ts | 13 ++++++------- Sources/macros.js | 3 +-- 7 files changed, 34 insertions(+), 17 deletions(-) diff --git a/BREAKING_CHANGES.md b/BREAKING_CHANGES.md index 6aaac730201..38ca533713f 100644 --- a/BREAKING_CHANGES.md +++ b/BREAKING_CHANGES.md @@ -3,6 +3,7 @@ - **TypeScript declarations**: expanded coverage and corrected types may expose build errors. Remove conflicting local declaration shims and fix code rejected by the new types. - **TypeScript libraries**: typed-array factories now declare BigInt support. Add `ES2020.BigInt` if your `lib` list does not include `ES2020` or later. Your `target` can stay unchanged. - **Accessor types**: `setXFrom` methods and setters that return no value now declare `void`. Getter types include `null` or `undefined` where applicable. +- **`TYPED_ARRAYS` removed.** The named export and `macro.TYPED_ARRAYS` property are removed. Use native typed-array constructors, `newTypedArray(typeName, ...args)`, or `newTypedArrayFrom(typeName, values)`. Unknown or unavailable constructors still fall back to `Float64Array`. - **`Point` type removed.** Import `Vector3` from `@kitware/vtk.js/types` instead. - **vtkVolumeProperty**: replace `setAverageIPScalarRange(min, max)` with `setIpScalarRange(min, max)`. - **vtkOpenGLFramebuffer**: replace `getColorTexture()` with `getColorBuffers()[0]`. diff --git a/Sources/Common/Core/DataArray/index.d.ts b/Sources/Common/Core/DataArray/index.d.ts index 00f5cf485b5..fe82a717360 100644 --- a/Sources/Common/Core/DataArray/index.d.ts +++ b/Sources/Common/Core/DataArray/index.d.ts @@ -465,7 +465,7 @@ declare function createRangeHelper(): vtkRangeHelper; * * ```js * const isFloat32 = ('Float32Array' === getDataType(array)); - * const clone = new macro.TYPED_ARRAYS[getDataType(array)](array.length); + * const clone = macro.newTypedArray(getDataType(array), array.length); * ``` * * @param typedArray to extract its type from diff --git a/Sources/Common/DataModel/Planes/index.js b/Sources/Common/DataModel/Planes/index.js index 917fcbcd305..73ca7f05a23 100644 --- a/Sources/Common/DataModel/Planes/index.js +++ b/Sources/Common/DataModel/Planes/index.js @@ -1,4 +1,4 @@ -import macro, { TYPED_ARRAYS } from 'vtk.js/Sources/macros'; +import macro from 'vtk.js/Sources/macros'; import vtkDataArray from 'vtk.js/Sources/Common/Core/DataArray'; import vtkImplicitFunction from 'vtk.js/Sources/Common/DataModel/ImplicitFunction'; import vtkMath from 'vtk.js/Sources/Common/Core/Math'; @@ -17,10 +17,8 @@ function vtkPlanes(publicAPI, model) { model.classHierarchy.push('vtkPlanes'); // Initialize internal variables - model.planes = - model.planes || macro.newTypedArray(TYPED_ARRAYS.Float64Array, 24); - model.bounds = - model.bounds || macro.newTypedArray(TYPED_ARRAYS.Float64Array, 6); + model.planes = model.planes || new Float64Array(24); + model.bounds = model.bounds || new Float64Array(6); model.plane = vtkPlane.newInstance(); diff --git a/Sources/Common/DataModel/Triangle/index.js b/Sources/Common/DataModel/Triangle/index.js index b6966f0cf59..acbf483a659 100644 --- a/Sources/Common/DataModel/Triangle/index.js +++ b/Sources/Common/DataModel/Triangle/index.js @@ -1,4 +1,4 @@ -import macro, { TYPED_ARRAYS } from 'vtk.js/Sources/macros'; +import macro from 'vtk.js/Sources/macros'; import vtkCell from 'vtk.js/Sources/Common/DataModel/Cell'; import * as vtkMath from 'vtk.js/Sources/Common/Core/Math'; import vtkLine from 'vtk.js/Sources/Common/DataModel/Line'; @@ -670,7 +670,7 @@ function vtkTriangle(publicAPI, model) { const J = [v1[0] - v0[0], v1[1] - v0[1], v2[0] - v0[0], v2[1] - v0[1]]; // Compute inverse Jacobian (expects flat array) - const JI = macro.newTypedArray(TYPED_ARRAYS.Float64Array, 4); + const JI = new Float64Array(4); vtkMath.invertMatrix(J, JI, 2); // returns flat array [JI00, JI01, JI10, JI11] // Compute derivatives diff --git a/Sources/Testing/testMacro.js b/Sources/Testing/testMacro.js index 38ffe671f4a..f593551c604 100644 --- a/Sources/Testing/testMacro.js +++ b/Sources/Testing/testMacro.js @@ -1,5 +1,25 @@ import { it, expect } from 'vitest'; import macro from 'vtk.js/Sources/macros'; +import * as macros from 'vtk.js/Sources/macros'; + +it('Typed-array factories work without a public constructor registry', () => { + expect(macros).not.toHaveProperty('TYPED_ARRAYS'); + expect(macro).not.toHaveProperty('TYPED_ARRAYS'); + + const values = macro.newTypedArray('Float64Array', 3); + expect(values).toBeInstanceOf(Float64Array); + expect(Array.from(values)).toEqual([0, 0, 0]); + + const copy = macros.newTypedArrayFrom('Uint16Array', [1, 2, 3]); + expect(copy).toBeInstanceOf(Uint16Array); + expect(Array.from(copy)).toEqual([1, 2, 3]); + + expect(macro.newTypedArray('UnknownArray', 2)).toBeInstanceOf(Float64Array); + const object = {}; + macro.obj(object, { values: copy }); + expect(object.getState().values).toEqual([1, 2, 3]); + object.delete(); +}); const MY_ENUM = { ZERO: 0, diff --git a/Sources/macros.d.ts b/Sources/macros.d.ts index c52088c0c5c..bc7428dce63 100644 --- a/Sources/macros.d.ts +++ b/Sources/macros.d.ts @@ -64,7 +64,7 @@ export function vtkOnceErrorMacro(str: string): void; /** * A way to create typed array based on its name without using the window namespace */ -export const TYPED_ARRAYS: { +type TypedArrayConstructors = { Float32Array: typeof Float32Array; Float64Array: typeof Float64Array; Uint8Array: typeof Uint8Array; @@ -79,10 +79,10 @@ export const TYPED_ARRAYS: { }; // Unavailable optional constructors use the Float64Array fallback. -type TypedArrayForName = { +type TypedArrayForName = { [K in T]: - | InstanceType> - | ({} extends Pick ? Float64Array : never); + | InstanceType> + | ({} extends Pick ? Float64Array : never); }[T]; /** @@ -90,7 +90,7 @@ type TypedArrayForName = { * @param type name of the typed array constructor (e.g. 'Float32Array') * @param args arguments forwarded to the typed array constructor */ -export function newTypedArray( +export function newTypedArray( type: K, ...args: any[] ): TypedArrayForName; @@ -104,7 +104,7 @@ export function newTypedArray( * @param type name of the typed array constructor (e.g. 'Float32Array') * @param args arguments forwarded to the typed array's `from()` method */ -export function newTypedArrayFrom( +export function newTypedArrayFrom( type: K, ...args: any[] ): TypedArrayForName; @@ -802,7 +802,6 @@ declare const Macro: { setLoggerFunction: typeof setLoggerFunction; throttle: typeof throttle; traverseInstanceTree: typeof traverseInstanceTree; - TYPED_ARRAYS: typeof TYPED_ARRAYS; uncapitalize: typeof uncapitalize; VOID: typeof VOID; vtkDebugMacro: typeof vtkDebugMacro; diff --git a/Sources/macros.js b/Sources/macros.js index b61068b88bb..6eeb4f67a70 100644 --- a/Sources/macros.js +++ b/Sources/macros.js @@ -94,7 +94,7 @@ export function vtkOnceErrorMacro(str) { // TypedArray // ---------------------------------------------------------------------------- -export const TYPED_ARRAYS = Object.create(null); +const TYPED_ARRAYS = Object.create(null); TYPED_ARRAYS.Float32Array = Float32Array; TYPED_ARRAYS.Float64Array = Float64Array; TYPED_ARRAYS.Uint8Array = Uint8Array; @@ -1818,7 +1818,6 @@ export default { setLoggerFunction, throttle, traverseInstanceTree, - TYPED_ARRAYS, // deprecated todo remove on breaking API revision uncapitalize, VOID, vtkDebugMacro,