( onEvent: (event: Api.Event<any>) => Effect<void, never, any>, currentMetadata: () => Api.Metadata )
| 26 | |
| 27 | /** @internal */ |
| 28 | export const makeEventEmitter = ( |
| 29 | onEvent: (event: Api.Event<any>) => Effect<void, never, any>, |
| 30 | currentMetadata: () => Api.Metadata |
| 31 | ): EventEmitter => { |
| 32 | let lastStepIndex = -1 |
| 33 | let stepAttempt = 0 |
| 34 | const emit = (event: Api.Event<any>) => effect.ignoreCause(onEvent(event)) |
| 35 | return { |
| 36 | begin: effect.clockWith((clock) => |
| 37 | effect.suspend(() => { |
| 38 | const meta = currentMetadata() |
| 39 | if (meta.stepIndex !== lastStepIndex) { |
| 40 | lastStepIndex = meta.stepIndex |
| 41 | stepAttempt = 0 |
| 42 | } |
| 43 | stepAttempt++ |
| 44 | const state: AttemptState = { |
| 45 | attempt: meta.attempt, |
| 46 | stepAttempt, |
| 47 | stepIndex: meta.stepIndex, |
| 48 | startNanos: clock.monotonicTimeNanosUnsafe() |
| 49 | } |
| 50 | return effect.as( |
| 51 | emit({ |
| 52 | _tag: "AttemptStart", |
| 53 | attempt: state.attempt, |
| 54 | stepAttempt: state.stepAttempt, |
| 55 | stepIndex: state.stepIndex |
| 56 | }), |
| 57 | state |
| 58 | ) |
| 59 | }) |
| 60 | ), |
| 61 | end: (state, exit) => |
| 62 | effect.clockWith((clock) => { |
| 63 | const duration = Duration.nanos(clock.monotonicTimeNanosUnsafe() - state.startNanos) |
| 64 | return emit( |
| 65 | exit._tag === "Success" |
| 66 | ? { |
| 67 | _tag: "AttemptSuccess", |
| 68 | attempt: state.attempt, |
| 69 | stepAttempt: state.stepAttempt, |
| 70 | stepIndex: state.stepIndex, |
| 71 | duration |
| 72 | } |
| 73 | : { |
| 74 | _tag: "AttemptFailure", |
| 75 | attempt: state.attempt, |
| 76 | stepAttempt: state.stepAttempt, |
| 77 | stepIndex: state.stepIndex, |
| 78 | duration, |
| 79 | cause: exit.cause |
| 80 | } |
| 81 | ) |
| 82 | }) |
| 83 | } |
| 84 | } |
| 85 |
no test coverage detected