Skip to content

Commit 56199ce

Browse files
committed
Moved transforms to a single file
1 parent a36382c commit 56199ce

File tree

2 files changed

+107
-122
lines changed

2 files changed

+107
-122
lines changed

packages/sse/README.md

Lines changed: 107 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Primitives for [Server-Sent Events](https://developer.mozilla.org/en-US/docs/Web
1313
- [`makeSSE`](#makesse) — Base non-reactive primitive. Creates an `EventSource` and returns a cleanup function. No Solid lifecycle.
1414
- [`createSSE`](#createsse) — Reactive primitive. Accepts a reactive URL, integrates with Solid's owner lifecycle, and returns signals for `data`, `error`, and `readyState`.
1515
- [`makeSSEWorker`](./WORKERS.md) — Runs the SSE connection inside a Web Worker or SharedWorker. See [WORKERS.md](./WORKERS.md).
16-
- [Built-in transformers](./TRANSFORMS.md)`json`, `ndjson`, `lines`, `number`, `safe`, `pipe`. See [TRANSFORMS.md](./TRANSFORMS.md).
16+
- [Built-in transformers](#built-in-transformers)`json`, `ndjson`, `lines`, `number`, `safe`, `pipe`.
1717

1818
## Installation
1919

@@ -154,19 +154,6 @@ SSEReadyState.CLOSED; // 2
154154

155155
`EventSource` has native browser-level reconnection built in. For transient network drops the browser automatically retries. The `reconnect` option in `createSSE` is for _application-level_ reconnection — it fires only when `readyState` becomes `SSEReadyState.CLOSED`, meaning the browser has given up entirely. You generally do not need `reconnect: true` for normal usage.
156156

157-
## Built-in transformers
158-
159-
Ready-made `transform` functions for the most common SSE data formats. See [TRANSFORMS.md](./TRANSFORMS.md) for full documentation and examples.
160-
161-
| Transformer | Description |
162-
| ---------------------------------------------------------------------- | --------------------------------------------------------------- |
163-
| [`json`](./TRANSFORMS.md#json) | Parse data as a single JSON value |
164-
| [`ndjson`](./TRANSFORMS.md#ndjson) | Parse newline-delimited JSON into an array |
165-
| [`lines`](./TRANSFORMS.md#lines) | Split data into a `string[]` by newline |
166-
| [`number`](./TRANSFORMS.md#number) | Parse data as a number via `Number()` |
167-
| [`safe(transform, fallback?)`](./TRANSFORMS.md#safetransform-fallback) | Fault-tolerant wrapper — returns `fallback` instead of throwing |
168-
| [`pipe(a, b)`](./TRANSFORMS.md#pipea-b) | Compose two transforms into one |
169-
170157
## Integration with `@solid-primitives/event-bus`
171158

172159
Because `bus.emit` matches the `(event: MessageEvent) => void` shape of `onMessage`, you can wire them directly:
@@ -231,6 +218,112 @@ For high-frequency streams or performance-sensitive apps you can offload the `Ev
231218

232219
See [WORKERS.md](./WORKERS.md) for setup instructions, SharedWorker usage, and the full type reference.
233220

221+
## Built-in transformers
222+
223+
Ready-made `transform` functions for the most common SSE data formats. Pass one as the `transform` option to `createSSE`:
224+
225+
```ts
226+
import { createSSE, json } from "@solid-primitives/sse";
227+
228+
const { data } = createSSE<{ status: string }>(url, { transform: json });
229+
```
230+
231+
| Transformer | Description |
232+
| --------------------------------- | --------------------------------------------------------------- |
233+
| [`json`](#json) | Parse data as a single JSON value |
234+
| [`ndjson`](#ndjson) | Parse newline-delimited JSON into an array |
235+
| [`lines`](#lines) | Split data into a `string[]` by newline |
236+
| [`number`](#number) | Parse data as a number via `Number()` |
237+
| [`safe`](#safetransform-fallback) | Fault-tolerant wrapper — returns `fallback` instead of throwing |
238+
| [`pipe`](#pipea-b) | Compose two transforms into one |
239+
240+
### `json`
241+
242+
Parse the message data as a single JSON value. Equivalent to `JSON.parse` but named for consistency with the other transformers.
243+
244+
```ts
245+
import { createSSE, json } from "@solid-primitives/sse";
246+
247+
const { data } = createSSE<{ status: string; ts: number }>(url, { transform: json });
248+
// data() === { status: "ok", ts: 1718000000 }
249+
```
250+
251+
### `ndjson`
252+
253+
Parse the message data as [newline-delimited JSON](https://ndjson.org/) (NDJSON / JSON Lines). Each non-empty line is parsed as a separate JSON value and the transformer returns an array.
254+
255+
Use this when the server batches multiple objects into one SSE event:
256+
257+
```
258+
data: {"id":1,"type":"tick"}
259+
data: {"id":2,"type":"tick"}
260+
261+
```
262+
263+
```ts
264+
import { createSSE, ndjson } from "@solid-primitives/sse";
265+
266+
const { data } = createSSE<TickEvent[]>(url, { transform: ndjson });
267+
// data() === [{ id: 1, type: "tick" }, { id: 2, type: "tick" }]
268+
```
269+
270+
### `lines`
271+
272+
Split the message data into individual lines, returning a `string[]`. Empty lines are filtered out. Useful for multi-line text events that are not JSON.
273+
274+
```ts
275+
import { createSSE, lines } from "@solid-primitives/sse";
276+
277+
const { data } = createSSE<string[]>(url, { transform: lines });
278+
// data() === ["line one", "line two"]
279+
```
280+
281+
### `number`
282+
283+
Parse the message data as a number using `Number()` semantics. Handy for streams that emit counters, progress percentages, sensor readings, or prices.
284+
285+
```ts
286+
import { createSSE, number } from "@solid-primitives/sse";
287+
288+
const { data } = createSSE<number>(url, { transform: number });
289+
// data() === 42
290+
```
291+
292+
Note: follows `Number()` coercion — an empty string becomes `0` and non-numeric strings become `NaN`.
293+
294+
### `safe(transform, fallback?)`
295+
296+
Wraps any transform in a `try/catch`. When the inner transform throws, `safe` returns `fallback` instead of propagating the error. This keeps the stream alive across malformed events.
297+
298+
```ts
299+
import { createSSE, json, number, safe } from "@solid-primitives/sse";
300+
301+
// Returns undefined on a bad event instead of throwing
302+
const { data } = createSSE<MyEvent>(url, { transform: safe(json) });
303+
304+
// With an explicit fallback value
305+
const { data } = createSSE<number>(url, { transform: safe(number, 0) });
306+
```
307+
308+
### `pipe(a, b)`
309+
310+
Composes two transforms into one: the output of `a` is passed as the input of `b`. Useful for building custom transforms from existing primitives without writing anonymous functions.
311+
312+
```ts
313+
import { createSSE, ndjson, json, safe, pipe } from "@solid-primitives/sse";
314+
315+
// Parse NDJSON then keep only "tick" rows
316+
type RawEvent = { type: string };
317+
const { data } = createSSE<RawEvent[]>(url, {
318+
transform: pipe(ndjson<RawEvent>, rows => rows.filter(r => r.type === "tick")),
319+
});
320+
321+
// Safe JSON with a post-processing step
322+
const { data } = createSSE<string>(url, {
323+
transform: pipe(safe(json<{ label: string }>), ev => ev?.label ?? ""),
324+
});
325+
```
326+
234327
## Changelog
235328

236329
See [CHANGELOG.md](./CHANGELOG.md).

packages/sse/TRANSFORMS.md

Lines changed: 0 additions & 108 deletions
This file was deleted.

0 commit comments

Comments
 (0)