| 15 | ): FiberStore.FiberStore<R, E, A> => new FiberStoreImpl(runtime) |
| 16 | |
| 17 | class FiberStoreImpl<R, E, A> implements FiberStore.FiberStore<R, E, A> { |
| 18 | constructor( |
| 19 | readonly runtime: Runtime.Runtime<R> |
| 20 | ) {} |
| 21 | |
| 22 | // listeners |
| 23 | private listeners: Array<() => void> = [] |
| 24 | private notify() { |
| 25 | for (let i = 0; i < this.listeners.length; i++) { |
| 26 | this.listeners[i]() |
| 27 | } |
| 28 | } |
| 29 | public subscribe = (listener: () => void) => { |
| 30 | this.listeners.push(listener) |
| 31 | this.maybeResume() |
| 32 | return () => { |
| 33 | const index = this.listeners.indexOf(listener) |
| 34 | if (index >= 0) { |
| 35 | this.listeners.splice(index, 1) |
| 36 | } |
| 37 | queueMicrotask(() => { |
| 38 | if (this.listeners.length === 0) { |
| 39 | this.interruptIfRunning() |
| 40 | } |
| 41 | }) |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | // state |
| 46 | private trackedProps = TrackedProperties.initial() |
| 47 | private resultBag: ResultBag.ResultBag<E, A> = ResultBag.make(Result.initial(), this.trackedProps) |
| 48 | private setResult(result: Result.Result<E, A>) { |
| 49 | TrackedProperties.updateFromResult(this.trackedProps, result) |
| 50 | this.resultBag = ResultBag.make(result, this.trackedProps) |
| 51 | this.notify() |
| 52 | } |
| 53 | public snapshot = () => { |
| 54 | return this.resultBag |
| 55 | } |
| 56 | |
| 57 | // lifecycle |
| 58 | private stream: Stream.Stream<R, E, A> | undefined = undefined |
| 59 | private fiberState: |
| 60 | | { |
| 61 | readonly fiber: Fiber.RuntimeFiber<never, void> |
| 62 | readonly interruptedRef: Ref.Ref<boolean> |
| 63 | } |
| 64 | | undefined = undefined |
| 65 | public run(stream: Stream.Stream<R, E, A>) { |
| 66 | this.interruptIfRunning() |
| 67 | this.stream = stream |
| 68 | |
| 69 | const interruptedRef = Ref.unsafeMake(false) |
| 70 | const maybeSetResult = (result: Result.Result<E, A>) => |
| 71 | Effect.flatMap( |
| 72 | Ref.get(interruptedRef), |
| 73 | (interrupted) => |
| 74 | interrupted |
nothing calls this directly
no test coverage detected