Skip to content

Commit

Permalink
wrap env refs with globalValue
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart authored and gcanti committed Jun 1, 2024
1 parent c63efd0 commit 9f194e7
Showing 1 changed file with 29 additions and 32 deletions.
61 changes: 29 additions & 32 deletions packages/effect/src/Micro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,20 +363,21 @@ const EnvRefProto = {
* @since 3.3.0
* @category environment refs
*/
export const envRefMake = <A>(key: string, initial: A): EnvRef<A> => {
const self = Object.create(EnvRefProto)
self.key = key
self.initial = initial
return self
}
export const envRefMake = <A>(key: string, initial: LazyArg<A>): EnvRef<A> =>
globalValue(key, () => {
const self = Object.create(EnvRefProto)
self.key = key
self.initial = initial
return self
})

/**
* @since 3.3.0
* @category environment refs
*/
export const currentAbortController: EnvRef<AbortController> = envRefMake(
"effect/Micro/currentAbortController",
new AbortController()
() => new AbortController()
)

/**
Expand All @@ -385,7 +386,7 @@ export const currentAbortController: EnvRef<AbortController> = envRefMake(
*/
export const currentAbortSignal: EnvRef<AbortSignal> = envRefMake(
"effect/Micro/currentAbortSignal",
currentAbortController.initial.signal
() => currentAbortController.initial.signal
)

/**
Expand All @@ -394,7 +395,7 @@ export const currentAbortSignal: EnvRef<AbortSignal> = envRefMake(
*/
export const currentContext: EnvRef<Context.Context<never>> = envRefMake(
"effect/Micro/currentContext",
Context.empty()
() => Context.empty()
)

/**
Expand All @@ -403,7 +404,25 @@ export const currentContext: EnvRef<Context.Context<never>> = envRefMake(
*/
export const currentConcurrency: EnvRef<"unbounded" | number> = envRefMake(
"effect/Micro/currentConcurrency",
"unbounded"
() => "unbounded"
)

const currentInterruptible: EnvRef<boolean> = envRefMake(
"effect/Micro/currentInterruptible",
() => true
)

/**
* @since 3.3.0
* @category env refs
*/
export const withConcurrency: {
(concurrency: "unbounded" | number): <A, E, R>(self: Micro<A, E, R>) => Micro<A, E, R>
<A, E, R>(self: Micro<A, E, R>, concurrency: "unbounded" | number): Micro<A, E, R>
} = dual(
2,
<A, E, R>(self: Micro<A, E, R>, concurrency: "unbounded" | number): Micro<A, E, R> =>
locally(self, currentConcurrency, concurrency)
)

// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -2643,28 +2662,6 @@ export const runSync = <A, E>(effect: Micro<A, E>): A => {
return result.right
}

// ========================================================================
// env refs
// ========================================================================

const currentInterruptible: EnvRef<boolean> = envRefMake(
"effect/Micro/currentInterruptible",
true
)

/**
* @since 3.3.0
* @category env refs
*/
export const withConcurrency: {
(concurrency: "unbounded" | number): <A, E, R>(self: Micro<A, E, R>) => Micro<A, E, R>
<A, E, R>(self: Micro<A, E, R>, concurrency: "unbounded" | number): Micro<A, E, R>
} = dual(
2,
<A, E, R>(self: Micro<A, E, R>, concurrency: "unbounded" | number): Micro<A, E, R> =>
locally(self, currentConcurrency, concurrency)
)

// ----------------------------------------------------------------------------
// Errors
// ----------------------------------------------------------------------------
Expand Down

0 comments on commit 9f194e7

Please sign in to comment.