crossflow 0.0.6

Reactive programming and workflow engine in bevy
Documentation
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/*
 * Copyright (C) 2025 Open Source Robotics Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
*/

use bevy_ecs::{
    prelude::{ChildOf, Commands, Component, Entity, World},
    system::Command,
};

use std::{borrow::Cow, cell::RefCell, collections::HashMap, rc::Rc, sync::Arc};

use tokio::sync::mpsc::unbounded_channel;

use crate::{
    AddExecution, AddOperation, Builder, DefaultStreamBufferContainer, DeferredRoster,
    ExitTargetStorage, InnerChannel, Input, InputBundle, InputSlot, ManageInput, OperationRequest,
    OperationResult, OperationRoster, OperationSetup, OrBroken, Output, Push, Receiver,
    RedirectScopeStream, RedirectWorkflowStream, ReportUnhandled, ScopeStorage, SingleInputStorage,
    StreamEffect, StreamRedirect, StreamRequest, StreamTargetMap, TakenStream, UnusedStreams,
    UnusedTarget,
};

pub struct NamedStream<S: StreamEffect>(std::marker::PhantomData<fn(S)>);

impl<S: StreamEffect> NamedStream<S> {
    pub fn spawn_scope_stream(
        in_scope: Entity,
        out_scope: Entity,
        commands: &mut Commands,
    ) -> (InputSlot<S::Input>, Output<S::Output>) {
        let source = commands.spawn(()).id();
        let target = commands.spawn(UnusedTarget).id();
        commands.queue(AddOperation::new(
            Some(in_scope),
            source,
            RedirectScopeStream::<S>::new(target),
        ));

        (
            InputSlot::new(in_scope, source),
            Output::new(out_scope, target),
        )
    }

    pub fn spawn_workflow_stream(
        name: impl Into<Cow<'static, str>>,
        builder: &mut Builder,
    ) -> InputSlot<S::Input> {
        let source = builder.commands.spawn(()).id();
        builder.commands.queue(AddOperation::new(
            Some(builder.scope()),
            source,
            RedirectWorkflowStream::new(NamedStreamRedirect::<S>::static_name(name.into())),
        ));
        InputSlot::new(builder.scope(), source)
    }

    pub fn spawn_node_stream(
        name: impl Into<Cow<'static, str>>,
        source: Entity,
        map: &mut StreamTargetMap,
        builder: &mut Builder,
    ) -> Output<S::Output> {
        let target = builder
            .commands
            .spawn((SingleInputStorage::new(source), UnusedTarget))
            .id();

        map.add_named::<S::Output>(name.into(), target, builder.commands());
        Output::new(builder.scope(), target)
    }

    pub fn take_stream(
        name: impl Into<Cow<'static, str>>,
        source: Entity,
        map: &mut StreamTargetMap,
        commands: &mut Commands,
    ) -> Receiver<S::Output> {
        let (sender, receiver) = unbounded_channel::<S::Output>();
        let target = commands.spawn(()).insert(ChildOf(source)).id();

        map.add_named::<S::Output>(name.into(), target, commands);
        commands.queue(AddExecution::new(None, target, TakenStream::new(sender)));

        receiver
    }

    pub fn collect_stream(
        name: impl Into<Cow<'static, str>>,
        source: Entity,
        target: Entity,
        map: &mut StreamTargetMap,
        commands: &mut Commands,
    ) {
        let name = name.into();
        let redirect = commands.spawn(()).insert(ChildOf(source)).id();
        commands.queue(AddExecution::new(
            None,
            redirect,
            Push::<S::Output>::new(target, true).with_name(name.clone()),
        ));
        map.add_named::<S::Output>(name, redirect, commands);
    }

    pub fn make_stream_channel(
        name: impl Into<Cow<'static, str>>,
        inner: &Arc<InnerChannel>,
        world: &World,
    ) -> NamedStreamChannel<S> {
        let targets =
            NamedStreamTargets::new::<S::Output>(world.get::<StreamTargetMap>(inner.source()));
        NamedStreamChannel::new(name.into(), Arc::new(targets), Arc::clone(&inner))
    }

