Effect CommunityEC
Effect Community•2w ago•
3 replies
💰Ludwig von Mises

Handling SIGINT with Terminal.readLine in Effect Typescript

How to capture a SIGINT when using Terminal.readLine ? I can't make it work in this very simple example.
const neverEndingEffect: Effect.Effect<
  void,
  Terminal.QuitException | PlatformError,
  Terminal.Terminal
> = Effect.gen(function* () {
  const terminal = yield* Terminal.Terminal;

  yield* terminal.display('Waiting for your input... Press Ctrl+C to exit\n\n');

  while (true) {
    yield* terminal.display('Enter some text (or Ctrl+C to exit): ');
    const input = yield* terminal.readLine;
    yield* terminal.display(`You entered: ${input}\n\n`);
  }
});

// Run the effect with Terminal layer and handle SIGINT
const program = neverEndingEffect.pipe(Effect.provide(NodeTerminal.layer));

const fiber = Effect.runFork(program);

// Set up SIGINT handler to interrupt the fiber
process.once('SIGINT', () => {
  console.log('\n\nReceived SIGINT (Ctrl+C), exiting...');
  Effect.runPromise(Fiber.interrupt(fiber)).then(() => process.exit(0));
});
Was this page helpful?