DEVELOPMENT ENVIRONMENT

~liljamo/deck-builder

ref: e09fcc3b0fe1575540988ea9672edaa0a1d90aa7 deck-builder/client/src/macros/async_task.rs -rw-r--r-- 5.7 KiB
e09fcc3bJonni Liljamo feat(client): sane-ish supply pile spawning 1 year, 5 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
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
/*
 * 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 $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
                _ => {}
            }
        }
    };

    ($call_type:ty, |$response:ident, $global:ident, $menu_data:ident, $no:ident| $handler_func:expr) => {
        pub fn handle_call(
            mut commands: Commands,
            mut tasks: Query<(Entity, &mut $call_type)>,
            mut $global: ResMut<Global>,
            mut $menu_data: ResMut<MenuData>,
            mut $no: ResMut<NetworkingOptions>,
        ) {
            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, $global:ident, $game_data:ident| $handler_func:expr) => {
        pub fn handle_call(
            mut commands: Commands,
            mut tasks: Query<(Entity, &mut $call_type)>,
            mut $game_data: ResMut<GameData>,
        ) {
            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, |$unused:ident, $ihatethis:ident, $response:ident, $game_data:ident, $parse_ev_w:ident| $handler_func:expr) => {
        pub fn handle_call(
            mut commands: Commands,
            mut tasks: Query<(Entity, &mut $call_type)>,
            mut $game_data: ResMut<GameData>,
            mut $parse_ev_w: EventWriter<ParseGameStatusEvent>,
        ) {
            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
                _ => {}
            }
        }
    };
}