Skip to content

Commit 8531a22

Browse files
Extract PartitionedSemaphore and add module helpers (#1709)
Co-authored-by: Tim Smart <hello@timsmart.co>
1 parent 27fea0f commit 8531a22

File tree

9 files changed

+744
-179
lines changed

9 files changed

+744
-179
lines changed

.changeset/shiny-trains-hug.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"effect": patch
3+
---
4+
5+
Add module-level helpers for `Semaphore`, `Latch`, and extracted `PartitionedSemaphore` operations.

packages/effect/src/Latch.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,73 @@ export const makeUnsafe: (open?: boolean | undefined) => Latch = internal.makeLa
110110
* @since 3.8.0
111111
*/
112112
export const make: (open?: boolean | undefined) => Effect.Effect<Latch> = internal.makeLatch
113+
114+
/**
115+
* Opens the latch, releasing all fibers waiting on it.
116+
*
117+
* @category combinators
118+
* @since 4.0.0
119+
*/
120+
export const open = (self: Latch): Effect.Effect<boolean> => self.open
121+
122+
/**
123+
* Opens the latch, releasing all fibers waiting on it.
124+
*
125+
* @category unsafe
126+
* @since 4.0.0
127+
*/
128+
export const openUnsafe = (self: Latch): boolean => self.openUnsafe()
129+
130+
/**
131+
* Releases all fibers waiting on the latch, without opening it.
132+
*
133+
* @category combinators
134+
* @since 4.0.0
135+
*/
136+
export const release = (self: Latch): Effect.Effect<boolean> => self.release
137+
138+
const _await = (self: Latch): Effect.Effect<void> => self.await
139+
140+
export {
141+
/**
142+
* Waits for the latch to be opened.
143+
*
144+
* @category getters
145+
* @since 4.0.0
146+
*/
147+
_await as await
148+
}
149+
150+
/**
151+
* Closes the latch.
152+
*
153+
* @category combinators
154+
* @since 4.0.0
155+
*/
156+
export const close = (self: Latch): Effect.Effect<boolean> => self.close
157+
158+
/**
159+
* Closes the latch.
160+
*
161+
* @category unsafe
162+
* @since 4.0.0
163+
*/
164+
export const closeUnsafe = (self: Latch): boolean => self.closeUnsafe()
165+
166+
/**
167+
* Runs the given effect only when the latch is open.
168+
*
169+
* @category combinators
170+
* @since 4.0.0
171+
*/
172+
export const whenOpen: {
173+
(self: Latch): <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>
174+
<A, E, R>(self: Latch, effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R>
175+
} = ((...args: Array<any>) => {
176+
if (args.length === 1) {
177+
const [self] = args
178+
return (effect: Effect.Effect<any, any, any>) => self.whenOpen(effect)
179+
}
180+
const [self, effect] = args
181+
return self.whenOpen(effect)
182+
}) as any

0 commit comments

Comments
 (0)