|
| 1 | +use crate::WfContext; |
| 2 | +use futures::future::BoxFuture; |
| 3 | + |
| 4 | +// #[activity_definition] |
| 5 | +type MyActFn = fn(String) -> String; |
| 6 | + |
| 7 | +// Macro enforces types are serializable. |
| 8 | + |
| 9 | +// The biggest problem with activity definitions is they need to be defined in a crate which doesn't |
| 10 | +// depend on the entire SDK, because then the workflow code which uses them wouldn't be able to |
| 11 | +// be compiled down to WASM. Of course, the issue is activities _aren't_ compiled to WASM, and need |
| 12 | +// access to full native functionality. Thus users need to structure their app a bit oddly. They |
| 13 | +// can either define all their workflow code & activity _definitions_ in one crate, and then |
| 14 | +// depend on that crate from another crate containing their activity implementations / worker, or |
| 15 | +// they could make a crate with *just* activity definitions, which is depended on by the workflow |
| 16 | +// implementation crate and the worker crate independently. It all makes perfect sense, but is |
| 17 | +// maybe a bit annoying in terms of setup - though not really any worse than TS. |
| 18 | + |
| 19 | +// Macro generates this extension & implementation: |
| 20 | +// |
| 21 | +// The generated code taking `impl Into<Argtype>` is quite nice for ergonomics inside the workflow, |
| 22 | +// but might be impossible in some cases, so probably macro would need a flag to turn it off. |
| 23 | +pub trait MyActFnWfCtxExt { |
| 24 | + // In reality this returns the `CancellableFuture` type from SDK, would also need to move into |
| 25 | + // this crate. |
| 26 | + fn my_act_fn( |
| 27 | + &self, |
| 28 | + input: impl Into<String>, |
| 29 | + ) -> BoxFuture<'static, Result<String, ActivityFail>>; |
| 30 | +} |
| 31 | +impl MyActFnWfCtxExt for WfContext { |
| 32 | + fn my_act_fn(&self, _: impl Into<String>) -> BoxFuture<'static, Result<String, ActivityFail>> { |
| 33 | + // Type name is injected in this implementation, taken from macro |
| 34 | + todo!() |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +// To implement the activity in their implementation crate, the user would do something like: |
| 39 | +// worker.register_activity(MyActFn, |input: String| async move { .... }); |
| 40 | + |
| 41 | +// Placeholder. Activity failures as can be seen by the WF code. |
| 42 | +#[derive(Debug)] |
| 43 | +pub struct ActivityFail {} |
0 commit comments