bevy_query_observer 0.3.0

More sophisticated observers for 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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
//! Query observers that match when an entity starts and stops
//! matching a query.
//!
//! For more information,
//! [refer to the crate documentation](crate).

use alloc::vec::Vec;
use bevy_ecs::{
    archetype::Archetype,
    change_detection::Tick,
    component::ComponentId,
    entity_disabling::DefaultQueryFilters,
    error::ErrorContext,
    event::{EntityComponentsTrigger, EventKey},
    lifecycle::{ADD, INSERT, REMOVE, REPLACE},
    observer::TriggerContext,
    prelude::*,
    ptr::PtrMut,
    query::{FilteredAccess, QueryFilter, WorldQuery},
    storage::Table,
    system::{BoxedSystem, RunSystemError},
    world::{DeferredWorld, unsafe_world_cell::UnsafeWorldCell},
};
use bevy_platform::collections::HashMap;
use bevy_utils::prelude::DebugName;

pub mod start;
pub mod stop;

fn query_observer_runner(
    mut world: DeferredWorld,
    observer: Entity,
    trigger_context: &TriggerContext,
    event: PtrMut,
    trigger: PtrMut,
) {
    let world = world.as_unsafe_world_cell();
    let observer_cell = world.get_entity(observer).unwrap();

    // # Safety
    //
    // In both cases, this cannot be mutably aliased.
    let observer = unsafe { observer_cell.get::<QueryObserverOf>().unwrap().0 };

    let access = {
        let mut observer_state = unsafe {
            observer_cell
                .get_mut::<QueryObserverObserverState>()
                .unwrap()
        };

        let last_trigger = world.last_trigger_id();
        if observer_state.last_trigger_id == last_trigger {
            return;
        }
        observer_state.last_trigger_id = last_trigger;

        observer_state.access
    };

    let observer_cell = world.get_entity(observer).unwrap();

    // # Safety
    //
    // Because this state is private, no one else can get useful mutable access to
    // it, and it can't be dropped until after this function completes.
    let mut query_state = unsafe { observer_cell.get_mut::<QueryObserverState>().unwrap() };

    // # Safety
    //
    // Since we only construct this runner for lifecycle events, the trigger
    // _must_ be `EntityComponentsTrigger`. Note that this is especially unsafe
    // as well, as noted in the normal observer's safety comments and
    // <https://github.com/bevyengine/bevy/pull/20731#discussion_r2311907935>.
    //
    // Since we're explicitly not exposing this trigger, our safety constraints
    // aren't as dire.
    let trigger: &mut EntityComponentsTrigger<'_> = unsafe { trigger.deref_mut() };

    let (target, deferred) = match trigger_context.event_key {
        ADD => {
            // # Safety
            //
            // According to the event key, this must be an `Add` event.
            let event: &mut Add = unsafe { event.deref_mut() };
            (
                event.entity,
                matches!(access.add, LifecycleAccess::Deferred),
            )
        }
        INSERT => {
            // # Safety
            //
            // According to the event key, this must be an `Insert` event.
            let event: &mut Insert = unsafe { event.deref_mut() };
            (
                event.entity,
                matches!(access.insert, LifecycleAccess::Deferred),
            )
        }
        REPLACE => {
            // # Safety
            //
            // According to the event key, this must be a `Replace` event.
            let event: &mut Replace = unsafe { event.deref_mut() };
            (
                event.entity,
                matches!(access.replace, LifecycleAccess::Deferred),
            )
        }
        REMOVE => {
            // # Safety
            //
            // According to the event key, this must be a `Remove` event.
            let event: &mut Remove = unsafe { event.deref_mut() };
            (
                event.entity,
                matches!(access.remove, LifecycleAccess::Deferred),
            )
        }
        _ => panic!("triggered query observer with unexpected event key"),
    };

    let target_entity = world.get_entity(target).unwrap();
    let should_run = (query_state.evaluator)(
        world,
        target_entity.archetype(),
        query_state.kind,
        trigger.components,
    );

    if should_run {
        let last_key = query_state.last_key;
        query_state.last_key = Some(trigger_context.event_key);

        let last_trigger = world.last_trigger_id();
        let is_adjacent_tick = query_state.last_trigger_id + 1 == last_trigger;
        query_state.last_trigger_id = last_trigger;

        // A query observer with either add and insert or remove and replace
        // observers could erroneously trigger twice for the same "moment"
        // without this check.
        //
        // Since both add and insert and replace and remove are necessarily
        // adjacent, this cannot accidentally filter out legitimate events.
        if is_adjacent_tick {
            match last_key {
                Some(ADD)
                    if query_state.is_add_and_insert && trigger_context.event_key == INSERT =>
                {
                    // skip evaluation
                    return;
                }
                Some(REPLACE)
                    if query_state.is_remove_and_replace && trigger_context.event_key == REMOVE =>
                {
                    // also skip
                    return;
                }
                _ => {}
            }
        }
    }

    if should_run {
        if !deferred {
            let system = query_state.system.as_mut();
            unsafe {
                if let Err(RunSystemError::Failed(err)) = system
                    .validate_param_unsafe(world)
                    .map_err(From::from)
                    .and_then(|_| system.run_unsafe(target, world))
                {
                    let handler = world.default_error_handler();
                    handler(
                        err,
                        ErrorContext::Observer {
                            name: system.name(),
                            last_run: system.get_last_run(),
                        },
                    );
                }

                system.queue_deferred(world.into_deferred());
            }
        } else {
            unsafe {
                world
                    .into_deferred()
                    .commands()
                    .queue(move |world: &mut World| -> Result {
                        let mut state = world.get_entity_mut(observer)?;
                        let mut state = state
                            .take::<QueryObserverState>()
                            .ok_or("Expected `QueryObserverState`")?;

                        let result = state.system.run_without_applying_deferred(target, world);
                        world.entity_mut(observer).insert(state);
                        world.flush();

                        match result {
                            Err(RunSystemError::Failed(e)) => Err(e),
                            _ => Ok(()),
                        }
                    });
            }
        }
    }
}

