@meshinspector/meshlib-mt 3.1.3-337 → 3.1.3-429

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -80,18 +80,17 @@ const indices = new Uint32Array([
80
80
  3, 6, 2, 3, 7, 6, 0, 4, 7, 0, 7, 3, 1, 2, 6, 1, 6, 5,
81
81
  ]);
82
82
 
83
- const coords = ml.VertCoords.fromArray(positions);
84
- const tris = ml.Triangulation.fromArray(indices);
85
- const mesh = ml.Mesh.fromTriangles(coords, tris);
83
+ using coords = ml.VertCoords.fromArray(positions);
84
+ using tris = ml.Triangulation.fromArray(indices);
85
+ using mesh = ml.Mesh.fromTriangles(coords, tris);
86
86
 
87
87
  console.log('volume =', mesh.volume()); // ~8
88
-
89
- // Objects are backed by WebAssembly memory — free them explicitly.
90
- coords.delete();
91
- tris.delete();
92
- mesh.delete();
88
+ // `using` frees these WebAssembly-backed objects automatically at the end of scope
93
89
  ```
94
90
 
91
+ > `using` requires Node.js 24+ or a current browser. On older runtimes, call `.delete()`
92
+ > instead — see [Memory management](#memory-management).
93
+
95
94
  ## Using with bundlers
96
95
 
97
96
  Bundlers (Vite, webpack, Rollup) hash and relocate the sidecar `meshlib-mt.wasm`, so the module
@@ -116,15 +115,47 @@ no extra setup:
116
115
  import createMeshLib, { type Mesh } from '@meshinspector/meshlib-mt';
117
116
 
118
117
  const ml = await createMeshLib();
119
- const mesh: Mesh = ml.Mesh.fromTriangles(coords, tris);
118
+ using mesh: Mesh = ml.Mesh.fromTriangles(coords, tris);
120
119
  const { valid, distSq } = ml.findProjection(point, mesh);
121
120
  ```
122
121
 
123
122
  ## Memory management
124
123
 
125
124
  Values returned from the API (meshes, bit sets, settings, result objects, …) hold
126
- WebAssembly memory that the JavaScript garbage collector does not reclaim. Call
127
- `.delete()` on them when you are done to avoid leaks.
125
+ WebAssembly memory that the JavaScript garbage collector does not reclaim, so each one
126
+ must be freed explicitly.
127
+
128
+ The preferred way is JavaScript's explicit resource management: declare a handle with
129
+ `using` and it is freed automatically when its scope ends — even if an exception is thrown.
130
+
131
+ ```js
132
+ using mesh = ml.Mesh.fromTriangles(coords, tris);
133
+ // ... use mesh; it is freed at the end of this scope
134
+ ```
135
+
136
+ When the number of handles is dynamic (for example built in a loop), collect them in a
137
+ `DisposableStack`, which frees everything it holds, in reverse order, at the end of the scope:
138
+
139
+ ```js
140
+ using stack = new DisposableStack();
141
+ for (const path of inputPaths) {
142
+ const cloud = stack.use(ml.PointsLoad.fromAnySupportedFormat(path));
143
+ // ... use cloud
144
+ }
145
+ // every handle passed to stack.use(...) is freed here
146
+ ```
147
+
148
+ `using` and `DisposableStack` are part of JavaScript's Explicit Resource Management,
149
+ available in Node.js 24+ and current browsers. On older runtimes and browsers, call
150
+ `.delete()` on each object when you are done instead:
151
+
152
+ ```js
153
+ const mesh = ml.Mesh.fromTriangles(coords, tris);
154
+ // ... use mesh
155
+ mesh.delete();
156
+ ```
157
+
158
+ See also, on MDN: [`using`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/using) and [`DisposableStack`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DisposableStack).
128
159
 
129
160
  ## License
130
161