Comments (5)
How about:
void hasData(Data &data){
printf("hasData start\n");
auto fut = callback->call<bool>([data](Napi::Env env, std::vector<napi_value>& args) {
args = { Napi::Number::New(env, data.size) };
printf("hasData cb start\n");
},
[](const Napi::Value &val) {
printf("hasData cb return\n");
return val.As<Napi::Boolean>().Value();
});
printf("hasData cb queued\n");
fut.get();
printf("hasData end\n");
}
I.e. also print before the JS code is being run.
But I really have no clue what could be going wrong. I use this code on Windows, but I've never used Electron.
It would be good if you could write a small complete and self-contained example demonstrating the problem, or adapt a test to fail. See for instance https://github.com/mika-fischer/napi-thread-safe-callback/blob/master/test/tests.js#L300-L323 and https://github.com/mika-fischer/napi-thread-safe-callback/blob/master/test/tests.cpp#L150-L170
But it might be even better to use the NAPI-native solution instead: https://github.com/nodejs/node-addon-api/blob/master/napi.h#L2065
from napi-thread-safe-callback.
Could you try:
I don't see anything that's obviously wrong with this...
Could you try:
void hasData(Data &data)
printf("hasData start\n");
auto fut = (*callback)(
[data](Env env, std::vector<napi_value>& args) {
printf("hasData cb start\n");
args = { Napi::Number::New(env, data.size) };
},
[](const Napi::Value&, const Napi::Error&) {
printf("hasData cb return\n");
});
printf("hasData cb queued\n");
fut.get();
printf("hasData end\n");
}
from napi-thread-safe-callback.
Thanks for your quick reply,
I had compile error in the code you provided. Am I using different version? I'm using napi-thread-safe-callback": "0.0.6",
error C3313: 'fut': variable cannot have the type 'void'
But with slight modifications I managed to run this
void hasData(Data &data){
printf("hasData start\n");
auto fut = callback->call<bool>([data](Napi::Env env, std::vector<napi_value>& args) {
args = { Napi::Number::New(env, data.size) };
},
[](const Napi::Value &val) {
printf("hasData cb return\n");
return val.As<Napi::Boolean>().Value();
});
printf("hasData cb queued\n");
fut.get();
printf("hasData end\n");
}
And as I seen first cycle goes without issues. But in the second cycle it freeze. Output
hasData
hasData cb queued
hasData cb return
hasData end
hasData
hasData cb queued
<Freeze After>
My js is an electron app.
I'm calling this like
napiWrapr.runMethod(function(size) {
console.log('callback:', size);
return true;
})
This still works perfectly in mac and ubuntu. Only in windows I have this.
from napi-thread-safe-callback.
Thanks
This is unusual
In the first run from command prompt. The output is
hasData
hasData cb queued
hasData cb start
hasData cb return
hasData end
hasData
hasData cb queued
...Freeze
From the second time start the app by npm start
hasData
hasData cb queued
...Freeze
I will check NAPI-native solution, if you have anymore thoughts pls share
from napi-thread-safe-callback.
Not a problem here. When trying without electron no issues.
closing.
Thanks @mika-fischer
from napi-thread-safe-callback.
Related Issues (12)
- Promises HOT 4
- Save the context of calling the js HOT 8
- Is this package ABI stable? HOT 4
- Crash while deleting ThreadSafeCallback::Impl on OSX Big Sur HOT 2
- Add test for cases where Napi::Error::Fatal is thrown
- CMake Support HOT 2
- Blocked nodejs event loop during callback leads to error HOT 14
- Build error HOT 1
- example_async_work within class HOT 1
- Inline unref
- emit event as thread safe callback HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from napi-thread-safe-callback.