Sonnets immediately execute the provided root Stanza in a forking
manner. Stanzas must be declared as part of Sonnet instantiation.
import{
import Effect
@since ― 2.0.0
@since ― 2.0.0
@since ― 2.0.0
Effect}from"effect"
import{
import Sonnet
Sonnet}from"redux-sonnet"
declareconst
const initialize: Effect.Effect<void,never,never>
initialize:
import Effect
@since ― 2.0.0
@since ― 2.0.0
@since ― 2.0.0
Effect.
interfaceEffect<out A,out E =never,out R =never>
The Effect interface defines a value that describes a workflow or job,
which can succeed or fail.
Details
The Effect interface represents a computation that can model a workflow
involving various types of operations, such as synchronous, asynchronous,
concurrent, and parallel interactions. It operates within a context of type
R, and the result can either be a success with a value of type A or a
failure with an error of type E. The Effect is designed to handle complex
interactions with external resources, offering advanced features such as
fiber-based concurrency, scheduling, interruption handling, and scalability.
This makes it suitable for tasks that require fine-grained control over
concurrency and error management.
To execute an Effect value, you need a Runtime, which provides the
environment necessary to run and manage the computation.
The Effect interface defines a value that describes a workflow or job,
which can succeed or fail.
Details
The Effect interface represents a computation that can model a workflow
involving various types of operations, such as synchronous, asynchronous,
concurrent, and parallel interactions. It operates within a context of type
R, and the result can either be a success with a value of type A or a
failure with an error of type E. The Effect is designed to handle complex
interactions with external resources, offering advanced features such as
fiber-based concurrency, scheduling, interruption handling, and scalability.
This makes it suitable for tasks that require fine-grained control over
concurrency and error management.
To execute an Effect value, you need a Runtime, which provides the
environment necessary to run and manage the computation.
The Effect interface defines a value that describes a workflow or job,
which can succeed or fail.
Details
The Effect interface represents a computation that can model a workflow
involving various types of operations, such as synchronous, asynchronous,
concurrent, and parallel interactions. It operates within a context of type
R, and the result can either be a success with a value of type A or a
failure with an error of type E. The Effect is designed to handle complex
interactions with external resources, offering advanced features such as
fiber-based concurrency, scheduling, interruption handling, and scalability.
This makes it suitable for tasks that require fine-grained control over
concurrency and error management.
To execute an Effect value, you need a Runtime, which provides the
environment necessary to run and manage the computation.
@since ― 2.0.0
@since ― 2.0.0
Effect<void>
/**
* Run `initialize` first, and then run `watchThingOne` and `watchThingTwo`
* concurrently.
*/
const
const rootStanza: Effect.Effect<void,never,never>
Run initialize first, and then run watchThingOne and watchThingTwo
concurrently.
Use andThen when you need to run multiple actions in sequence, with the
second action depending on the result of the first. This is useful for
combining effects or handling computations that must happen in order.
Details
The second action can be:
A constant value (similar to
as
)
A function returning a value (similar to
map
)
A Promise
A function returning a Promise
An Effect
A function returning an Effect (similar to
flatMap
)
Note:andThen works well with both Option and Either types,
treating them as effects.
@example
// Title: Applying a Discount Based on Fetched Amount
import{ pipe, Effect }from"effect"
// Function to apply a discount safely to a transaction amount
constapplyDiscount= (
total:number,
discountRate:number
):Effect.Effect<number, Error>=>
discountRate ===0
? Effect.fail(newError("Discount rate cannot be zero"))
Combines multiple effects into one, returning results based on the input
structure.
Details
Use this function when you need to run multiple effects and combine their
results into a single output. It supports tuples, iterables, structs, and
records, making it flexible for different input types.
For instance, if the input is a tuple:
// ┌─── a tuple of effects
// ▼
Effect.all([effect1, effect2,...])
the effects are executed sequentially, and the result is a new effect
containing the results as a tuple. The results in the tuple match the order
of the effects passed to Effect.all.
Concurrency
You can control the execution order (e.g., sequential vs. concurrent) using
the concurrency option.
Short-Circuiting Behavior
This function stops execution on the first error it encounters, this is
called "short-circuiting". If any effect in the collection fails, the
remaining effects will not run, and the error will be propagated. To change
this behavior, you can use the mode option, which allows all effects to run
and collect results as Either or Option.
The mode option
The { mode: "either" } option changes the behavior of Effect.all to
ensure all effects run, even if some fail. Instead of stopping on the first
failure, this mode collects both successes and failures, returning an array
of Either instances where each result is either a Right (success) or a
Left (failure).
Similarly, the { mode: "validate" } option uses Option to indicate
success or failure. Each effect returns None for success and Some with
the error for failure.
@see ― forEach for iterating over elements and applying an effect.
@see ― allWith for a data-last version of this function.
NOTE: side-effect handlers are provided up-front to guarantee declarative
behavior.
@example
import{ configureStore, Tuple as RTKTuple }from"@reduxjs/toolkit"
import{ Sonnet, Stanza }from"redux-sonnet"
import{ Stream, Layer }from"effect"
const stanza = Stanza.fromStream(Stream.make(
{ type:"ACTION-1"},
{ type:"ACTION-2"},
{ type:"ACTION-3"},
))
const sonnet = Sonnet.make(
// ^?
stanza,
Sonnet.defaultLayer,
)
const store =configureStore({
// ^?
reducer:()=>{},
middleware:()=>newRTKTuple(sonnet)
})
@since ― 0.0.0
make(
import Effect
@since ― 2.0.0
@since ― 2.0.0
@since ― 2.0.0
Effect.
const void: Effect.Effect<void,never,never>
export void
Represents an effect that does nothing and produces no value.
When to Use
Use this effect when you need to represent an effect that does nothing.
This is useful in scenarios where you need to satisfy an effect-based
interface or control program flow without performing any operations. For
example, it can be used in situations where you want to return an effect
from a function but do not need to compute or return any result.
NOTE: side-effect handlers are provided up-front to guarantee declarative
behavior.
@example
import{ configureStore, Tuple as RTKTuple }from"@reduxjs/toolkit"
import{ Sonnet, Stanza }from"redux-sonnet"
import{ Stream, Layer }from"effect"
const stanza = Stanza.fromStream(Stream.make(
{ type:"ACTION-1"},
{ type:"ACTION-2"},
{ type:"ACTION-3"},
))
const sonnet = Sonnet.make(
// ^?
stanza,
Sonnet.defaultLayer,
)
const store =configureStore({
// ^?
reducer:()=>{},
middleware:()=>newRTKTuple(sonnet)
})
@since ― 0.0.0
make(
import Effect
@since ― 2.0.0
@since ― 2.0.0
@since ― 2.0.0
Effect.
const void: Effect.Effect<void,never,never>
export void
Represents an effect that does nothing and produces no value.
When to Use
Use this effect when you need to represent an effect that does nothing.
This is useful in scenarios where you need to satisfy an effect-based
interface or control program flow without performing any operations. For
example, it can be used in situations where you want to return an effect
from a function but do not need to compute or return any result.
NOTE: side-effect handlers are provided up-front to guarantee declarative
behavior.
@example
import{ configureStore, Tuple as RTKTuple }from"@reduxjs/toolkit"
import{ Sonnet, Stanza }from"redux-sonnet"
import{ Stream, Layer }from"effect"
const stanza = Stanza.fromStream(Stream.make(
{ type:"ACTION-1"},
{ type:"ACTION-2"},
{ type:"ACTION-3"},
))
const sonnet = Sonnet.make(
// ^?
stanza,
Sonnet.defaultLayer,
)
const store =configureStore({
// ^?
reducer:()=>{},
middleware:()=>newRTKTuple(sonnet)
})
@since ― 0.0.0
make(
import Effect
@since ― 2.0.0
@since ― 2.0.0
@since ― 2.0.0
Effect.
const void: Effect.Effect<void,never,never>
export void
Represents an effect that does nothing and produces no value.
When to Use
Use this effect when you need to represent an effect that does nothing.
This is useful in scenarios where you need to satisfy an effect-based
interface or control program flow without performing any operations. For
example, it can be used in situations where you want to return an effect
from a function but do not need to compute or return any result.
An array of Redux middleware to install, or a callback receiving getDefaultMiddleware and returning a Tuple of middleware.
If not supplied, defaults to the set of middleware returned by getDefaultMiddleware().
NOTE: side-effect handlers are provided up-front to guarantee declarative
behavior.
@example
import{ configureStore, Tuple as RTKTuple }from"@reduxjs/toolkit"
import{ Sonnet, Stanza }from"redux-sonnet"
import{ Stream, Layer }from"effect"
const stanza = Stanza.fromStream(Stream.make(
{ type:"ACTION-1"},
{ type:"ACTION-2"},
{ type:"ACTION-3"},
))
const sonnet = Sonnet.make(
// ^?
stanza,
Sonnet.defaultLayer,
)
const store =configureStore({
// ^?
reducer:()=>{},
middleware:()=>newRTKTuple(sonnet)
})
@since ― 0.0.0
make(
import Effect
@since ― 2.0.0
@since ― 2.0.0
@since ― 2.0.0
Effect.
const void: Effect.Effect<void,never,never>
export void
Represents an effect that does nothing and produces no value.
When to Use
Use this effect when you need to represent an effect that does nothing.
This is useful in scenarios where you need to satisfy an effect-based
interface or control program flow without performing any operations. For
example, it can be used in situations where you want to return an effect
from a function but do not need to compute or return any result.
Creates a store enhancer that applies middleware to the dispatch method
of the Redux store. This is handy for a variety of tasks, such as expressing
asynchronous actions in a concise manner, or logging every action payload.
See redux-thunk package as an example of the Redux middleware.
Because middleware is potentially asynchronous, this should be the first
store enhancer in the composition chain.
Note that each middleware will be given the dispatch and getState functions
as named arguments.
@param ― middlewares The middleware chain to be applied.
@returns ― A store enhancer applying the middleware.
applyMiddleware(
const sonnet: Sonnet.Sonnet<never,never>
sonnet)(
functioncreateStore<S, A extends Action, Ext extends{}={}, StateExt extends{}={}>(reducer: Reducer<S, A>,enhancer?: StoreEnhancer<Ext, StateExt>):Store<S, A, UnknownIfNonSpecific<StateExt>>&Ext(+1 overload)
@deprecated ―
We recommend using the configureStore method
of the @reduxjs/toolkit package, which replaces createStore.
Redux Toolkit is our recommended approach for writing Redux logic today,
including store setup, reducers, data fetching, and more.
configureStore from Redux Toolkit is an improved version of createStore that
simplifies setup and helps avoid common bugs.
You should not be using the redux core package by itself today, except for learning purposes.
The createStore method from the core redux package will not be removed, but we encourage
all users to migrate to using Redux Toolkit for all Redux code.
If you want to use createStore without this visual deprecation warning, use
the legacy_createStore import instead:
import { legacy_createStore as createStore} from 'redux'
Side-effect handlers, or Stanzas, are resourceful, meaning they can
request fulfillment of some set of services.
Layers
The second argument to Sonnet.make() is a Layer comprising the resources
available to running side-effect handlers. See the below example for usage
guidance. Note that SonnetService must be provisioned for all Sonnet
instantiations.
Re-using a ManagedRuntime
If the integrating application already makes use of a
ManagedRuntime, a MemoMap may be provided at Sonnet
instantiation to facilitate layer memoization.
The Effect interface defines a value that describes a workflow or job,
which can succeed or fail.
Details
The Effect interface represents a computation that can model a workflow
involving various types of operations, such as synchronous, asynchronous,
concurrent, and parallel interactions. It operates within a context of type
R, and the result can either be a success with a value of type A or a
failure with an error of type E. The Effect is designed to handle complex
interactions with external resources, offering advanced features such as
fiber-based concurrency, scheduling, interruption handling, and scalability.
This makes it suitable for tasks that require fine-grained control over
concurrency and error management.
To execute an Effect value, you need a Runtime, which provides the
environment necessary to run and manage the computation.
NOTE: side-effect handlers are provided up-front to guarantee declarative
behavior.
@example
import{ configureStore, Tuple as RTKTuple }from"@reduxjs/toolkit"
import{ Sonnet, Stanza }from"redux-sonnet"
import{ Stream, Layer }from"effect"
const stanza = Stanza.fromStream(Stream.make(
{ type:"ACTION-1"},
{ type:"ACTION-2"},
{ type:"ACTION-3"},
))
const sonnet = Sonnet.make(
// ^?
stanza,
Sonnet.defaultLayer,
)
const store =configureStore({
// ^?
reducer:()=>{},
middleware:()=>newRTKTuple(sonnet)
})
@since ― 0.0.0
make(
const sonnet: Sonnet.Sonnet<MyService,never>
import Effect
@since ― 2.0.0
@since ― 2.0.0
@since ― 2.0.0
Effect.
const void: Effect.Effect<void,never,never>
export void
Represents an effect that does nothing and produces no value.
When to Use
Use this effect when you need to represent an effect that does nothing.
This is useful in scenarios where you need to satisfy an effect-based
interface or control program flow without performing any operations. For
example, it can be used in situations where you want to return an effect
from a function but do not need to compute or return any result.