type Evaluator = fn(UnsafeWorldCell, &Archetype, QueryObserverKind, &[ComponentId]) -> bool;

#[derive(Component)]
struct QueryObserverState {
    evaluator: Evaluator,
    kind: QueryObserverKind,
    system: BoxedSystem<In<Entity>, ()>,
    last_key: Option<EventKey>,
    last_trigger_id: u32,
    is_add_and_insert: bool,
    is_remove_and_replace: bool,
}

/// A relationship linking an [`Observer`] of a specific query term
/// to the main [`QueryObserer`] entity.
#[derive(Component, Debug)]
#[relationship(relationship_target = QueryObservers)]
pub struct QueryObserverOf(pub Entity);

/// The set of all observers for each term in a query observer.
#[derive(Component, Debug)]
#[relationship_target(relationship = QueryObserverOf, linked_spawn)]
pub struct QueryObservers(Vec<Entity>);

/// A convenience trait for dynamically spawning a [`QueryObserver`].
pub trait SpawnQueryObserver {
    /// Spawns a new [`QueryObserver`], returning its [`Entity`].
    fn spawn_query_observer(&mut self, observer: QueryObserver) -> Entity;
}

impl SpawnQueryObserver for World {
    fn spawn_query_observer(&mut self, observer: QueryObserver) -> Entity {
        let target = self.spawn_empty().id();
        observer.insert_into(target, self);
        target
    }
}

impl SpawnQueryObserver for Commands<'_, '_> {
    fn spawn_query_observer(&mut self, observer: QueryObserver) -> Entity {
        let target = self.spawn_empty().id();
        self.queue(move |world: &mut World| observer.insert_into(target, world));
        target
    }
}

/// A convenience trait for dynamically inserting a [`QueryObserver`].
pub trait InsertQueryObserver {
    /// Insert a [`QueryObserver`] into the entity.
    fn insert_query_observer(&mut self, observer: QueryObserver) -> &mut Self;
}

impl InsertQueryObserver for EntityCommands<'_> {
    fn insert_query_observer(&mut self, observer: QueryObserver) -> &mut Self {
        let id = self.id();
        self.commands()
            .queue(move |world: &mut World| observer.insert_into(id, world));
        self
    }
}

/// An extension trait for manually triggering a [`QueryObserver`].
///
/// This can be used to build a simple reactivity framework in terms
/// of Bevy's lifecycle events. Triggering a query observer manually
/// allows it to respond to specific entities even if its lifecycle events
/// occurred before the observer was spawned.
pub trait TriggerQueryObserver {
    /// Trigger a [`QueryObserver`] (`observer`) with a given `target`.
    ///
    /// If the target doesn't fulfill the query, the system will not run.
    fn trigger_query_observer(&mut self, observer: Entity, target: Entity) -> &mut Self;
}

