|
| 1 | +/** |
| 2 | + * This test is programmatically represents the example shown in |
| 3 | + * `doc/threadsafe_function_ex.md` |
| 4 | + */ |
| 5 | + |
| 6 | +#include "napi.h" |
| 7 | + |
| 8 | +#if (NAPI_VERSION > 3) |
| 9 | + |
| 10 | +using namespace Napi; |
| 11 | + |
| 12 | +namespace { |
| 13 | + |
| 14 | +// Context of our TSFN. |
| 15 | +struct Context {}; |
| 16 | + |
| 17 | +// Data passed (as pointer) to ThreadSafeFunctionEx::[Non]BlockingCall |
| 18 | +using DataType = int; |
| 19 | + |
| 20 | +// Callback function |
| 21 | +static void Callback(Napi::Env env, Napi::Function jsCallback, Context *context, |
| 22 | + DataType *data) { |
| 23 | + // Check that the threadsafe function has not been finalized. Node calls this |
| 24 | + // callback for items remaining on the queue once finalization has completed. |
| 25 | + if (!(env == nullptr || jsCallback == nullptr)) { |
| 26 | + } |
| 27 | + if (data != nullptr) { |
| 28 | + delete data; |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +// Full type of our ThreadSafeFunctionEx |
| 33 | +using TSFN = ThreadSafeFunctionEx<Context, DataType, Callback>; |
| 34 | + |
| 35 | +// A JS-accessible wrap that holds a TSFN. |
| 36 | +class TSFNWrap : public ObjectWrap<TSFNWrap> { |
| 37 | + |
| 38 | +public: |
| 39 | + static Object Init(Napi::Env env, Object exports); |
| 40 | + TSFNWrap(const CallbackInfo &info); |
| 41 | + |
| 42 | +private: |
| 43 | + Napi::Value Start(const CallbackInfo &info); |
| 44 | + Napi::Value Release(const CallbackInfo &info); |
| 45 | +}; |
| 46 | + |
| 47 | +/** |
| 48 | + * @brief Initialize `TSFNWrap` on the environment. |
| 49 | + * |
| 50 | + * @param env |
| 51 | + * @param exports |
| 52 | + * @return Object |
| 53 | + */ |
| 54 | +Object TSFNWrap::Init(Napi::Env env, Object exports) { |
| 55 | + Function func = DefineClass(env, "TSFNWrap", |
| 56 | + {InstanceMethod("start", &TSFNWrap::Start), |
| 57 | + InstanceMethod("release", &TSFNWrap::Release)}); |
| 58 | + |
| 59 | + exports.Set("TSFNWrap", func); |
| 60 | + return exports; |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * @brief Construct a new TSFNWrap::TSFNWrap object |
| 65 | + * |
| 66 | + * @param info |
| 67 | + */ |
| 68 | +TSFNWrap::TSFNWrap(const CallbackInfo &info) : ObjectWrap<TSFNWrap>(info) {} |
| 69 | +} // namespace |
| 70 | + |
| 71 | +/** |
| 72 | + * @brief Instance method `TSFNWrap#start` |
| 73 | + * |
| 74 | + * @param info |
| 75 | + * @return undefined |
| 76 | + */ |
| 77 | +Napi::Value TSFNWrap::Start(const CallbackInfo &info) { |
| 78 | + Napi::Env env = info.Env(); |
| 79 | + return env.Undefined(); |
| 80 | +}; |
| 81 | + |
| 82 | +/** |
| 83 | + * @brief Instance method `TSFNWrap#release` |
| 84 | + * |
| 85 | + * @param info |
| 86 | + * @return undefined |
| 87 | + */ |
| 88 | +Napi::Value TSFNWrap::Release(const CallbackInfo &info) { |
| 89 | + Napi::Env env = info.Env(); |
| 90 | + return env.Undefined(); |
| 91 | +}; |
| 92 | + |
| 93 | +/** |
| 94 | + * @brief Module initialization function |
| 95 | + * |
| 96 | + * @param env |
| 97 | + * @return Object |
| 98 | + */ |
| 99 | +Object InitThreadSafeFunctionExExample(Env env) { |
| 100 | + return TSFNWrap::Init(env, Object::New(env)); |
| 101 | +} |
| 102 | + |
| 103 | +#endif |
0 commit comments