com.wallstop-studios.dxmessaging 2.0.0-rc05 → 2.0.0-rc07

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,20 +1,21 @@
1
1
  namespace DxMessaging.Unity
2
2
  {
3
- using Core;
4
3
  using System;
5
4
  using System.Collections.Generic;
5
+ using Core;
6
6
  using UnityEngine;
7
+ using UnityEngine.Serialization;
7
8
 
8
9
  [DisallowMultipleComponent]
9
10
  public sealed class MessagingComponent : MonoBehaviour
10
11
  {
11
- [SerializeField]
12
- private bool _emitMessagesWhenDisabled;
12
+ [FormerlySerializedAs("_emitMessagesWhenDisabled")]
13
+ public bool emitMessagesWhenDisabled;
13
14
 
14
15
  private MessageHandler _messageHandler;
15
16
 
16
17
  private readonly Dictionary<MonoBehaviour, MessageRegistrationToken> _registeredListeners =
17
- new Dictionary<MonoBehaviour, MessageRegistrationToken>();
18
+ new();
18
19
 
19
20
  public MessageRegistrationToken Create(MonoBehaviour listener)
20
21
  {
@@ -25,32 +26,46 @@
25
26
 
26
27
  if (gameObject.GetInstanceID() != listener.gameObject.GetInstanceID())
27
28
  {
28
- throw new ArgumentException($"Cannot create a RegistrationToken without an valid owner. {listener.gameObject.GetInstanceID()}.");
29
+ throw new ArgumentException(
30
+ $"Cannot create a RegistrationToken without an valid owner. {listener.gameObject.GetInstanceID()}."
31
+ );
29
32
  }
30
33
 
31
- if (_registeredListeners.TryGetValue(listener, out MessageRegistrationToken createdToken))
34
+ if (
35
+ _registeredListeners.TryGetValue(
36
+ listener,
37
+ out MessageRegistrationToken createdToken
38
+ )
39
+ )
32
40
  {
33
- MessagingDebug.Log(LogLevel.Warn, "Ignoring double RegistrationToken request for {0}.", listener);
41
+ MessagingDebug.Log(
42
+ LogLevel.Warn,
43
+ "Ignoring double RegistrationToken request for {0}.",
44
+ listener
45
+ );
34
46
  return createdToken;
35
47
  }
36
48
 
37
49
  if (_messageHandler == null)
38
50
  {
39
- _messageHandler = new MessageHandler(gameObject)
40
- {
41
- active = true
42
- };
51
+ _messageHandler = new MessageHandler(gameObject) { active = true };
43
52
  MessagingDebug.Log(
44
53
  LogLevel.Debug,
45
54
  "Creating MessageHandler for componentType {0}, GameObject name: {1}, InstanceId: {2}.",
46
- listener.GetType(), listener.gameObject.name, (InstanceId)gameObject);
55
+ listener.GetType(),
56
+ listener.gameObject.name,
57
+ (InstanceId)gameObject
58
+ );
47
59
  }
48
60
  else
49
61
  {
50
62
  MessagingDebug.Log(
51
63
  LogLevel.Debug,
52
64
  "Using existing MessageHandler for componentType {0}, GameObject name: {1}, InstanceId: {2}.",
53
- listener.GetType(), listener.gameObject.name, (InstanceId)gameObject);
65
+ listener.GetType(),
66
+ listener.gameObject.name,
67
+ (InstanceId)gameObject
68
+ );
54
69
  }
55
70
 
56
71
  createdToken = MessageRegistrationToken.Create(_messageHandler);
@@ -58,19 +73,19 @@
58
73
  return createdToken;
59
74
  }
60
75
 
61
- private void OnEnable()
76
+ public void OnEnable()
62
77
  {
63
78
  ToggleMessageHandler(true);
64
79
  }
65
80
 
66
- private void OnDisable()
81
+ public void OnDisable()
67
82
  {
68
83
  ToggleMessageHandler(false);
69
84
  }
70
85
 
