MCPcopy Create free account
hub / github.com/Effect-TS/effect / pullIntoWritableStream

Function pullIntoWritableStream

packages/effect/src/Channel.ts:1670–1712  ·  view source on GitHub ↗
(options: {
  readonly pull: Pull.Pull<Arr.NonEmptyReadonlyArray<A>, IE, unknown>
  readonly writable: WritableStream<A>
  readonly onError: (error: unknown) => E
  readonly closeOnDone?: boolean | undefined
})

Source from the content-addressed store, hash-verified

1668
1669/** @internal */
1670export const pullIntoWritableStream = <A, IE, E>(options: {
1671 readonly pull: Pull.Pull<Arr.NonEmptyReadonlyArray<A>, IE, unknown>
1672 readonly writable: WritableStream<A>
1673 readonly onError: (error: unknown) => E
1674 readonly closeOnDone?: boolean | undefined
1675}): Pull.Pull<never, IE | E, unknown> =>
1676 Effect.acquireUseRelease(
1677 Effect.sync(() => options.writable.getWriter()),
1678 (writer) => {
1679 const loop = options.pull.pipe(
1680 Effect.flatMap((chunk) =>
1681 Effect.forEach(
1682 chunk,
1683 (value) =>
1684 Effect.tryPromise({
1685 try: () => writer.ready.then(() => writer.write(value)),
1686 catch: options.onError
1687 }),
1688 { discard: true }
1689 )
1690 ),
1691 Effect.forever({ disableYield: true })
1692 )
1693 const withClose = options.closeOnDone !== false
1694 ? Pull.catchDone(loop, (done) =>
1695 Effect.andThen(
1696 Effect.tryPromise({
1697 try: () => writer.close(),
1698 catch: options.onError
1699 }),
1700 Cause.done(done)
1701 ))
1702 : loop
1703 return Effect.onError(
1704 withClose,
1705 (cause) =>
1706 Pull.isDoneCause(cause)
1707 ? Effect.void
1708 : Effect.promise(() => writer.abort(cause).catch(constVoid))
1709 )
1710 },
1711 (writer) => Effect.sync(() => writer.releaseLock())
1712 )
1713
1714/**
1715 * Creates a channel that writes upstream values to a lazily supplied Web

Callers 2

fromWritableStreamFunction · 0.85
fromTransformStreamFunction · 0.85

Calls 8

onErrorMethod · 0.80
abortMethod · 0.80
pipeMethod · 0.65
forEachMethod · 0.65
writeMethod · 0.65
closeMethod · 0.65
syncMethod · 0.45
doneMethod · 0.45

Tested by

no test coverage detected