Skip to content

Commit a663390

Browse files
authoredFeb 14, 2024··
Expose Random Tag and functions to use a specific random service implementation (#2137)
1 parent e787a57 commit a663390

File tree

5 files changed

+50
-0
lines changed

5 files changed

+50
-0
lines changed
 

‎.changeset/four-toes-double.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"effect": patch
3+
---
4+
5+
Expose Random Tag and functions to use a specific random service implementation

‎packages/effect/src/Effect.ts

+22
Original file line numberDiff line numberDiff line change
@@ -4524,6 +4524,28 @@ export const random: Effect<Random.Random> = effect.random
45244524
export const randomWith: <A, E, R>(f: (random: Random.Random) => Effect<A, E, R>) => Effect<A, E, R> =
45254525
defaultServices.randomWith
45264526

4527+
/**
4528+
* Executes the specified workflow with the specified implementation of the
4529+
* random service.
4530+
*
4531+
* @since 2.0.0
4532+
* @category random
4533+
*/
4534+
export const withRandom: {
4535+
<A extends Random.Random>(value: A): <A, E, R>(effect: Effect<A, E, R>) => Effect<A, E, R>
4536+
<A extends Random.Random, E, R>(effect: Effect<A, E, R>, value: A): Effect<A, E, R>
4537+
} = defaultServices.withRandom
4538+
4539+
/**
4540+
* Sets the implementation of the random service to the specified value and
4541+
* restores it to its original value when the scope is closed.
4542+
*
4543+
* @since 2.0.0
4544+
* @category constructors
4545+
*/
4546+
export const withRandomScoped: <A extends Random.Random>(value: A) => Effect<void, never, Scope.Scope> =
4547+
fiberRuntime.withRandomScoped
4548+
45274549
// -------------------------------------------------------------------------------------
45284550
// runtime
45294551
// -------------------------------------------------------------------------------------

‎packages/effect/src/Random.ts

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* @since 2.0.0
33
*/
44
import type * as Chunk from "./Chunk.js"
5+
import type * as Context from "./Context.js"
56
import type * as Effect from "./Effect.js"
67
import * as defaultServices from "./internal/defaultServices.js"
78
import * as internal from "./internal/random.js"
@@ -111,3 +112,9 @@ export const shuffle: <A>(elements: Iterable<A>) => Effect.Effect<Chunk.Chunk<A>
111112
*/
112113
export const randomWith: <A, E, R>(f: (random: Random) => Effect.Effect<A, E, R>) => Effect.Effect<A, E, R> =
113114
defaultServices.randomWith
115+
116+
/**
117+
* @since 2.0.0
118+
* @category context
119+
*/
120+
export const Random: Context.Tag<Random, Random> = internal.randomTag

‎packages/effect/src/internal/defaultServices.ts

+10
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,16 @@ export const randomWith = <A, E, R>(f: (random: Random.Random) => Effect.Effect<
102102
(services) => f(Context.get(services, random.randomTag))
103103
)
104104

105+
/** @internal */
106+
export const withRandom = dual<
107+
<A extends Random.Random>(value: A) => <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>,
108+
<A extends Random.Random, E, R>(effect: Effect.Effect<A, E, R>, value: A) => Effect.Effect<A, E, R>
109+
>(2, (effect, value) =>
110+
core.fiberRefLocallyWith(
111+
currentServices,
112+
Context.add(random.randomTag, value)
113+
)(effect))
114+
105115
/** @internal */
106116
export const next: Effect.Effect<number> = randomWith((random) => random.next)
107117

‎packages/effect/src/internal/fiberRuntime.ts

+6
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import * as MRef from "../MutableRef.js"
2828
import * as Option from "../Option.js"
2929
import { pipeArguments } from "../Pipeable.js"
3030
import * as Predicate from "../Predicate.js"
31+
import type * as Random from "../Random.js"
3132
import * as RA from "../ReadonlyArray.js"
3233
import * as Ref from "../Ref.js"
3334
import type { Entry, Request } from "../Request.js"
@@ -59,6 +60,7 @@ import * as metric from "./metric.js"
5960
import * as metricBoundaries from "./metric/boundaries.js"
6061
import * as metricLabel from "./metric/label.js"
6162
import * as OpCodes from "./opCodes/effect.js"
63+
import { randomTag } from "./random.js"
6264
import { complete } from "./request.js"
6365
import * as _runtimeFlags from "./runtimeFlags.js"
6466
import { OpSupervision } from "./runtimeFlags.js"
@@ -2705,6 +2707,10 @@ export const validateFirst = dual<
27052707
export const withClockScoped = <A extends Clock.Clock>(value: A) =>
27062708
fiberRefLocallyScopedWith(defaultServices.currentServices, Context.add(clock.clockTag, value))
27072709

2710+
/* @internal */
2711+
export const withRandomScoped = <A extends Random.Random>(value: A) =>
2712+
fiberRefLocallyScopedWith(defaultServices.currentServices, Context.add(randomTag, value))
2713+
27082714
/* @internal */
27092715
export const withConfigProviderScoped = (value: ConfigProvider) =>
27102716
fiberRefLocallyScopedWith(defaultServices.currentServices, Context.add(configProviderTag, value))

0 commit comments

Comments
 (0)
Please sign in to comment.