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

Function makeChannel

packages/effect/src/encoding/Xml.ts:925–973  ·  view source on GitHub ↗
(
  make: () => Machine<In, Out>
)

Source from the content-addressed store, hash-verified

923}
924
925const makeChannel = <In, Out, IE, Done>(
926 make: () => Machine<In, Out>
927): Channel.Channel<
928 Arr.NonEmptyReadonlyArray<Out>,
929 XmlError | IE,
930 Done,
931 Arr.NonEmptyReadonlyArray<In>,
932 IE,
933 Done
934> =>
935 Channel.fromTransform((upstream: Pull.Pull<Arr.NonEmptyReadonlyArray<In>, IE, Done>, _scope) =>
936 Effect.sync(() => {
937 const machine = make()
938 let done: Pull.Pull<never, never, Done> | undefined
939 let failure: XmlError | undefined
940
941 // Output completed before an error is emitted before the failure
942 const emit = (
943 inputs: ReadonlyArray<In> | undefined
944 ): Pull.Pull<Arr.NonEmptyReadonlyArray<Out>, XmlError | IE, Done> => {
945 try {
946 if (inputs === undefined) machine.end()
947 else for (let i = 0; i < inputs.length; i++) machine.push(inputs[i])
948 } catch (error) {
949 if (!isXmlError(error)) throw error
950 failure = error
951 }
952 const out = machine.out
953 if (Arr.isArrayNonEmpty(out)) {
954 machine.out = []
955 return Effect.succeed(out)
956 }
957 return pull
958 }
959
960 const step = Pull.matchEffect(upstream, {
961 onSuccess: emit,
962 onFailure: Effect.failCause,
963 onDone: (value) => {
964 done = Cause.done(value)
965 return emit(undefined)
966 }
967 })
968 const pull: Pull.Pull<Arr.NonEmptyReadonlyArray<Out>, XmlError | IE, Done> = Effect.suspend(() =>
969 failure !== undefined ? Effect.fail(failure) : done ?? step
970 )
971 return pull
972 })
973 )
974
975const makeParser = (options: DecodeOptions | undefined, bytes: boolean): () => Parser => {
976 const maxTokenSize = checkLimit("maxTokenSize", options?.maxTokenSize, defaultMaxTokenSize)

Callers 5

decodeStringFunction · 0.70
decodeFunction · 0.70
elementsFunction · 0.70
encodeStringFunction · 0.70
encodeFunction · 0.70

Calls 5

emitFunction · 0.70
failMethod · 0.65
makeFunction · 0.50
syncMethod · 0.45
doneMethod · 0.45

Tested by

no test coverage detected