    pub fn make_stream_buffer(target_map: Option<&StreamTargetMap>) -> NamedStreamBuffer<S::Input> {
        let targets = NamedStreamTargets::new::<S::Output>(target_map);
        NamedStreamBuffer {
            targets: Arc::new(targets),
            container: Default::default(),
        }
    }

    pub fn process_stream_buffer(
        name: impl Into<Cow<'static, str>>,
        buffer: NamedStreamBuffer<S::Input>,
        source: Entity,
        session: Entity,
        unused: &mut UnusedStreams,
        world: &mut World,
        roster: &mut OperationRoster,
    ) -> OperationResult {
        let name = name.into();
        let targets = buffer.targets;
        let mut was_unused = true;
        for value in Rc::into_inner(buffer.container)
            .or_broken()?
            .into_inner()
            .into_iter()
        {
            was_unused = false;
            let target = targets.get(&name);
            let mut request = StreamRequest {
                source,
                session,
                target: target.map(NamedTarget::as_entity),
                world,
                roster,
            };

            S::side_effect(value, &mut request)
                .and_then(|value| {
                    target
                        .map(|t| {
                            t.send_output(
                                NamedValue {
                                    name: name.clone(),
                                    value,
                                },
                                request,
                            )
                        })
                        .unwrap_or(Ok(()))
                })
                .report_unhandled(source, world);
        }

        if was_unused {
            unused.streams.push(std::any::type_name::<Self>());
        }

        Ok(())
    }

    pub fn defer_buffer(
        name: impl Into<Cow<'static, str>>,
        buffer: NamedStreamBuffer<S::Input>,
        source: Entity,
        session: Entity,
        commands: &mut Commands,
    ) {
        let name = name.into();
        let container: DefaultStreamBufferContainer<_> = buffer
            .container
            .take()
            .into_iter()
            .map(|value| NamedValue {
                name: name.clone(),
                value,
            })
            .collect();

        commands.queue(SendNamedStreams::<
            S,
            DefaultStreamBufferContainer<NamedValue<S::Input>>,
        >::new(container, source, session, buffer.targets));
    }
}

/// A container that can tie together a name with a value.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct NamedValue<T: 'static + Send + Sync> {
    pub name: Cow<'static, str>,
    pub value: T,
}

impl<T: 'static + Send + Sync> NamedValue<T> {
    pub fn new(name: impl Into<Cow<'static, str>>, value: T) -> Self {
        Self {
            name: name.into(),
            value,
        }
    }
}

#[derive(Default)]
pub struct NamedStreamTargets {
    /// Targets that connected to a specific stream name
    specific: HashMap<Cow<'static, str>, Entity>,
    /// A target that connected to a general NamedStream
    general: Option<Entity>,
    /// A target that accepts the base stream output
    anonymous: Option<Entity>,
}

impl NamedStreamTargets {
    pub fn new<T: 'static + Send + Sync>(targets: Option<&StreamTargetMap>) -> Self {
        let Some(targets) = targets else {
            return Self::default();
        };

        let output_type = std::any::TypeId::of::<T>();

        let mut specific = HashMap::new();
        for (name, (ty, target)) in &targets.named {
            if *ty == output_type {
                specific.insert(name.clone(), *target);
            }
        }

        let general = targets.get_anonymous::<NamedValue<T>>();
        let anonymous = targets.get_anonymous::<T>();

        Self {
            specific,
            general,
            anonymous,
        }
    }

    pub fn get(&self, name: &str) -> Option<NamedTarget> {
        self
            // First check the specifically named connections
            .specific
            .get(name)
            .copied()
            .map(NamedTarget::Value)
            // If there is no specifically named connection, then use a target
            // that accepts all named streams
            .or_else(|| self.general.map(NamedTarget::NamedValue))
            // If there is no target accepting all naemd streams then use one
            // that accepts the output type without any name
            .or_else(|| self.anonymous.map(NamedTarget::Value))
    }
}

