npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

napi_thread_safe_promise

v1.2.6

Published

C++ napi threadd safe promises

Downloads

49

Readme

NAPI-Thread-Safe-Promise

npm version https://img.shields.io/maintenance/yes/2021 https://img.shields.io/github/license/SurienDG/NAPI-Thread-Safe-Promise

Introduction

NOTE: This package assumes understanding of N-API

To be used in conjunction with the N-API module to call C++ code from JavaScript.

Allows the user to call thread safe promise functions such (resolve, reject) in the C++ code and return a promise to the javascript code.

Usage

  1. Install the package using node package manager:
npm install napi_thread_safe_promise
  1. Reference this package's include directory in binding.gyp:
'include_dirs': ["<!@(node -p \"require('napi_thread_safe_promise').include\")"],
  1. Include the header in your code:
#include "promiseWrapper.h"

Note: Currently latest node version has bugs with regards to gyp so use the latest stable version (should be v14.15.3 as of now)

Exception handling

To have the ability for exceptions add the following to binding.gyp:

'cflags!': [ '-fno-exceptions' ],
'cflags_cc!': [ '-fno-exceptions' ],
'xcode_settings': {
  'GCC_ENABLE_CPP_EXCEPTIONS': 'YES',
  'CLANG_CXX_LIBRARY': 'libc++',
  'MACOSX_DEPLOYMENT_TARGET': '10.7',
},
'msvs_settings': {
  'VCCLCompilerTool': { 'ExceptionHandling': 1 },
},
'conditions': [
  ['OS=="win"', { 'defines': [ '_HAS_EXCEPTIONS=1' ] }]
]

Examples

Async Promise Example (with macro)

Napi::Promise promiseFuncJS(const CallbackInfo& info)
{
  // macro takes in info variable as input
  return PROMISE(info, {
      // now we can write our code here with access to the resolve and reject functions

      // we can now take in resolve and reject function to our threaded function because they are thread safe
      // note other variables we declared here may not be thread safe (ex. info variable is not thread safe) 
      std::thread([resolve, reject]() {
          // we can pass strings to our resolve and reject functions
          resolve("test");

          // note secondary calls to resolve or reject will have no effect
           resolve("test123");
      }).detach();

  });
}

Note: if we wish to pass json data to our resolve function we just pass in the json string to the function and parse it in the javascript code

We can use a library like https://github.com/nlohmann/json and use the dump function to accomplish this

Async Promise Example (without using macro)

Napi::Promise promiseFuncJS(const CallbackInfo& info) {
  return promiseFuncWrapper(info.Env(),
      
      [&info](resolveFunc resolve, rejectFunc reject) { // anonymous function passed to thread safe resolve and reject functions

        // here we can write our threaded code

          std::string arg1 = info[0].As<Napi::String>();
          std::thread([resolve, reject, arg1]() {
            reject(arg1);
          }).detach();
      }
  );
}

JavaScript code for the code examples above

promiseFuncJS(test)
  .then(output => {
    console.log(output);
  })
  .catch(err => {
    console.error(err);
  });

Contribution

For contribution to this package, create a pull request and the maintainer will look into it.

License

NAPI-Thread-Safe-Promise is licensed under BSD-3-Clause.