Poco::JS::Core

template < typename TParameterType, typename TResultType >

class AsyncFunctionRunner

File Information

Library: JS/Core
Package: Execution
Header: Poco/JS/Core/AsyncFunction.h

Description

A utility class template for implementing asynchronous JavaScript functions in C++. Asynchronous functions implemented with this template take an argument, perform their job in a separate thread taken from the default Poco::ThreadPool, and return a Promise.

When implementing an asynchronous function with AsyncFunctionRunner, two functions need to be provided. One (evaluateFunc) to do the actual work, taking the parameter (converted to a C++ type) as argument, and returning a C++ type, and a second one (convertResultFunc) that converts the returned C++ value to a v8::Local<v8::Value> that is used to resolve the Promise. In case evaluateFunc throws, the Promise will be automatically rejected.

Inheritance

Direct Base Classes: Poco::Runnable

All Base Classes: Poco::Runnable

Member Summary

Member Functions: exception, executor, isolate, resolver, result, run, start

Inherited Functions: run

Types Aliases

ConvertResultFunc

using ConvertResultFunc = std::function < v8::Local < v8::Value > (v8::Local < v8::Context > &, const ResultType &)>;

EvaluateFunc

using EvaluateFunc = std::function < ResultType (const ParameterType &)>;

ParameterType

using ParameterType = TParameterType;

ResultType

using ResultType = TResultType;

Constructors

AsyncFunctionRunner inline

AsyncFunctionRunner(
    v8::Isolate * pIsolate,
    Poco::JS::Core::TimedJSExecutor::Ptr pExecutor,
    v8::Handle < v8::Promise::Resolver > resolver,
    EvaluateFunc && evaluateFunc,
    ConvertResultFunc && convertResultFunc,
    const ParameterType & parameter
);

Member Functions

exception inline

Poco::Exception * exception() const;

executor inline

Poco::JS::Core::TimedJSExecutor::Ptr & executor();

isolate inline

v8::Isolate * isolate();

resolver inline

v8::Persistent < v8::Promise::Resolver > & resolver();

result inline

const ResultType & result() const;

run virtual

void run();

start static inline

static void start(
    const v8::FunctionCallbackInfo < v8::Value > & args,
    v8::Local < v8::Context > & context,
    EvaluateFunc && evaluateFunc,
    ConvertResultFunc && convertResultFunc,
    const ParameterType & parameter
);

Creates a AsyncFunctionRunner with the given parameters and functions, starts the evaluation thread in the default Poco::ThreadPool, and sets the return value to a newly created Promise.

Securely control IoT edge devices from anywhere   Connect a Device