Read preferred directory nodeKey for a resource (requires Advice in context).
export const const preferred: (
resourceKey: string
) => Effect.Effect<
Option.Option<string>,
never,
Advice
>
Read preferred directory nodeKey for a resource (requires
Advice
in context).
preferred = (
resourceKey: stringresourceKey: string,
): import EffectEffect.interface Effect<out A, out E = never, out R = never>The Effect interface defines a value that lazily describes a workflow or
job. The workflow requires some context R, and may fail with an error of
type E, or succeed with a value of type A.
When to use
Use when you need to represent a lazy, composable workflow that can require
services, fail with a typed error, or succeed with a typed value.
Details
Effect values model resourceful interaction with the outside world,
including synchronous, asynchronous, concurrent, and parallel interaction.
They use a fiber-based concurrency model, with built-in support for
scheduling, fine-grained interruption, structured concurrency, and high
scalability.
To run an Effect value, you need a Runtime, which is a type that is
capable of executing Effect values.
Effect<import OptionOption.type Option<A> = Option.None<A> | Option.Some<A>The Option data type represents optional values. An Option<A> is either
Some<A>, containing a value of type A, or None, representing absence.
When to use
Use to represent initial values that may not yet exist
- Returning from partial functions (not defined for all inputs)
- Managing optional fields in data structures
Namespace containing utility types for Option.
When to use
Use to access type-level helpers associated with Option.
Option<string>, never, class AdviceLookup placement board — coordinator advice for nodeless /
Hyperlink.lookupClient
dial.
v1: last-write-wins, in-memory, no advisor ACL. Algorithms stay app-owned (who calls
advise
); Lookup only stores and surfaces the preference.
Advice> =>
import EffectEffect.const flatMap: <unknown, never, Advice, Option.Option<string>, never, Advice>(self: Effect.Effect<unknown, never, Advice>, f: (a: unknown) => Effect.Effect<Option.Option<string>, never, Advice>) => Effect.Effect<Option.Option<string>, never, Advice> (+1 overload)Chains effects to produce new Effect instances, useful for combining
operations that depend on previous results.
When to use
Use when you need to chain multiple effects, ensuring that each
step produces a new Effect while flattening any nested effects that may
occur.
Details
flatMap lets you sequence effects so that the result of one effect can be
used in the next step. It is similar to flatMap used with arrays but works
specifically with Effect instances, allowing you to avoid deeply nested
effect structures.
Since effects are immutable, flatMap always returns a new effect instead of
changing the original one.
Example (Choosing flatMap syntax variants)
import { Effect, pipe } from "effect"
const myEffect = Effect.succeed(1)
const transformation = (n: number) => Effect.succeed(n + 1)
const flatMappedWithPipe = pipe(myEffect, Effect.flatMap(transformation))
const flatMappedWithDataFirst = Effect.flatMap(myEffect, transformation)
const flatMappedWithMethod = myEffect.pipe(Effect.flatMap(transformation))
Example (Sequencing dependent effects)
import { Data, Effect, pipe } from "effect"
class DiscountRateError extends Data.TaggedError("DiscountRateError")<{}> {}
// Function to apply a discount safely to a transaction amount
const applyDiscount = (
total: number,
discountRate: number
): Effect.Effect<number, DiscountRateError> =>
discountRate === 0
? Effect.fail(new DiscountRateError())
: Effect.succeed(total - (total * discountRate) / 100)
// Simulated asynchronous task to fetch a transaction amount from database
const fetchTransactionAmount = Effect.promise(() => Promise.resolve(100))
// Chaining the fetch and discount application using `flatMap`
const finalAmount = pipe(
fetchTransactionAmount,
Effect.flatMap((amount) => applyDiscount(amount, 5))
)
Effect.runPromise(finalAmount).then(console.log)
// Output: 95
flatMap(class AdviceLookup placement board — coordinator advice for nodeless /
Hyperlink.lookupClient
dial.
v1: last-write-wins, in-memory, no advisor ACL. Algorithms stay app-owned (who calls
advise
); Lookup only stores and surfaces the preference.
Advice, (svc: unknownsvc) =>
svc: unknownsvc.preferred(new constructor PreferredRequest(props: Schema.Struct<Fields extends Schema.Struct.Fields>.ReadonlyMakeIn<{
readonly resourceKey: Schema.String;
}>, options?: Schema.MakeOptions | undefined): PreferredRequest
constructor PreferredRequest(props: Schema.Struct<Fields extends Schema.Struct.Fields>.ReadonlyMakeIn<{
readonly resourceKey: Schema.String;
}>, options?: Schema.MakeOptions | undefined): {
Type: Self;
Encoded: S["Encoded"];
DecodingServices: S["DecodingServices"];
EncodingServices: S["EncodingServices"];
Iso: S["Iso"];
identifier: string;
fields: S["fields"];
mapFields: (f: (fields: { readonly resourceKey: Schema.String }) => To, options?: { readonly unsafePreserveChecks?: boolean | undefined } | undefined) => Schema.Struct<{ [K in keyof Readonly<To>]: Readonly<To>[K]; }>;
extend: (identifier: string) => { (fields: NewFields, annotations?: Schema.Annotations.Declaration<Extended, readonly [Schema.Struct<{ [K in keyof { [K in keyof ('resourceKey' & keyof NewFields extends never ? { readonly resourceKey: Schema.String…;
Rebuild: Rebuild;
ast: Ast;
annotate: (annotations: Schema.Annotations.Bottom<PreferredRequest, readonly [Schema.Struct<{ readonly resourceKey: Schema.String }>]>) => Schema.decodeTo<Schema.declareConstructor<PreferredRequest, Schema.Struct.ReadonlySide<{ readonly resourceKey:…;
annotateKey: (annotations: Schema.Annotations.Key<PreferredRequest>) => Schema.decodeTo<Schema.declareConstructor<PreferredRequest, Schema.Struct.ReadonlySide<{ readonly resourceKey: Schema.String }, 'Encoded'>, readonly [Schema.Struct<{ readonly resou…;
check: (checks_0: Check<PreferredRequest>, ...checks: Array<Check<PreferredRequest>>) => Schema.decodeTo<Schema.declareConstructor<PreferredRequest, Schema.Struct.ReadonlySide<{ readonly resourceKey: Schema.String }, 'Encoded'>, readonly [Schema.…;
rebuild: (ast: Declaration) => Schema.decodeTo<Schema.declareConstructor<PreferredRequest, Schema.Struct.ReadonlySide<{ readonly resourceKey: Schema.String }, 'Encoded'>, readonly [Schema.Struct<{ readonly resourceKey: Schema.String }>], Schema.Str…;
make: (input: Schema.Struct.ReadonlyMakeIn<{ readonly resourceKey: Schema.String }>, options?: Schema.MakeOptions) => PreferredRequest;
makeOption: (input: Schema.Struct.ReadonlyMakeIn<{ readonly resourceKey: Schema.String }>, options?: Schema.MakeOptions) => Option.Option<PreferredRequest>;
makeEffect: (input: Schema.Struct.ReadonlyMakeIn<{ readonly resourceKey: Schema.String }>, options?: Schema.MakeOptions) => Effect.Effect<PreferredRequest, Schema.SchemaError, never>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
Read the preferred directory nodeKey for a resource (if any).
PreferredRequest({ resourceKey: stringresourceKey })),
);