Invoke vector listeners for zero-element vectors

PiperOrigin-RevId: 513662913
This commit is contained in:
Sebastian Schmidt 2023-03-02 15:55:44 -08:00 committed by Copybara-Service
parent 7664e0ef64
commit c3adf19706

View File

@ -29,10 +29,12 @@ export type EmptyPacketListener = (timestamp: number) => void;
/** /**
* A listener that receives a single element of vector-returning output packet. * A listener that receives a single element of vector-returning output packet.
* Receives one element at a time (in order). Once all elements are processed,
* the listener is invoked with `data` set to `unknown` and `done` set to true.
* Intended for internal usage. * Intended for internal usage.
*/ */
export type VectorListener<T> = export type VectorListener<T> = (data: T, done: boolean, timestamp: number) =>
(data: T, index: number, length: number, timestamp: number) => void; void;
/** /**
* Declarations for Emscripten's WebAssembly Module behavior, so TS compiler * Declarations for Emscripten's WebAssembly Module behavior, so TS compiler
@ -454,17 +456,12 @@ export class GraphRunner {
let buffer: T[] = []; let buffer: T[] = [];
this.wasmModule.simpleListeners = this.wasmModule.simpleListeners || {}; this.wasmModule.simpleListeners = this.wasmModule.simpleListeners || {};
this.wasmModule.simpleListeners[outputStreamName] = this.wasmModule.simpleListeners[outputStreamName] =
(data: unknown, index: number, length: number, timestamp: number) => { (data: unknown, done: boolean, timestamp: number) => {
// The Wasm listener gets invoked once for each element. Once we if (done) {
// receive all elements, we invoke the registered callback with
// the full array.
buffer[index] = data as T;
if (index === length - 1) {
// Invoke the user callback directly, as the Wasm layer may
// clean up the underlying data elements once we leave the scope
// of the listener.
callbackFcn(buffer, timestamp); callbackFcn(buffer, timestamp);
buffer = []; buffer = [];
} else {
buffer.push(data as T);
} }
}; };
} }