3rdparty/eventuals

Provide a way to run cleanup at the end of stream generation

Closed this issue · 2 comments

I don't know how to write "cleanup" code that runs at the end of stream generation. For example (pseudocode):

return TypeCheck<Generator<int>>(Iterate({1, 2})
    >> Map([](int value) { return value; })
    >> Then([]() { LOG(INFO) << "All done!; })
);

The final Then statement changes the type of the resulting eventual chain such that it's no longer able to create a generator from the result.

This came up when writing a gRPC server handler for a bidirectional streaming RPC, where the server handler wanted to run some extra logic after sending a final request to a client.

return TypeCheck<Generator<int>>(Iterate({1, 2})
    >> Map([](int value) { return value; })
    >> Then([](int value) { LOG(INFO) << "All done!; return value; })
);

?

If I understand your snippet correctly, that Then() eventual runs after every element. This issue is instead about running when the stream is done / after the final element has been generated, and only running once.

So with that sample code that iterates over 2 elements, the goal is to only print "All done!" once, rather that once for each of the N elements.

@CodingCanuck, the following snippet runs .ended() just once (tested it locally):

auto s = [&]() {
    return TypeCheck<void>(
        Iterate({1, 2})
        >> Map([](int value) { return value; })
        >> Loop<void>()
               .ended([](auto& k) {
                 std::cout << "Finished" << std::endl;
                 k.Start();
               }));
  };

  *s();

I don't have time right now, have you tried it with smth like Finally?