{"_id":"napi-thread-safe-callback","_rev":"6-23aace86d7c635c4d01024a8a58f6703","name":"napi-thread-safe-callback","description":"C++ utility class to perform callbacks into JavaScript from any thread","dist-tags":{"latest":"0.0.6"},"versions":{"0.0.1":{"name":"napi-thread-safe-callback","version":"0.0.1","description":"C++ utility class to perform callbacks into JavaScript from any thread","main":"index.js","scripts":{"test":"echo \"Error: no test specified\" && exit 1"},"keywords":["napi","c++","callback","thread"],"author":{"name":"Mika Fischer","email":"mika.fischer@zoopnet.de","url":"https://github.com/mika-fischer"},"license":"ISC","homepage":"https://github.com/mika-fischer/napi-thread-safe-callback","bugs":{"url":"https://github.com/mika-fischer/napi-thread-safe-callback/issues"},"repository":{"type":"git","url":"git+https://github.com/mika-fischer/napi-thread-safe-callback.git"},"gitHead":"de5cd83bd3458b8f9791e156bf7ea70aa6e104fd","_id":"napi-thread-safe-callback@0.0.1","_npmVersion":"5.0.3","_nodeVersion":"8.1.4","_npmUser":{"name":"mika-fischer","email":"mika.fischer@zoopnet.de"},"dist":{"integrity":"sha512-K6qUIaHuHnPt1xxb9oY59PWAVBO3HYkxI+9KQJRMavB/0nTg4mTT7eEl3X3m9grnTmZcldGwVQTAEOgaWWOMfA==","shasum":"e18501f12f7f8b964beacd49ce5d38caf0148c1a","tarball":"https://registry.npmjs.org/napi-thread-safe-callback/-/napi-thread-safe-callback-0.0.1.tgz","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIDo93nVWtmjkYv85bOGWXC0nSfz/h8JjemdzEIntNjbhAiEA2SuKb4qi2atAtiP0GfM3yeLV2O5SF8Qj/9AJooqFg2o="}]},"maintainers":[{"name":"mika-fischer","email":"mika.fischer@zoopnet.de"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/napi-thread-safe-callback-0.0.1.tgz_1500549919184_0.42574666417203844"},"directories":{}},"0.0.2":{"name":"napi-thread-safe-callback","version":"0.0.2","description":"C++ utility class to perform callbacks into JavaScript from any thread","main":"index.js","scripts":{"test":"npm run test-rebuild && npm run test-run","test-build":"node-gyp build --directory test","test-configure":"node-gyp configure --directory test --debug","test-rebuild":"node-gyp rebuild --directory test --debug","test-run":"cross-env NODE_OPTIONS=--napi-modules mocha --reporter spec"},"keywords":["napi","c++","callback","thread"],"author":{"name":"Mika Fischer","email":"mika.fischer@zoopnet.de","url":"https://github.com/mika-fischer"},"license":"ISC","homepage":"https://github.com/mika-fischer/napi-thread-safe-callback","bugs":{"url":"https://github.com/mika-fischer/napi-thread-safe-callback/issues"},"repository":{"type":"git","url":"git+https://github.com/mika-fischer/napi-thread-safe-callback.git"},"devDependencies":{"cross-env":"^5.0.5","mocha":"^3.5.0","node-addon-api":"^0.6.3"},"gitHead":"e03dda83fb1d2a63fb8332406e2436fc18a63fba","_id":"napi-thread-safe-callback@0.0.2","_npmVersion":"5.3.0","_nodeVersion":"8.4.0","_npmUser":{"name":"mika-fischer","email":"mika.fischer@zoopnet.de"},"dist":{"integrity":"sha512-fNkQOIuqYhQIM7cJUoYOTBHkHA1doJkFuAr/Xl9jrU4MrP99LYyblsybFyYS/BsRA4LiKQzP6/Z+KgJrZW2Tdg==","shasum":"a462e1d8d91aae1bbe5756bc5292912ebcbb2572","tarball":"https://registry.npmjs.org/napi-thread-safe-callback/-/napi-thread-safe-callback-0.0.2.tgz","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIEhZk6W7bGCC/LIlVRYMqgBwmvVqoCB/34U41Ckt4yHIAiEAr3K+UfqiMs7t37CzC+YN6QbAs8yzTuQvvsrKkpRzR50="}]},"maintainers":[{"name":"mika-fischer","email":"mika.fischer@zoopnet.de"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/napi-thread-safe-callback-0.0.2.tgz_1504256993009_0.3672533198259771"},"directories":{}},"0.0.3":{"name":"napi-thread-safe-callback","version":"0.0.3","description":"C++ utility class to perform callbacks into JavaScript from any thread","main":"index.js","scripts":{"test":"npm run test-rebuild && npm run test-run","test-build":"node-gyp build --directory test --debug","test-configure":"node-gyp configure --directory test --debug","test-rebuild":"node-gyp rebuild --directory test --debug","test-run":"mocha --reporter spec"},"keywords":["napi","c++","callback","thread"],"author":{"name":"Mika Fischer","email":"mika.fischer@zoopnet.de","url":"https://github.com/mika-fischer"},"license":"ISC","homepage":"https://github.com/mika-fischer/napi-thread-safe-callback","bugs":{"url":"https://github.com/mika-fischer/napi-thread-safe-callback/issues"},"repository":{"type":"git","url":"git+https://github.com/mika-fischer/napi-thread-safe-callback.git"},"devDependencies":{"mocha":"^3.5.0","node-addon-api":"^1.0.0"},"gitHead":"072d0ec2df860bdfb1d0b526530c55877f5c5c91","_id":"napi-thread-safe-callback@0.0.3","_npmVersion":"5.3.0","_nodeVersion":"8.6.0","_npmUser":{"name":"mika-fischer","email":"mika.fischer@zoopnet.de"},"dist":{"integrity":"sha512-nyZwUryORfKML1ub68MJ4tZFGVvf9gD1yxirFkEJCVLlRpp+yTg80sBu5QrdCuRhGIvIoQWcHMxqAc206xSUJg==","shasum":"7716f2b41cf91b34d8b1b641bbf75c0d8199e303","tarball":"https://registry.npmjs.org/napi-thread-safe-callback/-/napi-thread-safe-callback-0.0.3.tgz","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIEwCaLyu6NYZMH7qMAU4rI7JGtF+isB7LkRP0MNgHoWZAiAKHM4UkvEdIjq2D/8D9vpl1G9fc3B0YiN7FLNhXxaMRw=="}]},"maintainers":[{"name":"mika-fischer","email":"mika.fischer@zoopnet.de"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/napi-thread-safe-callback-0.0.3.tgz_1506683286723_0.9181186957284808"},"directories":{}},"0.0.4":{"name":"napi-thread-safe-callback","version":"0.0.4","description":"C++ utility class to perform callbacks into JavaScript from any thread","main":"index.js","scripts":{"test":"npm run test-rebuild && npm run test-run","test-build":"node-gyp build --directory test --debug","test-configure":"node-gyp configure --directory test --debug","test-rebuild":"node-gyp rebuild --directory test --debug","test-run":"mocha --reporter spec"},"keywords":["napi","c++","callback","thread"],"author":{"name":"Mika Fischer","email":"mika.fischer@zoopnet.de","url":"https://github.com/mika-fischer"},"license":"ISC","homepage":"https://github.com/mika-fischer/napi-thread-safe-callback","bugs":{"url":"https://github.com/mika-fischer/napi-thread-safe-callback/issues"},"repository":{"type":"git","url":"git+https://github.com/mika-fischer/napi-thread-safe-callback.git"},"devDependencies":{"mocha":"^3.5.0","node-addon-api":"^1.0.0"},"gitHead":"0353696142d499f964702684805b82252705f1f9","_id":"napi-thread-safe-callback@0.0.4","_npmVersion":"5.3.0","_nodeVersion":"8.6.0","_npmUser":{"name":"mika-fischer","email":"mika.fischer@zoopnet.de"},"dist":{"integrity":"sha512-2SwUlbL0nzTE/oSmZfEWM9vgeqaUvxYrVVQjEbos3mX7Znxj4erDtLRUsJBneJSGl6NUEBrYa/fNmI3aPnoYuw==","shasum":"ec447e6c88ca2a94d2d6a48944fc68a62eecc3c1","tarball":"https://registry.npmjs.org/napi-thread-safe-callback/-/napi-thread-safe-callback-0.0.4.tgz","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDTsGqSIkjsFfFMKYQIWeUHD4/4HCf4gjatovc9ClMg3QIhAMN3kaC2aK0StoFPoZamUysu5lNOw6EuSi50Un9vUW8Q"}]},"maintainers":[{"name":"mika-fischer","email":"mika.fischer@zoopnet.de"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/napi-thread-safe-callback-0.0.4.tgz_1507296221863_0.15870536514557898"},"directories":{}},"0.0.5":{"name":"napi-thread-safe-callback","version":"0.0.5","description":"C++ utility class to perform callbacks into JavaScript from any thread","main":"index.js","scripts":{"test":"npm run test-rebuild && npm run test-run","test-build":"node-gyp build --directory test --debug","test-configure":"node-gyp configure --directory test --debug","test-rebuild":"node-gyp rebuild --directory test --debug","test-run":"mocha --reporter spec"},"keywords":["napi","c++","callback","thread"],"author":{"name":"Mika Fischer","email":"mika.fischer@zoopnet.de","url":"https://github.com/mika-fischer"},"license":"ISC","homepage":"https://github.com/mika-fischer/napi-thread-safe-callback","bugs":{"url":"https://github.com/mika-fischer/napi-thread-safe-callback/issues"},"repository":{"type":"git","url":"git+https://github.com/mika-fischer/napi-thread-safe-callback.git"},"devDependencies":{"mocha":"^4.0.1","node-addon-api":"^1.1.0"},"gitHead":"95df75f3b5bab6666446e49fe28aaf3000442e92","_id":"napi-thread-safe-callback@0.0.5","_npmVersion":"5.3.0","_nodeVersion":"8.6.0","_npmUser":{"name":"mika-fischer","email":"mika.fischer@zoopnet.de"},"dist":{"integrity":"sha512-iU67HCzxmxVuPwRjjjY3LjakNZ2bYXjBguM1zuI8CAUX4sHfgbM3qKPy6obR7qX+VI3y2A1PWIwZhdv8SkHNgQ==","shasum":"8dd33e685e6bc8407348451cab9241553f7df91f","tarball":"https://registry.npmjs.org/napi-thread-safe-callback/-/napi-thread-safe-callback-0.0.5.tgz","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIBje2DK3HrkIJQp/jJScGXvrAc7VEsPJ+InQB5TMddu1AiAvSAxDxzUnopTVktwO2On4zKr5aOoz2BVBnG84o2Nfsg=="}]},"maintainers":[{"name":"mika-fischer","email":"mika.fischer@zoopnet.de"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/napi-thread-safe-callback-0.0.5.tgz_1511885239866_0.7584840580821037"},"directories":{}},"0.0.6":{"name":"napi-thread-safe-callback","version":"0.0.6","description":"C++ utility class to perform callbacks into JavaScript from any thread","main":"index.js","scripts":{"test":"npm run test-rebuild && npm run test-run","test-build":"node-gyp build --directory test --debug","test-configure":"node-gyp configure --directory test --debug","test-rebuild":"node-gyp rebuild --directory test --debug","test-run":"mocha --reporter spec"},"keywords":["napi","c++","callback","thread"],"author":{"name":"Mika Fischer","email":"mika.fischer@zoopnet.de","url":"https://github.com/mika-fischer"},"license":"ISC","homepage":"https://github.com/mika-fischer/napi-thread-safe-callback","bugs":{"url":"https://github.com/mika-fischer/napi-thread-safe-callback/issues"},"repository":{"type":"git","url":"git+https://github.com/mika-fischer/napi-thread-safe-callback.git"},"devDependencies":{"mocha":"^5.0.5","node-addon-api":"^1.2.0"},"gitHead":"e78c4a676121b91881527bcd14965b5e62a90220","_id":"napi-thread-safe-callback@0.0.6","_npmVersion":"5.6.0","_nodeVersion":"8.11.1","_npmUser":{"name":"mika-fischer","email":"mika.fischer@zoopnet.de"},"dist":{"integrity":"sha512-X7uHCOCdY4u0yamDxDrv3jF2NtYc8A1nvPzBQgvpoSX+WB3jAe2cVNsY448V1ucq7Whf9Wdy02HEUoLW5rJKWg==","shasum":"ef86a149b5312e480f74e89a614e6d9e3b17b456","tarball":"https://registry.npmjs.org/napi-thread-safe-callback/-/napi-thread-safe-callback-0.0.6.tgz","fileCount":11,"unpackedSize":39553,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJbg8mbCRA9TVsSAnZWagAAHwAQAItN+94sHaYfyPGY/mvY\nQhWZKIdUTK7Wpmyp5sn++pF9JKKKj9f3BpZveMFI8zUeIFjTc/TDG7Gt7iEY\nLNPUF/rAtNS7drmf1d8Yqaru6CZiiU6M1sJp3AWX0ptnw915PmXQRykMOVRt\neVlQJMcJESKpHTxp2+qWJHTDrSWB5eGe49ZeUikgC13SlXV177AsjHwetZH9\nxepDVgTsyxfO5rmxnVAZ2GjFlH7u8N1ao6pyHC6NTIIDb8jJK+/qWb8xXkwo\nFQcnVLuxY5oD82Z/VrBMFuR+Rs+V5+nGmVTqO8lp5t5tUAJJY8z0omTtJMZN\nRsE0ZjEKSu0nkttY0roxjBznzBSJS52X0bcwu1cnFuJCCVPGTc7FRDm1E5E5\n9AT8lTHVm0vUvnlo3D6zNTFm+CPc3qjMHRseIy8X0M9Lsfkc8fnK2PEC5RJy\n+r4vl9408pZf7o3fuMrnkU2ure8K+i8R6yVSWYNE3X/OA9CHknINjpaf4gxH\nrNmWIZVgh2E6uOxn0/m/5+a+xi2A5R3QQLpYTyh5C68gebBBvKvVspEoDMD/\n+tuwot7Z6rvApPEy1Lbt3+odBKQacI17VB1NXrgm0AkY/4WkdCQ6C+0X9Qgx\nYVGr07BsyCNCqSCaaCv2VBVagF/1K5RIrda6bZRwRdrtiQjMfa4mocoNx0hW\nTvxE\r\n=9fHF\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIDK/fYroh+gP/6l6MrZkqYZoG8Qienpn0y0Rwya7XWqUAiEAkiJjbWTIor7YPqi2+fqlcfDSCg07qyKIEu1QckZj/vI="}]},"maintainers":[{"name":"mika-fischer","email":"mika.fischer@zoopnet.de"}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/napi-thread-safe-callback_0.0.6_1535363483275_0.2689624558866208"},"_hasShrinkwrap":false}},"readme":"# Napi Thread-Safe Callback\r\n\r\n[![npm](https://img.shields.io/npm/v/napi-thread-safe-callback.svg)](https://www.npmjs.com/package/napi-thread-safe-callback)\r\n[![Build Status](https://travis-ci.org/mika-fischer/napi-thread-safe-callback.svg?branch=master)](https://travis-ci.org/mika-fischer/napi-thread-safe-callback)\r\n[![Build status](https://ci.appveyor.com/api/projects/status/475okhfy85tkeah7?svg=true)](https://ci.appveyor.com/project/mika-fischer/napi-thread-safe-callback)\r\n[![dependencies Status](https://david-dm.org/mika-fischer/napi-thread-safe-callback/status.svg?style=flat)](https://david-dm.org/mika-fischer/napi-thread-safe-callback)\r\n[![devDependencies Status](https://david-dm.org/mika-fischer/napi-thread-safe-callback/dev-status.svg?style=flat)](https://david-dm.org/mika-fischer/napi-thread-safe-callback?type=dev)\r\n\r\nThis package contains a header-only C++ helper class to facilitate\r\ncalling back into JavaScript from threads other than the Node.JS main thread.\r\n\r\n# Examples\r\n\r\n## Perform async work in new thread and call back with result/error\r\n```C++\r\nvoid example_async_work(const CallbackInfo& info)\r\n{\r\n    // Capture callback in main thread\r\n    auto callback = std::make_shared<ThreadSafeCallback>(info[0].As<Function>());\r\n    bool fail = info.Length() > 1;\r\n\r\n    // Pass callback to other thread\r\n    std::thread([callback, fail]\r\n    {\r\n        try\r\n        {\r\n            // Do some work to get a result\r\n            if (fail)\r\n                throw std::runtime_error(\"Failure during async work\");\r\n            std::string result = \"foo\";\r\n\r\n            // Call back with result\r\n            callback->call([result](Napi::Env env, std::vector<napi_value>& args)\r\n            {\r\n                // This will run in main thread and needs to construct the\r\n                // arguments for the call\r\n                args = { env.Undefined(), Napi::String::New(env, result) };\r\n            });\r\n        }\r\n        catch (std::exception& e)\r\n        {\r\n            // Call back with error\r\n            callback->callError(e.what());\r\n        }\r\n    }).detach();\r\n}\r\n```\r\n\r\n## Perform async work in new thread, call back with result/error and check return value\r\n```C++\r\nvoid example_async_return_value(const CallbackInfo& info)\r\n{\r\n    auto callback = std::make_shared<ThreadSafeCallback>(info[0].As<Function>());\r\n    std::thread([callback]\r\n    {\r\n        try\r\n        {\r\n            int result = 0;\r\n            while (true)\r\n            {\r\n                // Do some work...\r\n                try\r\n                {\r\n                    result += 1;\r\n                    if (result > 20)\r\n                        throw std::runtime_error(\"Failure during async work\");\r\n                }\r\n                catch (std::exception &e)\r\n                {\r\n                    // Call back with error\r\n                    // Note that this will cause the thread to exit and the\r\n                    // ThreadSafeCallback to be destroyed. But the callback\r\n                    // will still be called, even afterwards\r\n                    callback->callError(e.what());\r\n                    throw;\r\n                }\r\n                // Call back into Javascript with current result\r\n                auto future = callback->call<bool>(\r\n                    [result](Env env, std::vector<napi_value> &args) {\r\n                        args = {env.Undefined(), Number::New(env, result)};\r\n                    },\r\n                    [](const Value &val) {\r\n                        return val.As<Boolean>().Value();\r\n                    });\r\n                // This blocks until the JS call has returned a value\r\n                // In case the callback throws we just let the exception\r\n                // bubble up and end the thread\r\n                auto continue_running = future.get();\r\n                if (!continue_running)\r\n                    break;\r\n            }\r\n        }\r\n        catch (...) \r\n        {\r\n            // Ignore errors\r\n        }\r\n    }).detach();\r\n}\r\n```\r\n\r\n## Usage\r\n\r\n  1. Add a dependency on this package to `package.json`:\r\n```json\r\n  \"dependencies\": {\r\n    \"napi-thread-safe-callback\": \"0.0.1\",\r\n  }\r\n```\r\n\r\n  2. Reference this package's include directory in `binding.gyp`:\r\n```gyp\r\n  'include_dirs': [\"<!@(node -p \\\"require('napi-thread-safe-callback').include\\\")\"],\r\n```\r\n  3. Include the header in your code:\r\n```C++\r\n#include \"napi-thread-safe-callback.hpp\"\r\n```\r\n\r\n### Exception handling\r\n\r\nExceptions need to be enabled in the native module. To do this use the following\r\nin `binding.gyp`:\r\n```gyp\r\n  'cflags!': [ '-fno-exceptions' ],\r\n  'cflags_cc!': [ '-fno-exceptions' ],\r\n  'xcode_settings': {\r\n    'GCC_ENABLE_CPP_EXCEPTIONS': 'YES',\r\n    'CLANG_CXX_LIBRARY': 'libc++',\r\n    'MACOSX_DEPLOYMENT_TARGET': '10.7',\r\n  },\r\n  'msvs_settings': {\r\n    'VCCLCompilerTool': { 'ExceptionHandling': 1 },\r\n  },\r\n  'conditions': [\r\n    ['OS==\"win\"', { 'defines': [ '_HAS_EXCEPTIONS=1' ] }]\r\n  ]\r\n```\r\n","maintainers":[{"name":"mika-fischer","email":"mika.fischer@zoopnet.de"}],"time":{"modified":"2022-05-10T09:12:25.746Z","created":"2017-07-20T11:25:20.152Z","0.0.1":"2017-07-20T11:25:20.152Z","0.0.2":"2017-09-01T09:09:53.969Z","0.0.3":"2017-09-29T11:08:07.587Z","0.0.4":"2017-10-06T13:23:42.778Z","0.0.5":"2017-11-28T16:07:20.810Z","0.0.6":"2018-08-27T09:51:23.353Z"},"homepage":"https://github.com/mika-fischer/napi-thread-safe-callback","keywords":["napi","c++","callback","thread"],"repository":{"type":"git","url":"git+https://github.com/mika-fischer/napi-thread-safe-callback.git"},"author":{"name":"Mika Fischer","email":"mika.fischer@zoopnet.de","url":"https://github.com/mika-fischer"},"bugs":{"url":"https://github.com/mika-fischer/napi-thread-safe-callback/issues"},"license":"ISC","readmeFilename":"README.md"}