Skip to content

Add support for callback scopes #223

Closed

Description

@mhdawson

Now that nodejs/node#18089 has landed we should add equivalent support in node-addon-api.

Activity

  1. romandev commented on Feb 6, 2018

    @romandev
    Contributor

    I'll take this issue.

  2. mhdawson commented on Feb 7, 2018

    @mhdawson
    MemberAuthor

    @romandev thanks !

  3. romandev commented on Feb 19, 2018

    @romandev
    Contributor

    @mhdawson,

    Sorry for delayed work. I was on a vacation until last week. I'm looking into this issue now but need your confirmation.

    Shouldn't we need to implement the async hooks wrapper(e.g. napi_async_init) first before implementing this feature? I found the related comments in the PR[1] but didn't find any follow-up change in C++ wrapper side. Do you have a plan to make a followup change? Otherwise, I'm available.

    [1] #140

  4. mhdawson commented on Mar 1, 2018

    @mhdawson
    MemberAuthor

    @romandev that is a very good point. If you can work on adding async hooks wrapper that would be great :)

  5. ebickle commented on Mar 5, 2018

    @ebickle

    Is there any way to use callback scopes from a napi_async_work? Normally an async worker would create it's Javascript values from within the scope of the complete_callback, but there's an issue right now around error handling - there's no way to return a Javascript error from the execute_callback, just a string or manual workarounds.

    Noticed the comments here mentioning an async hooks wrapper for napi_async_init; might be another use-case worth thinking about.

  6. rolftimmermans commented on Mar 5, 2018

    @rolftimmermans
    Contributor

    What's the story for new features like this when running precompiled libraries on older versions of Node? Would this library provide a backport for older Node.js versions or would a library depending on nodejs/node#18089 have to target Node.js 9.6.0+ only?

  7. mhdawson commented on Mar 5, 2018

    @mhdawson
    MemberAuthor

    @rolftimmermans, in general not all new features would be backported to earlier versions. However, I am hoping that we will backport what becomes non-experimental in 10.x to 8.x (and we'll have to look at 6.x) in order to accelerate initial adoption.

    The complication is that not all versions of 8.x will have the feature, is that something you can work around by requiring Node.js 8.X or higher ?

  8. mhdawson commented on Mar 5, 2018

    @mhdawson
    MemberAuthor

    @ebickle I have a todo to look at the error issue with complete_callback, but I'm going to be travelling a fair amount over the next 3 weeks so I'm not quite sure how/when I'm going to get to it.

  9. rolftimmermans commented on Mar 6, 2018

    @rolftimmermans
    Contributor

    @mhdawson I guess it is possible to depend on a certain 8.X version, but it's less than ideal. The feature was introduced in 9.6 it seems, so not sure if the specific 8.X version would matter?

  10. mhdawson commented on Mar 6, 2018

    @mhdawson
    MemberAuthor

    The issue is that if/when we backport, it will go into the latest 8.X release. So it would be available in 8.Y or later and 9.6 and later.

  11. rolftimmermans commented on Mar 6, 2018

    @rolftimmermans
    Contributor

    The issue is that if/when we backport, it will go into the latest 8.X release. So it would be available in 8.Y or later and 9.6 and later.

    I see. It's probably less than ideal because a lot of distros have already shipped with Node 8. So people who can easily install a future 8.X version can probably also install 9 or 10.

  12. rolftimmermans commented on Mar 6, 2018

    @rolftimmermans
    Contributor

    The following code is how I implemented a C++ wrapper in my code currently. Maybe this is useful for someone looking to create a similar wrapper in this project?

    #define NAPI_THROW_IF_FAILED(env, status, ...)                                   \
        if ((status) != napi_ok) {                                                   \
            Napi::Error::New(env).ThrowAsJavaScriptException();                      \
            return;                                                                  \
        }
    
    struct CallbackScope {
        napi_env env;
        napi_handle_scope handle_scope;
        napi_callback_scope callback_scope;
    
        CallbackScope(napi_env env) : env(env) {
            napi_status status;
            napi_async_context context;
            napi_value resource_name, resource_object;
    
            status = napi_open_handle_scope(env, &handle_scope);
            NAPI_THROW_IF_FAILED(env, status);
    
            status = napi_create_string_utf8(env, "zmq", NAPI_AUTO_LENGTH, &resource_name);
            NAPI_THROW_IF_FAILED(env, status);
    
            status = napi_create_object(env, &resource_object);
            NAPI_THROW_IF_FAILED(env, status);
    
            status = napi_async_init(env, resource_object, resource_name, &context);
            NAPI_THROW_IF_FAILED(env, status);
    
            status = napi_open_callback_scope(env, resource_object, context, &callback_scope);
            NAPI_THROW_IF_FAILED(env, status);
        }
    
        ~CallbackScope() {
            napi_status status;
            status = napi_close_callback_scope(env, callback_scope);
            NAPI_THROW_IF_FAILED(env, status);
    
            status = napi_close_handle_scope(env, handle_scope);
            NAPI_THROW_IF_FAILED(env, status);
        }
    };
    
  13. mhdawson commented on Mar 6, 2018

    @mhdawson
    MemberAuthor

    @rolftimmermans thanks.

    @romandev FYI, in case you are first to get to working on adding support to the wrapper for callback scopes.

  14. NickNaso commented on Oct 21, 2018

    @NickNaso
    Member

    Hi everyone I think that this issue could be close when we will land this PR #362 Is it right?

  15. NickNaso commented on Oct 23, 2018

    @NickNaso
    Member

    @mhdawson could we close this issue now?

  16. mhdawson commented on Oct 23, 2018

    @mhdawson
    MemberAuthor

    Closing

  17. NickNaso commented on Nov 2, 2018

    @NickNaso
    Member

    I'm closing this, we just released version 1.6.0 that contains the Napi::CallbackScope. Feel free to reopen if I misunderstood something.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions