1 | use crate::{ |
2 | bindgen_runtime::{ToNapiValue, TypeName}, |
3 | Env, Error, Result, |
4 | }; |
5 | |
6 | pub trait Task: Send + Sized { |
7 | type Output: Send + Sized + 'static; |
8 | type JsValue: ToNapiValue + TypeName; |
9 | |
10 | /// Compute logic in libuv thread |
11 | fn compute(&mut self) -> Result<Self::Output>; |
12 | |
13 | /// Into this method if `compute` return `Ok` |
14 | fn resolve(&mut self, env: Env, output: Self::Output) -> Result<Self::JsValue>; |
15 | |
16 | /// Into this method if `compute` return `Err` |
17 | fn reject(&mut self, _env: Env, err: Error) -> Result<Self::JsValue> { |
18 | Err(err) |
19 | } |
20 | |
21 | // after resolve or reject |
22 | fn finally(&mut self, _env: Env) -> Result<()> { |
23 | Ok(()) |
24 | } |
25 | } |
26 | |