#[derive(Clone, Copy)]
pub enum NamedTarget {
    /// Send a named value
    NamedValue(Entity),
    /// Send only the value
    Value(Entity),
}

impl NamedTarget {
    pub fn as_entity(self) -> Entity {
        match self {
            Self::NamedValue(target) => target,
            Self::Value(target) => target,
        }
    }

    pub fn send_output<T: 'static + Send + Sync>(
        self,
        NamedValue { name, value }: NamedValue<T>,
        request: StreamRequest,
    ) -> OperationResult {
        match self {
            NamedTarget::NamedValue(_) => request.send_output(NamedValue { name, value }),
            NamedTarget::Value(_) => request.send_output(value),
        }
    }
}

pub struct SendNamedStreams<S, Container> {
    container: Container,
    source: Entity,
    session: Entity,
    targets: Arc<NamedStreamTargets>,
    _ignore: std::marker::PhantomData<fn(S)>,
}

impl<S, Container> SendNamedStreams<S, Container> {
    pub fn new(
        container: Container,
        source: Entity,
        session: Entity,
        targets: Arc<NamedStreamTargets>,
    ) -> Self {
        Self {
            container,
            source,
            session,
            targets,
            _ignore: Default::default(),
        }
    }
}

impl<S, Container> Command for SendNamedStreams<S, Container>
where
    S: StreamEffect,
    Container: 'static + Send + Sync + IntoIterator<Item = NamedValue<S::Input>>,
{
    fn apply(self, world: &mut World) {
        world.get_resource_or_insert_with(DeferredRoster::default);
        world.resource_scope::<DeferredRoster, _>(|world, mut deferred| {
            for data in self.container {
                let NamedValue { name, value } = data;
                let target = self.targets.get(&name);
                let mut request = StreamRequest {
                    source: self.source,
                    session: self.session,
                    target: target.map(NamedTarget::as_entity),
                    world,
                    roster: &mut deferred,
                };

                S::side_effect(value, &mut request)
                    .and_then(move |value| {
                        target
                            .map(|t| t.send_output(NamedValue { name, value }, request))
                            .unwrap_or(Ok(()))
                    })
                    .report_unhandled(self.source, world);
            }
        });
    }
}

pub struct NamedStreamRedirect<S: StreamEffect> {
    static_name: Option<Cow<'static, str>>,
    _ignore: std::marker::PhantomData<fn(S)>,
}

impl<S: StreamEffect> NamedStreamRedirect<S> {
    pub fn dynamic() -> Self {
        Self {
            static_name: None,
            _ignore: Default::default(),
        }
    }

    pub fn static_name(static_name: Cow<'static, str>) -> Self {
        Self {
            static_name: Some(static_name),
            _ignore: Default::default(),
        }
    }
}

