Si vous n’en avez jamais entendu parler, Effect^ est une puissante bibliothèque TypeScript conçue pour aider les développeurs à créer facilement des programmes complexes, synchrones et asynchrones.
StudioCMS utilise Effect pour gérer la majeure partie de notre code, ce qui simplifie et facilite la réutilisation des fonctions. Il nous a également permis d’optimiser les temps de chargement des tableaux de bord grâce à une approche entièrement nouvelle du code asynchrone.
Lorsque vous travaillez avec StudioCMS et Effect, au lieu de devoir l’installer vous-même, StudioCMS fournit les outils Effect via l’exportation suivante
example.ts
import{
importEffect
@since ― 2.0.0
@since ― 2.0.0
@since ― 2.0.0
Effect,
importContext
@since ― 2.0.0
@since ― 2.0.0
Context,
importSchema
Schema,
importData
Data}from'studiocms/effect';
Cela inclut exactement les mêmes exportations de base que l’exportation par défaut d’Effect import {} from 'effect'; ainsi que d’autres utilitaires utiles fournis par effect/Function et des utilitaires personnalisés de StudioCMS.
custom-utils.ts
import{
constconvertToVanilla: <A, E>(effect: Effect<A, E, never>) =>Promise<A>
Converts an Effect into an object containing both synchronous and asynchronous execution methods.
@param ― effect - The Effect instance to be converted.
convertToVanilla,
consterrorTap: ((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:
By providing a message first, which returns a function that takes an Effect and logs the error message.
By providing both the Effect and the message directly.
@param ― message - The error message to log. Can be a single value or an array of values.
@param ― self - The Effect to which the error logging will be applied.
@returns ― A new Effect that logs the provided error message when executed.
Generates a logger function that wraps an effectful generator function with logging capabilities.
@param ― label - 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,
constpipeLogger: ((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.
@param ― label - A string label used to identify the log entries and span.
@param ― effect - The Effect to be wrapped with logging functionality.
@returns ― A new Effect that includes runtime logging and a log span.
pipeLogger,
construntimeLogger: ((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.
@param ― label - A string label to associate with the logger for identifying log messages.
@returns ― A higher-order function that takes an Effect and returns a new Effect with
the logger configuration applied.