impl TriggerQueryObserver for World {
    fn trigger_query_observer(&mut self, observer: Entity, target: Entity) -> &mut Self {
        let world = self.as_unsafe_world_cell();

        let observer_cell = world.get_entity(observer).unwrap();

        // # Safety
        //
        // Because this state is private, no one else can get useful mutable access to
        // it, and it can't be dropped until after this function completes.
        let mut query_state = unsafe { observer_cell.get_mut::<QueryObserverState>().unwrap() };

        let system = query_state.system.as_mut();
        unsafe {
            if let Err(RunSystemError::Failed(err)) = system
                .validate_param_unsafe(world)
                .map_err(From::from)
                .and_then(|_| system.run_unsafe(target, world))
            {
                let handler = world.default_error_handler();
                handler(
                    err,
                    ErrorContext::Observer {
                        name: system.name(),
                        last_run: system.get_last_run(),
                    },
                );
            }

            system.queue_deferred(world.into_deferred());
        }

        self
    }
}

/// An observer that triggers
/// when an entity starts or stops matching a query.
/// [`QueryObserver`] also triggers when a matching entity's data
/// changes according to lifecycle events.
pub struct QueryObserver {
    system: BoxedSystem<In<Entity>, ()>,
    entities: Option<Vec<Entity>>,
    get_access: fn(&mut World, QueryObserverKind) -> Access,
    kind: QueryObserverKind,
    evaluator: Evaluator,
}

impl QueryObserver {
    /// Observes the given [`Entity`] (in addition to any entity already being observed).
    pub fn watch_entity(&mut self, entity: Entity) {
        self.entities.get_or_insert_default().push(entity);
    }

    /// Observes the given [`Entity`] (in addition to any entity already being observed).
    pub fn with_entity(mut self, entity: Entity) -> Self {
        self.watch_entity(entity);
        self
    }

    /// Observes each [`Entity`] in the iterator (in addition to any entity already being observed).
    pub fn watch_entities<I>(&mut self, entities: I)
    where
        I: IntoIterator<Item = Entity>,
    {
        self.entities.get_or_insert_default().extend(entities)
    }

    /// Observes each [`Entity`] in the iterator (in addition to any entity already being observed).
    pub fn with_entities<I>(mut self, entities: I) -> Self
    where
        I: IntoIterator<Item = Entity>,
    {
        self.watch_entities(entities);
        self
    }

    /// Returns the name of the [`QueryObserver`]'s system.
    pub fn system_name(&self) -> DebugName {
        self.system.name()
    }

    /// Insert a [`QueryObserver`] into the given [`Entity`].
    ///
    /// Unlike [`Observer`], [`QueryObserver`] is not itself a [`Component`].
    /// This method inserts the [`QueryObserver`]'s private state into `state_entity`,
    /// along with related entities containing custom [`Observer`]s for each unique
    /// component in the query.
    ///
    /// To properly clean up a [`QueryObserver`] entity, all entities related by [`QueryObservers`]
    /// should be despawned and the entity should be cleared.
    pub fn insert_into(self, state_entity: Entity, world: &mut World) {
        let QueryObserver {
            mut system,
            entities,
            get_access,
            kind,
            evaluator,
        } = self;

        system.initialize(world);

        let access = get_access(world, kind);
        let state = QueryObserverState {
            evaluator,
            system,
            kind,
            last_key: None,
            last_trigger_id: 0,
            is_add_and_insert: access.is_add_and_insert(),
            is_remove_and_replace: access.is_remove_and_replace(),
        };

        let mut observer_sets = HashMap::<_, Vec<ComponentId>>::default();
        for component in access.set {
            let set = observer_sets.entry(component.access).or_default();
            if !set.contains(&component.id) {
                set.push(component.id);
            }
        }

        let observers = observer_sets.into_iter().map(move |(access, ids)| {
            let mut observer = Observer::with_dynamic_runner(query_observer_runner);
            if let Some(entities) = &entities {
                observer.watch_entities(entities.iter().copied());
            }

            for key in access.event_keys() {
                // # Safety
                //
                // Our default runner knows how to handler these events.
                observer = unsafe { observer.with_event_key(key) };
            }

            for component in ids {
                observer = observer.with_component(component);
            }

            (
                QueryObserverObserverState {
                    access,
                    last_trigger_id: 0,
                },
                observer,
            )
        });

        world
            .entity_mut(state_entity)
            .insert((state, QueryObservers::spawn(SpawnIter(observers))));
    }
}

