Giter Club home page Giter Club logo

Comments (5)

mika-fischer avatar mika-fischer commented on June 15, 2024 1

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.

mika-fischer avatar mika-fischer commented on June 15, 2024

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.

deshan avatar deshan commented on June 15, 2024

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.

deshan avatar deshan commented on June 15, 2024

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.

deshan avatar deshan commented on June 15, 2024

Not a problem here. When trying without electron no issues.
closing.
Thanks @mika-fischer

from napi-thread-safe-callback.

Related Issues (12)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.