-
Notifications
You must be signed in to change notification settings - Fork 1
/
task.tcc
43 lines (38 loc) · 1.16 KB
/
task.tcc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#define task_tcc
#include "shared_functor.h"
#include "task.h"
namespace kaiu {
namespace promise {
template <typename Result, typename... Args>
UnboundTask<Result, Args...> task(
Factory<Result, Args...> factory,
const EventLoopPool action_pool,
const EventLoopPool reaction_pool)
{
auto newFactory = [factory, action_pool, reaction_pool]
(EventLoop& loop, Args... args) {
Promise<Result> promise;
auto action = [factory, promise, reaction_pool, args...]
(EventLoop& loop) {
auto resolve = [promise, reaction_pool, &loop] (Result result) {
auto proxy = [promise, result = std::move(result)] (EventLoop&) mutable {
promise->resolve(std::move(result));
};
loop.push(reaction_pool, detail::make_shared_functor(proxy));
};
auto reject = [promise, reaction_pool, &loop] (std::exception_ptr error) {
auto proxy = [promise, error] (EventLoop&) {
promise->reject(error);
};
loop.push(reaction_pool, proxy);
};
factory(args...)
->then(resolve, reject);
};
loop.push(action_pool, action);
return promise;
};
return curry_wrap<Promise<Result>, sizeof...(Args) + 1, Factory<Result, EventLoop&, Args...>>(newFactory);
}
}
}