Saltearse al contenido

Effect

Esta página aún no está disponible en tu idioma.

If you have not heard about it before, Effect^ is a powerful TypeScript library designed to help developers easily create complex, synchronous, and asynchronous programs.

StudioCMS utilizes Effect to manage most of our code, bringing simplicity and easier reuse of functions. It has also helped us optimize dashboard loading times by taking an entirely new approach to async code.

While working with StudioCMS and Effect, instead of needing to install it yourself, StudioCMS provides the Effect tools via the following export

example.ts
import {
import Effect

@since2.0.0

@since2.0.0

@since2.0.0

Effect
,
import Context

@since2.0.0

@since2.0.0

Context
,
import Schema
Schema
,
import Data
Data
} from 'studiocms/effect';

This includes all the same base exports from Effect’s default export import {} from 'effect'; as well as some other useful utilities from effect/Function and custom ones from StudioCMS.

custom-utils.ts
import {
const convertToVanilla: <A, E>(effect: Effect<A, E, never>) => Promise<A>

Converts an Effect into an object containing both synchronous and asynchronous execution methods.

@parameffect - The Effect instance to be converted.

convertToVanilla
,
const errorTap: ((message: any | ReadonlyArray<any>) => <A, E, R>(self: Effect<A, E, R>) => Effect<A, E, R>) & (<A, E, R>(self: Effect<A, E, R>, message: any | ReadonlyArray<any>) => Effect<A, E, R>)

A utility function that logs an error message when an Effect.fail() is executed.

This function is curried and can be used in two forms:

  1. By providing a message first, which returns a function that takes an Effect and logs the error message.
  2. By providing both the Effect and the message directly.

@parammessage - The error message to log. Can be a single value or an array of values.

@paramself - The Effect to which the error logging will be applied.

@returnsA new Effect that logs the provided error message when executed.

errorTap
,
function genLogger(label: string): <Eff extends YieldWrap<Effect<any, any, any>>, AEff>(f: (resume: Adapter) => Generator<Eff, AEff, never>) => Effect<AEff, [Eff] extends [never] ? never : [Eff] extends [YieldWrap<Effect<infer _A, infer E, infer _R>>] ? E : never, [Eff] extends [never] ? never : [Eff] extends [YieldWrap<Effect<infer _A, infer _E, infer R>>] ? R : never>

Generates a logger function that wraps an effectful generator function with logging capabilities.

@paramlabel - A string label used to identify the logger.

@returns

A function that takes a generator function f and returns an Effect.Effect instance.

The returned function accepts a generator function f that yields wrapped effects (YieldWrap<Effect.Effect>). It logs the execution of the generator function and its effects using the provided label.

genLogger
,
const pipeLogger: ((label: string) => <A, E, R>(effect: Effect<A, E, R>) => Effect<A, E, R>) & (<A, E, R>(effect: Effect<A, E, R>, label: string) => Effect<A, E, R>)

Wraps an Effect with additional logging functionality.

This function applies a runtime logger and a log span to the provided Effect, enabling detailed logging for debugging and monitoring purposes.

@paramlabel - A string label used to identify the log entries and span.

@parameffect - The Effect to be wrapped with logging functionality.

@returnsA new Effect that includes runtime logging and a log span.

pipeLogger
,
const runtimeLogger: ((label: string) => <A, E, R>(self: Effect<A, E, R>) => Effect<A, E, R>) & (<A, E, R>(self: Effect<A, E, R>, label: string) => Effect<A, E, R>)

Creates a runtime logger effect transformer that applies a specific label to log messages and configures the logging behavior based on the provided log level.

@paramlabel - A string label to associate with the logger for identifying log messages.

@returnsA higher-order function that takes an Effect and returns a new Effect with the logger configuration applied.

runtimeLogger
} from 'studiocms/effect';
Want to learn more about Effect? Check out the Effect documentation^