You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to do something like node-usb-detection but using node-addon-api. When a USB drive was unmounted from the computer, the addon code should send an event to the Javascript code.
// Main.hextern Napi::Function gEmitter;
extern napi_env gEnv;
// Main.cc
#include<napi.h>
#include<node_api.h>
#include"Main.h"
Napi::Function gEmitter;
napi_env gEnv;
// Q1: is it okay to cache JS emitter as a global variable in addon?// Q2: is it necessary to cache the `env` object?voidBindEmitter(const Napi::CallbackInfo& info) {
gEnv = info.Env();
gEmitter = info[0].As<Napi::Function>();
}
Napi::Object Init(Napi::Env env, Napi::Object exports) {
exports.Set(Napi::String::New(env, "bindEmitter"), Napi::Function::New(env, BindEmitter));
return exports;
}
NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init)
voidNapi_DiskUtility::ObserveUnmount(const Napi::CallbackInfo &info) {
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserverForName:NSWorkspaceDidUnmountNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notification) {
NSDictionary *userInfo = [(NSDictionary *)notification valueForKey:@"userInfo"];
NSString *devicePath = [userInfo valueForKey:@"NSDevicePath"];
NSLog(@"Drive unmounted: %@", devicePath);
// Q3: this line crashes, the error is `Fatal error in V8: v8::HandleScope::CreateHandle() Cannot create a handle without a HandleScope`gEmit.Call({
Napi::String::New(gEnv, "unmount"), Napi::String::New(gEnv, devicePath.UTF8String)
});
}];
}
Please refer to the 3 questions in my code. I believe I am not using handle scope properly, but I am not sure what is the right way to make it right. Ideally I want to use emitter to emit events to Javascript from any native threads.
Further more, if this demo can work, I would extend it and wrap a logging method so that all addon logs can be emitted to Javascript and be processed using the same Javascript logging package.
Is it possible? Thank you for your help!
The text was updated successfully, but these errors were encountered:
Q1) No. doing that caching means that multiple contexts (typically worker-threads, might be other ways too) will get their contexts mixed up, as this global will be shared across them all.
Q2) No. anywhere you are allowed to use it, you will have easy access to it
Q3) You aren't allowed to do this call from other threads.
Instead you can do this via a TypedThreadSafeFunction, as you can essentially call that function with some non-node data types, and in the callback you are allowed to use env and node-api types so can convert it to node-api types and do your final call.
Perhaps a useful reference is a PR I was working on to convert node-usb-detection to node-api MadLittleMods/node-usb-detection#127. I dont know how good this code is, and I think there were some memory leaks, but it did work and I think is mostly sane
I am trying to do something like node-usb-detection but using
node-addon-api
. When a USB drive was unmounted from the computer, the addon code should send an event to the Javascript code.Here is what I have:
Please refer to the 3 questions in my code. I believe I am not using handle scope properly, but I am not sure what is the right way to make it right. Ideally I want to use
emitter
to emit events to Javascript from any native threads.Further more, if this demo can work, I would extend it and wrap a logging method so that all addon logs can be emitted to Javascript and be processed using the same Javascript logging package.
Is it possible? Thank you for your help!
The text was updated successfully, but these errors were encountered: