DEVELOPMENT ENVIRONMENT

~liljamo/deck-builder

ref: 37439492a26c900dafdd6cadc057be136dd22d7b deck-builder/client/src/macros/async_task.rs -rw-r--r-- 3.0 KiB
37439492Jonni Liljamo feat(client): functional login and register 1 year, 6 months ago
                                                                                
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
/*
 * This file is part of laurelin_client
 * Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
 *
 * Licensed under GPL-3.0-only.
 * See LICENSE for licensing information.
 */

#[macro_export]
macro_rules! async_task_start_call {
    ($call_event_type:ty, $call_type:expr, |$ev:ident| $func:expr) => {
        pub fn start_call(
            mut commands: Commands,
            mut start_ev_r: EventReader<$call_event_type>,
        ) {
            for ev in start_ev_r.iter() {
                let thread_pool = AsyncComputeTaskPool::get();
                let $ev = ev.clone();
                let task = thread_pool.spawn(async move {
                    $func
                });
                commands.spawn($call_type(task));
            }
        }
    };

    ($call_event_type:ty, $call_type:expr, |$ev:ident, $no:ident| $func:expr) => {
        pub fn start_call(
            mut commands: Commands,
            mut start_ev_r: EventReader<$call_event_type>,
            no: Res<NetworkingOptions>
        ) {
            for ev in start_ev_r.iter() {
                let thread_pool = AsyncComputeTaskPool::get();
                let $ev = ev.clone();
                let mut $no = no.clone();
                let task = thread_pool.spawn(async move {
                    $func
                });
                commands.spawn($call_type(task));
            }
        }
    };
}

#[macro_export]
macro_rules! async_task_handle_call {
    ($call_type:ty, |$response:ident| $handler_func:expr) => {
        pub fn handle_call(
            mut commands: Commands,
            mut tasks: Query<(Entity, &mut $call_type)>,
        ) {
            match tasks.get_single_mut() {
                Ok((entity, mut task)) => {
                    if let Some($response) = future::block_on(future::poll_once(&mut task.0)) {
                        $handler_func;

                        // remove the task
                        commands.entity(entity).remove::<$call_type>();
                        commands.entity(entity).despawn_recursive();
                    }
                }
                // NOTE: don't do anything if the wanted thingy doesn't exist
                _ => {}
            }
        }
    };

    ($call_type:ty, |$response:ident, $menu_data:ident| $handler_func:expr) => {
        pub fn handle_call(
            mut commands: Commands,
            mut tasks: Query<(Entity, &mut $call_type)>,
            mut $menu_data: ResMut<MenuData>,
        ) {
            match tasks.get_single_mut() {
                Ok((entity, mut task)) => {
                    if let Some($response) = future::block_on(future::poll_once(&mut task.0)) {
                        $handler_func;

                        // remove the task
                        commands.entity(entity).remove::<$call_type>();
                        commands.entity(entity).despawn_recursive();
                    }
                }
                // NOTE: don't do anything if the wanted thingy doesn't exist
                _ => {}
            }
        }
    };

}