#[derive(Component)]
struct StaticStreamName(Option<Cow<'static, str>>);

impl<S: StreamEffect> StreamRedirect for NamedStreamRedirect<S> {
    type Input = NamedValue<S::Input>;

    fn setup(self, OperationSetup { source, world }: OperationSetup) -> OperationResult {
        let mut entity_mut = world.entity_mut(source);
        if self.static_name.is_some() {
            // We have a static name for this redirect then we expect to receive
            // a plain S::Input
            entity_mut.insert(InputBundle::<S::Input>::new());
        } else {
            // If there is no static name then this is meant to receive any
            // named input.
            entity_mut.insert(InputBundle::<NamedValue<S::Input>>::new());
        }

        entity_mut.insert(StaticStreamName(self.static_name));

        Ok(())
    }

    fn execute(
        OperationRequest {
            source,
            world,
            roster,
        }: OperationRequest,
    ) -> OperationResult {
        let mut source_mut = world.get_entity_mut(source).or_broken()?;

        let static_name = source_mut.get::<StaticStreamName>().or_broken()?.0.clone();
        let (scoped_session, name, value) = match static_name {
            Some(name) => {
                // If this named stream has a static name then we expect to receive
                // a plain S::Input and use the stream's static name.
                let Input {
                    session: scoped_session,
                    data: value,
                } = source_mut.take_input::<S::Input>()?;
                (scoped_session, name, value)
            }
            None => {
                // f this named stream does not have a static name then it's for
                // a dynamically named stream, so we expect to receive a named
                // value.
                let Input {
                    session: scoped_session,
                    data: NamedValue { name, value },
                } = source_mut.take_input::<NamedValue<S::Input>>()?;
                (scoped_session, name, value)
            }
        };

        let scope = world.get::<ScopeStorage>(source).or_broken()?.get();

        let exit = world
            .get::<ExitTargetStorage>(scope)
            .or_broken()?
            .map
            .get(&scoped_session)
            .or_not_ready()?;

        let exit_source = exit.source;
        let parent_session = exit.parent_session;

        let stream_targets = world.get::<StreamTargetMap>(exit_source).or_broken()?;

        let target = stream_targets.get_named_or_anonymous::<S::Output>(&name);
        let mut request = StreamRequest {
            source,
            session: parent_session,
            target: target.map(NamedTarget::as_entity),
            world,
            roster,
        };

        S::side_effect(value, &mut request).and_then(|value| {
            target
                .map(|t| t.send_output(NamedValue { name, value }, request))
                .unwrap_or(Ok(()))
        })?;

        Ok(())
    }
}

pub struct NamedStreamChannel<S> {
    name: Cow<'static, str>,
    targets: Arc<NamedStreamTargets>,
    inner: Arc<InnerChannel>,
    _ignore: std::marker::PhantomData<fn(S)>,
}

impl<S: StreamEffect> NamedStreamChannel<S> {
    pub fn send(&self, data: S::Input) {
        let f = send_named_stream::<S>(
            self.inner.source,
            self.inner.session,
            Arc::clone(&self.targets),
            self.name.clone(),
            data,
        );

        self.inner.sender.send(Box::new(f)).ok();
    }

    fn new(
        name: Cow<'static, str>,
        targets: Arc<NamedStreamTargets>,
        inner: Arc<InnerChannel>,
    ) -> Self {
        Self {
            name,
            targets,
            inner,
            _ignore: Default::default(),
        }
    }
}

impl<S> Clone for NamedStreamChannel<S> {
    fn clone(&self) -> Self {
        Self {
            name: self.name.clone(),
            targets: Arc::clone(&self.targets),
            inner: Arc::clone(&self.inner),
            _ignore: Default::default(),
        }
    }
}

pub(crate) fn send_named_stream<S: StreamEffect>(
    source: Entity,
    session: Entity,
    targets: Arc<NamedStreamTargets>,
    name: Cow<'static, str>,
    value: S::Input,
) -> impl FnOnce(&mut World, &mut OperationRoster) {
    move |world: &mut World, roster: &mut OperationRoster| {
        let target = targets.get(&name);
        let mut request = StreamRequest {
            source,
            session,
            target: target.map(NamedTarget::as_entity),
            world,
            roster,
        };

        S::side_effect(value, &mut request)
            .and_then(|value| {
                target
                    .map(|t| t.send_output(NamedValue { name, value }, request))
                    .unwrap_or(Ok(()))
            })
            .report_unhandled(source, world);
    }
}

pub struct NamedStreamBuffer<T: 'static + Send + Sync> {
    targets: Arc<NamedStreamTargets>,
    container: Rc<RefCell<DefaultStreamBufferContainer<T>>>,
}

impl<T: 'static + Send + Sync> Clone for NamedStreamBuffer<T> {
    fn clone(&self) -> Self {
        Self {
            targets: Arc::clone(&self.targets),
            container: Rc::clone(&self.container),
        }
    }
}

impl<T: 'static + Send + Sync> NamedStreamBuffer<T> {
    pub fn send(&self, input: T) {
        self.container.borrow_mut().push(input);
    }
}