# Parsers

Lower-level parse* helpers if you need to feed the engine yourself.


The `parse*` helpers sit one layer below `connect*`. They accept an `AsyncIterable<string>` (raw JSON chunks from `fetch`, `Response.body`, SSE, etc.) and yield typed values. Pipe their output into the connect helpers.

## `parseElementStream<TRow>(stream)`

Source emits a streaming JSON array. The parser yields each element as it parses — you don't wait for the array to close.

```ts
import { parseElementStream } from "@pretable/stream-adapter";

const res = await fetch("/api/events");
for await (const row of parseElementStream<Row>(res.body)) {
  console.log("got row", row);
}
```

In the connect-element pattern:

```ts
import {
  connectElementStream,
  parseElementStream,
} from "@pretable/stream-adapter";

const res = await fetch("/api/events");
connectElementStream<Row>(grid, parseElementStream<Row>(res.body));
```

## `parsePartialStream<TRow>(stream)`

Source emits a streaming JSON object. The parser yields successive `Partial<TRow>` snapshots as keys complete.

```ts
import { parsePartialStream } from "@pretable/stream-adapter";

const res = await fetch("/api/single-row-events");
for await (const partial of parsePartialStream<Row>(res.body)) {
  console.log("snapshot so far", partial);
}
```

In the connect-partial pattern:

```ts
import {
  connectPartialStream,
  parsePartialStream,
} from "@pretable/stream-adapter";

const res = await fetch("/api/single-row-events");
connectPartialStream<Row>(grid, parsePartialStream<Row>(res.body), {
  rowId: "row-001",
});
```

## Errors

Both parsers throw on malformed JSON. Wrap the consumer (or the connect call) in a try/catch and inspect via the `done` promise:

```ts
const conn = connectElementStream(grid, parseElementStream(res.body));
conn.done.catch((err) => console.error("stream failed:", err));
```
