Supervisor
On this page
A Supervisor<A>
is a tool that manages the creation and termination of fibers, producing some visible value of type A
based on its supervision.
Creating
track
To create a supervisor, you can use the Supervisor.track
function. It generates a new supervisor that keeps track of its child fibers in a set.
Supervising
supervised
Whenever you need to supervise an effect, you can employ the Effect.supervised
function. This function takes a supervisor and returns an effect that behaves the same as the original effect. However, all child fibers forked within this effect are reported to the specified supervisor.
By doing this, you associate the behavior of child fibers with the provided supervisor, giving you access to all the information about these child fibers through the supervisor.
Example
In the following example, we will periodically monitor the number of fibers throughout our application's lifecycle:
ts
import {Effect ,Supervisor ,Schedule ,Fiber ,FiberStatus } from "effect"constprogram =Effect .gen (function* (_ ) {constsupervisor = yield*_ (Supervisor .track )constfibFiber = yield*_ (fib (20).pipe (Effect .supervised (supervisor ),Effect .fork ))constpolicy =Schedule .spaced ("500 millis").pipe (Schedule .whileInputEffect ((_ ) =>Fiber .status (fibFiber ).pipe (Effect .map ((status ) =>status !==FiberStatus .done ))))constmonitorFiber = yield*_ (monitorFibers (supervisor ).pipe (Effect .repeat (policy ),Effect .fork ))yield*_ (Fiber .join (monitorFiber ))constresult = yield*_ (Fiber .join (fibFiber ))console .log (`fibonacci result: ${result }`)})constmonitorFibers = (supervisor :Supervisor .Supervisor <Array <Fiber .RuntimeFiber <any, any>>>):Effect .Effect <void> =>Effect .gen (function* (_ ) {constfibers = yield*_ (supervisor .value )console .log (`number of fibers: ${fibers .length }`)})constfib = (n : number):Effect .Effect <number> =>Effect .gen (function* (_ ) {if (n <= 1) {return 1}yield*_ (Effect .sleep ("500 millis"))constfiber1 = yield*_ (Effect .fork (fib (n - 2)))constfiber2 = yield*_ (Effect .fork (fib (n - 1)))constv1 = yield*_ (Fiber .join (fiber1 ))constv2 = yield*_ (Fiber .join (fiber2 ))returnv1 +v2 })Effect .runPromise (program )/*Output:number of fibers: 0number of fibers: 2number of fibers: 6number of fibers: 14number of fibers: 30number of fibers: 62number of fibers: 126number of fibers: 254number of fibers: 510number of fibers: 1022number of fibers: 2034number of fibers: 3795number of fibers: 5810number of fibers: 6474number of fibers: 4942number of fibers: 2515number of fibers: 832number of fibers: 170number of fibers: 18number of fibers: 0fibonacci result: 10946*/
ts
import {Effect ,Supervisor ,Schedule ,Fiber ,FiberStatus } from "effect"constprogram =Effect .gen (function* (_ ) {constsupervisor = yield*_ (Supervisor .track )constfibFiber = yield*_ (fib (20).pipe (Effect .supervised (supervisor ),Effect .fork ))constpolicy =Schedule .spaced ("500 millis").pipe (Schedule .whileInputEffect ((_ ) =>Fiber .status (fibFiber ).pipe (Effect .map ((status ) =>status !==FiberStatus .done ))))constmonitorFiber = yield*_ (monitorFibers (supervisor ).pipe (Effect .repeat (policy ),Effect .fork ))yield*_ (Fiber .join (monitorFiber ))constresult = yield*_ (Fiber .join (fibFiber ))console .log (`fibonacci result: ${result }`)})constmonitorFibers = (supervisor :Supervisor .Supervisor <Array <Fiber .RuntimeFiber <any, any>>>):Effect .Effect <void> =>Effect .gen (function* (_ ) {constfibers = yield*_ (supervisor .value )console .log (`number of fibers: ${fibers .length }`)})constfib = (n : number):Effect .Effect <number> =>Effect .gen (function* (_ ) {if (n <= 1) {return 1}yield*_ (Effect .sleep ("500 millis"))constfiber1 = yield*_ (Effect .fork (fib (n - 2)))constfiber2 = yield*_ (Effect .fork (fib (n - 1)))constv1 = yield*_ (Fiber .join (fiber1 ))constv2 = yield*_ (Fiber .join (fiber2 ))returnv1 +v2 })Effect .runPromise (program )/*Output:number of fibers: 0number of fibers: 2number of fibers: 6number of fibers: 14number of fibers: 30number of fibers: 62number of fibers: 126number of fibers: 254number of fibers: 510number of fibers: 1022number of fibers: 2034number of fibers: 3795number of fibers: 5810number of fibers: 6474number of fibers: 4942number of fibers: 2515number of fibers: 832number of fibers: 170number of fibers: 18number of fibers: 0fibonacci result: 10946*/