71
- private void ToggleMessageHandler(bool newActive)
86
+ public void ToggleMessageHandler(bool newActive)
72
87
  {
73
- if (!newActive && _emitMessagesWhenDisabled)
88
+ if (!newActive && emitMessagesWhenDisabled)
74
89
  {
75
90
  return;
76
91
  }
@@ -81,4 +96,4 @@
81
96
  }
82
97
  }
83
98
  }
84
- }
99
+ }
@@ -2,6 +2,7 @@
2
2
  {
3
3
  using System.Collections;
4
4
  using System.Collections.Generic;
5
+ using System.Linq;
5
6
  using DxMessaging.Core;
6
7
  using DxMessaging.Core.Extensions;
7
8
  using DxMessaging.Core.Messages;
@@ -16,12 +17,20 @@
16
17
  [UnityTest]
17
18
  public IEnumerator SimpleGameObjectBroadcastNormal()
18
19
  {
19
- GameObject test1 = new(nameof(SimpleGameObjectBroadcastNormal) + "1", typeof(EmptyMessageAwareComponent));
20
+ GameObject test1 = new(
21
+ nameof(SimpleGameObjectBroadcastNormal) + "1",
22
+ typeof(EmptyMessageAwareComponent)
23
+ );
20
24
  _spawned.Add(test1);
21
- GameObject test2 = new(nameof(SimpleGameObjectBroadcastNormal) + "2", typeof(EmptyMessageAwareComponent));
25
+ GameObject test2 = new(
26
+ nameof(SimpleGameObjectBroadcastNormal) + "2",
27
+ typeof(EmptyMessageAwareComponent)
28
+ );
22
29
  _spawned.Add(test2);
23
- EmptyMessageAwareComponent component1 = test1.GetComponent<EmptyMessageAwareComponent>();
24
- EmptyMessageAwareComponent component2 = test2.GetComponent<EmptyMessageAwareComponent>();
30
+ EmptyMessageAwareComponent component1 =
31
+ test1.GetComponent<EmptyMessageAwareComponent>();
32
+ EmptyMessageAwareComponent component2 =
33
+ test2.GetComponent<EmptyMessageAwareComponent>();
25
34
 
26
35
  int test1ReceiveCount = 0;
27
36
  int test2ReceiveCount = 0;
@@ -29,8 +38,14 @@
29
38
  MessageRegistrationToken token1 = GetToken(component1);
30
39
  MessageRegistrationToken token2 = GetToken(component2);
31
40
 
32
- _ = token1.RegisterGameObjectBroadcast<SimpleBroadcastMessage>(test1, _ => ++test1ReceiveCount);
33
- _ = token2.RegisterGameObjectBroadcast<SimpleBroadcastMessage>(test2, _ => ++test2ReceiveCount);
41
+ _ = token1.RegisterGameObjectBroadcast<SimpleBroadcastMessage>(
42
+ test1,
43
+ _ => ++test1ReceiveCount
44
+ );
45
+ _ = token2.RegisterGameObjectBroadcast<SimpleBroadcastMessage>(
46
+ test2,
47
+ _ => ++test2ReceiveCount
48
+ );
34
49
 
35
50
  SimpleBroadcastMessage message = new();
36
51
  message.EmitGameObjectBroadcast(test1);
@@ -69,12 +84,20 @@
69
84
  [UnityTest]
70
85
  public IEnumerator SimpleGameObjectBroadcastNoCopy()
71
86
  {
72
- GameObject test1 = new(nameof(SimpleGameObjectBroadcastNoCopy) + "1", typeof(EmptyMessageAwareComponent));
87
+ GameObject test1 = new(
88
+ nameof(SimpleGameObjectBroadcastNoCopy) + "1",
89
+ typeof(EmptyMessageAwareComponent)
90
+ );
73
91
  _spawned.Add(test1);
74
- GameObject test2 = new(nameof(SimpleGameObjectBroadcastNoCopy) + "2", typeof(EmptyMessageAwareComponent));
92
+ GameObject test2 = new(
93
+ nameof(SimpleGameObjectBroadcastNoCopy) + "2",
94
+ typeof(EmptyMessageAwareComponent)
95
+ );
75
96
  _spawned.Add(test2);
76
- EmptyMessageAwareComponent component1 = test1.GetComponent<EmptyMessageAwareComponent>();
77
- EmptyMessageAwareComponent component2 = test2.GetComponent<EmptyMessageAwareComponent>();
97
+ EmptyMessageAwareComponent component1 =
98
+ test1.GetComponent<EmptyMessageAwareComponent>();
99
+ EmptyMessageAwareComponent component2 =
100
+ test2.GetComponent<EmptyMessageAwareComponent>();
78
101
 
79
102
  int test1ReceiveCount = 0;
80
103
  void Test1Receive(ref SimpleBroadcastMessage message)
@@ -130,12 +153,20 @@
130
153
  [UnityTest]
131
154
  public IEnumerator SimpleGameObjectBroadcastDualMode()
132
155
  {
133
- GameObject test1 = new(nameof(SimpleGameObjectBroadcastDualMode) + "1", typeof(EmptyMessageAwareComponent));
156
+ GameObject test1 = new(
157
+ nameof(SimpleGameObjectBroadcastDualMode) + "1",
158
+ typeof(EmptyMessageAwareComponent)
159
+ );
134
160
  _spawned.Add(test1);
135
- GameObject test2 = new(nameof(SimpleGameObjectBroadcastDualMode) + "2", typeof(EmptyMessageAwareComponent));
161
+ GameObject test2 = new(
162
+ nameof(SimpleGameObjectBroadcastDualMode) + "2",
163
+ typeof(EmptyMessageAwareComponent)
164
+ );
136
165
  _spawned.Add(test2);
137
- EmptyMessageAwareComponent component1 = test1.GetComponent<EmptyMessageAwareComponent>();
138
- EmptyMessageAwareComponent component2 = test2.GetComponent<EmptyMessageAwareComponent>();
166
+ EmptyMessageAwareComponent component1 =
167
+ test1.GetComponent<EmptyMessageAwareComponent>();
168
+ EmptyMessageAwareComponent component2 =
169
+ test2.GetComponent<EmptyMessageAwareComponent>();
139
170
 
140
171
  int test1ReceiveCount = 0;
141
172
  void Test1Receive(ref SimpleBroadcastMessage message)
@@ -153,10 +184,19 @@
153
184
  MessageRegistrationToken token2 = GetToken(component2);
154
185
 
155
186
  HashSet<MessageRegistrationHandle> handles = new();
156
- var handle = token1.RegisterGameObjectBroadcast<SimpleBroadcastMessage>(test1, Test1Receive);
187
+ var handle = token1.RegisterGameObjectBroadcast<SimpleBroadcastMessage>(
188
+ test1,
189
+ Test1Receive
190
+ );
157
191
  _ = handles.Add(handle);
158
- handle = token1.RegisterGameObjectBroadcast<SimpleBroadcastMessage>(test1, _ => ++test1ReceiveCount);
159
- handle = token2.RegisterGameObjectBroadcast<SimpleBroadcastMessage>(test2, Test2Receive);
192
+ handle = token1.RegisterGameObjectBroadcast<SimpleBroadcastMessage>(
193
+ test1,
194
+ _ => ++test1ReceiveCount
195
+ );
196
+ handle = token2.RegisterGameObjectBroadcast<SimpleBroadcastMessage>(
197
+ test2,
198
+ Test2Receive
199
+ );
160
200
  _ = handles.Add(handle);
161
201
 
162
202
  SimpleBroadcastMessage message = new();
@@ -195,12 +235,20 @@
195
235
  [UnityTest]
196
236
  public IEnumerator SimpleComponentBroadcastNormal()
197
237
  {
198
- GameObject test1 = new(nameof(SimpleComponentBroadcastNormal) + "1", typeof(EmptyMessageAwareComponent));
238
+ GameObject test1 = new(
239
+ nameof(SimpleComponentBroadcastNormal) + "1",
240
+ typeof(EmptyMessageAwareComponent)
241
+ );
199
242
  _spawned.Add(test1);
200
- GameObject test2 = new(nameof(SimpleComponentBroadcastNormal) + "3", typeof(EmptyMessageAwareComponent));
243
+ GameObject test2 = new(
244
+ nameof(SimpleComponentBroadcastNormal) + "3",
245
+ typeof(EmptyMessageAwareComponent)
246
+ );
201
247
  _spawned.Add(test2);
202
- EmptyMessageAwareComponent component1 = test1.GetComponent<EmptyMessageAwareComponent>();
203
- EmptyMessageAwareComponent component2 = test2.GetComponent<EmptyMessageAwareComponent>();
248
+ EmptyMessageAwareComponent component1 =
249
+ test1.GetComponent<EmptyMessageAwareComponent>();
250
+ EmptyMessageAwareComponent component2 =
251
+ test2.GetComponent<EmptyMessageAwareComponent>();
204
252
 
205
253
  int test1ReceiveCount = 0;
206
254
  int test2ReceiveCount = 0;
@@ -208,8 +256,14 @@
208
256
  MessageRegistrationToken token1 = GetToken(component1);
209
257
  MessageRegistrationToken token2 = GetToken(component2);
210
258
 
211
- _ = token1.RegisterComponentBroadcast<SimpleBroadcastMessage>(component1, _ => ++test1ReceiveCount);
212
- _ = token2.RegisterComponentBroadcast<SimpleBroadcastMessage>(component2, _ => ++test2ReceiveCount);
259
+ _ = token1.RegisterComponentBroadcast<SimpleBroadcastMessage>(
260
+ component1,
261
+ _ => ++test1ReceiveCount
262
+ );
263
+ _ = token2.RegisterComponentBroadcast<SimpleBroadcastMessage>(
264
+ component2,
265
+ _ => ++test2ReceiveCount
266
+ );
213
267
 
214
268
  SimpleBroadcastMessage message = new();
215
269
  message.EmitComponentBroadcast(component1);
@@ -226,7 +280,8 @@
226
280
  Assert.AreEqual(1, test1ReceiveCount);
227
281
  Assert.AreEqual(1, test2ReceiveCount);
228
282
 
229
- EmptyMessageAwareComponent component3 = test3.AddComponent<EmptyMessageAwareComponent>();
283
+ EmptyMessageAwareComponent component3 =
284
+ test3.AddComponent<EmptyMessageAwareComponent>();
230
285
  message.EmitComponentBroadcast(component3);
231
286
  Assert.AreEqual(1, test1ReceiveCount);
232
287
  Assert.AreEqual(1, test2ReceiveCount);
@@ -248,12 +303,20 @@
248
303
  [UnityTest]
249
304
  public IEnumerator SimpleComponentBroadcastNoCopy()
250
305
  {
251
- GameObject test1 = new(nameof(SimpleComponentBroadcastNoCopy) + "1", typeof(EmptyMessageAwareComponent));
306
+ GameObject test1 = new(
307
+ nameof(SimpleComponentBroadcastNoCopy) + "1",
308
+ typeof(EmptyMessageAwareComponent)
309
+ );
252
310
  _spawned.Add(test1);
253
- GameObject test2 = new(nameof(SimpleComponentBroadcastNoCopy) + "2", typeof(EmptyMessageAwareComponent));
311
+ GameObject test2 = new(
312
+ nameof(SimpleComponentBroadcastNoCopy) + "2",
313
+ typeof(EmptyMessageAwareComponent)
314
+ );
254
315
  _spawned.Add(test2);
255
- EmptyMessageAwareComponent component1 = test1.GetComponent<EmptyMessageAwareComponent>();
256
- EmptyMessageAwareComponent component2 = test2.GetComponent<EmptyMessageAwareComponent>();
316
+ EmptyMessageAwareComponent component1 =
317
+ test1.GetComponent<EmptyMessageAwareComponent>();
318
+ EmptyMessageAwareComponent component2 =
319
+ test2.GetComponent<EmptyMessageAwareComponent>();
257
320
 
258
321
  int test1ReceiveCount = 0;
259
322
  void Test1Receive(ref SimpleBroadcastMessage message)
@@ -288,7 +351,8 @@
288
351
  Assert.AreEqual(1, test1ReceiveCount);
289
352
  Assert.AreEqual(1, test2ReceiveCount);
290
353
 
291
- EmptyMessageAwareComponent component3 = test3.AddComponent<EmptyMessageAwareComponent>();
354
+ EmptyMessageAwareComponent component3 =
355
+ test3.AddComponent<EmptyMessageAwareComponent>();
292
356
  message.EmitComponentBroadcast(component3);
293
357
  Assert.AreEqual(1, test1ReceiveCount);
294
358
  Assert.AreEqual(1, test2ReceiveCount);
@@ -310,12 +374,20 @@
310
374
  [UnityTest]
311
375
  public IEnumerator SimpleComponentBroadcastDualMode()
312
376
  {
313
- GameObject test1 = new(nameof(SimpleComponentBroadcastDualMode) + "1", typeof(EmptyMessageAwareComponent));
377
+ GameObject test1 = new(
378
+ nameof(SimpleComponentBroadcastDualMode) + "1",
379
+ typeof(EmptyMessageAwareComponent)
380
+ );
314
381
  _spawned.Add(test1);
315
- GameObject test2 = new(nameof(SimpleComponentBroadcastDualMode) + "2", typeof(EmptyMessageAwareComponent));
382
+ GameObject test2 = new(
383
+ nameof(SimpleComponentBroadcastDualMode) + "2",
384
+ typeof(EmptyMessageAwareComponent)
385
+ );
316
386
  _spawned.Add(test2);
317
- EmptyMessageAwareComponent component1 = test1.GetComponent<EmptyMessageAwareComponent>();
318
- EmptyMessageAwareComponent component2 = test2.GetComponent<EmptyMessageAwareComponent>();
387
+ EmptyMessageAwareComponent component1 =
388
+ test1.GetComponent<EmptyMessageAwareComponent>();
389
+ EmptyMessageAwareComponent component2 =
390
+ test2.GetComponent<EmptyMessageAwareComponent>();
319
391
 
320
392
  int test1ReceiveCount = 0;
321
393
  void Test1Receive(ref SimpleBroadcastMessage message)
@@ -333,7 +405,10 @@
333
405
  MessageRegistrationToken token2 = GetToken(component2);
334
406
 
335
407
  _ = token1.RegisterComponentBroadcast<SimpleBroadcastMessage>(component1, Test1Receive);
336
- _ = token1.RegisterComponentBroadcast<SimpleBroadcastMessage>(component1, _ => ++test1ReceiveCount);
408
+ _ = token1.RegisterComponentBroadcast<SimpleBroadcastMessage>(
409
+ component1,
410
+ _ => ++test1ReceiveCount
411
+ );
337
412
  _ = token2.RegisterComponentBroadcast<SimpleBroadcastMessage>(component2, Test2Receive);
338
413
 
339
414
  SimpleBroadcastMessage message = new();
@@ -351,7 +426,8 @@
351
426
  Assert.AreEqual(2, test1ReceiveCount);
352
427
  Assert.AreEqual(1, test2ReceiveCount);
353
428
 
354
- EmptyMessageAwareComponent component3 = test3.AddComponent<EmptyMessageAwareComponent>();
429
+ EmptyMessageAwareComponent component3 =
430
+ test3.AddComponent<EmptyMessageAwareComponent>();
355
431
  message.EmitComponentBroadcast(component3);
356
432
  Assert.AreEqual(2, test1ReceiveCount);
357
433
  Assert.AreEqual(1, test2ReceiveCount);
@@ -373,12 +449,20 @@
373
449
  [UnityTest]
374
450
  public IEnumerator SimpleBroadcastWithoutSourceNormal()
375
451
  {
376
- GameObject test1 = new(nameof(SimpleBroadcastWithoutSourceNormal) + "1", typeof(EmptyMessageAwareComponent));
452
+ GameObject test1 = new(
453
+ nameof(SimpleBroadcastWithoutSourceNormal) + "1",
454
+ typeof(EmptyMessageAwareComponent)
455
+ );
377
456
  _spawned.Add(test1);
378
- GameObject test2 = new(nameof(SimpleBroadcastWithoutSourceNormal) + "2", typeof(EmptyMessageAwareComponent));
457
+ GameObject test2 = new(
458
+ nameof(SimpleBroadcastWithoutSourceNormal) + "2",
459
+ typeof(EmptyMessageAwareComponent)
460
+ );
379
461
  _spawned.Add(test2);
380
- EmptyMessageAwareComponent component1 = test1.GetComponent<EmptyMessageAwareComponent>();
381
- EmptyMessageAwareComponent component2 = test2.GetComponent<EmptyMessageAwareComponent>();
462
+ EmptyMessageAwareComponent component1 =
463
+ test1.GetComponent<EmptyMessageAwareComponent>();
464
+ EmptyMessageAwareComponent component2 =
465
+ test2.GetComponent<EmptyMessageAwareComponent>();
382
466
 
383
467
  int test1ReceiveCount = 0;
384
468
  void Test1Receive(InstanceId id, SimpleBroadcastMessage message)
@@ -413,7 +497,8 @@
413
497
  Assert.AreEqual(3, test1ReceiveCount);
414
498
  Assert.AreEqual(3, test2ReceiveCount);
415
499
 
416
- EmptyMessageAwareComponent component3 = test3.AddComponent<EmptyMessageAwareComponent>();
500
+ EmptyMessageAwareComponent component3 =
501
+ test3.AddComponent<EmptyMessageAwareComponent>();
417
502
  message.EmitComponentBroadcast(component3);
418
503
  Assert.AreEqual(4, test1ReceiveCount);
419
504
  Assert.AreEqual(4, test2ReceiveCount);
@@ -435,12 +520,20 @@
435
520
  [UnityTest]
436
521
  public IEnumerator SimpleBroadcastWithoutSourceNoCopy()
437
522
  {
438
- GameObject test1 = new(nameof(SimpleBroadcastWithoutSourceNoCopy) + "1", typeof(EmptyMessageAwareComponent));
523
+ GameObject test1 = new(
524
+ nameof(SimpleBroadcastWithoutSourceNoCopy) + "1",
525
+ typeof(EmptyMessageAwareComponent)
526
+ );
439
527
  _spawned.Add(test1);
440
- GameObject test2 = new(nameof(SimpleBroadcastWithoutSourceNoCopy) + "2", typeof(EmptyMessageAwareComponent));
528
+ GameObject test2 = new(
529
+ nameof(SimpleBroadcastWithoutSourceNoCopy) + "2",
530
+ typeof(EmptyMessageAwareComponent)
531
+ );
441
532
  _spawned.Add(test2);
442
- EmptyMessageAwareComponent component1 = test1.GetComponent<EmptyMessageAwareComponent>();
443
- EmptyMessageAwareComponent component2 = test2.GetComponent<EmptyMessageAwareComponent>();
533
+ EmptyMessageAwareComponent component1 =
534
+ test1.GetComponent<EmptyMessageAwareComponent>();
535
+ EmptyMessageAwareComponent component2 =
536
+ test2.GetComponent<EmptyMessageAwareComponent>();
444
537
 
445
538
  int test1ReceiveCount = 0;
446
539
  void Test1Receive(ref InstanceId id, ref SimpleBroadcastMessage message)
@@ -475,7 +568,8 @@
475
568
  Assert.AreEqual(3, test1ReceiveCount);
476
569
  Assert.AreEqual(3, test2ReceiveCount);
477
570
 
478
- EmptyMessageAwareComponent component3 = test3.AddComponent<EmptyMessageAwareComponent>();
571
+ EmptyMessageAwareComponent component3 =
572
+ test3.AddComponent<EmptyMessageAwareComponent>();
479
573
  message.EmitComponentBroadcast(component3);
480
574
  Assert.AreEqual(4, test1ReceiveCount);
481
575
  Assert.AreEqual(4, test2ReceiveCount);
@@ -497,12 +591,20 @@
497
591
  [UnityTest]
498
592
  public IEnumerator SimpleBroadcastWithoutSourceDualMode()
499
593
  {
500
- GameObject test1 = new(nameof(SimpleBroadcastWithoutSourceDualMode) + "1", typeof(EmptyMessageAwareComponent));
594
+ GameObject test1 = new(
595
+ nameof(SimpleBroadcastWithoutSourceDualMode) + "1",
596
+ typeof(EmptyMessageAwareComponent)
597
+ );
501
598
  _spawned.Add(test1);
502
- GameObject test2 = new(nameof(SimpleBroadcastWithoutSourceDualMode) + "2", typeof(EmptyMessageAwareComponent));
599
+ GameObject test2 = new(
600
+ nameof(SimpleBroadcastWithoutSourceDualMode) + "2",
601
+ typeof(EmptyMessageAwareComponent)
602
+ );
503
603
  _spawned.Add(test2);
504
- EmptyMessageAwareComponent component1 = test1.GetComponent<EmptyMessageAwareComponent>();
505
- EmptyMessageAwareComponent component2 = test2.GetComponent<EmptyMessageAwareComponent>();
604
+ EmptyMessageAwareComponent component1 =
605
+ test1.GetComponent<EmptyMessageAwareComponent>();
606
+ EmptyMessageAwareComponent component2 =
607
+ test2.GetComponent<EmptyMessageAwareComponent>();
506
608
 
507
609
  int test1ReceiveCount = 0;
508
610
  void Test1Receive(ref InstanceId id, ref SimpleBroadcastMessage message)
@@ -537,7 +639,8 @@
537
639
  Assert.AreEqual(3, test1ReceiveCount);
538
640
  Assert.AreEqual(3, test2ReceiveCount);
539
641
 
540
- EmptyMessageAwareComponent component3 = test3.AddComponent<EmptyMessageAwareComponent>();
642
+ EmptyMessageAwareComponent component3 =
643
+ test3.AddComponent<EmptyMessageAwareComponent>();
541
644
  message.EmitComponentBroadcast(component3);
542
645
  Assert.AreEqual(4, test1ReceiveCount);
543
646
  Assert.AreEqual(4, test2ReceiveCount);
@@ -559,7 +662,10 @@
559
662
  [UnityTest]
560
663
  public IEnumerator BroadcastUntyped()
561
664
  {
562
- GameObject test = new(nameof(BroadcastUntyped) + "1", typeof(EmptyMessageAwareComponent));
665
+ GameObject test = new(
666
+ nameof(BroadcastUntyped) + "1",
667
+ typeof(EmptyMessageAwareComponent)
668
+ );
563
669
  _spawned.Add(test);
564
670
 
565
671
  int gameObjectCount = 0;
@@ -596,5 +702,131 @@
596
702
 
597
703
  yield break;
598
704
  }
705
+
706
+ [UnityTest]
707
+ public IEnumerator PriorityGameObject()
708
+ {
709
+ GameObject test = new(
710
+ nameof(PriorityGameObject) + "1",
711
+ typeof(EmptyMessageAwareComponent)
712
+ );
713
+ _spawned.Add(test);
714
+
715
+ int[] received = new int[100];
716
+ EmptyMessageAwareComponent component = test.GetComponent<EmptyMessageAwareComponent>();
717
+ MessageRegistrationToken token = GetToken(component);
718
+ for (int i = 0; i < received.Length; ++i)
719
+ {
720
+ int priority = i;
721
+ token.RegisterGameObjectBroadcast(
722
+ test,
723
+ (ref SimpleBroadcastMessage _) =>
724
+ {
725
+ int previous = received[priority]++;
726
+ if (0 < priority)
727
+ {
728
+ Assert.AreEqual(previous + 1, received[priority - 1]);
729
+ }
730
+ },
731
+ priority: priority
732
+ );
733
+ token.RegisterGameObjectBroadcastPostProcessor(
734
+ test,
735
+ (ref SimpleBroadcastMessage _) =>
736
+ {
737
+ int previous = received[priority]++;
738
+ Assert.AreEqual(1, previous % 2);
739
+ if (0 < priority)
740
+ {
741
+ Assert.AreEqual(previous + 1, received[priority - 1]);
742
+ }
743
+ },
744
+ priority: priority
745
+ );
746
+ }
747
+
748
+ SimpleBroadcastMessage message = new();
749
+ const int numRuns = 100;
750
+ for (int i = 0; i < numRuns; ++i)
751
+ {
752
+ // Should do something
753
+ message.EmitGameObjectBroadcast(test);
754
+ // Should do nothing
755
+ message.EmitComponentBroadcast(component);
756
+ }
757
+
758
+ Assert.AreEqual(
759
+ 1,
760
+ received.Distinct().Count(),
761
+ "Expected received to be uniform, found: [{0}].",
762
+ string.Join(",", received.Distinct().OrderBy(x => x))
763
+ );
764
+
765
+ Assert.AreEqual(numRuns * 2, received.Distinct().Single());
766
+ yield break;
767
+ }
768
+
769
+ [UnityTest]
770
+ public IEnumerator PriorityComponent()
771
+ {
772
+ GameObject test = new(
773
+ nameof(PriorityComponent) + "1",
774
+ typeof(EmptyMessageAwareComponent)
775
+ );
776
+ _spawned.Add(test);
777
+
778
+ int[] received = new int[100];
779
+ EmptyMessageAwareComponent component = test.GetComponent<EmptyMessageAwareComponent>();
780
+ MessageRegistrationToken token = GetToken(component);
781
+ for (int i = 0; i < received.Length; ++i)
782
+ {
783
+ int priority = i;
784
+ token.RegisterComponentBroadcast(
785
+ component,
786
+ (ref SimpleBroadcastMessage _) =>
787
+ {
788
+ int previous = received[priority]++;
789
+ if (0 < priority)
790
+ {
791
+ Assert.AreEqual(previous + 1, received[priority - 1]);
792
+ }
793
+ },
794
+ priority: priority
795
+ );
796
+ token.RegisterComponentBroadcastPostProcessor(
797
+ component,
798
+ (ref SimpleBroadcastMessage _) =>
799
+ {
800
+ int previous = received[priority]++;
801
+ Assert.AreEqual(1, previous % 2);
802
+ if (0 < priority)
803
+ {
804
+ Assert.AreEqual(previous + 1, received[priority - 1]);
805
+ }
806
+ },
807
+ priority: priority
808
+ );
809
+ }
810
+
811
+ SimpleBroadcastMessage message = new();
812
+ const int numRuns = 100;
813
+ for (int i = 0; i < numRuns; ++i)
814
+ {
815
+ // Should do something
816
+ message.EmitComponentBroadcast(component);
817
+ // Should do nothing
818
+ message.EmitGameObjectBroadcast(test);
819
+ }
820
+
821
+ Assert.AreEqual(
822
+ 1,
823
+ received.Distinct().Count(),
824
+ "Expected received to be uniform, found: [{0}].",
825
+ string.Join(",", received.Distinct().OrderBy(x => x))
826
+ );
827
+
828
+ Assert.AreEqual(numRuns * 2, received.Distinct().Single());
829
+ yield break;
830
+ }
599
831
  }
600
832
  }