Micro for Effect Users
The Micro module is designed as a lighter alternative to the standard Effect module, tailored for situations where it is beneficial to reduce the bundle size.
This module is standalone and does not include more complex functionalities such as Layer, Ref, Queue, and Deferred. This feature set makes Micro especially suitable for libraries that wish to utilize Effect functionalities while keeping the bundle size to a minimum, particularly for those aiming to provide Promise
-based APIs.
Micro also supports use cases where a client application uses Micro, and a server employs the full suite of Effect features, maintaining both compatibility and logical consistency across various application components.
Integrating Micro adds a minimal footprint to your bundle, starting at 5kb gzipped, which may increase depending on the features you use.
Micro is a part of the Effect library and can be imported just like any other module:
You can also import it using a namespace import like this:
The Micro
type uses three type parameters:
which mirror those of the Effect
type.
The MicroExit
type is a streamlined version of the Exit type, designed to capture the outcome of a Micro
computation. It uses the Either data type to distinguish between successful outcomes and failures:
The MicroCause
type is a streamlined version of the Cause type.
Similar to how Cause
is a union of types, MicroCause
comes in three forms:
Variant | Description |
---|---|
Die | Indicates an unforeseen defect that wasn’t planned for in the system’s logic. |
Fail<E> | Covers anticipated errors that are recognized and typically handled within the application. |
Interrupt | Signifies an operation that has been purposefully stopped. |
The MicroSchedule
type is a streamlined version of the Schedule type.
Represents a function that can be used to calculate the delay between repeats.
The function takes the current attempt number and the elapsed time since
the first attempt, and returns the delay for the next attempt. If the
function returns None
, the repetition will stop.
Below, you’ll find a series of comparisons between the functionalities of Effect
and Micro
. Each table lists a functionality of Effect
alongside its counterpart in Micro
. The icons used have the following meanings:
- ⚠️: The feature is available in
Micro
, but with some differences fromEffect
. - ❌: The feature is not available in
Effect
.
Effect | Micro | |
---|---|---|
Effect.try | ⚠️ Micro.try | requires a try block |
Effect.tryPromise | ⚠️ Micro.tryPromise | requires a try block |
Effect.sleep | ⚠️ Micro.sleep | only handles milliseconds |
Effect.failCause | ⚠️ Micro.failWith | uses MicroCause instead of Cause |
Effect.failCauseSync | ⚠️ Micro.failWithSync | uses MicroCause instead of Cause |
❌ | Micro.make | |
❌ | Micro.fromOption | |
❌ | Micro.fromEither |
Effect | Micro | |
---|---|---|
Effect.runSyncExit | ⚠️ Micro.runSyncExit | returns a MicroExit instead of an Exit |
Effect.runPromiseExit | ⚠️ Micro.runPromiseExit | returns a MicroExit instead of an Exit |
Effect.runFork | ⚠️ Micro.runFork | returns a Handle instead of a RuntimeFiber |
The Micro.runSyncExit
function is used to execute an Effect synchronously, which means it runs immediately and returns the result as a MicroExit.
Example (Handling Results as MicroExit)
The Micro.runPromiseExit
function is used to execute an Effect and obtain the result as a Promise
that resolves to a MicroExit.
Example (Handling Results as MicroExit)
The Micro.runFork
function executes the effect and return a Handle
that can be awaited, joined, or aborted.
You can listen for the result by adding an observer using the handle’s addObserver
method.
Example (Observing an Asynchronous Effect)
Effect | Micro | |
---|---|---|
Effect.andThen | ⚠️ Micro.andThen | doesn’t handle Promise or () => Promise as argument |
Effect.tap | ⚠️ Micro.tap | doesn’t handle () => Promise as argument |
Effect.all | ⚠️ Micro.all | no batching and mode options |
Effect.forEach | ⚠️ Micro.forEach | no batching option |
Effect.filter | ⚠️ Micro.filter | no batching option |
Effect.filterMap | ⚠️ Micro.filterMap | effectful |
Effect | Micro | |
---|---|---|
Effect.exit | ⚠️ Micro.exit | returns a MicroExit instead of an Exit |
Effect | Micro | |
---|---|---|
❌ | Micro.catchCauseIf |
Effect | Micro | |
---|---|---|
❌ | Micro.timeoutOrElse |
To access a service while using Micro.gen
, you need to wrap the service tag using the Micro.service
function:
Example (Accessing a Service in Micro.gen
)
Effect | Micro | |
---|---|---|
Scope | ⚠️ MicroScope | returns a MicroScope instead of a Scope |
Scope.make | ⚠️ Micro.scopeMake | returns a MicroScope instead of a Scope |
Effect | Micro | |
---|---|---|
Effect.retry | ⚠️ Micro.retry | different options |
Effect | Micro | |
---|---|---|
Effect.repeat | ⚠️ Micro.repeat | different options |
❌ (Effect.exit + Effect.repeat ) | ⚠️ Micro.repeatExit |
Effect | Micro | |
---|---|---|
❌ | Micro.timeoutOrElse |
Effect | Micro | |
---|---|---|
Effect.sandbox | ⚠️ Micro.sandbox | MicroCause<E> instead of Cause<E> |
Effect | Micro | |
---|---|---|
❌ | Micro.filterOrFailWith | |
Effect.tapErrorCause | ⚠️ Micro.tapErrorCause | MicroCause<E> instead of Cause<E> |
❌ | Micro.tapCauseIf | |
Effect.tapDefect | ⚠️ Micro.tapDefect | unknown instead of Cause<never> |
Effect | Micro | |
---|---|---|
Effect.provide | ⚠️ Micro.provideContext | only handles Context |
❌ | Micro.provideScope | |
❌ | Micro.service |
Effect | Micro | |
---|---|---|
Effect.addFinalizer | ⚠️ Micro.addFinalizer | MicroExit instead of Exit and no R |
Effect.acquireRelease | ⚠️ Micro.acquireRelease | MicroExit instead of Exit |
Effect.acquireUseRelease | ⚠️ Micro.acquireUseRelease | MicroExit instead of Exit |
Effect.onExit | ⚠️ Micro.onExit | MicroExit instead of Exit |
Effect.onError | ⚠️ Micro.onError | uses MicroCause instead of Cause |
❌ | Micro.onExitIf |
Effect | Micro | |
---|---|---|
Effect.fork | ⚠️ Micro.fork | Handle instead of RuntimeFiber |
Effect.forkDaemon | ⚠️ Micro.forkDaemon | Handle instead of RuntimeFiber |
Effect.forkIn | ⚠️ Micro.forkIn | Handle instead of RuntimeFiber |
Effect.forkScoped | ⚠️ Micro.forkScoped | Handle instead of RuntimeFiber |