MCPcopy
hub / github.com/sparkjsdev/spark / getTransferable

Function getTransferable

src/utils.ts:297–320  ·  view source on GitHub ↗
(ctx: unknown)

Source from the content-addressed store, hash-verified

295// Recursively finds all ArrayBuffers in an object and returns them as an array
296// to use as transferable objects to send between workers.
297export function getTransferable(ctx: unknown): Transferable[] {
298 const buffers: Transferable[] = [];
299 const seen = new Set();
300
301 function traverse(obj: unknown) {
302 if (obj && typeof obj === "object" && !seen.has(obj)) {
303 seen.add(obj);
304
305 if (obj instanceof ArrayBuffer) {
306 buffers.push(obj);
307 } else if (ArrayBuffer.isView(obj)) {
308 // Handles TypedArrays and DataView
309 buffers.push(obj.buffer as ArrayBuffer);
310 } else if (Array.isArray(obj)) {
311 obj.forEach(traverse);
312 } else {
313 Object.values(obj).forEach(traverse);
314 }
315 }
316 }
317
318 traverse(ctx);
319 return buffers;
320}
321
322// Create an array of the given size and initialize element with initFunction()
323export function newArray<T>(

Callers 3

callMethod · 0.90
onMessageFunction · 0.90
callMethod · 0.70

Calls 1

traverseFunction · 0.70

Tested by

no test coverage detected