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

Function pullIntoWritableStream

packages/effect/src/Channel.ts:1783–1833  ·  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
  readonly onAbort?: ((cause: Cause.Cause<IE | E>) => void) | undefined
})

Source from the content-addressed store, hash-verified

1781
1782/** @internal */
1783export const pullIntoWritableStream = <A, IE, E>(options: {
1784 readonly pull: Pull.Pull<Arr.NonEmptyReadonlyArray<A>, IE, unknown>
1785 readonly writable: WritableStream<A>
1786 readonly onError: (error: unknown) => E
1787 readonly closeOnDone?: boolean | undefined
1788 readonly onAbort?: ((cause: Cause.Cause<IE | E>) => void) | undefined
1789}): Pull.Pull<never, IE | E, unknown> =>
1790 Effect.acquireUseRelease(
1791 Effect.sync(() => options.writable.getWriter()),
1792 (writer) => {
1793 const loop = options.pull.pipe(
1794 Effect.flatMap((chunk) =>
1795 Effect.forEach(
1796 chunk,
1797 (value) =>
1798 Effect.tryPromise({
1799 try: () => writer.ready.then(() => writer.write(value)),
1800 catch: options.onError
1801 }),
1802 { discard: true }
1803 )
1804 ),
1805 Effect.forever({ disableYield: true }),
1806 // Fail on a destination error even while an upstream pull is pending.
1807 Effect.raceFirst(Effect.andThen(
1808 Effect.tryPromise({ try: () => writer.closed, catch: options.onError }),
1809 Effect.never
1810 ))
1811 )
1812 const withClose = options.closeOnDone !== false
1813 ? Pull.catchDone(loop, (done) =>
1814 Effect.andThen(
1815 Effect.tryPromise({
1816 try: () => writer.close(),
1817 catch: options.onError
1818 }),
1819 Cause.done(done)
1820 ))
1821 : loop
1822 return Effect.onError(
1823 withClose,
1824 (cause) => {
1825 if (Pull.isDoneCause(cause)) return Effect.void
1826 const reason = unwrapUpstreamFailure(cause) as Cause.Cause<IE | E>
1827 options.onAbort?.(reason)
1828 return Effect.promise(() => writer.abort(reason).catch(constVoid))
1829 }
1830 )
1831 },
1832 (writer) => Effect.sync(() => writer.releaseLock())
1833 )
1834
1835/**
1836 * 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
pipeMethod · 0.65
forEachMethod · 0.65
writeMethod · 0.65
closeMethod · 0.65
abortMethod · 0.65
syncMethod · 0.45
doneMethod · 0.45

Tested by

no test coverage detected