impl core::fmt::Debug for QueryObserver {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("QueryObserver")
            .field("system", &self.system)
            .field("kind", &self.kind)
            .field("entities", &self.entities)
            .finish_non_exhaustive()
    }
}

/// Describes whether a query term requires access, and
/// whether that access should be deferred (for example,
/// allowing a component to be removed before evaluation).
#[derive(PartialEq, Eq, Default, Hash, Clone, Copy, Debug)]
pub enum LifecycleAccess {
    /// No access required.
    #[default]
    None,
    /// Immediate access is required.
    Immediate,
    /// Deferred access is required, allowing
    /// the effects of a lifecycle event (like removing a component)
    /// to occur before running.
    Deferred,
}

impl LifecycleAccess {
    fn set_immediate(&mut self) {
        *self = Self::Immediate;
    }

    fn set_deferred(&mut self) {
        if !matches!(self, Self::Immediate) {
            *self = Self::Deferred;
        }
    }
}

/// Distinguishes [`Start`][start::Start] and [`Stop`][stop::Stop]
/// query observers.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum QueryObserverKind {
    /// A [`Start`][start::Start] observer.
    Start,
    /// A [`Stop`][stop::Stop] observer.
    Stop,
}

/// Provides a simplified set of component access information
/// for query observers.
pub trait QueryObserverAccess {
    /// Report a query term's component access given the [`QueryObserverKind`].
    fn report_access(world: &mut World, kind: QueryObserverKind, access: &mut Access);

    /// Evaluate whether an archetype matches the query term.
    ///
    /// Certain queries, like `Start<(), Without<Name>>` require a method
    /// like this in order to produce
    /// [approximately correct behavior](crate#limitations).
    fn evaluate_archetype(
        world: UnsafeWorldCell,
        archetype: &Archetype,
        kind: QueryObserverKind,
        triggered_by: &[ComponentId],
    ) -> bool {
        {
            let _ = world;
            let _ = archetype;
            let _ = kind;
            let _ = triggered_by;
        }

        true
    }
}

impl<T: Component> QueryObserverAccess for &'static T {
    fn report_access(world: &mut World, kind: QueryObserverKind, access: &mut Access) {
        let component = world.register_component::<T>();
        let entry = access.entry(component);

        match kind {
            QueryObserverKind::Start => {
                entry.insert.set_immediate();
            }
            QueryObserverKind::Stop => {
                entry.replace.set_immediate();
            }
        }
    }

    fn evaluate_archetype(
        world: UnsafeWorldCell,
        archetype: &Archetype,
        _kind: QueryObserverKind,
        _triggered_by: &[ComponentId],
    ) -> bool {
        archetype.contains(world.components().component_id::<T>().unwrap())
    }
}

impl<T: Component> QueryObserverAccess for &'static mut T {
    fn report_access(world: &mut World, kind: QueryObserverKind, access: &mut Access) {
        let component = world.register_component::<T>();
        let entry = access.entry(component);

        match kind {
            QueryObserverKind::Start => {
                entry.insert.set_immediate();
            }
            QueryObserverKind::Stop => {
                entry.replace.set_immediate();
            }
        }
    }

    fn evaluate_archetype(
        world: UnsafeWorldCell,
        archetype: &Archetype,
        _kind: QueryObserverKind,
        _triggered_by: &[ComponentId],
    ) -> bool {
        archetype.contains(world.components().component_id::<T>().unwrap())
    }
}

impl<T: Component> QueryObserverAccess for Option<&'static T> {
    fn report_access(world: &mut World, kind: QueryObserverKind, access: &mut Access) {
        let component = world.register_component::<T>();
        let entry = access.entry(component);

        match kind {
            QueryObserverKind::Start => {
                entry.insert.set_immediate();
                entry.remove.set_deferred();
            }
            QueryObserverKind::Stop => {
                // TODO: can't express before add
                // entry.add.set_immediate();
                entry.replace.set_immediate();
            }
        }
    }
}

impl<T: Component> QueryObserverAccess for Option<&'static mut T> {
    fn report_access(world: &mut World, kind: QueryObserverKind, access: &mut Access) {
        let component = world.register_component::<T>();
        let entry = access.entry(component);

        match kind {
            QueryObserverKind::Start => {
                entry.insert.set_immediate();
                entry.remove.set_deferred();
            }
            QueryObserverKind::Stop => {
                // TODO: can't express before add
                // entry.add.set_immediate();
                entry.replace.set_immediate();
            }
        }
    }
}

