Hey Zac!
You are the god here! Thanks so much for helping with this!
So this is need:
const result = addon.task(options, function(data) {
console.log(data);
});
setTimeout(() => {
result.cancel()
}, 5000)
See the cancel?
I got the function, which is a FuncitonTemplate, but it is on its own, I
can't access some data, to set work->running = false
void CancelFunction(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
// i need
// work->running = false;
args.GetReturnValue().Set(String::NewFromUtf8(isolate, "hello world"));
}
static void WorkAsync(uv_work_t *req) {
Work *work = static_cast<Work *>(req->data);
int i = 0;
while(work->running) {
//sleep(1);
usleep(1000 * 1000 * 0.25);
cout << i++ << endl;
}
work->result = "Async task processed.";
}
So this is the result function which is a FunctionTemplate:
Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, CancelFunction);
Local<Function> fn = tpl->GetFunction();
// omit this to make it anonymous
fn->SetName(String::NewFromUtf8(isolate, "CancelFunction"));
It works, but I want to pass it a small struct, to set it to be
work->running = false, so that the thread would stop running.
So everything is perfect, I can work total speed when I need.
Thanks for responding!
Ciao!
On Wednesday, May 24, 2017 at 6:07:11 AM UTC+2, Zac Hansen wrote:
>
> Or if you want to parameterize the function with data that is the same all
> calls, then the third parameter to FunctionTemplate can take a v8::External
> which is essentially a void*.
>
>
>
> On Tuesday, May 23, 2017 at 7:28:00 PM UTC-7, Zac Hansen wrote:
>>
>> when you call the javascript function you get back from the template, you
>> have to pass it the parameters you want called on it. Or am I
>> misunderstanding your question?
>>
>> On Tuesday, May 23, 2017 at 4:52:51 PM UTC-7, Patrik Laszlo wrote:
>>>
>>> Hi!
>>>
>>> How are you?
>>> This works, async function:
>>>
>>> static void WorkAsync(uv_work_t *req) {
>>> Work *work = static_cast<Work *>(req->data);
>>>
>>> int i = 0;
>>> while(work->running) {
>>> //sleep(1);
>>> usleep(1000 * 1000 * 0.25);
>>> cout << i++ << endl;
>>> }
>>> work->result = "Async task processed.";
>>> }
>>>
>>>
>>>
>>> I can return in my addon, a cancel callback. Callad CancelFunction.
>>> I would like to set work->running = false, so it stops computing.
>>> But with FunctionTempalte. I cannot give a pointer :) How can I do it?
>>>
>>> Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate,
>>> CancelFunction);
>>> Local<Function> fn = tpl->GetFunction();
>>>
>>>
>>> I can work it, by I cannot pass a pointer to the work . Is it easy?
>>>
>>> Thanks so much!
>>> Bye!
>>>
>>
--
--
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users
---
You received this message because you are subscribed to the Google Groups
"v8-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.