-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
127 additions
and
9 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
packages/core/src/test-helpers/pipeableStreamToObservable.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { PipeableStream } from "react-dom/server" | ||
import { Observable, scan } from "rxjs" | ||
import { PassThrough } from "stream" | ||
|
||
export function pipeableStreamToObservable( | ||
stream: PipeableStream, | ||
): Observable<string> { | ||
return new Observable((subscriber) => { | ||
const passthrough = new PassThrough() | ||
const sub = readStream$<string>(passthrough) | ||
.pipe(scan((acc, v) => acc + v, "")) | ||
.subscribe(subscriber) | ||
|
||
stream.pipe(passthrough) | ||
|
||
return () => { | ||
sub.unsubscribe() | ||
} | ||
}) | ||
} | ||
|
||
function readStream$<T>(stream: NodeJS.ReadableStream) { | ||
return new Observable<T>((subscriber) => { | ||
const dataHandler = (data: T) => subscriber.next(data) | ||
stream.addListener("data", dataHandler) | ||
|
||
const errorHandler = (error: any) => subscriber.error(error) | ||
stream.addListener("error", errorHandler) | ||
|
||
const closeHandler = () => subscriber.complete() | ||
stream.addListener("close", closeHandler) | ||
stream.addListener("end", closeHandler) | ||
|
||
return () => { | ||
stream.removeListener("data", dataHandler) | ||
stream.removeListener("error", errorHandler) | ||
stream.removeListener("close", closeHandler) | ||
stream.removeListener("end", closeHandler) | ||
} | ||
}) | ||
} |