Skip to content

Stanza.ts

This module provides the core Redux middleware for instantiating a Redux Sonnet. It includes analogues to redux-observable’s Epics.

Added in v0.0.0

constructors

fromEffect

Construct a Stanza from an Effect such that the output dispatches to Redux.

Signature

export declare const fromEffect: <R>(
self: Effect.Effect<Action, never, R>
) => Effect.Effect<void, never, Sonnet.SonnetService | R>

Example

import {
import Effect

@since2.0.0

@since2.0.0

@since2.0.0

Effect
} from "effect"
import {
import Stanza
Stanza
} from "redux-sonnet"
import Stanza
Stanza
.
const fromEffect: <never>(self: Effect.Effect<Action, never, never>) => Effect.Effect<void, never, SonnetService>

Construct a Stanza from an Effect such that the output dispatches to Redux.

@example

import { Effect } from "effect"
import { Stanza } from "redux-sonnet"
Stanza.fromEffect(
Effect.succeed({ type: "ACTION" })
)

@since0.0.0

fromEffect
(
import Effect

@since2.0.0

@since2.0.0

@since2.0.0

Effect
.
const succeed: <{
type: string;
}>(value: {
type: string;
}) => Effect.Effect<{
type: string;
}, never, never>

Creates an Effect that always succeeds with a given value.

When to Use

Use this function when you need an effect that completes successfully with a specific value without any errors or external dependencies.

@seefail to create an effect that represents a failure.

@example

// Title: Creating a Successful Effect
import { Effect } from "effect"
// Creating an effect that represents a successful scenario
//
// ┌─── Effect<number, never, never>
// ▼
const success = Effect.succeed(42)

@since2.0.0

succeed
({
type: string
type
: "ACTION" }))

Added in v0.0.0

fromStream

Construct a Stanza from a Stream such that each output dispatches to Redux.

Signature

export declare const fromStream: <R>(
self: Stream.Stream<Action, never, R>
) => Effect.Effect<void, never, Sonnet.SonnetService | R>

Example

import {
function pipe<A>(a: A): A (+19 overloads)

Pipes the value of an expression into a pipeline of functions.

When to Use

This is useful in combination with data-last functions as a simulation of methods:

as.map(f).filter(g)

becomes:

import { pipe, Array } from "effect"
pipe(as, Array.map(f), Array.filter(g))

Details

The pipe function is a utility that allows us to compose functions in a readable and sequential manner. It takes the output of one function and passes it as the input to the next function in the pipeline. This enables us to build complex transformations by chaining multiple functions together.

import { pipe } from "effect"
const result = pipe(input, func1, func2, ..., funcN)

In this syntax, input is the initial value, and func1, func2, ..., funcN are the functions to be applied in sequence. The result of each function becomes the input for the next function, and the final result is returned.

Here's an illustration of how pipe works:

┌───────┐ ┌───────┐ ┌───────┐ ┌───────┐ ┌───────┐ ┌────────┐
│ input │───►│ func1 │───►│ func2 │───►│ ... │───►│ funcN │───►│ result │
└───────┘ └───────┘ └───────┘ └───────┘ └───────┘ └────────┘

It's important to note that functions passed to pipe must have a single argument because they are only called with a single argument.

@example

// Example: Chaining Arithmetic Operations
import { pipe } from "effect"
// Define simple arithmetic operations
const increment = (x: number) => x + 1
const double = (x: number) => x * 2
const subtractTen = (x: number) => x - 10
// Sequentially apply these operations using `pipe`
const result = pipe(5, increment, double, subtractTen)
console.log(result)
// Output: 2

@since2.0.0

pipe
,
import Schedule
Schedule
,
import Stream
Stream
} from "effect"
import {
import Stanza
Stanza
} from "redux-sonnet"
import Stanza
Stanza
.
const fromStream: <never>(self: Stream.Stream<Action, never, never>) => Effect<void, never, SonnetService>

Construct a Stanza from a Stream such that each output dispatches to Redux.

@example

import { Stream, Schedule, pipe } from "effect"
import { Stanza } from "redux-sonnet"
Stanza.fromStream(pipe(
Stream.range(1, 3),
Stream.schedule(Schedule.spaced("1 second")),
Stream.map((i) => ({ type: `ACTION-${i}` })),
))

@since0.0.0