impl<T: Component> QueryObserverAccess for Has<T> {
    fn report_access(world: &mut World, kind: QueryObserverKind, access: &mut Access) {
        let component = world.register_component::<T>();
        let entry = access.entry(component);

        match kind {
            QueryObserverKind::Start => {
                entry.add.set_immediate();
                entry.remove.set_deferred();
            }
            QueryObserverKind::Stop => {
                // TODO: can't express before add
                // entry.add.set_immediate();
                entry.remove.set_immediate();
            }
        }
    }
}

/// A filter that allows all disabling components.
struct AllowAll;

// # Safety
//
// This is a trivial implementation
unsafe impl QueryFilter for AllowAll {
    const IS_ARCHETYPAL: bool = true;

    unsafe fn filter_fetch(
        _state: &Self::State,
        _fetch: &mut Self::Fetch<'_>,
        _entity: Entity,
        _table_row: bevy_ecs::storage::TableRow,
    ) -> bool {
        true
    }
}

/// SAFETY:
/// `update_component_access` does not add any accesses.
/// This is sound because [`QueryFilter::filter_fetch`] does not access any components.
/// This is sound because it doesn't affect the query
unsafe impl WorldQuery for AllowAll {
    type Fetch<'w> = ();
    type State = Vec<ComponentId>;

    fn shrink_fetch<'wlong: 'wshort, 'wshort>(_: Self::Fetch<'wlong>) -> Self::Fetch<'wshort> {}

    #[inline]
    unsafe fn init_fetch(_: UnsafeWorldCell, _: &Self::State, _: Tick, _: Tick) {}

    // Even if the components are sparse, this implementation doesn't do anything with it
    const IS_DENSE: bool = true;

    #[inline]
    unsafe fn set_archetype(_: &mut (), _: &Self::State, _: &Archetype, _: &Table) {}

    #[inline]
    unsafe fn set_table(_: &mut (), _: &Self::State, _: &Table) {}

    #[inline]
    fn update_component_access(state: &Self::State, access: &mut FilteredAccess) {
        for id in state {
            access.access_mut().add_archetypal(*id);
        }
    }

    fn init_state(world: &mut World) -> Self::State {
        world
            .resource::<DefaultQueryFilters>()
            .disabling_ids()
            .collect()
    }

    fn get_state(_: &bevy_ecs::component::Components) -> Option<Self::State> {
        None
    }

    fn matches_component_set(_: &Self::State, _: &impl Fn(ComponentId) -> bool) -> bool {
        // Allow<T> always matches
        true
    }
}

impl QueryObserverAccess for AllowAll {
    fn report_access(world: &mut World, kind: QueryObserverKind, access: &mut Access) {
        for id in world.resource::<DefaultQueryFilters>().disabling_ids() {
            if !access.contains(id) {
                match kind {
                    QueryObserverKind::Start => {
                        access.entry(id).remove.set_deferred();
                    }
                    QueryObserverKind::Stop => {
                        access.entry(id).add.set_immediate();
                    }
                }
            }
        }
    }

    fn evaluate_archetype(
        world: UnsafeWorldCell,
        archetype: &Archetype,
        _: QueryObserverKind,
        triggered_by: &[ComponentId],
    ) -> bool {
        // # Safety
        //
        // The world must not be mutable accessed within this trait.
        let filter = unsafe { world.get_resource::<DefaultQueryFilters>().unwrap() };
        for id in filter.disabling_ids() {
            if archetype.contains(id) && !triggered_by.contains(&id) {
                return false;
            }
        }

        true
    }
}

impl QueryObserverAccess for Entity {
    fn report_access(_world: &mut World, _kind: QueryObserverKind, _access: &mut Access) {}
}

impl QueryObserverAccess for () {
    fn report_access(_world: &mut World, _kind: QueryObserverKind, _access: &mut Access) {}
}

impl<T: Component> QueryObserverAccess for With<T> {
    fn report_access(world: &mut World, kind: QueryObserverKind, access: &mut Access) {
        let component = world.register_component::<T>();
        let entry = access.entry(component);

        match kind {
            QueryObserverKind::Start => {
                entry.add.set_immediate();
            }
            QueryObserverKind::Stop => {
                entry.remove.set_immediate();
            }
        }
    }

    fn evaluate_archetype(
        world: UnsafeWorldCell,
        archetype: &Archetype,
        _: QueryObserverKind,
        _: &[ComponentId],
    ) -> bool {
        let id = world.components().component_id::<T>().unwrap();
        archetype.contains(id)
    }
}

