Skip to content

Update ThreadSafeFunction::{Non}BlockingCall to forward user callback #890

@ekraihan

Description

@ekraihan

The {Non}BlockingCall methods of ThreadSafeFunction take the user-provided "callback" argument by value and then copy it into an std::function. I request that the callback argument be taken by forwarding reference and that the callback be forwarded to its final destination. Example:

template <typename DataType, typename Callback>
inline napi_status ThreadSafeFunction::BlockingCall(
    DataType* data, Callback&& callback) const {
  auto wrapper = [data, callback=std::forward<Callback>(callback)](Env env, Function jsCallback) {
    callback(env, jsCallback, data);
  };
  return CallInternal(new CallbackWrapper(std::move(wrapper)), napi_tsfn_blocking);
}

Note that wrapper is also moved into CallbackWrapper.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions