<A, E, R>(update: (a: A) => Effect.Effect<Option.Option<A>, E, R>): (
self: SubscriptionRef<A>
) => Effect.Effect<A, E, R>
<A, E, R>(
self: SubscriptionRef<A>,
update: (a: A) => Effect.Effect<Option.Option<A>, E, R>
): Effect.Effect<A, E, R>Applies an effectful optional update and returns the current value afterward.
When to use
Use to conditionally update a SubscriptionRef effectfully and read the
value that is current after the update decision.
Details
If the effect succeeds with Option.some, the new value is set, published,
and returned. If it succeeds with Option.none, the unchanged current value
is returned without publishing.
Example (Conditionally updating with an effect and reading the new value)
import { Effect, Option, SubscriptionRef } from "effect"
const program = Effect.gen(function*() {
const ref = yield* SubscriptionRef.make(10)
const newValue = yield* SubscriptionRef.updateSomeAndGetEffect(
ref,
(n) => Effect.succeed(n > 5 ? Option.some(n + 3) : Option.none())
)
console.log("New value:", newValue)
})export const const updateSomeAndGetEffect: {
<A, E, R>(
update: (
a: A
) => Effect.Effect<Option.Option<A>, E, R>
): (
self: SubscriptionRef<A>
) => Effect.Effect<A, E, R>
<A, E, R>(
self: SubscriptionRef<A>,
update: (
a: A
) => Effect.Effect<Option.Option<A>, E, R>
): Effect.Effect<A, E, R>
}
Applies an effectful optional update and returns the current value afterward.
When to use
Use to conditionally update a SubscriptionRef effectfully and read the
value that is current after the update decision.
Details
If the effect succeeds with Option.some, the new value is set, published,
and returned. If it succeeds with Option.none, the unchanged current value
is returned without publishing.
Example (Conditionally updating with an effect and reading the new value)
import { Effect, Option, SubscriptionRef } from "effect"
const program = Effect.gen(function*() {
const ref = yield* SubscriptionRef.make(10)
const newValue = yield* SubscriptionRef.updateSomeAndGetEffect(
ref,
(n) => Effect.succeed(n > 5 ? Option.some(n + 3) : Option.none())
)
console.log("New value:", newValue)
})
updateSomeAndGetEffect: {
<function (type parameter) A in <A, E, R>(update: (a: A) => Effect.Effect<Option.Option<A>, E, R>): (self: SubscriptionRef<A>) => Effect.Effect<A, E, R>A, function (type parameter) E in <A, E, R>(update: (a: A) => Effect.Effect<Option.Option<A>, E, R>): (self: SubscriptionRef<A>) => Effect.Effect<A, E, R>E, function (type parameter) R in <A, E, R>(update: (a: A) => Effect.Effect<Option.Option<A>, E, R>): (self: SubscriptionRef<A>) => Effect.Effect<A, E, R>R>(
update: (
a: A
) => Effect.Effect<Option.Option<A>, E, R>
update: (a: Aa: function (type parameter) A in <A, E, R>(update: (a: A) => Effect.Effect<Option.Option<A>, E, R>): (self: SubscriptionRef<A>) => Effect.Effect<A, E, R>A) => import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<import OptionOption.type Option.Option = /*unresolved*/ anyOption<function (type parameter) A in <A, E, R>(update: (a: A) => Effect.Effect<Option.Option<A>, E, R>): (self: SubscriptionRef<A>) => Effect.Effect<A, E, R>A>, function (type parameter) E in <A, E, R>(update: (a: A) => Effect.Effect<Option.Option<A>, E, R>): (self: SubscriptionRef<A>) => Effect.Effect<A, E, R>E, function (type parameter) R in <A, E, R>(update: (a: A) => Effect.Effect<Option.Option<A>, E, R>): (self: SubscriptionRef<A>) => Effect.Effect<A, E, R>R>
): (self: SubscriptionRef<A>(parameter) self: {
value: A;
semaphore: Semaphore.Semaphore;
pubsub: PubSub.PubSub<A>;
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; <…;
}
self: interface SubscriptionRef<in out A>A mutable reference whose updates are serialized and published to
subscribers.
When to use
Use to observe the current value and subsequent updates as a
stream.
The SubscriptionRef namespace containing type definitions associated with
subscription references.
SubscriptionRef<function (type parameter) A in <A, E, R>(update: (a: A) => Effect.Effect<Option.Option<A>, E, R>): (self: SubscriptionRef<A>) => Effect.Effect<A, E, R>A>) => import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<function (type parameter) A in <A, E, R>(update: (a: A) => Effect.Effect<Option.Option<A>, E, R>): (self: SubscriptionRef<A>) => Effect.Effect<A, E, R>A, function (type parameter) E in <A, E, R>(update: (a: A) => Effect.Effect<Option.Option<A>, E, R>): (self: SubscriptionRef<A>) => Effect.Effect<A, E, R>E, function (type parameter) R in <A, E, R>(update: (a: A) => Effect.Effect<Option.Option<A>, E, R>): (self: SubscriptionRef<A>) => Effect.Effect<A, E, R>R>
<function (type parameter) A in <A, E, R>(self: SubscriptionRef<A>, update: (a: A) => Effect.Effect<Option.Option<A>, E, R>): Effect.Effect<A, E, R>A, function (type parameter) E in <A, E, R>(self: SubscriptionRef<A>, update: (a: A) => Effect.Effect<Option.Option<A>, E, R>): Effect.Effect<A, E, R>E, function (type parameter) R in <A, E, R>(self: SubscriptionRef<A>, update: (a: A) => Effect.Effect<Option.Option<A>, E, R>): Effect.Effect<A, E, R>R>(self: SubscriptionRef<A>(parameter) self: {
value: A;
semaphore: Semaphore.Semaphore;
pubsub: PubSub.PubSub<A>;
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; <…;
}
self: interface SubscriptionRef<in out A>A mutable reference whose updates are serialized and published to
subscribers.
When to use
Use to observe the current value and subsequent updates as a
stream.
The SubscriptionRef namespace containing type definitions associated with
subscription references.
SubscriptionRef<function (type parameter) A in <A, E, R>(self: SubscriptionRef<A>, update: (a: A) => Effect.Effect<Option.Option<A>, E, R>): Effect.Effect<A, E, R>A>, update: (
a: A
) => Effect.Effect<Option.Option<A>, E, R>
update: (a: Aa: function (type parameter) A in <A, E, R>(self: SubscriptionRef<A>, update: (a: A) => Effect.Effect<Option.Option<A>, E, R>): Effect.Effect<A, E, R>A) => import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<import OptionOption.type Option.Option = /*unresolved*/ anyOption<function (type parameter) A in <A, E, R>(self: SubscriptionRef<A>, update: (a: A) => Effect.Effect<Option.Option<A>, E, R>): Effect.Effect<A, E, R>A>, function (type parameter) E in <A, E, R>(self: SubscriptionRef<A>, update: (a: A) => Effect.Effect<Option.Option<A>, E, R>): Effect.Effect<A, E, R>E, function (type parameter) R in <A, E, R>(self: SubscriptionRef<A>, update: (a: A) => Effect.Effect<Option.Option<A>, E, R>): Effect.Effect<A, E, R>R>): import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<function (type parameter) A in <A, E, R>(self: SubscriptionRef<A>, update: (a: A) => Effect.Effect<Option.Option<A>, E, R>): Effect.Effect<A, E, R>A, function (type parameter) E in <A, E, R>(self: SubscriptionRef<A>, update: (a: A) => Effect.Effect<Option.Option<A>, E, R>): Effect.Effect<A, E, R>E, function (type parameter) R in <A, E, R>(self: SubscriptionRef<A>, update: (a: A) => Effect.Effect<Option.Option<A>, E, R>): Effect.Effect<A, E, R>R>
} = import dualdual(2, <function (type parameter) A in <A, E, R>(self: SubscriptionRef<A>, update: (a: A) => Effect.Effect<Option.Option<A>, E, R>): anyA, function (type parameter) E in <A, E, R>(self: SubscriptionRef<A>, update: (a: A) => Effect.Effect<Option.Option<A>, E, R>): anyE, function (type parameter) R in <A, E, R>(self: SubscriptionRef<A>, update: (a: A) => Effect.Effect<Option.Option<A>, E, R>): anyR>(
self: SubscriptionRef<A>(parameter) self: {
value: A;
semaphore: Semaphore.Semaphore;
pubsub: PubSub.PubSub<A>;
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; <…;
}
self: interface SubscriptionRef<in out A>A mutable reference whose updates are serialized and published to
subscribers.
When to use
Use to observe the current value and subsequent updates as a
stream.
The SubscriptionRef namespace containing type definitions associated with
subscription references.
SubscriptionRef<function (type parameter) A in <A, E, R>(self: SubscriptionRef<A>, update: (a: A) => Effect.Effect<Option.Option<A>, E, R>): anyA>,
update: (
a: A
) => Effect.Effect<Option.Option<A>, E, R>
update: (a: Aa: function (type parameter) A in <A, E, R>(self: SubscriptionRef<A>, update: (a: A) => Effect.Effect<Option.Option<A>, E, R>): anyA) => import EffectEffect.type Effect.Effect = /*unresolved*/ anyEffect<import OptionOption.type Option.Option = /*unresolved*/ anyOption<function (type parameter) A in <A, E, R>(self: SubscriptionRef<A>, update: (a: A) => Effect.Effect<Option.Option<A>, E, R>): anyA>, function (type parameter) E in <A, E, R>(self: SubscriptionRef<A>, update: (a: A) => Effect.Effect<Option.Option<A>, E, R>): anyE, function (type parameter) R in <A, E, R>(self: SubscriptionRef<A>, update: (a: A) => Effect.Effect<Option.Option<A>, E, R>): anyR>
) =>
self: SubscriptionRef<A>(parameter) self: {
value: A;
semaphore: Semaphore.Semaphore;
pubsub: PubSub.PubSub<A>;
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; <…;
}
self.SubscriptionRef<in out A>.semaphore: Semaphore.Semaphore(property) SubscriptionRef<in out A>.semaphore: {
resize: (this: Semaphore, permits: number) => Effect.Effect<void>;
withPermits: (this: Semaphore, permits: number) => <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>;
withPermit: <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>;
withPermitsIfAvailable: (this: Semaphore, permits: number) => <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<Option.Option<A>, E, R>;
take: (this: Semaphore, permits: number) => Effect.Effect<number>;
release: (this: Semaphore, permits: number) => Effect.Effect<number>;
releaseAll: Effect.Effect<number>;
}
semaphore.withPermit(import EffectEffect.suspend(() => {
const const current: Acurrent = self: SubscriptionRef<A>(parameter) self: {
value: A;
semaphore: Semaphore.Semaphore;
pubsub: PubSub.PubSub<A>;
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; <…;
}
self.SubscriptionRef<A>.value: Avalue
return import EffectEffect.map(update: (
a: A
) => Effect.Effect<Option.Option<A>, E, R>
update(const current: Acurrent), (option: Option.Option<A>option) => {
if (import OptionOption.isNone(option: Option.Option<A>option)) return const current: Acurrent
const setUnsafe: <A>(
self: SubscriptionRef<A>,
value: A
) => void
setUnsafe(self: SubscriptionRef<A>(parameter) self: {
value: A;
semaphore: Semaphore.Semaphore;
pubsub: PubSub.PubSub<A>;
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; <…;
}
self, option: Option.Some<A>(parameter) option: {
_tag: "Some";
_op: "Some";
value: A;
valueOrUndefined: A;
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; <…;
toString: () => string;
toJSON: () => unknown;
}
option.value)
return option: Option.Some<A>(parameter) option: {
_tag: "Some";
_op: "Some";
value: A;
valueOrUndefined: A;
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; <…;
toString: () => string;
toJSON: () => unknown;
}
option.value
})
})))