impl<T: Component> QueryObserverAccess for Without<T> {
    fn report_access(world: &mut World, kind: QueryObserverKind, access: &mut Access) {
        let component = world.register_component::<T>();
        let entry = access.entry(component);

        match kind {
            QueryObserverKind::Start => {
                entry.remove.set_deferred();
            }
            QueryObserverKind::Stop => {
                // TODO: can't express before add
                entry.add.set_immediate();
            }
        }
    }

    fn evaluate_archetype(
        world: UnsafeWorldCell,
        archetype: &Archetype,
        _kind: QueryObserverKind,
        triggered_by: &[ComponentId],
    ) -> bool {
        let id = world.components().component_id::<T>().unwrap();

        if triggered_by.contains(&id) {
            true
        } else {
            !archetype.contains(id)
        }
    }
}

macro_rules! query_observer_data {
    ($($ty:ident),*) => {
        impl<$($ty),*> QueryObserverAccess for ($($ty,)*)
        where $($ty: QueryObserverAccess),*
        {
            fn report_access(
                world: &mut World,
                kind: QueryObserverKind,
                access: &mut Access,
            ) {
                $(
                    <$ty as QueryObserverAccess>::report_access(world, kind, access);
                )*
            }

            fn evaluate_archetype(
                world: UnsafeWorldCell,
                archetype: &Archetype,
                kind: QueryObserverKind,
                triggered_by: &[ComponentId],
            ) -> bool {
                $(
                    <$ty as QueryObserverAccess>::evaluate_archetype(world, archetype, kind, triggered_by)
                )&&*
            }
        }
    };
}

variadics_please::all_tuples!(query_observer_data, 1, 15, T);

/// Describes which lifecycle events, if any, a query observer
/// term needs to respond to.
#[derive(PartialEq, Eq, Default, Hash, Clone, Copy, Debug)]
pub struct QueryAccess {
    /// Access for the add lifecycle event.
    pub add: LifecycleAccess,
    /// Access for the insert lifecycle event.
    pub insert: LifecycleAccess,
    /// Access for the replace lifecycle event.
    pub replace: LifecycleAccess,
    /// Access for the remove lifecycle event.
    pub remove: LifecycleAccess,
}

impl QueryAccess {
    fn event_keys(&self) -> impl Iterator<Item = EventKey> {
        let add = (self.add != LifecycleAccess::None).then_some(ADD);
        let insert = (self.insert != LifecycleAccess::None).then_some(INSERT);
        let replace = (self.replace != LifecycleAccess::None).then_some(REPLACE);
        let remove = (self.remove != LifecycleAccess::None).then_some(REMOVE);

        add.into_iter().chain(insert).chain(replace).chain(remove)
    }
}

#[derive(Component)]
struct QueryObserverObserverState {
    access: QueryAccess,
    last_trigger_id: u32,
}

#[derive(Debug)]
struct ComponentAccess {
    id: ComponentId,
    access: QueryAccess,
}

/// The full set of components a query observer needs
/// to access.
#[derive(Default, Debug)]
pub struct Access {
    set: Vec<ComponentAccess>,
}

impl Access {
    fn is_add_and_insert(&self) -> bool {
        let mut add = false;
        let mut insert = false;

        for access in self.set.iter() {
            if !matches!(access.access.add, LifecycleAccess::None) {
                add = true;
            }

            if !matches!(access.access.insert, LifecycleAccess::None) {
                insert = true;
            }
        }

        add && insert
    }

    fn is_remove_and_replace(&self) -> bool {
        let mut remove = false;
        let mut replace = false;

        for access in self.set.iter() {
            if !matches!(access.access.remove, LifecycleAccess::None) {
                remove = true;
            }

            if !matches!(access.access.replace, LifecycleAccess::None) {
                replace = true;
            }
        }

        remove && replace
    }
}

impl Access {
    /// Initialize or fetch an entry for `id`.
    pub fn entry(&mut self, id: ComponentId) -> &mut QueryAccess {
        let position = self.set.iter().position(|a| a.id == id).unwrap_or_else(|| {
            self.set.push(ComponentAccess {
                id,
                access: Default::default(),
            });

            self.set.len() - 1
        });

        &mut self.set[position].access
    }

    /// Returns `true` if the set contains the component `id`.
    pub fn contains(&self, id: ComponentId) -> bool {
        self.set.iter().any(|a| a.id == id)
    }
}

/// A marker type for infallible query observers.
#[derive(Debug)]
pub struct Infallible;