github Effect-TS/effect @effect/experimental@0.10.0

Minor Changes

Patch Changes

  • #2246 5234a9a Thanks @tim-smart! - add support for SpanEvent's to DevTools protocol

  • #2254 98b921a Thanks @tim-smart! - send a final metrics snapshot on DevTools shutdown

  • #2256 f82875f Thanks @tim-smart! - add Machine module to experimental

    The Machine module can be used to create effectful state machines. Here is an
    example of a machine that sends emails:

    import { Machine } from "@effect/experimental";
    import { runMain } from "@effect/platform-node/NodeRuntime";
    import { Data, Effect, List, Request, Schedule } from "effect";
    
    class SendError extends Data.TaggedError("SendError")<{
      readonly email: string;
      readonly reason: string;
    }> {}
    
    class SendEmail extends Request.TaggedClass("SendEmail")<
      void,
      SendError,
      {
        readonly email: string;
        readonly message: string;
      }
    > {}
    
    class ProcessEmail extends Request.TaggedClass("ProcessEmail")<
      void,
      never,
      {}
    > {}
    
    class Shutdown extends Request.TaggedClass("Shutdown")<void, never, {}> {}
    
    const mailer = Machine.makeWith<List.List<SendEmail>>()((_, previous) =>
      Effect.gen(function* (_) {
        const ctx = yield* _(Machine.MachineContext);
        const state = previous ?? List.empty();
    
        if (List.isCons(state)) {
          yield* _(
            ctx.unsafeSend(new ProcessEmail()),
            Effect.replicateEffect(List.size(state)),
          );
        }
    
        return Machine.procedures.make(state).pipe(
          Machine.procedures.addPrivate<ProcessEmail>()(
            "ProcessEmail",
            ({ state }) =>
              Effect.gen(function* (_) {
                if (List.isNil(state)) {
                  return [void 0, state];
                }
                const req = state.head;
                yield* _(
                  Effect.log(`Sending email to ${req.email}`),
                  Effect.delay(500),
                );
                return [void 0, state.tail];
              }),
          ),
          Machine.procedures.add<SendEmail>()("SendEmail", (ctx) =>
            ctx
              .send(new ProcessEmail())
              .pipe(Effect.as([void 0, List.append(ctx.state, ctx.request)])),
          ),
          Machine.procedures.add<Shutdown>()("Shutdown", () =>
            Effect.log("Shutting down").pipe(Effect.zipRight(Effect.interrupt)),
          ),
        );
      }),
    ).pipe(Machine.retry(Schedule.forever));
    
    Effect.gen(function* (_) {
      const actor = yield* _(Machine.boot(mailer));
      yield* _(
        actor.send(
          new SendEmail({ email: "test@example.com", message: "Hello, World!" }),
        ),
      );
      yield* _(
        actor.send(
          new SendEmail({ email: "test@example.com", message: "Hello, World!" }),
        ),
      );
      yield* _(
        actor.send(
          new SendEmail({ email: "test@example.com", message: "Hello, World!" }),
        ),
      );
      yield* _(actor.send(new Shutdown()));
    }).pipe(Effect.scoped, runMain);
  • Updated dependencies [e03811e, ac41d84, 0f3d99c, 6137533, f373529, 1bf9f31, e3ff789, 6137533, 507ba40, 4064ea0, e466afe, 465be79, f373529, de74eb8, d8e6940, fa9663c, fa9663c]:

    • effect@2.4.2
    • @effect/platform@0.47.0
    • @effect/schema@0.63.3
    • @effect/platform-node@0.45.0

Don't miss a new effect release

NewReleases is sending notifications on new releases.