Skip to content

Commit

Permalink
handle injection of execution_count
Browse files Browse the repository at this point in the history
  • Loading branch information
DerThorsten committed Mar 20, 2024
1 parent 36592d5 commit 94b2f28
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
3 changes: 3 additions & 0 deletions include/xeus/xrequest_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ namespace xeus
xexecute_request_context(nl::json header, channel origin, guid_list id, std::function<void(const xexecute_request_context&,nl::json)> on_send_reply);

void send_reply(nl::json reply);
void set_on_send_callback(std::function<void(const xexecute_request_context&,nl::json)> augment_reply);
std::function<void(const xexecute_request_context&, nl::json)> get_on_send_callback() const;

private:
std::function<void(const xexecute_request_context&, nl::json)> m_on_send_reply;
};
Expand Down
12 changes: 10 additions & 2 deletions src/xinterpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,23 @@ namespace xeus
++m_execution_count;
publish_execution_input(context, code, m_execution_count);
}
// copy m_execution_count in a local variable to capture it in the lambda
auto execution_count = m_execution_count;

auto inner_callback = context.get_on_send_callback();
context.set_on_send_callback( [inner_callback, execution_count](const xexecute_request_context& context,nl::json reply)
{
reply["execution_count"] = execution_count;
inner_callback(context, reply);
});


execute_request_impl(
std::move(context),
m_execution_count, code, silent,
store_history, user_expressions, allow_stdin
);

// reply["execution_count"] = m_execution_count;
// return reply;
}

nl::json xinterpreter::complete_request(const std::string& code, int cursor_pos)
Expand Down
12 changes: 12 additions & 0 deletions src/xrequest_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,16 @@ namespace xeus
m_on_send_reply(std::move(on_send_reply))
{
}

// get and set callback
void xexecute_request_context::set_on_send_callback(std::function<void(const xexecute_request_context&,nl::json)> augment_reply)
{
m_on_send_reply = std::move(augment_reply);
}

// get callback
std::function<void(const xexecute_request_context&, nl::json)> xexecute_request_context::get_on_send_callback() const
{
return m_on_send_reply;
}
}

0 comments on commit 94b2f28

Please sign in to comment.