Skip to main content

thread_flow/
runtime.rs

1// SPDX-FileCopyrightText: 2025 Knitli Inc. <knitli@knit.li>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4use async_trait::async_trait;
5use std::future::Future;
6
7/// Strategy pattern for handling runtime environment differences
8/// (CLI/Local vs Cloudflare/Edge)
9#[async_trait]
10pub trait RuntimeStrategy: Send + Sync {
11    /// Spawn a future in the environment's preferred way
12    fn spawn<F>(&self, future: F)
13    where
14        F: Future<Output = ()> + Send + 'static;
15
16    // Abstract other environment specifics (storage, config, etc.)
17}
18
19pub struct LocalStrategy;
20
21#[async_trait]
22impl RuntimeStrategy for LocalStrategy {
23    fn spawn<F>(&self, future: F)
24    where
25        F: Future<Output = ()> + Send + 'static,
26    {
27        tokio::spawn(future);
28    }
29}
30
31pub struct EdgeStrategy;
32
33#[async_trait]
34impl RuntimeStrategy for EdgeStrategy {
35    fn spawn<F>(&self, future: F)
36    where
37        F: Future<Output = ()> + Send + 'static,
38    {
39        // Cloudflare Workers specific spawning if needed, or generic tokio
40        tokio::spawn(future);
41    }
42}