fromStream
(
pipe<Stream.Stream<number, never, never>, Stream.Stream<number, never, never>, Stream.Stream<{
type: string;
}, never, never>>(a: Stream.Stream<number, never, never>, ab: (a: Stream.Stream<...>) => Stream.Stream<...>, bc: (b: Stream.Stream<...>) => Stream.Stream<...>): Stream.Stream<...> (+19 overloads)

Pipes the value of an expression into a pipeline of functions.

When to Use

This is useful in combination with data-last functions as a simulation of methods:

as.map(f).filter(g)

becomes:

import { pipe, Array } from "effect"
pipe(as, Array.map(f), Array.filter(g))

Details

The pipe function is a utility that allows us to compose functions in a readable and sequential manner. It takes the output of one function and passes it as the input to the next function in the pipeline. This enables us to build complex transformations by chaining multiple functions together.

import { pipe } from "effect"
const result = pipe(input, func1, func2, ..., funcN)

In this syntax, input is the initial value, and func1, func2, ..., funcN are the functions to be applied in sequence. The result of each function becomes the input for the next function, and the final result is returned.

Here's an illustration of how pipe works:

┌───────┐ ┌───────┐ ┌───────┐ ┌───────┐ ┌───────┐ ┌────────┐
│ input │───►│ func1 │───►│ func2 │───►│ ... │───►│ funcN │───►│ result │
└───────┘ └───────┘ └───────┘ └───────┘ └───────┘ └────────┘

It's important to note that functions passed to pipe must have a single argument because they are only called with a single argument.

@example

// Example: Chaining Arithmetic Operations
import { pipe } from "effect"
// Define simple arithmetic operations
const increment = (x: number) => x + 1
const double = (x: number) => x * 2
const subtractTen = (x: number) => x - 10
// Sequentially apply these operations using `pipe`
const result = pipe(5, increment, double, subtractTen)
console.log(result)
// Output: 2

@since2.0.0

pipe
(
import Stream
Stream
.
const range: (min: number, max: number, chunkSize?: number) => Stream.Stream<number>

Constructs a stream from a range of integers, including both endpoints.

@example

import { Effect, Stream } from "effect"
// A Stream with a range of numbers from 1 to 5
const stream = Stream.range(1, 5)
// Effect.runPromise(Stream.runCollect(stream)).then(console.log)
// { _id: 'Chunk', values: [ 1, 2, 3, 4, 5 ] }

@since2.0.0

range
(1, 3),
import Stream
Stream
.
const schedule: <number, number, never, number>(schedule: Schedule.Schedule<number, number, never>) => <E, R>(self: Stream.Stream<number, E, R>) => Stream.Stream<number, E, R> (+1 overload)

Schedules the output of the stream using the provided schedule.

@since2.0.0

schedule
(
import Schedule
Schedule
.
const spaced: (duration: DurationInput) => Schedule.Schedule<number>

Returns a schedule that recurs continuously, each repetition spaced the specified duration from the last run.

@since2.0.0

spaced
("1 second")),
import Stream
Stream
.
const map: <number, {
type: string;
}>(f: (a: number) => {
type: string;
}) => <E, R>(self: Stream.Stream<number, E, R>) => Stream.Stream<{
type: string;
}, E, R> (+1 overload)

Transforms the elements of this stream using the supplied function.

@example

import { Effect, Stream } from "effect"
const stream = Stream.make(1, 2, 3).pipe(Stream.map((n) => n + 1))
// Effect.runPromise(Stream.runCollect(stream)).then(console.log)
// { _id: 'Chunk', values: [ 2, 3, 4 ] }

@since2.0.0

map
((
i: number
i
) => ({
type: string
type
: `ACTION-${
i: number
i
}` }))
)
)

Added in v0.0.0

make

Constructs a Stanza from a effect/Stream processor.

Signature

export declare const make: <R>(
processor: (
action$: Stream.Stream<Action>,
state: {
changes: Stream.Stream<any>
latest: Stream.Stream<any>
ref: SynchronizedRef.SynchronizedRef<any>
}
) => Stream.Stream<Action, never, R>
) => Stanza<Sonnet.Sonnet.Context | R>

Added in v0.0.0

models

Stanza (interface)

A Stanza is an Effect that produces void, never fails, and requires the SonnetService.

Signature

export interface Stanza<R = never>
extends
Pipeable.Pipeable,
Effect.Effect<void, never, Sonnet.SonnetService | R>
{
// XXX: need to add support for isStanza
// [TypeId]: TypeId
}

Added in v0.0.0

refinements

isStanza

Signature

export declare const isStanza: (u: unknown) => u is Stanza

Added in v0.0.0

utils

TypeId

Signature

export declare const TypeId: typeof TypeId

Added in v0.0.0

TypeId (type alias)

Signature

export type TypeId = typeof TypeId

Added in v0.0.0