<Err, Done>(encoding?: string, options?: TextDecoderOptions): Channel<
Arr.NonEmptyReadonlyArray<string>,
Err,
Done,
Arr.NonEmptyReadonlyArray<Uint8Array>,
Err,
Done
>Decodes incoming Uint8Array chunks into strings using TextDecoder.
Details
Input chunks are decoded with streaming enabled so multi-byte characters may
span Uint8Array boundaries. The optional encoding and options are
passed to TextDecoder.
export const const decodeText: <Err, Done>(
encoding?: string,
options?: TextDecoderOptions
) => Channel<
Arr.NonEmptyReadonlyArray<string>,
Err,
Done,
Arr.NonEmptyReadonlyArray<Uint8Array>,
Err,
Done
>
Decodes incoming Uint8Array chunks into strings using TextDecoder.
Details
Input chunks are decoded with streaming enabled so multi-byte characters may
span Uint8Array boundaries. The optional encoding and options are
passed to TextDecoder.
decodeText = <function (type parameter) Err in <Err, Done>(encoding?: string, options?: TextDecoderOptions): Channel<Arr.NonEmptyReadonlyArray<string>, Err, Done, Arr.NonEmptyReadonlyArray<Uint8Array>, Err, Done>Err, function (type parameter) Done in <Err, Done>(encoding?: string, options?: TextDecoderOptions): Channel<Arr.NonEmptyReadonlyArray<string>, Err, Done, Arr.NonEmptyReadonlyArray<Uint8Array>, Err, Done>Done>(encoding: string | undefinedencoding?: string, options: TextDecoderOptions | undefinedoptions?: TextDecoderOptions): interface Channel<out OutElem, out OutErr = never, out OutDone = void, in InElem = unknown, in InErr = unknown, in InDone = unknown, out Env = never>A Channel is a nexus of I/O operations, which supports both reading and
writing. A channel may read values of type InElem and write values of type
OutElem. When the channel finishes, it yields a value of type OutDone. A
channel may fail with a value of type OutErr.
Details
Channels are the foundation of Streams: both streams and sinks are built on
channels. Most users shouldn't have to use channels directly, as streams and
sinks are much more convenient and cover all common use cases. However, when
adding new stream and sink operators, or doing something highly specialized,
it may be useful to use channels directly.
Channels compose in a variety of ways:
- Piping: One channel can be piped to another channel, assuming the
input type of the second is the same as the output type of the first.
- Sequencing: The terminal value of one channel can be used to create
another channel, and both the first channel and the function that makes
the second channel can be composed into a channel.
- Concatenating: The output of one channel can be used to create other
channels, which are all concatenated together. The first channel and the
function that makes the other channels can be composed into a channel.
Example (Typing channels)
import type { Channel } from "effect"
// A channel that outputs numbers and requires no environment
type NumberChannel = Channel.Channel<number>
// A channel that outputs strings, can fail with Error, completes with boolean
type StringChannel = Channel.Channel<string, Error, boolean>
// A channel with all type parameters specified
type FullChannel = Channel.Channel<
string, // OutElem - output elements
Error, // OutErr - output errors
number, // OutDone - completion value
number, // InElem - input elements
string, // InErr - input errors
boolean, // InDone - input completion
{ db: string } // Env - required environment
>
Channel<
import ArrArr.type Arr.NonEmptyReadonlyArray = /*unresolved*/ anyNonEmptyReadonlyArray<string>,
function (type parameter) Err in <Err, Done>(encoding?: string, options?: TextDecoderOptions): Channel<Arr.NonEmptyReadonlyArray<string>, Err, Done, Arr.NonEmptyReadonlyArray<Uint8Array>, Err, Done>Err,
function (type parameter) Done in <Err, Done>(encoding?: string, options?: TextDecoderOptions): Channel<Arr.NonEmptyReadonlyArray<string>, Err, Done, Arr.NonEmptyReadonlyArray<Uint8Array>, Err, Done>Done,
import ArrArr.type Arr.NonEmptyReadonlyArray = /*unresolved*/ anyNonEmptyReadonlyArray<interface Uint8Array<TArrayBuffer extends ArrayBufferLike = ArrayBufferLike>A typed array of 8-bit unsigned integer values. The contents are initialized to 0. If the
requested number of bytes could not be allocated an exception is raised.
Uint8Array>,
function (type parameter) Err in <Err, Done>(encoding?: string, options?: TextDecoderOptions): Channel<Arr.NonEmptyReadonlyArray<string>, Err, Done, Arr.NonEmptyReadonlyArray<Uint8Array>, Err, Done>Err,
function (type parameter) Done in <Err, Done>(encoding?: string, options?: TextDecoderOptions): Channel<Arr.NonEmptyReadonlyArray<string>, Err, Done, Arr.NonEmptyReadonlyArray<Uint8Array>, Err, Done>Done
> =>
const fromTransform: <
OutElem,
OutErr,
OutDone,
InElem,
InErr,
InDone,
EX,
EnvX,
Env
>(
transform: (
upstream: Pull.Pull<InElem, InErr, InDone>,
scope: Scope.Scope
) => Effect.Effect<
Pull.Pull<OutElem, OutErr, OutDone, EnvX>,
EX,
Env
>
) => Channel<
OutElem,
Pull.ExcludeDone<OutErr> | EX,
OutDone,
InElem,
InErr,
InDone,
Env | EnvX
>
Creates a Channel from a transformation function that operates on upstream pulls.
Example (Creating channels from transforms)
import { Channel, Effect } from "effect"
const channel = Channel.fromTransform((upstream, scope) =>
Effect.succeed(upstream)
)
fromTransform((upstream: Pull.Pull<
readonly [
Uint8Array<ArrayBufferLike>,
...Uint8Array<ArrayBufferLike>[]
],
Err,
Done,
never
>
(parameter) upstream: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
upstream, _scope: Scope.Scope(parameter) _scope: {
strategy: "sequential" | "parallel";
state: State.Open | State.Closed | State.Empty;
}
_scope) =>
import EffectEffect.const sync: <A>(
thunk: LazyArg<A>
) => Effect<A>
Creates an Effect that represents a synchronous side-effectful computation.
When to use
Use when you need to wrap a synchronous side-effectful operation that is not
expected to throw.
Details
The provided function is evaluated lazily when the effect runs.
Gotchas
The function must not throw. If it throws, the thrown value is treated as a
defect, not as a typed failure. Use try when throwing is expected.
Example (Capturing synchronous logging in an Effect)
import { Effect } from "effect"
const log = (message: string) =>
Effect.sync(() => {
console.log(message) // side effect
})
// ┌─── Effect<void, never, never>
// ▼
const program = log("Hello, World!")
sync(() => {
const const decoder: TextDecoderdecoder = new var TextDecoder: new (
label?: string,
options?: TextDecoderOptions
) => TextDecoder
The TextDecoder interface represents a decoder for a specific text encoding, such as UTF-8, ISO-8859-2, KOI8-R, GBK, etc.
TextDecoder(encoding: string | undefinedencoding, options: TextDecoderOptions | undefinedoptions)
const const streamOptions: {
stream: boolean
}
streamOptions = { stream: booleanstream: true }
return import EffectEffect.const map: {
<A, B>(f: (a: A) => B): <E, R>(
self: Effect<A, E, R>
) => Effect<B, E, R>
<A, E, R, B>(
self: Effect<A, E, R>,
f: (a: A) => B
): Effect<B, E, R>
}
map(upstream: Pull.Pull<
readonly [
Uint8Array<ArrayBufferLike>,
...Uint8Array<ArrayBufferLike>[]
],
Err,
Done,
never
>
(parameter) upstream: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
upstream, import ArrArr.map((line: anyline) => const decoder: TextDecoderdecoder.TextDecoder.decode(input?: AllowSharedBufferSource, options?: TextDecodeOptions): stringThe TextDecoder.decode() method returns a string containing text decoded from the buffer passed as a parameter.
decode(line: anyline, const streamOptions: {
stream: boolean
}
streamOptions)))
})
)