Skip to content

Commit

Permalink
add abstract asMicro to Micro.Class to improve DX
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart committed Sep 24, 2024
1 parent 5c34964 commit 9baefc6
Showing 1 changed file with 9 additions and 19 deletions.
28 changes: 9 additions & 19 deletions packages/effect/src/Micro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,10 @@ const MicroBase: MicroClass = (function() {
* @category constructors
*/
export abstract class Class<out A, out E = never, out R = never> extends MicroBase<A, E, R> {
abstract [runSymbol](env: Env<any>, onExit: (exit: MicroExit<A, E>) => void): void
abstract asMicro(): Micro<A, E, R>
[runSymbol](env: Env<any>, onExit: (exit: MicroExit<A, E>) => void): void {
this.asMicro()[runSymbol](env, onExit)
}
}

// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -881,7 +884,7 @@ const EnvRefProto: ThisType<EnvRef<any>> = {
...MicroProto,
[EnvRefTypeId]: EnvRefTypeId,
[runSymbol](env: Env<any>, onExit: (exit: MicroExit<any, any>) => void) {
onExit(Either.right(envGet(env, this)))
getEnvRef(this)[runSymbol](env, onExit)
}
}

Expand Down Expand Up @@ -3742,23 +3745,6 @@ export const isHandle = (u: unknown): u is Handle<unknown, unknown> =>
typeof u === "object" && u !== null && HandleTypeId in u

class HandleImpl<A, E> extends Class<A, E> implements Handle<A, E> {
[runSymbol](_env: Env<any>, onExit: (exit: MicroExit<A, E>) => void) {
if (this._controller.signal.aborted) {
return onExit(exitInterrupt)
}

const observer = (exit: MicroExit<A, E>) => {
this.removeObserver(observer)
onExit(exit)
}

this._controller.signal.addEventListener("abort", () => {
this.removeObserver(observer)
onExit(exitInterrupt)
}, { once: true })

this.addObserver(observer)
}
readonly [HandleTypeId]: HandleTypeId

readonly observers: Set<(exit: MicroExit<A, E>) => void> = new Set()
Expand Down Expand Up @@ -3834,6 +3820,10 @@ class HandleImpl<A, E> extends Class<A, E> implements Handle<A, E> {
return this.await
})
}

asMicro(): Micro<A, E> {
return this.join
}
}

/**
Expand Down

0 comments on commit 9baefc6

Please sign in to comment.