-
Notifications
You must be signed in to change notification settings - Fork 0
/
flats.ts
36 lines (33 loc) · 1.07 KB
/
flats.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import DIE from "phpdie";
export function flats<T>() {
const emptyError = new Error(
"Flatten for empty array [] in stream is not supported yet, To fix this error, you can add a .filter(array=>array.length) stage before flat",
);
return new TransformStream<T[], T>({
transform: async (a, ctrl) => {
a.length || DIE(emptyError);
a.map((e) => ctrl.enqueue(e));
},
});
// const t = new TransformStream<T, T>(
// undefined,
// { highWaterMark: 1 },
// { highWaterMark: 0 }
// );
// const writer = t.writable.getWriter();
// const emptyError = new Error(
// "Flatten for empty array [] in stream is not supported yet"
// );
// const writable = new WritableStream<T[]>(
// {
// write: async (chunks, ctrl) => {
// chunks.length || DIE(emptyError);
// for await (const chunk of chunks) await writer.write(chunk);
// },
// close: () => writer.close(),
// abort: (reason) => writer.abort(reason),
// },
// { highWaterMark: 1 }
// );
// return { writable, readable: t.readable };
}