com.wallstop-studios.dxmessaging 2.0.0-rc06 → 2.0.0-rc08

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,10 +1,10 @@
1
1
  namespace DxMessaging.Core
2
2
  {
3
- using MessageBus;
4
3
  using System;
5
4
  using System.Collections.Generic;
6
5
  using System.Linq;
7
6
  using System.Runtime.CompilerServices;
7
+ using MessageBus;
8
8
  using Messages;
9
9
 
10
10
  /// <summary>
@@ -13,8 +13,13 @@
13
13
  /// </summary>
14
14
  public sealed class MessageHandler
15
15
  {
16
- public delegate void FastHandler<TMessage>(ref TMessage message) where TMessage : IMessage;
17
- public delegate void FastHandlerWithContext<TMessage>(ref InstanceId context, ref TMessage message) where TMessage : IMessage;
16
+ public delegate void FastHandler<TMessage>(ref TMessage message)
17
+ where TMessage : IMessage;
18
+ public delegate void FastHandlerWithContext<TMessage>(
19
+ ref InstanceId context,
20
+ ref TMessage message
21
+ )
22
+ where TMessage : IMessage;
18
23
 
19
24
  /// <summary>
20
25
  /// MessageBus for all MessageHandlers to use. Currently immutable, but may change in the future.
@@ -27,10 +32,13 @@
27
32
  /// <note>
28
33
  /// Ideally, this would be something like a Dictionary[T,Handler[T]], but that can't be done with C#s type system.
29
34
  /// </note>
30
- private readonly Dictionary<IMessageBus, Dictionary<Type, object>> _handlersByTypeByMessageBus;
35
+ private readonly Dictionary<
36
+ IMessageBus,
37
+ Dictionary<Type, object>
38
+ > _handlersByTypeByMessageBus;
31
39
 
32
40
  /// <summary>
33
- /// Whether or not this MessageHandler will process messages.
41
+ /// Whether this MessageHandler will process messages.
34
42
  /// </summary>
35
43
  public bool active;
36
44
 
@@ -53,16 +61,28 @@
53
61
  /// </note>
54
62
  /// <param name="message">Message to handle.</param>
55
63
  /// <param name="messageBus">The specific MessageBus to use.</param>
56
- public void HandleUntargetedMessage<TMessage>(ref TMessage message, IMessageBus messageBus) where TMessage : IMessage
64
+ /// <param name="priority">Priority at which to run the handlers.</param>
65
+ public void HandleUntargetedMessage<TMessage>(
66
+ ref TMessage message,
67
+ IMessageBus messageBus,
68
+ int priority
69
+ )
70
+ where TMessage : IMessage
57
71
  {
58
72
  if (!active)
59
73
  {
60
74
  return;
61
75
  }
62
76
 
63
- if (GetHandlerForType(message.MessageType, messageBus, out TypedHandler<TMessage> handler))
77
+ if (
78
+ GetHandlerForType(
79
+ message.MessageType,
80
+ messageBus,
81
+ out TypedHandler<TMessage> handler
82
+ )
83
+ )
64
84
  {
65
- handler.HandleUntargeted(ref message);
85
+ handler.HandleUntargeted(ref message, priority);
66
86
  }
67
87
  }
68
88
 
@@ -74,16 +94,28 @@
74
94
  /// </note>
75
95
  /// <param name="message">Message to handle.</param>
76
96
  /// <param name="messageBus">The specific MessageBus to use.</param>
77
- public void HandleUntargetedPostProcessing<TMessage>(ref TMessage message, IMessageBus messageBus) where TMessage : IUntargetedMessage
97
+ /// <param name="priority">Priority at which to run the handlers.</param>
98
+ public void HandleUntargetedPostProcessing<TMessage>(
99
+ ref TMessage message,
100
+ IMessageBus messageBus,
101
+ int priority
102
+ )
103
+ where TMessage : IUntargetedMessage
78
104
  {
79
105
  if (!active)
80
106
  {
81
107
  return;
82
108
  }
83
109
 
84
- if (GetHandlerForType(message.MessageType, messageBus, out TypedHandler<TMessage> handler))
110
+ if (
111
+ GetHandlerForType(
112
+ message.MessageType,
113
+ messageBus,
114
+ out TypedHandler<TMessage> handler
115
+ )
116
+ )
85
117
  {
86
- handler.HandleUntargetedPostProcessing(ref message);
118
+ handler.HandleUntargetedPostProcessing(ref message, priority);
87
119
  }
88
120
  }
89
121
 
@@ -96,16 +128,29 @@
96
128
  /// <param name="target">Target Id the message is for.</param>
97
129
  /// <param name="message">Message to handle.</param>
98
130
  /// <param name="messageBus">The specific MessageBus to use.</param>
99
- public void HandleTargeted<TMessage>(ref InstanceId target, ref TMessage message, IMessageBus messageBus) where TMessage : ITargetedMessage
131
+ /// <param name="priority">Priority at which to run the handlers.</param>
132
+ public void HandleTargeted<TMessage>(
133
+ ref InstanceId target,
134
+ ref TMessage message,
135
+ IMessageBus messageBus,
136
+ int priority
137
+ )
138
+ where TMessage : ITargetedMessage
100
139
  {
101
140
  if (!active)
102
141
  {
103
142
  return;
104
143
  }
105
144
 
106
- if (GetHandlerForType(message.MessageType, messageBus, out TypedHandler<TMessage> handler))
145
+ if (
146
+ GetHandlerForType(
147
+ message.MessageType,
148
+ messageBus,
149
+ out TypedHandler<TMessage> handler
150
+ )
151
+ )
107
152
  {
108
- handler.HandleTargeted(ref target, ref message);
153
+ handler.HandleTargeted(ref target, ref message, priority);
109
154
  }
110
155
  }
111
156
 
@@ -118,17 +163,29 @@
118
163
  /// <param name="target">Target Id the message is for.</param>
119
164
  /// <param name="message">Message to handle.</param>
120
165
  /// <param name="messageBus">The specific MessageBus to use.</param>
121
-
122
- public void HandleTargetedWithoutTargeting<TMessage>(ref InstanceId target, ref TMessage message, IMessageBus messageBus) where TMessage : ITargetedMessage
166
+ /// <param name="priority">Priority at which to run the handlers.</param>
167
+ public void HandleTargetedWithoutTargeting<TMessage>(
168
+ ref InstanceId target,
169
+ ref TMessage message,
170
+ IMessageBus messageBus,
171
+ int priority
172
+ )
173
+ where TMessage : ITargetedMessage
123
174
  {
124
175
  if (!active)
125
176
  {
126
177
  return;
127
178
  }
128
179
 
129
- if (GetHandlerForType(message.MessageType, messageBus, out TypedHandler<TMessage> handler))
180
+ if (
181
+ GetHandlerForType(
182
+ message.MessageType,
183
+ messageBus,
184
+ out TypedHandler<TMessage> handler
185
+ )
186
+ )
130
187
  {
131
- handler.HandleTargetedWithoutTargeting(ref target, ref message);
188
+ handler.HandleTargetedWithoutTargeting(ref target, ref message, priority);
132
189
  }
133
190
  }
134
191
 
@@ -141,17 +198,29 @@
141
198
  /// <param name="target">Target Id the message is for.</param>
142
199
  /// <param name="message">Message to handle.</param>
143
200
  /// <param name="messageBus">The specific MessageBus to use.</param>
144
-
145
- public void HandleTargetedPostProcessing<TMessage>(ref InstanceId target, ref TMessage message, IMessageBus messageBus) where TMessage : ITargetedMessage
201
+ /// <param name="priority">Priority at which to run the handlers.</param>
202
+ public void HandleTargetedPostProcessing<TMessage>(
203
+ ref InstanceId target,
204
+ ref TMessage message,
205
+ IMessageBus messageBus,
206
+ int priority
207
+ )
208
+ where TMessage : ITargetedMessage
146
209
  {
147
210
  if (!active)
148
211
  {
149
212
  return;
150
213
  }
151
214
 
152
- if (GetHandlerForType(message.MessageType, messageBus, out TypedHandler<TMessage> handler))
215
+ if (
216
+ GetHandlerForType(
217
+ message.MessageType,
218
+ messageBus,
219
+ out TypedHandler<TMessage> handler
220
+ )
221
+ )
153
222
  {
154
- handler.HandleTargetedPostProcessing(ref target, ref message);
223
+ handler.HandleTargetedPostProcessing(ref target, ref message, priority);
155
224
  }
156
225
  }
157
226
 
@@ -164,17 +233,33 @@
164
233
  /// <param name="target">Target Id the message is for.</param>
165
234
  /// <param name="message">Message to handle.</param>
166
235
  /// <param name="messageBus">The specific MessageBus to use.</param>
167
-
168
- public void HandleTargetedWithoutTargetingPostProcessing<TMessage>(ref InstanceId target, ref TMessage message, IMessageBus messageBus) where TMessage : ITargetedMessage
236
+ /// <param name="priority">Priority at which to run the handlers.</param>
237
+ public void HandleTargetedWithoutTargetingPostProcessing<TMessage>(
238
+ ref InstanceId target,
239
+ ref TMessage message,
240
+ IMessageBus messageBus,
241
+ int priority
242
+ )
243
+ where TMessage : ITargetedMessage
169
244
  {
170
245
  if (!active)
171
246
  {
172
247
  return;
173
248
  }
174
249
 
175
- if (GetHandlerForType(message.MessageType, messageBus, out TypedHandler<TMessage> handler))
250
+ if (
251
+ GetHandlerForType(
252
+ message.MessageType,
253
+ messageBus,
254
+ out TypedHandler<TMessage> handler
255
+ )
256
+ )
176
257
  {
177
- handler.HandleTargetedWithoutTargetingPostProcessing(ref target, ref message);
258
+ handler.HandleTargetedWithoutTargetingPostProcessing(
259
+ ref target,
260
+ ref message,
261
+ priority
262
+ );
178
263
  }
179
264
  }
180
265
 
@@ -187,16 +272,29 @@
187
272
  /// <param name="source">Source Id the broadcast message is from.</param>
188
273
  /// <param name="message">Message to handle</param>
189
274
  /// <param name="messageBus">The specific MessageBus to use.</param>
190
- public void HandleSourcedBroadcast<TMessage>(ref InstanceId source, ref TMessage message, IMessageBus messageBus) where TMessage : IBroadcastMessage
275
+ /// <param name="priority">Priority at which to run the handlers.</param>
276
+ public void HandleSourcedBroadcast<TMessage>(
277
+ ref InstanceId source,
278
+ ref TMessage message,
279
+ IMessageBus messageBus,
280
+ int priority
281
+ )
282
+ where TMessage : IBroadcastMessage
191
283
  {
192
284
  if (!active)
193
285
  {
194
286
  return;
195
287
  }
196
288
 
197
- if (GetHandlerForType(message.MessageType, messageBus, out TypedHandler<TMessage> handler))
289
+ if (
290
+ GetHandlerForType(
291
+ message.MessageType,
292
+ messageBus,
293
+ out TypedHandler<TMessage> handler
294
+ )
295
+ )
198
296
  {
199
- handler.HandleSourcedBroadcast(ref source, ref message);
297
+ handler.HandleSourcedBroadcast(ref source, ref message, priority);
200
298
  }
201
299
  }
202
300
 
@@ -209,16 +307,29 @@
209
307
  /// <param name="source">Source Id the broadcast message is from.</param>
210
308
  /// <param name="message">Message to handle</param>
211
309
  /// <param name="messageBus">The specific MessageBus to use.</param>
212
- public void HandleSourcedBroadcastWithoutSource<TMessage>(ref InstanceId source, ref TMessage message, IMessageBus messageBus) where TMessage : IBroadcastMessage
310
+ /// <param name="priority">Priority at which to run the handlers.</param>
311
+ public void HandleSourcedBroadcastWithoutSource<TMessage>(
312
+ ref InstanceId source,
313
+ ref TMessage message,
314
+ IMessageBus messageBus,
315
+ int priority
316
+ )
317
+ where TMessage : IBroadcastMessage
213
318
  {
214
319
  if (!active)
215
320
  {
216
321
  return;
217
322
  }
218
323
 
219
- if (GetHandlerForType(message.MessageType, messageBus, out TypedHandler<TMessage> handler))
324
+ if (
325
+ GetHandlerForType(
326
+ message.MessageType,
327
+ messageBus,
328
+ out TypedHandler<TMessage> handler
329
+ )
330
+ )
220
331
  {
221
- handler.HandleSourcedBroadcastWithoutSource(ref source, ref message);
332
+ handler.HandleSourcedBroadcastWithoutSource(ref source, ref message, priority);
222
333
  }
223
334
  }
224
335
 
@@ -231,16 +342,29 @@
231
342
  /// <param name="source">Source Id the broadcast message is from.</param>
232
343
  /// <param name="message">Message to handle</param>
233
344
  /// <param name="messageBus">The specific MessageBus to use.</param>
234
- public void HandleSourcedBroadcastPostProcessing<TMessage>(ref InstanceId source, ref TMessage message, IMessageBus messageBus) where TMessage :IBroadcastMessage
345
+ /// <param name="priority">Priority at which to run the handlers.</param>
346
+ public void HandleSourcedBroadcastPostProcessing<TMessage>(
347
+ ref InstanceId source,
348
+ ref TMessage message,
349
+ IMessageBus messageBus,
350
+ int priority
351
+ )
352
+ where TMessage : IBroadcastMessage
235
353
  {
236
354
  if (!active)
237
355
  {
238
356
  return;
239
357
  }
240
358
 
241
- if (GetHandlerForType(message.MessageType, messageBus, out TypedHandler<TMessage> handler))
359
+ if (
360
+ GetHandlerForType(
361
+ message.MessageType,
362
+ messageBus,
363
+ out TypedHandler<TMessage> handler
364
+ )
365
+ )
242
366
  {
243
- handler.HandleSourcedBroadcastPostProcessing(ref source, ref message);
367
+ handler.HandleSourcedBroadcastPostProcessing(ref source, ref message, priority);
244
368
  }
245
369
  }
246
370
 
@@ -253,16 +377,33 @@
253
377
  /// <param name="source">Source Id the broadcast message is from.</param>
254
378
  /// <param name="message">Message to handle</param>
255
379
  /// <param name="messageBus">The specific MessageBus to use.</param>
256
- public void HandleSourcedBroadcastWithoutSourcePostProcessing<TMessage>(ref InstanceId source, ref TMessage message, IMessageBus messageBus) where TMessage : IBroadcastMessage
380
+ /// <param name="priority">Priority at which to run the handlers.</param>
381
+ public void HandleSourcedBroadcastWithoutSourcePostProcessing<TMessage>(
382
+ ref InstanceId source,
383
+ ref TMessage message,
384
+ IMessageBus messageBus,
385
+ int priority
386
+ )
387
+ where TMessage : IBroadcastMessage
257
388
  {
258
389
  if (!active)
259
390
  {
260
391
  return;
261
392
  }
262
393
 
263
- if (GetHandlerForType(message.MessageType, messageBus, out TypedHandler<TMessage> handler))
394
+ if (
395
+ GetHandlerForType(
396
+ message.MessageType,
397
+ messageBus,
398
+ out TypedHandler<TMessage> handler
399
+ )
400
+ )
264
401
  {
265
- handler.HandleBroadcastWithoutSourcePostProcessing(ref source, ref message);
402
+ handler.HandleBroadcastWithoutSourcePostProcessing(
403
+ ref source,
404
+ ref message,
405
+ priority
406
+ );
266
407
  }
267
408
  }
268
409
 
@@ -271,7 +412,10 @@
271
412
  /// </summary>
272
413
  /// <param name="message">Message to handle.</param>
273
414
  /// <param name="messageBus">The specific MessageBus to use.</param>
274
- public void HandleGlobalUntargetedMessage(ref IUntargetedMessage message, IMessageBus messageBus)
415
+ public void HandleGlobalUntargetedMessage(
416
+ ref IUntargetedMessage message,
417
+ IMessageBus messageBus
418
+ )
275
419
  {
276
420
  if (!active)
277
421
  {
@@ -291,7 +435,11 @@
291
435
  /// <param name="target">Target of the message.</param>
292
436
  /// <param name="message">Message to handle.</param>
293
437
  /// <param name="messageBus">The specific MessageBus to use.</param>
294
- public void HandleGlobalTargetedMessage(ref InstanceId target, ref ITargetedMessage message, IMessageBus messageBus)
438
+ public void HandleGlobalTargetedMessage(
439
+ ref InstanceId target,
440
+ ref ITargetedMessage message,
441
+ IMessageBus messageBus
442
+ )
295
443
  {
296
444
  if (!active)
297
445
  {
@@ -311,7 +459,11 @@
311
459
  /// <param name="source">Source that this message is from.</param>
312
460
  /// <param name="message">Message to handle.</param>
313
461
  /// <param name="messageBus">The specific MessageBus to use.</param>
314
- public void HandleGlobalSourcedBroadcastMessage(ref InstanceId source, ref IBroadcastMessage message, IMessageBus messageBus)
462
+ public void HandleGlobalSourcedBroadcastMessage(
463
+ ref InstanceId source,
464
+ ref IBroadcastMessage message,
465
+ IMessageBus messageBus
466
+ )
315
467
  {
316
468
  if (!active)
317
469
  {
@@ -333,19 +485,29 @@
333
485
  /// <param name="targetedMessageHandler">MessageHandler to accept all BroadcastMessages for all entities.</param>
334
486
  /// <param name="messageBus">IMessageBus override to register with, if any. Null/not provided defaults to the GlobalMessageBus.</param>
335
487
  /// <returns>The de-registration action.</returns>
336
- public Action RegisterGlobalAcceptAll(Action<IUntargetedMessage> untargetedMessageHandler, Action<InstanceId, ITargetedMessage> targetedMessageHandler, Action<InstanceId, IBroadcastMessage> broadcastMessageHandler, IMessageBus messageBus = null)
488
+ public Action RegisterGlobalAcceptAll(
489
+ Action<IUntargetedMessage> untargetedMessageHandler,
490
+ Action<InstanceId, ITargetedMessage> targetedMessageHandler,
491
+ Action<InstanceId, IBroadcastMessage> broadcastMessageHandler,
492
+ IMessageBus messageBus = null
493
+ )
337
494
  {
338
495
  messageBus ??= MessageBus;
339
496
  Action messageBusDeregistration = messageBus.RegisterGlobalAcceptAll(this);
340
497
  TypedHandler<IMessage> typedHandler = GetOrCreateHandlerForType<IMessage>(messageBus);
341
498
 
342
- void NullDeregistration()
343
- {
344
-
345
- }
346
- Action untargetedDeregistration = typedHandler.AddGlobalUntargetedHandler(untargetedMessageHandler, NullDeregistration);
347
- Action targetedDeregistration = typedHandler.AddGlobalTargetedHandler(targetedMessageHandler, NullDeregistration);
348
- Action broadcastDeregistration = typedHandler.AddGlobalBroadcastHandler(broadcastMessageHandler, NullDeregistration);
499
+ Action untargetedDeregistration = typedHandler.AddGlobalUntargetedHandler(
500
+ untargetedMessageHandler,
501
+ NullDeregistration
502
+ );
503
+ Action targetedDeregistration = typedHandler.AddGlobalTargetedHandler(
504
+ targetedMessageHandler,
505
+ NullDeregistration
506
+ );
507
+ Action broadcastDeregistration = typedHandler.AddGlobalBroadcastHandler(
508
+ broadcastMessageHandler,
509
+ NullDeregistration
510
+ );
349
511
 
350
512
  return () =>
351
513
  {
@@ -354,6 +516,11 @@
354
516
  broadcastDeregistration();
355
517
  messageBusDeregistration?.Invoke();
356
518
  };
519
+
520
+ void NullDeregistration()
521
+ {
522
+ // No-op
523
+ }
357
524
  }
358
525
 
359
526
  /// <summary>
@@ -364,19 +531,29 @@
364
531
  /// <param name="targetedMessageHandler">MessageHandler to accept all BroadcastMessages for all entities.</param>
365
532
  /// <param name="messageBus">IMessageBus override to register with, if any. Null/not provided defaults to the GlobalMessageBus.</param>
366
533
  /// <returns>The de-registration action.</returns>
367
- public Action RegisterGlobalAcceptAll(FastHandler<IUntargetedMessage> untargetedMessageHandler, FastHandlerWithContext<ITargetedMessage> targetedMessageHandler, FastHandlerWithContext<IBroadcastMessage> broadcastMessageHandler, IMessageBus messageBus = null)
534
+ public Action RegisterGlobalAcceptAll(
535
+ FastHandler<IUntargetedMessage> untargetedMessageHandler,
536
+ FastHandlerWithContext<ITargetedMessage> targetedMessageHandler,
537
+ FastHandlerWithContext<IBroadcastMessage> broadcastMessageHandler,
538
+ IMessageBus messageBus = null
539
+ )
368
540
  {
369
541
  messageBus ??= MessageBus;
370
542
  Action messageBusDeregistration = messageBus.RegisterGlobalAcceptAll(this);
371
543
  TypedHandler<IMessage> typedHandler = GetOrCreateHandlerForType<IMessage>(messageBus);
372
544
 
373
- void NullDeregistration()
374
- {
375
-
376
- }
377
- Action untargetedDeregistration = typedHandler.AddGlobalUntargetedHandler(untargetedMessageHandler, NullDeregistration);
378
- Action targetedDeregistration = typedHandler.AddGlobalTargetedHandler(targetedMessageHandler, NullDeregistration);
379
- Action broadcastDeregistration = typedHandler.AddGlobalBroadcastHandler(broadcastMessageHandler, NullDeregistration);
545
+ Action untargetedDeregistration = typedHandler.AddGlobalUntargetedHandler(
546
+ untargetedMessageHandler,
547
+ NullDeregistration
548
+ );
549
+ Action targetedDeregistration = typedHandler.AddGlobalTargetedHandler(
550
+ targetedMessageHandler,
551
+ NullDeregistration
552
+ );
553
+ Action broadcastDeregistration = typedHandler.AddGlobalBroadcastHandler(
554
+ broadcastMessageHandler,
555
+ NullDeregistration
556
+ );
380
557
 
381
558
  return () =>
382
559
  {
@@ -385,6 +562,11 @@
385
562
  broadcastDeregistration();
386
563
  messageBusDeregistration?.Invoke();
387
564
  };
565
+
566
+ void NullDeregistration()
567
+ {
568
+ // No-op
569
+ }
388
570
  }
389
571
 
390
572
  /// <summary>
@@ -393,14 +575,30 @@
393
575
  /// <typeparam name="T">Type of Message to be handled.</typeparam>
394
576
  /// <param name="target">Target Id of TargetedMessages to listen for.</param>
395
577
  /// <param name="messageHandler">Function that actually handles the message.</param>
578
+ /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
396
579
  /// <param name="messageBus">IMessageBus override to register with, if any. Null/not provided defaults to the GlobalMessageBus.</param>
397
580
  /// <returns>The de-registration action.</returns>
398
- public Action RegisterTargetedMessageHandler<T>(InstanceId target, Action<T> messageHandler, IMessageBus messageBus = null) where T : ITargetedMessage
581
+ public Action RegisterTargetedMessageHandler<T>(
582
+ InstanceId target,
583
+ Action<T> messageHandler,
584
+ int priority = 0,
585
+ IMessageBus messageBus = null
586
+ )
587
+ where T : ITargetedMessage
399
588
  {
400
589
  messageBus ??= MessageBus;
401
- Action messageBusDeregistration = messageBus.RegisterTargeted<T>(target, this);
590
+ Action messageBusDeregistration = messageBus.RegisterTargeted<T>(
591
+ target,
592
+ this,
593
+ priority: priority
594
+ );
402
595
  TypedHandler<T> typedHandler = GetOrCreateHandlerForType<T>(messageBus);
403
- return typedHandler.AddTargetedHandler(target, messageHandler, messageBusDeregistration);
596
+ return typedHandler.AddTargetedHandler(
597
+ target,
598
+ messageHandler,
599
+ messageBusDeregistration,
600
+ priority
601
+ );
404
602
  }
405
603
 
406
604
  /// <summary>
@@ -409,14 +607,30 @@
409
607
  /// <typeparam name="T">Type of Message to be handled.</typeparam>
410
608
  /// <param name="target">Target Id of TargetedMessages to listen for.</param>
411
609
  /// <param name="messageHandler">Function that actually handles the message.</param>
610
+ /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
412
611
  /// <param name="messageBus">IMessageBus override to register with, if any. Null/not provided defaults to the GlobalMessageBus.</param>
413
612
  /// <returns>The de-registration action.</returns>
414
- public Action RegisterTargetedMessageHandler<T>(InstanceId target, FastHandler<T> messageHandler, IMessageBus messageBus = null) where T : ITargetedMessage
613
+ public Action RegisterTargetedMessageHandler<T>(
614
+ InstanceId target,
615
+ FastHandler<T> messageHandler,
616
+ int priority = 0,
617
+ IMessageBus messageBus = null
618
+ )
619
+ where T : ITargetedMessage
415
620
  {
416
621
  messageBus ??= MessageBus;
417
- Action messageBusDeregistration = messageBus.RegisterTargeted<T>(target, this);
622
+ Action messageBusDeregistration = messageBus.RegisterTargeted<T>(
623
+ target,
624
+ this,
625
+ priority: priority
626
+ );
418
627
  TypedHandler<T> typedHandler = GetOrCreateHandlerForType<T>(messageBus);
419
- return typedHandler.AddTargetedHandler(target, messageHandler, messageBusDeregistration);
628
+ return typedHandler.AddTargetedHandler(
629
+ target,
630
+ messageHandler,
631
+ messageBusDeregistration,
632
+ priority
633
+ );
420
634
  }
421
635
 
422
636
  /// <summary>
@@ -425,14 +639,30 @@
425
639
  /// <typeparam name="T">Type of Message to be handled.</typeparam>
426
640
  /// <param name="target">Target Id of TargetedMessages to listen for.</param>
427
641
  /// <param name="messageHandler">Function that actually handles the message.</param>
642
+ /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
428
643
  /// <param name="messageBus">IMessageBus override to register with, if any. Null/not provided defaults to the GlobalMessageBus.</param>
429
644
  /// <returns>The de-registration action.</returns>
430
- public Action RegisterTargetedPostProcessor<T>(InstanceId target, Action<T> messageHandler, IMessageBus messageBus = null) where T : ITargetedMessage
645
+ public Action RegisterTargetedPostProcessor<T>(
646
+ InstanceId target,
647
+ Action<T> messageHandler,
648
+ int priority = 0,
649
+ IMessageBus messageBus = null
650
+ )
651
+ where T : ITargetedMessage
431
652
  {
432
653
  messageBus ??= MessageBus;
433
- Action messageBusDeregistration = messageBus.RegisterTargetedPostProcessor<T>(target, this);
654
+ Action messageBusDeregistration = messageBus.RegisterTargetedPostProcessor<T>(
655
+ target,
656
+ this,
657
+ priority
658
+ );
434
659
  TypedHandler<T> typedHandler = GetOrCreateHandlerForType<T>(messageBus);
435
- return typedHandler.AddTargetedPostProcessor(target, messageHandler, messageBusDeregistration);
660
+ return typedHandler.AddTargetedPostProcessor(
661
+ target,
662
+ messageHandler,
663
+ messageBusDeregistration,
664
+ priority
665
+ );
436
666
  }
437
667
 
438
668
  /// <summary>
@@ -441,14 +671,30 @@
441
671
  /// <typeparam name="T">Type of Message to be handled.</typeparam>
442
672
  /// <param name="target">Target Id of TargetedMessages to listen for.</param>
443
673
  /// <param name="messageHandler">Function that actually handles the message.</param>
674
+ /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
444
675
  /// <param name="messageBus">IMessageBus override to register with, if any. Null/not provided defaults to the GlobalMessageBus.</param>
445
676
  /// <returns>The de-registration action.</returns>
446
- public Action RegisterTargetedPostProcessor<T>(InstanceId target, FastHandler<T> messageHandler, IMessageBus messageBus = null) where T : ITargetedMessage
677
+ public Action RegisterTargetedPostProcessor<T>(
678
+ InstanceId target,
679
+ FastHandler<T> messageHandler,
680
+ int priority = 0,
681
+ IMessageBus messageBus = null
682
+ )
683
+ where T : ITargetedMessage
447
684
  {
448
685
  messageBus ??= MessageBus;
449
- Action messageBusDeregistration = messageBus.RegisterTargetedPostProcessor<T>(target, this);
686
+ Action messageBusDeregistration = messageBus.RegisterTargetedPostProcessor<T>(
687
+ target,
688
+ this,
689
+ priority: priority
690
+ );
450
691
  TypedHandler<T> typedHandler = GetOrCreateHandlerForType<T>(messageBus);
451
- return typedHandler.AddTargetedPostProcessor(target, messageHandler, messageBusDeregistration);
692
+ return typedHandler.AddTargetedPostProcessor(
693
+ target,
694
+ messageHandler,
695
+ messageBusDeregistration,
696
+ priority
697
+ );
452
698
  }
453
699
 
454
700
  /// <summary>
@@ -456,14 +702,28 @@
456
702
  /// </summary>
457
703
  /// <typeparam name="T">Type of Message to be handled.</typeparam>
458
704
  /// <param name="messageHandler">Function that actually handles the message.</param>
705
+ /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
459
706
  /// <param name="messageBus">IMessageBus override to register with, if any. Null/not provided defaults to the GlobalMessageBus.</param>
460
707
  /// <returns>The de-registration action.</returns>
461
- public Action RegisterTargetedWithoutTargetingPostProcessor<T>(Action<InstanceId, T> messageHandler, IMessageBus messageBus = null) where T : ITargetedMessage
708
+ public Action RegisterTargetedWithoutTargetingPostProcessor<T>(
709
+ Action<InstanceId, T> messageHandler,
710
+ int priority = 0,
711
+ IMessageBus messageBus = null
712
+ )
713
+ where T : ITargetedMessage
462
714
  {
463
715
  messageBus ??= MessageBus;
464
- Action messageBusDeregistration = messageBus.RegisterTargetedWithoutTargetingPostProcessor<T>(this);
716
+ Action messageBusDeregistration =
717
+ messageBus.RegisterTargetedWithoutTargetingPostProcessor<T>(
718
+ priority: priority,
719
+ messageHandler: this
720
+ );
465
721
  TypedHandler<T> typedHandler = GetOrCreateHandlerForType<T>(messageBus);
466
- return typedHandler.AddTargetedWithoutTargetingPostProcessor(messageHandler, messageBusDeregistration);
722
+ return typedHandler.AddTargetedWithoutTargetingPostProcessor(
723
+ messageHandler,
724
+ messageBusDeregistration,
725
+ priority
726
+ );
467
727
  }
468
728
 
469
729
  /// <summary>
@@ -471,14 +731,28 @@
471
731
  /// </summary>
472
732
  /// <typeparam name="T">Type of Message to be handled.</typeparam>
473
733
  /// <param name="messageHandler">Function that actually handles the message.</param>
734
+ /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
474
735
  /// <param name="messageBus">IMessageBus override to register with, if any. Null/not provided defaults to the GlobalMessageBus.</param>
475
736
  /// <returns>The de-registration action.</returns>
476
- public Action RegisterTargetedWithoutTargetingPostProcessor<T>(FastHandlerWithContext<T> messageHandler, IMessageBus messageBus = null) where T : ITargetedMessage
737
+ public Action RegisterTargetedWithoutTargetingPostProcessor<T>(
738
+ FastHandlerWithContext<T> messageHandler,
739
+ int priority = 0,
740
+ IMessageBus messageBus = null
741
+ )
742
+ where T : ITargetedMessage
477
743
  {
478
744
  messageBus ??= MessageBus;
479
- Action messageBusDeregistration = messageBus.RegisterTargetedWithoutTargetingPostProcessor<T>(this);
745
+ Action messageBusDeregistration =
746
+ messageBus.RegisterTargetedWithoutTargetingPostProcessor<T>(
747
+ priority: priority,
748
+ messageHandler: this
749
+ );
480
750
  TypedHandler<T> typedHandler = GetOrCreateHandlerForType<T>(messageBus);
481
- return typedHandler.AddTargetedWithoutTargetingPostProcessor(messageHandler, messageBusDeregistration);
751
+ return typedHandler.AddTargetedWithoutTargetingPostProcessor(
752
+ messageHandler,
753
+ messageBusDeregistration,
754
+ priority
755
+ );
482
756
  }
483
757
 
484
758
  /// <summary>
@@ -486,14 +760,27 @@
486
760
  /// </summary>
487
761
  /// <typeparam name="T">Type of Message to be handled.</typeparam>
488
762
  /// <param name="messageHandler">Function that actually handles the message.</param>
763
+ /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
489
764
  /// <param name="messageBus">IMessageBus override to register with, if any. Null/not provided defaults to the GlobalMessageBus.</param>
490
765
  /// <returns>The de-registration action.</returns>
491
- public Action RegisterTargetedWithoutTargeting<T>(Action<InstanceId, T> messageHandler, IMessageBus messageBus = null) where T : ITargetedMessage
766
+ public Action RegisterTargetedWithoutTargeting<T>(
767
+ Action<InstanceId, T> messageHandler,
768
+ int priority = 0,
769
+ IMessageBus messageBus = null
770
+ )
771
+ where T : ITargetedMessage
492
772
  {
493
773
  messageBus ??= MessageBus;
494
- Action messageBusDeregistration = messageBus.RegisterTargetedWithoutTargeting<T>(this);
774
+ Action messageBusDeregistration = messageBus.RegisterTargetedWithoutTargeting<T>(
775
+ this,
776
+ priority: priority
777
+ );
495
778
  TypedHandler<T> typedHandler = GetOrCreateHandlerForType<T>(messageBus);
496
- return typedHandler.AddTargetedWithoutTargetingHandler(messageHandler, messageBusDeregistration);
779
+ return typedHandler.AddTargetedWithoutTargetingHandler(
780
+ messageHandler,
781
+ messageBusDeregistration,
782
+ priority
783
+ );
497
784
  }
498
785
 
499
786
  /// <summary>
@@ -501,14 +788,27 @@
501
788
  /// </summary>
502
789
  /// <typeparam name="T">Type of Message to be handled.</typeparam>
503
790
  /// <param name="messageHandler">Function that actually handles the message.</param>
791
+ /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
504
792
  /// <param name="messageBus">IMessageBus override to register with, if any. Null/not provided defaults to the GlobalMessageBus.</param>
505
793
  /// <returns>The de-registration action.</returns>
506
- public Action RegisterTargetedWithoutTargeting<T>(FastHandlerWithContext<T> messageHandler, IMessageBus messageBus = null) where T : ITargetedMessage
794
+ public Action RegisterTargetedWithoutTargeting<T>(
795
+ FastHandlerWithContext<T> messageHandler,
796
+ int priority = 0,
797
+ IMessageBus messageBus = null
798
+ )
799
+ where T : ITargetedMessage
507
800
  {
508
801
  messageBus ??= MessageBus;
509
- Action messageBusDeregistration = messageBus.RegisterTargetedWithoutTargeting<T>(this);
802
+ Action messageBusDeregistration = messageBus.RegisterTargetedWithoutTargeting<T>(
803
+ this,
804
+ priority: priority
805
+ );
510
806
  TypedHandler<T> typedHandler = GetOrCreateHandlerForType<T>(messageBus);
511
- return typedHandler.AddTargetedWithoutTargetingHandler(messageHandler, messageBusDeregistration);
807
+ return typedHandler.AddTargetedWithoutTargetingHandler(
808
+ messageHandler,
809
+ messageBusDeregistration,
810
+ priority
811
+ );
512
812
  }
513
813
 
514
814
  /// <summary>
@@ -516,14 +816,27 @@
516
816
  /// </summary>
517
817
  /// <typeparam name="T">Type of Message to be handled.</typeparam>
518
818
  /// <param name="messageHandler">Function that actually handles the message.</param>
819
+ /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
519
820
  /// <param name="messageBus">IMessageBus override to register with, if any. Null/not provided defaults to the GlobalMessageBus.</param>
520
821
  /// <returns>The de-registration action.</returns>
521
- public Action RegisterUntargetedMessageHandler<T>(Action<T> messageHandler, IMessageBus messageBus = null) where T : IUntargetedMessage
822
+ public Action RegisterUntargetedMessageHandler<T>(
823
+ Action<T> messageHandler,
824
+ int priority = 0,
825
+ IMessageBus messageBus = null
826
+ )
827
+ where T : IUntargetedMessage
522
828
  {
523
829
  messageBus ??= MessageBus;
524
- Action messageBusDeregistration = messageBus.RegisterUntargeted<T>(this);
830
+ Action messageBusDeregistration = messageBus.RegisterUntargeted<T>(
831
+ this,
832
+ priority: priority
833
+ );
525
834
  TypedHandler<T> typedHandler = GetOrCreateHandlerForType<T>(messageBus);
526
- return typedHandler.AddUntargetedHandler(messageHandler, messageBusDeregistration);
835
+ return typedHandler.AddUntargetedHandler(
836
+ messageHandler,
837
+ messageBusDeregistration,
838
+ priority
839
+ );
527
840
  }
528
841
 
529
842
  /// <summary>
@@ -531,14 +844,27 @@
531
844
  /// </summary>
532
845
  /// <typeparam name="T">Type of Message to be handled.</typeparam>
533
846
  /// <param name="messageHandler">Function that actually handles the message.</param>
847
+ /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
534
848
  /// <param name="messageBus">IMessageBus override to register with, if any. Null/not provided defaults to the GlobalMessageBus.</param>
535
849
  /// <returns>The de-registration action.</returns>
536
- public Action RegisterUntargetedMessageHandler<T>(FastHandler<T> messageHandler, IMessageBus messageBus = null) where T : IUntargetedMessage
850
+ public Action RegisterUntargetedMessageHandler<T>(
851
+ FastHandler<T> messageHandler,
852
+ int priority = 0,
853
+ IMessageBus messageBus = null
854
+ )
855
+ where T : IUntargetedMessage
537
856
  {
538
857
  messageBus ??= MessageBus;
539
- Action messageBusDeregistration = messageBus.RegisterUntargeted<T>(this);
858
+ Action messageBusDeregistration = messageBus.RegisterUntargeted<T>(
859
+ this,
860
+ priority: priority
861
+ );
540
862
  TypedHandler<T> typedHandler = GetOrCreateHandlerForType<T>(messageBus);
541
- return typedHandler.AddUntargetedHandler(messageHandler, messageBusDeregistration);
863
+ return typedHandler.AddUntargetedHandler(
864
+ messageHandler,
865
+ messageBusDeregistration,
866
+ priority
867
+ );
542
868
  }
543
869
 
544
870
  /// <summary>
@@ -546,14 +872,27 @@
546
872
  /// </summary>
547
873
  /// <typeparam name="T">Type of Message to be handled.</typeparam>
548
874
  /// <param name="messageHandler">Function that actually handles the message.</param>
875
+ /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
549
876
  /// <param name="messageBus">IMessageBus override to register with, if any. Null/not provided defaults to the GlobalMessageBus.</param>
550
877
  /// <returns>The de-registration action.</returns>
551
- public Action RegisterUntargetedPostProcessor<T>(Action<T> messageHandler, IMessageBus messageBus = null) where T : IUntargetedMessage
878
+ public Action RegisterUntargetedPostProcessor<T>(
879
+ Action<T> messageHandler,
880
+ int priority = 0,
881
+ IMessageBus messageBus = null
882
+ )
883
+ where T : IUntargetedMessage
552
884
  {
553
885
  messageBus ??= MessageBus;
554
- Action messageBusDeregistration = messageBus.RegisterUntargetedPostProcessor<T>(this);
886
+ Action messageBusDeregistration = messageBus.RegisterUntargetedPostProcessor<T>(
887
+ priority: priority,
888
+ messageHandler: this
889
+ );
555
890
  TypedHandler<T> typedHandler = GetOrCreateHandlerForType<T>(messageBus);
556
- return typedHandler.AddUntargetedPostProcessor(messageHandler, messageBusDeregistration);
891
+ return typedHandler.AddUntargetedPostProcessor(
892
+ messageHandler,
893
+ messageBusDeregistration,
894
+ priority
895
+ );
557
896
  }
558
897
 
559
898
  /// <summary>
@@ -561,14 +900,27 @@
561
900
  /// </summary>
562
901
  /// <typeparam name="T">Type of Message to be handled.</typeparam>
563
902
  /// <param name="messageHandler">Function that actually handles the message.</param>
903
+ /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
564
904
  /// <param name="messageBus">IMessageBus override to register with, if any. Null/not provided defaults to the GlobalMessageBus.</param>
565
905
  /// <returns>The de-registration action.</returns>
566
- public Action RegisterUntargetedPostProcessor<T>(FastHandler<T> messageHandler, IMessageBus messageBus = null) where T : IUntargetedMessage
906
+ public Action RegisterUntargetedPostProcessor<T>(
907
+ FastHandler<T> messageHandler,
908
+ int priority = 0,
909
+ IMessageBus messageBus = null
910
+ )
911
+ where T : IUntargetedMessage
567
912
  {
568
913
  messageBus ??= MessageBus;
569
- Action messageBusDeregistration = messageBus.RegisterUntargetedPostProcessor<T>(this);
914
+ Action messageBusDeregistration = messageBus.RegisterUntargetedPostProcessor<T>(
915
+ priority: priority,
916
+ messageHandler: this
917
+ );
570
918
  TypedHandler<T> typedHandler = GetOrCreateHandlerForType<T>(messageBus);
571
- return typedHandler.AddUntargetedPostProcessor(messageHandler, messageBusDeregistration);
919
+ return typedHandler.AddUntargetedPostProcessor(
920
+ messageHandler,
921
+ messageBusDeregistration,
922
+ priority
923
+ );
572
924
  }
573
925
 
574
926
  /// <summary>
@@ -577,15 +929,31 @@
577
929
  /// <typeparam name="T">Type of Message to be handled.</typeparam>
578
930
  /// <param name="source">Source Id of BroadcastMessages to listen for.</param>
579
931
  /// <param name="messageHandler">Function that actually handles the message.</param>
932
+ /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
580
933
  /// <param name="messageBus">IMessageBus override to register with, if any. Null/not provided defaults to the GlobalMessageBus.</param>
581
934
  /// <returns>The de-registration action.</returns>
582
- public Action RegisterSourcedBroadcastMessageHandler<T>(InstanceId source, Action<T> messageHandler, IMessageBus messageBus = null)
935
+ public Action RegisterSourcedBroadcastMessageHandler<T>(
936
+ InstanceId source,
937
+ Action<T> messageHandler,
938
+ int priority = 0,
939
+ IMessageBus messageBus = null
940
+ )
583
941
  where T : IBroadcastMessage
584
942
  {
585
943
  messageBus ??= MessageBus;
586
- Action messageBusDeregistration = messageBus.RegisterSourcedBroadcast<T>(source, this);
587
- TypedHandler<T> typedHandler = GetOrCreateHandlerForType<T>(messageBus);;
588
- return typedHandler.AddSourcedBroadcastHandler(source, messageHandler, messageBusDeregistration);
944
+ Action messageBusDeregistration = messageBus.RegisterSourcedBroadcast<T>(
945
+ source,
946
+ this,
947
+ priority: priority
948
+ );
949
+ TypedHandler<T> typedHandler = GetOrCreateHandlerForType<T>(messageBus);
950
+ ;
951
+ return typedHandler.AddSourcedBroadcastHandler(
952
+ source,
953
+ messageHandler,
954
+ messageBusDeregistration,
955
+ priority
956
+ );
589
957
  }
590
958
 
591
959
  /// <summary>
@@ -594,15 +962,30 @@
594
962
  /// <typeparam name="T">Type of Message to be handled.</typeparam>
595
963
  /// <param name="source">Source Id of BroadcastMessages to listen for.</param>
596
964
  /// <param name="messageHandler">Function that actually handles the message.</param>
965
+ /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
597
966
  /// <param name="messageBus">IMessageBus override to register with, if any. Null/not provided defaults to the GlobalMessageBus.</param>
598
967
  /// <returns>The de-registration action.</returns>
599
- public Action RegisterSourcedBroadcastMessageHandler<T>(InstanceId source, FastHandler<T> messageHandler, IMessageBus messageBus = null)
968
+ public Action RegisterSourcedBroadcastMessageHandler<T>(
969
+ InstanceId source,
970
+ FastHandler<T> messageHandler,
971
+ int priority = 0,
972
+ IMessageBus messageBus = null
973
+ )
600
974
  where T : IBroadcastMessage
601
975
  {
602
976
  messageBus ??= MessageBus;
603
- Action messageBusDeregistration = messageBus.RegisterSourcedBroadcast<T>(source, this);
604
- TypedHandler<T> typedHandler = GetOrCreateHandlerForType<T>(messageBus); ;
605
- return typedHandler.AddSourcedBroadcastHandler(source, messageHandler, messageBusDeregistration);
977
+ Action messageBusDeregistration = messageBus.RegisterSourcedBroadcast<T>(
978
+ source,
979
+ this,
980
+ priority: priority
981
+ );
982
+ TypedHandler<T> typedHandler = GetOrCreateHandlerForType<T>(messageBus);
983
+ return typedHandler.AddSourcedBroadcastHandler(
984
+ source,
985
+ messageHandler,
986
+ messageBusDeregistration,
987
+ priority
988
+ );
606
989
  }
607
990
 
608
991
  /// <summary>
@@ -610,14 +993,27 @@
610
993
  /// </summary>
611
994
  /// <typeparam name="T">Type of Message to be handled.</typeparam>
612
995
  /// <param name="messageHandler">Function that actually handles the message.</param>
996
+ /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
613
997
  /// <param name="messageBus">IMessageBus override to register with, if any. Null/not provided defaults to the GlobalMessageBus.</param>
614
998
  /// <returns>The de-registration action.</returns>
615
- public Action RegisterSourcedBroadcastWithoutSource<T>(Action<InstanceId, T> messageHandler, IMessageBus messageBus = null) where T : IBroadcastMessage
999
+ public Action RegisterSourcedBroadcastWithoutSource<T>(
1000
+ Action<InstanceId, T> messageHandler,
1001
+ int priority = 0,
1002
+ IMessageBus messageBus = null
1003
+ )
1004
+ where T : IBroadcastMessage
616
1005
  {
617
1006
  messageBus ??= MessageBus;
618
- Action messageBusDeregistration = messageBus.RegisterSourcedBroadcastWithoutSource<T>(this);
1007
+ Action messageBusDeregistration = messageBus.RegisterSourcedBroadcastWithoutSource<T>(
1008
+ this,
1009
+ priority: priority
1010
+ );
619
1011
  TypedHandler<T> typedHandler = GetOrCreateHandlerForType<T>(messageBus);
620
- return typedHandler.AddSourcedBroadcastWithoutSourceHandler(messageHandler, messageBusDeregistration);
1012
+ return typedHandler.AddSourcedBroadcastWithoutSourceHandler(
1013
+ messageHandler,
1014
+ messageBusDeregistration,
1015
+ priority
1016
+ );
621
1017
  }
622
1018
 
623
1019
  /// <summary>
@@ -625,14 +1021,27 @@
625
1021
  /// </summary>
626
1022
  /// <typeparam name="T">Type of Message to be handled.</typeparam>
627
1023
  /// <param name="messageHandler">Function that actually handles the message.</param>
1024
+ /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
628
1025
  /// <param name="messageBus">IMessageBus override to register with, if any. Null/not provided defaults to the GlobalMessageBus.</param>
629
1026
  /// <returns>The de-registration action.</returns>
630
- public Action RegisterSourcedBroadcastWithoutSource<T>(FastHandlerWithContext<T> messageHandler, IMessageBus messageBus = null) where T : IBroadcastMessage
1027
+ public Action RegisterSourcedBroadcastWithoutSource<T>(
1028
+ FastHandlerWithContext<T> messageHandler,
1029
+ int priority = 0,
1030
+ IMessageBus messageBus = null
1031
+ )
1032
+ where T : IBroadcastMessage
631
1033
  {
632
1034
  messageBus ??= MessageBus;
633
- Action messageBusDeregistration = messageBus.RegisterSourcedBroadcastWithoutSource<T>(this);
1035
+ Action messageBusDeregistration = messageBus.RegisterSourcedBroadcastWithoutSource<T>(
1036
+ this,
1037
+ priority: priority
1038
+ );
634
1039
  TypedHandler<T> typedHandler = GetOrCreateHandlerForType<T>(messageBus);
635
- return typedHandler.AddSourcedBroadcastWithoutSourceHandler(messageHandler, messageBusDeregistration);
1040
+ return typedHandler.AddSourcedBroadcastWithoutSourceHandler(
1041
+ messageHandler,
1042
+ messageBusDeregistration,
1043
+ priority
1044
+ );
636
1045
  }
637
1046
 
638
1047
  /// <summary>
@@ -641,14 +1050,30 @@
641
1050
  /// <typeparam name="T">Type of Message to be handled.</typeparam>
642
1051
  /// <param name="source">Source object to listen for BroadcastMessages on.</param>
643
1052
  /// <param name="messageHandler">Function that actually handles the message.</param>
1053
+ /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
644
1054
  /// <param name="messageBus">IMessageBus override to register with, if any. Null/not provided defaults to the GlobalMessageBus.</param>
645
1055
  /// <returns>The de-registration action.</returns>
646
- public Action RegisterSourcedBroadcastPostProcessor<T>(InstanceId source, Action<T> messageHandler, IMessageBus messageBus = null) where T : IBroadcastMessage
1056
+ public Action RegisterSourcedBroadcastPostProcessor<T>(
1057
+ InstanceId source,
1058
+ Action<T> messageHandler,
1059
+ int priority = 0,
1060
+ IMessageBus messageBus = null
1061
+ )
1062
+ where T : IBroadcastMessage
647
1063
  {
648
1064
  messageBus ??= MessageBus;
649
- Action messageBusDeregistration = messageBus.RegisterBroadcastPostProcessor<T>(source, this);
1065
+ Action messageBusDeregistration = messageBus.RegisterBroadcastPostProcessor<T>(
1066
+ source,
1067
+ messageHandler: this,
1068
+ priority: priority
1069
+ );
650
1070
  TypedHandler<T> typedHandler = GetOrCreateHandlerForType<T>(messageBus);
651
- return typedHandler.AddBroadcastPostProcessor(source, messageHandler, messageBusDeregistration);
1071
+ return typedHandler.AddBroadcastPostProcessor(
1072
+ source,
1073
+ messageHandler,
1074
+ messageBusDeregistration,
1075
+ priority
1076
+ );
652
1077
  }
653
1078
 
654
1079
  /// <summary>
@@ -657,14 +1082,30 @@
657
1082
  /// <typeparam name="T">Type of Message to be handled.</typeparam>
658
1083
  /// <param name="source">Source object to listen for BroadcastMessages on.</param>
659
1084
  /// <param name="messageHandler">Function that actually handles the message.</param>
1085
+ /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
660
1086
  /// <param name="messageBus">IMessageBus override to register with, if any. Null/not provided defaults to the GlobalMessageBus.</param>
661
1087
  /// <returns>The de-registration action.</returns>
662
- public Action RegisterSourcedBroadcastPostProcessor<T>(InstanceId source, FastHandler<T> messageHandler, IMessageBus messageBus = null) where T : IBroadcastMessage
1088
+ public Action RegisterSourcedBroadcastPostProcessor<T>(
1089
+ InstanceId source,
1090
+ FastHandler<T> messageHandler,
1091
+ int priority = 0,
1092
+ IMessageBus messageBus = null
1093
+ )
1094
+ where T : IBroadcastMessage
663
1095
  {
664
1096
  messageBus ??= MessageBus;
665
- Action messageBusDeregistration = messageBus.RegisterBroadcastPostProcessor<T>(source, this);
1097
+ Action messageBusDeregistration = messageBus.RegisterBroadcastPostProcessor<T>(
1098
+ source,
1099
+ priority: priority,
1100
+ messageHandler: this
1101
+ );
666
1102
  TypedHandler<T> typedHandler = GetOrCreateHandlerForType<T>(messageBus);
667
- return typedHandler.AddBroadcastPostProcessor(source, messageHandler, messageBusDeregistration);
1103
+ return typedHandler.AddBroadcastPostProcessor(
1104
+ source,
1105
+ messageHandler,
1106
+ messageBusDeregistration,
1107
+ priority
1108
+ );
668
1109
  }
669
1110
 
670
1111
  /// <summary>
@@ -672,14 +1113,28 @@
672
1113
  /// </summary>
673
1114
  /// <typeparam name="T">Type of Message to be handled.</typeparam>
674
1115
  /// <param name="messageHandler">Function that actually handles the message.</param>
1116
+ /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
675
1117
  /// <param name="messageBus">IMessageBus override to register with, if any. Null/not provided defaults to the GlobalMessageBus.</param>
676
1118
  /// <returns>The de-registration action.</returns>
677
- public Action RegisterSourcedBroadcastWithoutSourcePostProcessor<T>(Action<InstanceId, T> messageHandler, IMessageBus messageBus = null) where T : IBroadcastMessage
1119
+ public Action RegisterSourcedBroadcastWithoutSourcePostProcessor<T>(
1120
+ Action<InstanceId, T> messageHandler,
1121
+ int priority = 0,
1122
+ IMessageBus messageBus = null
1123
+ )
1124
+ where T : IBroadcastMessage
678
1125
  {
679
1126
  messageBus ??= MessageBus;
680
- Action messageBusDeregistration = messageBus.RegisterBroadcastWithoutSourcePostProcessor<T>(this);
1127
+ Action messageBusDeregistration =
1128
+ messageBus.RegisterBroadcastWithoutSourcePostProcessor<T>(
1129
+ priority: priority,
1130
+ messageHandler: this
1131
+ );
681
1132
  TypedHandler<T> typedHandler = GetOrCreateHandlerForType<T>(messageBus);
682
- return typedHandler.AddBroadcastWithoutSourcePostProcessor(messageHandler, messageBusDeregistration);
1133
+ return typedHandler.AddBroadcastWithoutSourcePostProcessor(
1134
+ messageHandler,
1135
+ messageBusDeregistration,
1136
+ priority
1137
+ );
683
1138
  }
684
1139
 
685
1140
  /// <summary>
@@ -687,14 +1142,28 @@
687
1142
  /// </summary>
688
1143
  /// <typeparam name="T">Type of Message to be handled.</typeparam>
689
1144
  /// <param name="messageHandler">Function that actually handles the message.</param>
1145
+ /// <param name="priority">Priority at which to run the handler, lower runs earlier than higher.</param>
690
1146
  /// <param name="messageBus">IMessageBus override to register with, if any. Null/not provided defaults to the GlobalMessageBus.</param>
691
1147
  /// <returns>The de-registration action.</returns>
692
- public Action RegisterSourcedBroadcastWithoutSourcePostProcessor<T>(FastHandlerWithContext<T> messageHandler, IMessageBus messageBus = null) where T : IBroadcastMessage
1148
+ public Action RegisterSourcedBroadcastWithoutSourcePostProcessor<T>(
1149
+ FastHandlerWithContext<T> messageHandler,
1150
+ int priority = 0,
1151
+ IMessageBus messageBus = null
1152
+ )
1153
+ where T : IBroadcastMessage
693
1154
  {
694
1155
  messageBus ??= MessageBus;
695
- Action messageBusDeregistration = messageBus.RegisterBroadcastWithoutSourcePostProcessor<T>(this);
1156
+ Action messageBusDeregistration =
1157
+ messageBus.RegisterBroadcastWithoutSourcePostProcessor<T>(
1158
+ priority: priority,
1159
+ messageHandler: this
1160
+ );
696
1161
  TypedHandler<T> typedHandler = GetOrCreateHandlerForType<T>(messageBus);
697
- return typedHandler.AddBroadcastWithoutSourcePostProcessor(messageHandler, messageBusDeregistration);
1162
+ return typedHandler.AddBroadcastWithoutSourcePostProcessor(
1163
+ messageHandler,
1164
+ messageBusDeregistration,
1165
+ priority
1166
+ );
698
1167
  }
699
1168
 
700
1169
  /// <summary>
@@ -705,7 +1174,12 @@
705
1174
  /// <param name="priority">Priority to register the interceptor at (interceptors are ran from low -> high priority)</param>
706
1175
  /// <param name="messageBus">Message bus to register the interceptor on.</param>
707
1176
  /// <returns>The de-registration action.</returns>
708
- public Action RegisterUntargetedInterceptor<T>(IMessageBus.UntargetedInterceptor<T> interceptor, int priority = 0, IMessageBus messageBus = null) where T : IUntargetedMessage
1177
+ public Action RegisterUntargetedInterceptor<T>(
1178
+ IMessageBus.UntargetedInterceptor<T> interceptor,
1179
+ int priority = 0,
1180
+ IMessageBus messageBus = null
1181
+ )
1182
+ where T : IUntargetedMessage
709
1183
  {
710
1184
  return (messageBus ?? MessageBus).RegisterUntargetedInterceptor(interceptor, priority);
711
1185
  }
@@ -718,7 +1192,12 @@
718
1192
  /// <param name="priority">Priority to register the interceptor at (interceptors are ran from low -> high priority)</param>
719
1193
  /// <param name="messageBus">Message bus to register the interceptor on.</param>
720
1194
  /// <returns>The de-registration action.</returns>
721
- public Action RegisterBroadcastInterceptor<T>(IMessageBus.BroadcastInterceptor<T> interceptor, int priority = 0, IMessageBus messageBus = null) where T : IBroadcastMessage
1195
+ public Action RegisterBroadcastInterceptor<T>(
1196
+ IMessageBus.BroadcastInterceptor<T> interceptor,
1197
+ int priority = 0,
1198
+ IMessageBus messageBus = null
1199
+ )
1200
+ where T : IBroadcastMessage
722
1201
  {
723
1202
  return (messageBus ?? MessageBus).RegisterBroadcastInterceptor(interceptor, priority);
724
1203
  }
@@ -731,7 +1210,12 @@
731
1210
  /// <param name="priority">Priority to register the interceptor at (interceptors are ran from low -> high priority)</param>
732
1211
  /// <param name="messageBus">Message bus to register the interceptor on.</param>
733
1212
  /// <returns>The de-registration action.</returns>
734
- public Action RegisterTargetedInterceptor<T>(IMessageBus.TargetedInterceptor<T> interceptor, int priority = 0, IMessageBus messageBus = null) where T : ITargetedMessage
1213
+ public Action RegisterTargetedInterceptor<T>(
1214
+ IMessageBus.TargetedInterceptor<T> interceptor,
1215
+ int priority = 0,
1216
+ IMessageBus messageBus = null
1217
+ )
1218
+ where T : ITargetedMessage
735
1219
  {
736
1220
  return (messageBus ?? MessageBus).RegisterTargetedInterceptor(interceptor, priority);
737
1221
  }
@@ -741,12 +1225,14 @@
741
1225
  return new
742
1226
  {
743
1227
  OwnerId = owner,
744
- HandlerTypes = string.Join(",",
745
- _handlersByTypeByMessageBus.Values
746
- .SelectMany(handlers => handlers.Keys)
1228
+ HandlerTypes = string.Join(
1229
+ ",",
1230
+ _handlersByTypeByMessageBus
1231
+ .Values.SelectMany(handlers => handlers.Keys)
747
1232
  .Distinct()
748
1233
  .Select(type => type.Name)
749
- .OrderBy(_ => _))
1234
+ .OrderBy(_ => _)
1235
+ ),
750
1236
  }.ToString();
751
1237
  }
752
1238
 
@@ -755,11 +1241,17 @@
755
1241
  /// </summary>
756
1242
  /// <typeparam name="T">Type of Message to retrieve a Handler for.</typeparam>
757
1243
  /// <returns>Non-Null Handler for the specific type.</returns>
758
- private TypedHandler<T> GetOrCreateHandlerForType<T>(IMessageBus messageBus) where T : IMessage
1244
+ private TypedHandler<T> GetOrCreateHandlerForType<T>(IMessageBus messageBus)
1245
+ where T : IMessage
759
1246
  {
760
1247
  Type type = typeof(T);
761
1248
 
762
- if (!_handlersByTypeByMessageBus.TryGetValue(messageBus, out Dictionary<Type, object> handlersByType))
1249
+ if (
1250
+ !_handlersByTypeByMessageBus.TryGetValue(
1251
+ messageBus,
1252
+ out Dictionary<Type, object> handlersByType
1253
+ )
1254
+ )
763
1255
  {
764
1256
  handlersByType = new Dictionary<Type, object>();
765
1257
  _handlersByTypeByMessageBus[messageBus] = handlersByType;
@@ -767,7 +1259,7 @@
767
1259
 
768
1260
  if (handlersByType.TryGetValue(type, out object existingTypedHandler))
769
1261
  {
770
- return (TypedHandler<T>) existingTypedHandler;
1262
+ return (TypedHandler<T>)existingTypedHandler;
771
1263
  }
772
1264
 
773
1265
  TypedHandler<T> newTypedHandler = new();
@@ -782,11 +1274,21 @@
782
1274
  /// <param name="messageBus">The specific MessageBus to use.</param>
783
1275
  /// <param name="existingTypedHandler">Existing typed message handler, if one exists.</param>
784
1276
  /// <returns>Existing handler for the specific type, or null if none exists..</returns>
785
- private bool GetHandlerForType<T>(Type type, IMessageBus messageBus, out TypedHandler<T> existingTypedHandler) where T : IMessage
1277
+ private bool GetHandlerForType<T>(
1278
+ Type type,
1279
+ IMessageBus messageBus,
1280
+ out TypedHandler<T> existingTypedHandler
1281
+ )
1282
+ where T : IMessage
786
1283
  {
787
- if (_handlersByTypeByMessageBus.TryGetValue(messageBus, out Dictionary<Type, object> handlersByType) && handlersByType.TryGetValue(type, out object untypedHandler))
788
- {
789
- existingTypedHandler = (TypedHandler<T>) untypedHandler;
1284
+ if (
1285
+ _handlersByTypeByMessageBus.TryGetValue(
1286
+ messageBus,
1287
+ out Dictionary<Type, object> handlersByType
1288
+ ) && handlersByType.TryGetValue(type, out object untypedHandler)
1289
+ )
1290
+ {
1291
+ existingTypedHandler = (TypedHandler<T>)untypedHandler;
790
1292
  return true;
791
1293
  }
792
1294
 
@@ -796,19 +1298,32 @@
796
1298
 
797
1299
  private abstract class TypedHandler
798
1300
  {
799
- protected static readonly Stack<List<Action<IUntargetedMessage>>> GlobalUntargetedHandlersStack = new();
800
- protected static readonly Stack<List<Action<InstanceId, ITargetedMessage>>> GlobalTargetedHandlersStack = new();
801
- protected static readonly Stack<List<Action<InstanceId, IBroadcastMessage>>> GlobalBroadcastHandlersStack = new();
802
- protected static readonly Stack<List<FastHandler<IUntargetedMessage>>> GlobalUntargetedFastHandlersStack = new();
803
- protected static readonly Stack<List<FastHandlerWithContext<ITargetedMessage>>> GlobalTargetedFastHandlersStack = new();
804
- protected static readonly Stack<List<FastHandlerWithContext<IBroadcastMessage>>> GlobalBroadcastFastHandlersStack = new();
1301
+ protected static readonly Stack<
1302
+ List<Action<IUntargetedMessage>>
1303
+ > GlobalUntargetedHandlersStack = new();
1304
+ protected static readonly Stack<
1305
+ List<Action<InstanceId, ITargetedMessage>>
1306
+ > GlobalTargetedHandlersStack = new();
1307
+ protected static readonly Stack<
1308
+ List<Action<InstanceId, IBroadcastMessage>>
1309
+ > GlobalBroadcastHandlersStack = new();
1310
+ protected static readonly Stack<
1311
+ List<FastHandler<IUntargetedMessage>>
1312
+ > GlobalUntargetedFastHandlersStack = new();
1313
+ protected static Stack<
1314
+ List<FastHandlerWithContext<ITargetedMessage>>
1315
+ > GlobalTargetedFastHandlersStack = new();
1316
+ protected static Stack<
1317
+ List<FastHandlerWithContext<IBroadcastMessage>>
1318
+ > GlobalBroadcastFastHandlersStack = new();
805
1319
  }
806
1320
 
807
1321
  /// <summary>
808
1322
  /// One-size-fits-all wrapper around all possible Messaging sinks for a particular MessageHandler & MessageType.
809
1323
  /// </summary>
810
1324
  /// <typeparam name="T">Message type that this Handler exists to serve.</typeparam>
811
- private sealed class TypedHandler<T> : TypedHandler where T: IMessage
1325
+ private sealed class TypedHandler<T> : TypedHandler
1326
+ where T : IMessage
812
1327
  {
813
1328
  // Buffers so we don't allocate memory as often
814
1329
  private static Stack<List<Action<T>>> HandlersStack;
@@ -816,41 +1331,99 @@
816
1331
  private static Stack<List<FastHandler<T>>> FastHandlersStack;
817
1332
  private static Stack<List<FastHandlerWithContext<T>>> FastHandlersWithContextStack;
818
1333
 
819
- private Dictionary<InstanceId, Dictionary<Action<T>, int>> _targetedHandlers;
820
- private Dictionary<Action<T>, int> _untargetedHandlers;
821
- private Dictionary<InstanceId, Dictionary<Action<T>, int>> _broadcastHandlers;
822
- private Dictionary<InstanceId, Dictionary<Action<T>, int>> _targetedPostProcessingHandlers;
823
- private Dictionary<Action<T>, int> _untargetedPostProcessingHandlers;
824
- private Dictionary<InstanceId, Dictionary<Action<T>, int>> _broadcastPostProcessingHandlers;
825
- private Dictionary<InstanceId, Dictionary<FastHandler<T>, int>> _targetedFastHandlers;
826
- private Dictionary<FastHandler<T>, int> _untargetedFastHandlers;
827
- private Dictionary<InstanceId, Dictionary<FastHandler<T>, int>> _broadcastFastHandlers;
828
- private Dictionary<InstanceId, Dictionary<FastHandler<T>, int>> _targetedPostProcessingFastHandlers;
829
- private Dictionary<FastHandler<T>, int> _untargetedPostProcessingFastHandlers;
830
- private Dictionary<InstanceId, Dictionary<FastHandler<T>, int>> _broadcastPostProcessingFastHandlers;
1334
+ private Dictionary<
1335
+ InstanceId,
1336
+ Dictionary<int, Dictionary<Action<T>, int>>
1337
+ > _targetedHandlers;
1338
+ private Dictionary<int, Dictionary<Action<T>, int>> _untargetedHandlers;
1339
+ private Dictionary<
1340
+ InstanceId,
1341
+ Dictionary<int, Dictionary<Action<T>, int>>
1342
+ > _broadcastHandlers;
1343
+ private Dictionary<
1344
+ InstanceId,
1345
+ Dictionary<int, Dictionary<Action<T>, int>>
1346
+ > _targetedPostProcessingHandlers;
1347
+ private Dictionary<int, Dictionary<Action<T>, int>> _untargetedPostProcessingHandlers;
1348
+ private Dictionary<
1349
+ InstanceId,
1350
+ Dictionary<int, Dictionary<Action<T>, int>>
1351
+ > _broadcastPostProcessingHandlers;
1352
+ private Dictionary<
1353
+ InstanceId,
1354
+ Dictionary<int, Dictionary<FastHandler<T>, int>>
1355
+ > _targetedFastHandlers;
1356
+ private Dictionary<int, Dictionary<FastHandler<T>, int>> _untargetedFastHandlers;
1357
+ private Dictionary<
1358
+ InstanceId,
1359
+ Dictionary<int, Dictionary<FastHandler<T>, int>>
1360
+ > _broadcastFastHandlers;
1361
+ private Dictionary<
1362
+ InstanceId,
1363
+ Dictionary<int, Dictionary<FastHandler<T>, int>>
1364
+ > _targetedPostProcessingFastHandlers;
1365
+ private Dictionary<
1366
+ int,
1367
+ Dictionary<FastHandler<T>, int>
1368
+ > _untargetedPostProcessingFastHandlers;
1369
+ private Dictionary<
1370
+ InstanceId,
1371
+ Dictionary<int, Dictionary<FastHandler<T>, int>>
1372
+ > _broadcastPostProcessingFastHandlers;
831
1373
  private Dictionary<Action<IUntargetedMessage>, int> _globalUntargetedHandlers;
832
1374
  private Dictionary<Action<InstanceId, ITargetedMessage>, int> _globalTargetedHandlers;
833
1375
  private Dictionary<Action<InstanceId, IBroadcastMessage>, int> _globalBroadcastHandlers;
834
1376
  private Dictionary<FastHandler<IUntargetedMessage>, int> _globalUntargetedFastHandlers;
835
- private Dictionary<FastHandlerWithContext<ITargetedMessage>, int> _globalTargetedFastHandlers;
836
- private Dictionary<FastHandlerWithContext<IBroadcastMessage>, int> _globalBroadcastFastHandlers;
837
- private Dictionary<Action<InstanceId, T>, int> _targetedWithoutTargetingHandlers;
838
- private Dictionary<FastHandlerWithContext<T>, int> _fastTargetedWithoutTargetingHandlers;
839
- private Dictionary<Action<InstanceId, T>, int> _broadcastWithoutSourceHandlers;
840
- private Dictionary<FastHandlerWithContext<T>, int> _fastBroadcastWithoutSourceHandlers;
841
- private Dictionary<Action<InstanceId, T>, int> _targetedWithoutTargetingPostProcessingHandlers;
842
- private Dictionary<FastHandlerWithContext<T>, int> _fastTargetedWithoutTargetingPostProcessingHandlers;
843
- private Dictionary<Action<InstanceId, T>, int> _broadcastWithoutSourcePostProcessingHandlers;
844
- private Dictionary<FastHandlerWithContext<T>, int> _fastBroadcastWithoutSourcePostProcessingHandlers;
1377
+ private Dictionary<
1378
+ FastHandlerWithContext<ITargetedMessage>,
1379
+ int
1380
+ > _globalTargetedFastHandlers;
1381
+ private Dictionary<
1382
+ FastHandlerWithContext<IBroadcastMessage>,
1383
+ int
1384
+ > _globalBroadcastFastHandlers;
1385
+ private Dictionary<
1386
+ int,
1387
+ Dictionary<Action<InstanceId, T>, int>
1388
+ > _targetedWithoutTargetingHandlers;
1389
+ private Dictionary<
1390
+ int,
1391
+ Dictionary<FastHandlerWithContext<T>, int>
1392
+ > _fastTargetedWithoutTargetingHandlers;
1393
+ private Dictionary<
1394
+ int,
1395
+ Dictionary<Action<InstanceId, T>, int>
1396
+ > _broadcastWithoutSourceHandlers;
1397
+ private Dictionary<
1398
+ int,
1399
+ Dictionary<FastHandlerWithContext<T>, int>
1400
+ > _fastBroadcastWithoutSourceHandlers;
1401
+ private Dictionary<
1402
+ int,
1403
+ Dictionary<Action<InstanceId, T>, int>
1404
+ > _targetedWithoutTargetingPostProcessingHandlers;
1405
+ private Dictionary<
1406
+ int,
1407
+ Dictionary<FastHandlerWithContext<T>, int>
1408
+ > _fastTargetedWithoutTargetingPostProcessingHandlers;
1409
+ private Dictionary<
1410
+ int,
1411
+ Dictionary<Action<InstanceId, T>, int>
1412
+ > _broadcastWithoutSourcePostProcessingHandlers;
1413
+ private Dictionary<
1414
+ int,
1415
+ Dictionary<FastHandlerWithContext<T>, int>
1416
+ > _fastBroadcastWithoutSourcePostProcessingHandlers;
845
1417
 
846
1418
  /// <summary>
847
1419
  /// Emits the UntargetedMessage to all subscribed listeners.
848
1420
  /// </summary>
849
1421
  /// <param name="message">Message to emit.</param>
850
- public void HandleUntargeted(ref T message)
1422
+ /// <param name="priority">Priority at which to run the handlers.</param>
1423
+ public void HandleUntargeted(ref T message, int priority)
851
1424
  {
852
- RunFastHandlers(_untargetedFastHandlers, ref message);
853
- RunHandlers(_untargetedHandlers, ref message);
1425
+ RunFastHandlers(_untargetedFastHandlers, ref message, priority);
1426
+ RunHandlers(_untargetedHandlers, ref message, priority);
854
1427
  }
855
1428
 
856
1429
  /// <summary>
@@ -858,10 +1431,16 @@
858
1431
  /// </summary>
859
1432
  /// <param name="target">Target the message is for.</param>
860
1433
  /// <param name="message">Message to emit.</param>
861
- public void HandleTargeted(ref InstanceId target, ref T message)
1434
+ /// <param name="priority">Priority at which to run the handlers.</param>
1435
+ public void HandleTargeted(ref InstanceId target, ref T message, int priority)
862
1436
  {
863
- RunFastHandlersWithContext(ref target, _targetedFastHandlers, ref message);
864
- RunHandlersWithContext(ref target, _targetedHandlers, ref message);
1437
+ RunFastHandlersWithContext(
1438
+ ref target,
1439
+ _targetedFastHandlers,
1440
+ ref message,
1441
+ priority
1442
+ );
1443
+ RunHandlersWithContext(ref target, _targetedHandlers, ref message, priority);
865
1444
  }
866
1445
 
867
1446
  /// <summary>
@@ -869,10 +1448,21 @@
869
1448
  /// </summary>
870
1449
  /// <param name="target">Target the message is for.</param>
871
1450
  /// <param name="message">Message to emit.</param>
872
- public void HandleTargetedWithoutTargeting(ref InstanceId target, ref T message)
873
- {
874
- RunFastHandlers(ref target, ref FastHandlersWithContextStack, _fastTargetedWithoutTargetingHandlers, ref message);
875
- RunHandlers(ref target, _targetedWithoutTargetingHandlers, ref message);
1451
+ /// <param name="priority">Priority at which to run the handlers.</param>
1452
+ public void HandleTargetedWithoutTargeting(
1453
+ ref InstanceId target,
1454
+ ref T message,
1455
+ int priority
1456
+ )
1457
+ {
1458
+ RunFastHandlers(
1459
+ ref target,
1460
+ ref FastHandlersWithContextStack,
1461
+ _fastTargetedWithoutTargetingHandlers,
1462
+ ref message,
1463
+ priority
1464
+ );
1465
+ RunHandlers(ref target, _targetedWithoutTargetingHandlers, ref message, priority);
876
1466
  }
877
1467
 
878
1468
  /// <summary>
@@ -880,10 +1470,16 @@
880
1470
  /// </summary>
881
1471
  /// <param name="source">Source the message is from.</param>
882
1472
  /// <param name="message">Message to emit.</param>
883
- public void HandleSourcedBroadcast(ref InstanceId source, ref T message)
1473
+ /// <param name="priority">Priority at which to run the handlers.</param>
1474
+ public void HandleSourcedBroadcast(ref InstanceId source, ref T message, int priority)
884
1475
  {
885
- RunFastHandlersWithContext(ref source, _broadcastFastHandlers, ref message);
886
- RunHandlersWithContext(ref source, _broadcastHandlers, ref message);
1476
+ RunFastHandlersWithContext(
1477
+ ref source,
1478
+ _broadcastFastHandlers,
1479
+ ref message,
1480
+ priority
1481
+ );
1482
+ RunHandlersWithContext(ref source, _broadcastHandlers, ref message, priority);
887
1483
  }
888
1484
 
889
1485
  /// <summary>
@@ -891,10 +1487,21 @@
891
1487
  /// </summary>
892
1488
  /// <param name="source">Source the message is from.</param>
893
1489
  /// <param name="message">Message to emit.</param>
894
- public void HandleSourcedBroadcastWithoutSource(ref InstanceId source, ref T message)
895
- {
896
- RunFastHandlers(ref source, ref FastHandlersWithContextStack, _fastBroadcastWithoutSourceHandlers, ref message);
897
- RunHandlers(ref source, _broadcastWithoutSourceHandlers, ref message);
1490
+ /// <param name="priority">Priority at which to run the handlers.</param>
1491
+ public void HandleSourcedBroadcastWithoutSource(
1492
+ ref InstanceId source,
1493
+ ref T message,
1494
+ int priority
1495
+ )
1496
+ {
1497
+ RunFastHandlers(
1498
+ ref source,
1499
+ ref FastHandlersWithContextStack,
1500
+ _fastBroadcastWithoutSourceHandlers,
1501
+ ref message,
1502
+ priority
1503
+ );
1504
+ RunHandlers(ref source, _broadcastWithoutSourceHandlers, ref message, priority);
898
1505
  }
899
1506
 
900
1507
  /// <summary>
@@ -903,14 +1510,22 @@
903
1510
  /// <param name="message">Message to emit.</param>
904
1511
  public void HandleGlobalUntargeted(ref IUntargetedMessage message)
905
1512
  {
906
- RunFastHandlers(GlobalUntargetedFastHandlersStack, _globalUntargetedFastHandlers, ref message);
1513
+ RunFastHandlers(
1514
+ GlobalUntargetedFastHandlersStack,
1515
+ _globalUntargetedFastHandlers,
1516
+ ref message
1517
+ );
907
1518
 
908
1519
  if (_globalUntargetedHandlers is not { Count: > 0 })
909
1520
  {
910
1521
  return;
911
1522
  }
912
1523
 
913
- if (GlobalUntargetedHandlersStack.TryPop(out List<Action<IUntargetedMessage>> handlers))
1524
+ if (
1525
+ GlobalUntargetedHandlersStack.TryPop(
1526
+ out List<Action<IUntargetedMessage>> handlers
1527
+ )
1528
+ )
914
1529
  {
915
1530
  handlers.Clear();
916
1531
  handlers.AddRange(_globalUntargetedHandlers.Keys);
@@ -940,21 +1555,32 @@
940
1555
  /// <param name="message">Message to emit.</param>
941
1556
  public void HandleGlobalTargeted(ref InstanceId target, ref ITargetedMessage message)
942
1557
  {
943
- RunFastHandlers(ref target, GlobalTargetedFastHandlersStack, _globalTargetedFastHandlers, ref message);
1558
+ RunFastHandlers(
1559
+ ref target,
1560
+ ref GlobalTargetedFastHandlersStack,
1561
+ _globalTargetedFastHandlers,
1562
+ ref message
1563
+ );
944
1564
 
945
1565
  if (_globalTargetedHandlers is not { Count: > 0 })
946
1566
  {
947
1567
  return;
948
1568
  }
949
1569
 
950
- if (GlobalTargetedHandlersStack.TryPop(out List<Action<InstanceId, ITargetedMessage>> handlers))
1570
+ if (
1571
+ GlobalTargetedHandlersStack.TryPop(
1572
+ out List<Action<InstanceId, ITargetedMessage>> handlers
1573
+ )
1574
+ )
951
1575
  {
952
1576
  handlers.Clear();
953
1577
  handlers.AddRange(_globalTargetedHandlers.Keys);
954
1578
  }
955
1579
  else
956
1580
  {
957
- handlers = new List<Action<InstanceId, ITargetedMessage>>(_globalTargetedHandlers.Keys);
1581
+ handlers = new List<Action<InstanceId, ITargetedMessage>>(
1582
+ _globalTargetedHandlers.Keys
1583
+ );
958
1584
  }
959
1585
 
960
1586
  try
@@ -977,21 +1603,32 @@
977
1603
  /// <param name="message">Message to emit.</param>
978
1604
  public void HandleGlobalBroadcast(ref InstanceId source, ref IBroadcastMessage message)
979
1605
  {
980
- RunFastHandlers(ref source, GlobalBroadcastFastHandlersStack, _globalBroadcastFastHandlers, ref message);
1606
+ RunFastHandlers(
1607
+ ref source,
1608
+ ref GlobalBroadcastFastHandlersStack,
1609
+ _globalBroadcastFastHandlers,
1610
+ ref message
1611
+ );
981
1612
 
982
1613
  if (_globalBroadcastHandlers is not { Count: > 0 })
983
1614
  {
984
1615
  return;
985
1616
  }
986
1617
 
987
- if (GlobalBroadcastHandlersStack.TryPop(out List<Action<InstanceId, IBroadcastMessage>> handlers))
1618
+ if (
1619
+ GlobalBroadcastHandlersStack.TryPop(
1620
+ out List<Action<InstanceId, IBroadcastMessage>> handlers
1621
+ )
1622
+ )
988
1623
  {
989
1624
  handlers.Clear();
990
1625
  handlers.AddRange(_globalBroadcastHandlers.Keys);
991
1626
  }
992
1627
  else
993
1628
  {
994
- handlers = new List<Action<InstanceId, IBroadcastMessage>>(_globalBroadcastHandlers.Keys);
1629
+ handlers = new List<Action<InstanceId, IBroadcastMessage>>(
1630
+ _globalBroadcastHandlers.Keys
1631
+ );
995
1632
  }
996
1633
 
997
1634
  try
@@ -1007,34 +1644,90 @@
1007
1644
  }
1008
1645
  }
1009
1646
 
1010
- public void HandleUntargetedPostProcessing(ref T message)
1011
- {
1012
- RunFastHandlers(_untargetedPostProcessingFastHandlers, ref message);
1013
- RunHandlers(_untargetedPostProcessingHandlers, ref message);
1014
- }
1015
-
1016
- public void HandleTargetedPostProcessing(ref InstanceId target, ref T message)
1017
- {
1018
- RunFastHandlersWithContext(ref target, _targetedPostProcessingFastHandlers, ref message);
1019
- RunHandlersWithContext(ref target, _targetedPostProcessingHandlers, ref message);
1020
- }
1021
-
1022
- public void HandleTargetedWithoutTargetingPostProcessing(ref InstanceId target, ref T message)
1023
- {
1024
- RunFastHandlersWithContext(ref target, _fastTargetedWithoutTargetingPostProcessingHandlers, ref message);
1025
- RunHandlers(ref target, _targetedWithoutTargetingPostProcessingHandlers, ref message);
1026
- }
1027
-
1028
- public void HandleSourcedBroadcastPostProcessing(ref InstanceId source, ref T message)
1029
- {
1030
- RunFastHandlersWithContext(ref source, _broadcastPostProcessingFastHandlers, ref message);
1031
- RunHandlersWithContext(ref source, _broadcastPostProcessingHandlers, ref message);
1032
- }
1033
-
1034
- public void HandleBroadcastWithoutSourcePostProcessing(ref InstanceId source, ref T message)
1035
- {
1036
- RunFastHandlersWithContext(ref source, _fastBroadcastWithoutSourcePostProcessingHandlers, ref message);
1037
- RunHandlers(ref source, _broadcastWithoutSourcePostProcessingHandlers, ref message);
1647
+ public void HandleUntargetedPostProcessing(ref T message, int priority)
1648
+ {
1649
+ RunFastHandlers(_untargetedPostProcessingFastHandlers, ref message, priority);
1650
+ RunHandlers(_untargetedPostProcessingHandlers, ref message, priority);
1651
+ }
1652
+
1653
+ public void HandleTargetedPostProcessing(
1654
+ ref InstanceId target,
1655
+ ref T message,
1656
+ int priority
1657
+ )
1658
+ {
1659
+ RunFastHandlersWithContext(
1660
+ ref target,
1661
+ _targetedPostProcessingFastHandlers,
1662
+ ref message,
1663
+ priority
1664
+ );
1665
+ RunHandlersWithContext(
1666
+ ref target,
1667
+ _targetedPostProcessingHandlers,
1668
+ ref message,
1669
+ priority
1670
+ );
1671
+ }
1672
+
1673
+ public void HandleTargetedWithoutTargetingPostProcessing(
1674
+ ref InstanceId target,
1675
+ ref T message,
1676
+ int priority
1677
+ )
1678
+ {
1679
+ RunFastHandlersWithContext(
1680
+ ref target,
1681
+ _fastTargetedWithoutTargetingPostProcessingHandlers,
1682
+ ref message,
1683
+ priority
1684
+ );
1685
+ RunHandlers(
1686
+ ref target,
1687
+ _targetedWithoutTargetingPostProcessingHandlers,
1688
+ ref message,
1689
+ priority
1690
+ );
1691
+ }
1692
+
1693
+ public void HandleSourcedBroadcastPostProcessing(
1694
+ ref InstanceId source,
1695
+ ref T message,
1696
+ int priority
1697
+ )
1698
+ {
1699
+ RunFastHandlersWithContext(
1700
+ ref source,
1701
+ _broadcastPostProcessingFastHandlers,
1702
+ ref message,
1703
+ priority
1704
+ );
1705
+ RunHandlersWithContext(
1706
+ ref source,
1707
+ _broadcastPostProcessingHandlers,
1708
+ ref message,
1709
+ priority
1710
+ );
1711
+ }
1712
+
1713
+ public void HandleBroadcastWithoutSourcePostProcessing(
1714
+ ref InstanceId source,
1715
+ ref T message,
1716
+ int priority
1717
+ )
1718
+ {
1719
+ RunFastHandlersWithContext(
1720
+ ref source,
1721
+ _fastBroadcastWithoutSourcePostProcessingHandlers,
1722
+ ref message,
1723
+ priority
1724
+ );
1725
+ RunHandlers(
1726
+ ref source,
1727
+ _broadcastWithoutSourcePostProcessingHandlers,
1728
+ ref message,
1729
+ priority
1730
+ );
1038
1731
  }
1039
1732
 
1040
1733
  /// <summary>
@@ -1043,10 +1736,16 @@
1043
1736
  /// <param name="target">Target the handler is for.</param>
1044
1737
  /// <param name="handler">Relevant MessageHandler.</param>
1045
1738
  /// <param name="deregistration">Deregistration action for the handler.</param>
1739
+ /// <param name="priority">Priority at which to add the handler.</param>
1046
1740
  /// <returns>De-registration action to un-register the handler.</returns>
1047
- public Action AddTargetedHandler(InstanceId target, Action<T> handler, Action deregistration)
1741
+ public Action AddTargetedHandler(
1742
+ InstanceId target,
1743
+ Action<T> handler,
1744
+ Action deregistration,
1745
+ int priority
1746
+ )
1048
1747
  {
1049
- return AddHandler(target, ref _targetedHandlers, handler, deregistration);
1748
+ return AddHandler(target, ref _targetedHandlers, handler, deregistration, priority);
1050
1749
  }
1051
1750
 
1052
1751
  /// <summary>
@@ -1055,10 +1754,22 @@
1055
1754
  /// <param name="target">Target the handler is for.</param>
1056
1755
  /// <param name="handler">Relevant MessageHandler.</param>
1057
1756
  /// <param name="deregistration">Deregistration action for the handler.</param>
1757
+ /// <param name="priority">Priority at which to add the handler.</param>
1058
1758
  /// <returns>De-registration action to un-register the handler.</returns>
1059
- public Action AddTargetedHandler(InstanceId target, FastHandler<T> handler, Action deregistration)
1060
- {
1061
- return AddHandler(target, ref _targetedFastHandlers, handler, deregistration);
1759
+ public Action AddTargetedHandler(
1760
+ InstanceId target,
1761
+ FastHandler<T> handler,
1762
+ Action deregistration,
1763
+ int priority
1764
+ )
1765
+ {
1766
+ return AddHandler(
1767
+ target,
1768
+ ref _targetedFastHandlers,
1769
+ handler,
1770
+ deregistration,
1771
+ priority
1772
+ );
1062
1773
  }
1063
1774
 
1064
1775
  /// <summary>
@@ -1066,10 +1777,20 @@
1066
1777
  /// </summary>
1067
1778
  /// <param name="handler">Relevant MessageHandler.</param>
1068
1779
  /// <param name="deregistration">Deregistration action for the handler.</param>
1780
+ /// <param name="priority">Priority at which to add the handler.</param>
1069
1781
  /// <returns>De-registration action to un-register the handler.</returns>
1070
- public Action AddTargetedWithoutTargetingHandler(Action<InstanceId, T> handler, Action deregistration)
1782
+ public Action AddTargetedWithoutTargetingHandler(
1783
+ Action<InstanceId, T> handler,
1784
+ Action deregistration,
1785
+ int priority
1786
+ )
1071
1787
  {
1072
- return AddHandler(ref _targetedWithoutTargetingHandlers, handler, deregistration);
1788
+ return AddHandler(
1789
+ ref _targetedWithoutTargetingHandlers,
1790
+ handler,
1791
+ deregistration,
1792
+ priority
1793
+ );
1073
1794
  }
1074
1795
 
1075
1796
  /// <summary>
@@ -1077,10 +1798,20 @@
1077
1798
  /// </summary>
1078
1799
  /// <param name="handler">Relevant MessageHandler.</param>
1079
1800
  /// <param name="deregistration">Deregistration action for the handler.</param>
1801
+ /// <param name="priority">Priority at which to add the handler.</param>
1080
1802
  /// <returns>De-registration action to un-register the handler.</returns>
1081
- public Action AddTargetedWithoutTargetingHandler(FastHandlerWithContext<T> handler, Action deregistration)
1803
+ public Action AddTargetedWithoutTargetingHandler(
1804
+ FastHandlerWithContext<T> handler,
1805
+ Action deregistration,
1806
+ int priority
1807
+ )
1082
1808
  {
1083
- return AddHandler(ref _fastTargetedWithoutTargetingHandlers, handler, deregistration);
1809
+ return AddHandler(
1810
+ ref _fastTargetedWithoutTargetingHandlers,
1811
+ handler,
1812
+ deregistration,
1813
+ priority
1814
+ );
1084
1815
  }
1085
1816
 
1086
1817
  /// <summary>
@@ -1088,10 +1819,15 @@
1088
1819
  /// </summary>
1089
1820
  /// <param name="handler">Relevant MessageHandler.</param>
1090
1821
  /// <param name="deregistration">Deregistration action for the handler.</param>
1822
+ /// <param name="priority">Priority at which to add the handler.</param>
1091
1823
  /// <returns>De-registration action to un-register the handler.</returns>
1092
- public Action AddUntargetedHandler(Action<T> handler, Action deregistration)
1824
+ public Action AddUntargetedHandler(
1825
+ Action<T> handler,
1826
+ Action deregistration,
1827
+ int priority
1828
+ )
1093
1829
  {
1094
- return AddHandler(ref _untargetedHandlers, handler, deregistration);
1830
+ return AddHandler(ref _untargetedHandlers, handler, deregistration, priority);
1095
1831
  }
1096
1832
 
1097
1833
  /// <summary>
@@ -1099,11 +1835,15 @@
1099
1835
  /// </summary>
1100
1836
  /// <param name="handler">Relevant MessageHandler.</param>
1101
1837
  /// <param name="deregistration">Deregistration action for the handler.</param>
1838
+ /// <param name="priority">Priority at which to add the handler.</param>
1102
1839
  /// <returns>De-registration action to un-register the handler.</returns>
1103
-
1104
- public Action AddUntargetedHandler(FastHandler<T> handler, Action deregistration)
1840
+ public Action AddUntargetedHandler(
1841
+ FastHandler<T> handler,
1842
+ Action deregistration,
1843
+ int priority
1844
+ )
1105
1845
  {
1106
- return AddHandler(ref _untargetedFastHandlers, handler, deregistration);
1846
+ return AddHandler(ref _untargetedFastHandlers, handler, deregistration, priority);
1107
1847
  }
1108
1848
 
1109
1849
  /// <summary>
@@ -1112,10 +1852,22 @@
1112
1852
  /// <param name="source">Source of the handler is for.</param>
1113
1853
  /// <param name="handler">Relevant MessageHandler.</param>
1114
1854
  /// <param name="deregistration">Deregistration action for the handler.</param>
1855
+ /// <param name="priority">Priority at which to add the handler.</param>
1115
1856
  /// <returns>De-registration action to un-register the handler.</returns>
1116
- public Action AddSourcedBroadcastHandler(InstanceId source, Action<T> handler, Action deregistration)
1117
- {
1118
- return AddHandler(source, ref _broadcastHandlers, handler, deregistration);
1857
+ public Action AddSourcedBroadcastHandler(
1858
+ InstanceId source,
1859
+ Action<T> handler,
1860
+ Action deregistration,
1861
+ int priority
1862
+ )
1863
+ {
1864
+ return AddHandler(
1865
+ source,
1866
+ ref _broadcastHandlers,
1867
+ handler,
1868
+ deregistration,
1869
+ priority
1870
+ );
1119
1871
  }
1120
1872
 
1121
1873
  /// <summary>
@@ -1124,10 +1876,22 @@
1124
1876
  /// <param name="source">Source of the handler is for.</param>
1125
1877
  /// <param name="handler">Relevant MessageHandler.</param>
1126
1878
  /// <param name="deregistration">Deregistration action for the handler.</param>
1879
+ /// <param name="priority">Priority at which to add the handler.</param>
1127
1880
  /// <returns>De-registration action to un-register the handler.</returns>
1128
- public Action AddSourcedBroadcastHandler(InstanceId source, FastHandler<T> handler, Action deregistration)
1129
- {
1130
- return AddHandler(source, ref _broadcastFastHandlers, handler, deregistration);
1881
+ public Action AddSourcedBroadcastHandler(
1882
+ InstanceId source,
1883
+ FastHandler<T> handler,
1884
+ Action deregistration,
1885
+ int priority
1886
+ )
1887
+ {
1888
+ return AddHandler(
1889
+ source,
1890
+ ref _broadcastFastHandlers,
1891
+ handler,
1892
+ deregistration,
1893
+ priority
1894
+ );
1131
1895
  }
1132
1896
 
1133
1897
  /// <summary>
@@ -1135,10 +1899,20 @@
1135
1899
  /// </summary>
1136
1900
  /// <param name="handler">Relevant MessageHandler.</param>
1137
1901
  /// <param name="deregistration">Deregistration action for the handler.</param>
1902
+ /// <param name="priority">Priority at which to add the handler.</param>
1138
1903
  /// <returns>De-registration action to un-register the handler.</returns>
1139
- public Action AddSourcedBroadcastWithoutSourceHandler(Action<InstanceId, T> handler, Action deregistration)
1904
+ public Action AddSourcedBroadcastWithoutSourceHandler(
1905
+ Action<InstanceId, T> handler,
1906
+ Action deregistration,
1907
+ int priority
1908
+ )
1140
1909
  {
1141
- return AddHandler(ref _broadcastWithoutSourceHandlers, handler, deregistration);
1910
+ return AddHandler(
1911
+ ref _broadcastWithoutSourceHandlers,
1912
+ handler,
1913
+ deregistration,
1914
+ priority
1915
+ );
1142
1916
  }
1143
1917
 
1144
1918
  /// <summary>
@@ -1146,10 +1920,20 @@
1146
1920
  /// </summary>
1147
1921
  /// <param name="handler">Relevant MessageHandler.</param>
1148
1922
  /// <param name="deregistration">Deregistration action for the handler.</param>
1923
+ /// <param name="priority">Priority at which to add the handler.</param>
1149
1924
  /// <returns>De-registration action to un-register the handler.</returns>
1150
- public Action AddSourcedBroadcastWithoutSourceHandler(FastHandlerWithContext<T> handler, Action deregistration)
1925
+ public Action AddSourcedBroadcastWithoutSourceHandler(
1926
+ FastHandlerWithContext<T> handler,
1927
+ Action deregistration,
1928
+ int priority
1929
+ )
1151
1930
  {
1152
- return AddHandler(ref _fastBroadcastWithoutSourceHandlers, handler, deregistration);
1931
+ return AddHandler(
1932
+ ref _fastBroadcastWithoutSourceHandlers,
1933
+ handler,
1934
+ deregistration,
1935
+ priority
1936
+ );
1153
1937
  }
1154
1938
 
1155
1939
  /// <summary>
@@ -1158,7 +1942,10 @@
1158
1942
  /// <param name="handler">Relevant MessageHandler.</param>
1159
1943
  /// <param name="deregistration">Deregistration action for the handler.</param>
1160
1944
  /// <returns>De-registration action to un-register the handler.</returns>
1161
- public Action AddGlobalUntargetedHandler(Action<IUntargetedMessage> handler, Action deregistration)
1945
+ public Action AddGlobalUntargetedHandler(
1946
+ Action<IUntargetedMessage> handler,
1947
+ Action deregistration
1948
+ )
1162
1949
  {
1163
1950
  return AddHandler(ref _globalUntargetedHandlers, handler, deregistration);
1164
1951
  }
@@ -1170,7 +1957,10 @@
1170
1957
  /// <param name="deregistration">Deregistration action for the handler.</param>
1171
1958
  /// <returns>De-registration action to un-register the handler.</returns>
1172
1959
 
1173
- public Action AddGlobalUntargetedHandler(FastHandler<IUntargetedMessage> handler, Action deregistration)
1960
+ public Action AddGlobalUntargetedHandler(
1961
+ FastHandler<IUntargetedMessage> handler,
1962
+ Action deregistration
1963
+ )
1174
1964
  {
1175
1965
  return AddHandler(ref _globalUntargetedFastHandlers, handler, deregistration);
1176
1966
  }
@@ -1181,7 +1971,10 @@
1181
1971
  /// <param name="handler">Relevant MessageHandler.</param>
1182
1972
  /// <param name="deregistration">Deregistration action for the handler.</param>
1183
1973
  /// <returns>De-registration action to un-register the handler.</returns>
1184
- public Action AddGlobalTargetedHandler(Action<InstanceId, ITargetedMessage> handler, Action deregistration)
1974
+ public Action AddGlobalTargetedHandler(
1975
+ Action<InstanceId, ITargetedMessage> handler,
1976
+ Action deregistration
1977
+ )
1185
1978
  {
1186
1979
  return AddHandler(ref _globalTargetedHandlers, handler, deregistration);
1187
1980
  }
@@ -1193,7 +1986,10 @@
1193
1986
  /// <param name="deregistration">Deregistration action for the handler.</param>
1194
1987
  /// <returns>De-registration action to un-register the handler.</returns>
1195
1988
 
1196
- public Action AddGlobalTargetedHandler(FastHandlerWithContext<ITargetedMessage> handler, Action deregistration)
1989
+ public Action AddGlobalTargetedHandler(
1990
+ FastHandlerWithContext<ITargetedMessage> handler,
1991
+ Action deregistration
1992
+ )
1197
1993
  {
1198
1994
  return AddHandler(ref _globalTargetedFastHandlers, handler, deregistration);
1199
1995
  }
@@ -1204,7 +2000,10 @@
1204
2000
  /// <param name="handler">Relevant MessageHandler.</param>
1205
2001
  /// <param name="deregistration">Deregistration action for the handler.</param>
1206
2002
  /// <returns>De-registration action to un-register the handler.</returns>
1207
- public Action AddGlobalBroadcastHandler(Action<InstanceId, IBroadcastMessage> handler, Action deregistration)
2003
+ public Action AddGlobalBroadcastHandler(
2004
+ Action<InstanceId, IBroadcastMessage> handler,
2005
+ Action deregistration
2006
+ )
1208
2007
  {
1209
2008
  return AddHandler(ref _globalBroadcastHandlers, handler, deregistration);
1210
2009
  }
@@ -1215,7 +2014,10 @@
1215
2014
  /// <param name="handler">Relevant MessageHandler.</param>
1216
2015
  /// <param name="deregistration">Deregistration action for the handler.</param>
1217
2016
  /// <returns>De-registration action to un-register the handler.</returns>
1218
- public Action AddGlobalBroadcastHandler(FastHandlerWithContext<IBroadcastMessage> handler, Action deregistration)
2017
+ public Action AddGlobalBroadcastHandler(
2018
+ FastHandlerWithContext<IBroadcastMessage> handler,
2019
+ Action deregistration
2020
+ )
1219
2021
  {
1220
2022
  return AddHandler(ref _globalBroadcastFastHandlers, handler, deregistration);
1221
2023
  }
@@ -1225,10 +2027,20 @@
1225
2027
  /// </summary>
1226
2028
  /// <param name="handler">Relevant MessageHandler.</param>
1227
2029
  /// <param name="deregistration">Deregistration action for the handler.</param>
2030
+ /// <param name="priority">Priority at which to add the handler.</param>
1228
2031
  /// <returns>De-registration action to un-register the handler.</returns>
1229
- public Action AddUntargetedPostProcessor(Action<T> handler, Action deregistration)
2032
+ public Action AddUntargetedPostProcessor(
2033
+ Action<T> handler,
2034
+ Action deregistration,
2035
+ int priority
2036
+ )
1230
2037
  {
1231
- return AddHandler(ref _untargetedPostProcessingHandlers, handler, deregistration);
2038
+ return AddHandler(
2039
+ ref _untargetedPostProcessingHandlers,
2040
+ handler,
2041
+ deregistration,
2042
+ priority
2043
+ );
1232
2044
  }
1233
2045
 
1234
2046
  /// <summary>
@@ -1236,10 +2048,20 @@
1236
2048
  /// </summary>
1237
2049
  /// <param name="handler">Relevant MessageHandler.</param>
1238
2050
  /// <param name="deregistration">Deregistration action for the handler.</param>
2051
+ /// <param name="priority">Priority at which to add the handler.</param>
1239
2052
  /// <returns>De-registration action to un-register the handler.</returns>
1240
- public Action AddUntargetedPostProcessor(FastHandler<T> handler, Action deregistration)
2053
+ public Action AddUntargetedPostProcessor(
2054
+ FastHandler<T> handler,
2055
+ Action deregistration,
2056
+ int priority
2057
+ )
1241
2058
  {
1242
- return AddHandler(ref _untargetedPostProcessingFastHandlers, handler, deregistration);
2059
+ return AddHandler(
2060
+ ref _untargetedPostProcessingFastHandlers,
2061
+ handler,
2062
+ deregistration,
2063
+ priority
2064
+ );
1243
2065
  }
1244
2066
 
1245
2067
  /// <summary>
@@ -1248,10 +2070,22 @@
1248
2070
  /// <param name="target">Target the handler is for.</param>
1249
2071
  /// <param name="handler">Relevant MessageHandler.</param>
1250
2072
  /// <param name="deregistration">Deregistration action for the handler.</param>
2073
+ /// <param name="priority">Priority at which to add the handler.</param>
1251
2074
  /// <returns>De-registration action to un-register the handler.</returns>
1252
- public Action AddTargetedPostProcessor(InstanceId target, Action<T> handler, Action deregistration)
1253
- {
1254
- return AddHandler(target, ref _targetedPostProcessingHandlers, handler, deregistration);
2075
+ public Action AddTargetedPostProcessor(
2076
+ InstanceId target,
2077
+ Action<T> handler,
2078
+ Action deregistration,
2079
+ int priority
2080
+ )
2081
+ {
2082
+ return AddHandler(
2083
+ target,
2084
+ ref _targetedPostProcessingHandlers,
2085
+ handler,
2086
+ deregistration,
2087
+ priority
2088
+ );
1255
2089
  }
1256
2090
 
1257
2091
  /// <summary>
@@ -1260,10 +2094,22 @@
1260
2094
  /// <param name="target">Target the handler is for.</param>
1261
2095
  /// <param name="handler">Relevant MessageHandler.</param>
1262
2096
  /// <param name="deregistration">Deregistration action for the handler.</param>
2097
+ /// <param name="priority">Priority at which to add the handler.</param>
1263
2098
  /// <returns>De-registration action to un-register the handler.</returns>
1264
- public Action AddTargetedPostProcessor(InstanceId target, FastHandler<T> handler, Action deregistration)
1265
- {
1266
- return AddHandler(target, ref _targetedPostProcessingFastHandlers, handler, deregistration);
2099
+ public Action AddTargetedPostProcessor(
2100
+ InstanceId target,
2101
+ FastHandler<T> handler,
2102
+ Action deregistration,
2103
+ int priority
2104
+ )
2105
+ {
2106
+ return AddHandler(
2107
+ target,
2108
+ ref _targetedPostProcessingFastHandlers,
2109
+ handler,
2110
+ deregistration,
2111
+ priority
2112
+ );
1267
2113
  }
1268
2114
 
1269
2115
  /// <summary>
@@ -1271,10 +2117,20 @@
1271
2117
  /// </summary>
1272
2118
  /// <param name="handler">Relevant MessageHandler.</param>
1273
2119
  /// <param name="deregistration">Deregistration action for the handler.</param>
2120
+ /// <param name="priority">Priority at which to add the handler.</param>
1274
2121
  /// <returns>De-registration action to un-register the handler.</returns>
1275
- public Action AddTargetedWithoutTargetingPostProcessor(Action<InstanceId, T> handler, Action deregistration)
2122
+ public Action AddTargetedWithoutTargetingPostProcessor(
2123
+ Action<InstanceId, T> handler,
2124
+ Action deregistration,
2125
+ int priority
2126
+ )
1276
2127
  {
1277
- return AddHandler(ref _targetedWithoutTargetingPostProcessingHandlers, handler, deregistration);
2128
+ return AddHandler(
2129
+ ref _targetedWithoutTargetingPostProcessingHandlers,
2130
+ handler,
2131
+ deregistration,
2132
+ priority
2133
+ );
1278
2134
  }
1279
2135
 
1280
2136
  /// <summary>
@@ -1282,10 +2138,20 @@
1282
2138
  /// </summary>
1283
2139
  /// <param name="handler">Relevant MessageHandler.</param>
1284
2140
  /// <param name="deregistration">Deregistration action for the handler.</param>
2141
+ /// <param name="priority">Priority at which to add the handler.</param>
1285
2142
  /// <returns>De-registration action to un-register the handler.</returns>
1286
- public Action AddTargetedWithoutTargetingPostProcessor(FastHandlerWithContext<T> handler, Action deregistration)
2143
+ public Action AddTargetedWithoutTargetingPostProcessor(
2144
+ FastHandlerWithContext<T> handler,
2145
+ Action deregistration,
2146
+ int priority
2147
+ )
1287
2148
  {
1288
- return AddHandler(ref _fastTargetedWithoutTargetingPostProcessingHandlers, handler, deregistration);
2149
+ return AddHandler(
2150
+ ref _fastTargetedWithoutTargetingPostProcessingHandlers,
2151
+ handler,
2152
+ deregistration,
2153
+ priority
2154
+ );
1289
2155
  }
1290
2156
 
1291
2157
  /// <summary>
@@ -1294,10 +2160,22 @@
1294
2160
  /// <param name="source">Source the handler is for.</param>
1295
2161
  /// <param name="handler">Relevant MessageHandler.</param>
1296
2162
  /// <param name="deregistration">Deregistration action for the handler.</param>
2163
+ /// <param name="priority">Priority at which to add the handler.</param>
1297
2164
  /// <returns>De-registration action to un-register the handler.</returns>
1298
- public Action AddBroadcastPostProcessor(InstanceId source, Action<T> handler, Action deregistration)
1299
- {
1300
- return AddHandler(source, ref _broadcastPostProcessingHandlers, handler, deregistration);
2165
+ public Action AddBroadcastPostProcessor(
2166
+ InstanceId source,
2167
+ Action<T> handler,
2168
+ Action deregistration,
2169
+ int priority
2170
+ )
2171
+ {
2172
+ return AddHandler(
2173
+ source,
2174
+ ref _broadcastPostProcessingHandlers,
2175
+ handler,
2176
+ deregistration,
2177
+ priority
2178
+ );
1301
2179
  }
1302
2180
 
1303
2181
  /// <summary>
@@ -1306,10 +2184,22 @@
1306
2184
  /// <param name="source">Source the handler is for.</param>
1307
2185
  /// <param name="handler">Relevant MessageHandler.</param>
1308
2186
  /// <param name="deregistration">Deregistration action for the handler.</param>
2187
+ /// <param name="priority">Priority at which to add the handler.</param>
1309
2188
  /// <returns>De-registration action to un-register the handler.</returns>
1310
- public Action AddBroadcastPostProcessor(InstanceId source, FastHandler<T> handler, Action deregistration)
1311
- {
1312
- return AddHandler(source, ref _broadcastPostProcessingFastHandlers, handler, deregistration);
2189
+ public Action AddBroadcastPostProcessor(
2190
+ InstanceId source,
2191
+ FastHandler<T> handler,
2192
+ Action deregistration,
2193
+ int priority
2194
+ )
2195
+ {
2196
+ return AddHandler(
2197
+ source,
2198
+ ref _broadcastPostProcessingFastHandlers,
2199
+ handler,
2200
+ deregistration,
2201
+ priority
2202
+ );
1313
2203
  }
1314
2204
 
1315
2205
  /// <summary>
@@ -1317,54 +2207,118 @@
1317
2207
  /// </summary>
1318
2208
  /// <param name="handler">Relevant MessageHandler.</param>
1319
2209
  /// <param name="deregistration">Deregistration action for the handler.</param>
2210
+ /// <param name="priority">Priority at which to add the handler.</param>
1320
2211
  /// <returns>De-registration action to un-register the handler.</returns>
1321
- public Action AddBroadcastWithoutSourcePostProcessor(Action<InstanceId, T> handler, Action deregistration)
2212
+ public Action AddBroadcastWithoutSourcePostProcessor(
2213
+ Action<InstanceId, T> handler,
2214
+ Action deregistration,
2215
+ int priority
2216
+ )
1322
2217
  {
1323
- return AddHandler(ref _broadcastWithoutSourcePostProcessingHandlers, handler, deregistration);
2218
+ return AddHandler(
2219
+ ref _broadcastWithoutSourcePostProcessingHandlers,
2220
+ handler,
2221
+ deregistration,
2222
+ priority
2223
+ );
1324
2224
  }
1325
2225
 
1326
2226
  /// <summary>
1327
2227
  /// Adds a fast Broadcast post processor to be called after all other handlers have been called.
1328
2228
  /// </summary>
1329
- /// <param name="source">Source the handler is for.</param>
1330
2229
  /// <param name="handler">Relevant MessageHandler.</param>
1331
2230
  /// <param name="deregistration">Deregistration action for the handler.</param>
2231
+ /// <param name="priority">Priority at which to add the handler.</param>
1332
2232
  /// <returns>De-registration action to un-register the handler.</returns>
1333
- public Action AddBroadcastWithoutSourcePostProcessor(FastHandlerWithContext<T> handler, Action deregistration)
1334
- {
1335
- return AddHandler(ref _fastBroadcastWithoutSourcePostProcessingHandlers, handler, deregistration);
1336
- }
1337
-
1338
- private static void RunFastHandlersWithContext<TMessage>(ref InstanceId context, Dictionary<FastHandlerWithContext<T>, int> fastHandlersByContext, ref TMessage message) where TMessage : IMessage
2233
+ public Action AddBroadcastWithoutSourcePostProcessor(
2234
+ FastHandlerWithContext<T> handler,
2235
+ Action deregistration,
2236
+ int priority
2237
+ )
2238
+ {
2239
+ return AddHandler(
2240
+ ref _fastBroadcastWithoutSourcePostProcessingHandlers,
2241
+ handler,
2242
+ deregistration,
2243
+ priority
2244
+ );
2245
+ }
2246
+
2247
+ private static void RunFastHandlersWithContext<TMessage>(
2248
+ ref InstanceId context,
2249
+ Dictionary<int, Dictionary<FastHandlerWithContext<T>, int>> fastHandlersByContext,
2250
+ ref TMessage message,
2251
+ int priority
2252
+ )
2253
+ where TMessage : IMessage
1339
2254
  {
1340
2255
  if (fastHandlersByContext is not { Count: > 0 })
1341
2256
  {
1342
2257
  return;
1343
2258
  }
1344
2259
 
1345
- RunFastHandlers(ref context, ref FastHandlersWithContextStack, fastHandlersByContext, ref message);
1346
- }
1347
-
1348
- private static void RunFastHandlersWithContext<TMessage>(ref InstanceId context, Dictionary<InstanceId, Dictionary<FastHandler<T>, int>> fastHandlersByContext, ref TMessage message) where TMessage : IMessage
1349
- {
1350
- if (fastHandlersByContext is not { Count: > 0 } || !fastHandlersByContext.TryGetValue(context, out Dictionary<FastHandler<T>, int> fastHandlers))
2260
+ RunFastHandlers(
2261
+ ref context,
2262
+ ref FastHandlersWithContextStack,
2263
+ fastHandlersByContext,
2264
+ ref message,
2265
+ priority
2266
+ );
2267
+ }
2268
+
2269
+ private static void RunFastHandlersWithContext<TMessage>(
2270
+ ref InstanceId context,
2271
+ Dictionary<
2272
+ InstanceId,
2273
+ Dictionary<int, Dictionary<FastHandler<T>, int>>
2274
+ > fastHandlersByContext,
2275
+ ref TMessage message,
2276
+ int priority
2277
+ )
2278
+ where TMessage : IMessage
2279
+ {
2280
+ if (
2281
+ fastHandlersByContext is not { Count: > 0 }
2282
+ || !fastHandlersByContext.TryGetValue(
2283
+ context,
2284
+ out Dictionary<int, Dictionary<FastHandler<T>, int>> fastHandlers
2285
+ )
2286
+ )
1351
2287
  {
1352
2288
  return;
1353
2289
  }
1354
2290
 
1355
- RunFastHandlers(fastHandlers, ref message);
2291
+ RunFastHandlers(fastHandlers, ref message, priority);
1356
2292
  }
1357
2293
 
1358
- private static void RunFastHandlers<TMessage>(Dictionary<FastHandler<T>, int> fastHandlers, ref TMessage message) where TMessage : IMessage
2294
+ private static void RunFastHandlers<TMessage>(
2295
+ Dictionary<int, Dictionary<FastHandler<T>, int>> fastHandlers,
2296
+ ref TMessage message,
2297
+ int priority
2298
+ )
2299
+ where TMessage : IMessage
1359
2300
  {
1360
2301
  if (fastHandlers is not { Count: > 0 })
1361
2302
  {
1362
2303
  return;
1363
2304
  }
1364
2305
 
2306
+ if (
2307
+ !fastHandlers.TryGetValue(
2308
+ priority,
2309
+ out Dictionary<FastHandler<T>, int> fastHandlersByPriority
2310
+ )
2311
+ )
2312
+ {
2313
+ return;
2314
+ }
2315
+
1365
2316
  ref T typedMessage = ref Unsafe.As<TMessage, T>(ref message);
1366
2317
 
1367
- List<FastHandler<T>> handlers = GetOrAddNewHandlerStack(ref FastHandlersStack, fastHandlers.Keys);
2318
+ List<FastHandler<T>> handlers = GetOrAddNewHandlerStack(
2319
+ ref FastHandlersStack,
2320
+ fastHandlersByPriority.Keys
2321
+ );
1368
2322
  try
1369
2323
  {
1370
2324
  foreach (FastHandler<T> fastHandler in handlers)
@@ -1378,7 +2332,13 @@
1378
2332
  }
1379
2333
  }
1380
2334
 
1381
- private static void RunFastHandlers<TMessage, U>(Stack<List<FastHandler<U>>> stack, Dictionary<FastHandler<U>, int> fastHandlers, ref TMessage message) where TMessage : IMessage where U : IMessage
2335
+ private static void RunFastHandlers<TMessage, U>(
2336
+ Stack<List<FastHandler<U>>> stack,
2337
+ Dictionary<FastHandler<U>, int> fastHandlers,
2338
+ ref TMessage message
2339
+ )
2340
+ where TMessage : IMessage
2341
+ where U : IMessage
1382
2342
  {
1383
2343
  if (fastHandlers is not { Count: > 0 })
1384
2344
  {
@@ -1387,7 +2347,10 @@
1387
2347
 
1388
2348
  ref U typedMessage = ref Unsafe.As<TMessage, U>(ref message);
1389
2349
 
1390
- List<FastHandler<U>> handlers = GetOrAddNewHandlerStack(ref stack, fastHandlers.Keys);
2350
+ List<FastHandler<U>> handlers = GetOrAddNewHandlerStack(
2351
+ ref stack,
2352
+ fastHandlers.Keys
2353
+ );
1391
2354
  try
1392
2355
  {
1393
2356
  foreach (FastHandler<U> fastHandler in handlers)
@@ -1402,23 +2365,69 @@
1402
2365
  }
1403
2366
 
1404
2367
  private static void RunFastHandlers<TMessage, U>(
1405
- ref InstanceId context, Stack<List<FastHandlerWithContext<U>>> stack,
1406
- Dictionary<FastHandlerWithContext<U>, int> fastHandlers, ref TMessage message)
1407
- where TMessage : IMessage where U : IMessage
1408
- {
1409
- RunFastHandlers(ref context, ref stack, fastHandlers, ref message);
2368
+ ref InstanceId context,
2369
+ ref Stack<List<FastHandlerWithContext<U>>> stack,
2370
+ Dictionary<FastHandlerWithContext<U>, int> priorityHandlers,
2371
+ ref TMessage message
2372
+ )
2373
+ where TMessage : IMessage
2374
+ where U : IMessage
2375
+ {
2376
+ if (priorityHandlers is not { Count: > 0 })
2377
+ {
2378
+ return;
2379
+ }
2380
+
2381
+ ref U typedMessage = ref Unsafe.As<TMessage, U>(ref message);
2382
+
2383
+ List<FastHandlerWithContext<U>> handlers = GetOrAddNewHandlerStack(
2384
+ ref stack,
2385
+ priorityHandlers.Keys
2386
+ );
2387
+ try
2388
+ {
2389
+ foreach (FastHandlerWithContext<U> fastHandler in handlers)
2390
+ {
2391
+ fastHandler(ref context, ref typedMessage);
2392
+ }
2393
+ }
2394
+ finally
2395
+ {
2396
+ stack.Push(handlers);
2397
+ }
1410
2398
  }
1411
2399
 
1412
- private static void RunFastHandlers<TMessage, U>(ref InstanceId context, ref Stack<List<FastHandlerWithContext<U>>> stack, Dictionary<FastHandlerWithContext<U>, int> fastHandlers, ref TMessage message) where TMessage : IMessage where U : IMessage
2400
+ private static void RunFastHandlers<TMessage, U>(
2401
+ ref InstanceId context,
2402
+ ref Stack<List<FastHandlerWithContext<U>>> stack,
2403
+ Dictionary<int, Dictionary<FastHandlerWithContext<U>, int>> fastHandlers,
2404
+ ref TMessage message,
2405
+ int priority
2406
+ )
2407
+ where TMessage : IMessage
2408
+ where U : IMessage
1413
2409
  {
1414
2410
  if (fastHandlers is not { Count: > 0 })
1415
2411
  {
1416
2412
  return;
1417
2413
  }
1418
2414
 
2415
+ if (
2416
+ !fastHandlers.TryGetValue(
2417
+ priority,
2418
+ out Dictionary<FastHandlerWithContext<U>, int> priorityHandlers
2419
+ )
2420
+ )
2421
+ {
2422
+ return;
2423
+ }
2424
+
1419
2425
  ref U typedMessage = ref Unsafe.As<TMessage, U>(ref message);
1420
2426
 
1421
- List<FastHandlerWithContext<U>> handlers = GetOrAddNewHandlerStack(ref stack, fastHandlers.Keys);
2427
+ List<FastHandlerWithContext<U>> handlers = GetOrAddNewHandlerStack(
2428
+ ref stack,
2429
+ priorityHandlers.Keys
2430
+ );
1422
2431
  try
1423
2432
  {
1424
2433
  foreach (FastHandlerWithContext<U> fastHandler in handlers)
@@ -1431,25 +2440,53 @@
1431
2440
  stack.Push(handlers);
1432
2441
  }
1433
2442
  }
1434
-
1435
- private static void RunHandlersWithContext<TMessage>(ref InstanceId context, Dictionary<InstanceId, Dictionary<Action<T>, int>> handlersByContext, ref TMessage message) where TMessage : IMessage
1436
- {
1437
- if (handlersByContext is not { Count: > 0 } || !handlersByContext.TryGetValue(context, out Dictionary<Action<T>, int> handlers))
2443
+
2444
+ private static void RunHandlersWithContext<TMessage>(
2445
+ ref InstanceId context,
2446
+ Dictionary<
2447
+ InstanceId,
2448
+ Dictionary<int, Dictionary<Action<T>, int>>
2449
+ > handlersByContext,
2450
+ ref TMessage message,
2451
+ int priority
2452
+ )
2453
+ where TMessage : IMessage
2454
+ {
2455
+ if (
2456
+ handlersByContext is not { Count: > 0 }
2457
+ || !handlersByContext.TryGetValue(
2458
+ context,
2459
+ out Dictionary<int, Dictionary<Action<T>, int>> handlers
2460
+ )
2461
+ )
1438
2462
  {
1439
2463
  return;
1440
2464
  }
1441
2465
 
1442
- RunHandlers(handlers, ref message);
2466
+ RunHandlers(handlers, ref message, priority);
1443
2467
  }
1444
2468
 
1445
- private static void RunHandlers<TMessage>(Dictionary<Action<T>, int> handlers, ref TMessage message) where TMessage : IMessage
2469
+ private static void RunHandlers<TMessage>(
2470
+ Dictionary<int, Dictionary<Action<T>, int>> sortedHandlers,
2471
+ ref TMessage message,
2472
+ int priority
2473
+ )
2474
+ where TMessage : IMessage
1446
2475
  {
1447
- if (handlers is not { Count: > 0 })
2476
+ if (sortedHandlers is not { Count: > 0 })
2477
+ {
2478
+ return;
2479
+ }
2480
+
2481
+ if (!sortedHandlers.TryGetValue(priority, out Dictionary<Action<T>, int> handlers))
1448
2482
  {
1449
2483
  return;
1450
2484
  }
1451
2485
 
1452
- List<Action<T>> typedHandlers = GetOrAddNewHandlerStack(ref HandlersStack, handlers.Keys);
2486
+ List<Action<T>> typedHandlers = GetOrAddNewHandlerStack(
2487
+ ref HandlersStack,
2488
+ handlers.Keys
2489
+ );
1453
2490
  try
1454
2491
  {
1455
2492
  ref T typedMessage = ref Unsafe.As<TMessage, T>(ref message);
@@ -1465,14 +2502,33 @@
1465
2502
  }
1466
2503
  }
1467
2504
 
1468
- private static void RunHandlers<TMessage>(ref InstanceId context, Dictionary<Action<InstanceId, T>, int> handlers, ref TMessage message) where TMessage : IMessage
2505
+ private static void RunHandlers<TMessage>(
2506
+ ref InstanceId context,
2507
+ Dictionary<int, Dictionary<Action<InstanceId, T>, int>> handlers,
2508
+ ref TMessage message,
2509
+ int priority
2510
+ )
2511
+ where TMessage : IMessage
1469
2512
  {
1470
2513
  if (handlers is not { Count: > 0 })
1471
2514
  {
1472
2515
  return;
1473
2516
  }
1474
2517
 
1475
- List<Action<InstanceId, T>> typedHandlers = GetOrAddNewHandlerStack(ref HandlersWithoutContextStack, handlers.Keys);
2518
+ if (
2519
+ !handlers.TryGetValue(
2520
+ priority,
2521
+ out Dictionary<Action<InstanceId, T>, int> handlersByPriority
2522
+ )
2523
+ )
2524
+ {
2525
+ return;
2526
+ }
2527
+
2528
+ List<Action<InstanceId, T>> typedHandlers = GetOrAddNewHandlerStack(
2529
+ ref HandlersWithoutContextStack,
2530
+ handlersByPriority.Keys
2531
+ );
1476
2532
  try
1477
2533
  {
1478
2534
  ref T typedMessage = ref Unsafe.As<TMessage, T>(ref message);
@@ -1488,40 +2544,65 @@
1488
2544
  }
1489
2545
  }
1490
2546
 
1491
- private static List<U> GetOrAddNewHandlerStack<U>(ref Stack<List<U>> stack, IEnumerable<U> handlers)
2547
+ private static List<U> GetOrAddNewHandlerStack<U>(
2548
+ ref Stack<List<U>> stack,
2549
+ IEnumerable<U> handlers
2550
+ )
1492
2551
  {
1493
2552
  stack ??= new Stack<List<U>>();
1494
- if (stack.TryPop(out List<U> typedHandlerStack))
2553
+ if (!stack.TryPop(out List<U> typedHandlerStack))
1495
2554
  {
1496
- typedHandlerStack.Clear();
1497
- typedHandlerStack.AddRange(handlers);
1498
- return typedHandlerStack;
2555
+ return new List<U>(handlers);
1499
2556
  }
1500
2557
 
1501
- return new List<U>(handlers);
2558
+ typedHandlerStack.Clear();
2559
+ typedHandlerStack.AddRange(handlers);
2560
+ return typedHandlerStack;
1502
2561
  }
1503
2562
 
1504
- private static Action AddHandler<U>(InstanceId context, ref Dictionary<InstanceId, Dictionary<U, int>> handlersByContext, U handler, Action deregistration)
2563
+ private static Action AddHandler<U>(
2564
+ InstanceId context,
2565
+ ref Dictionary<InstanceId, Dictionary<int, Dictionary<U, int>>> handlersByContext,
2566
+ U handler,
2567
+ Action deregistration,
2568
+ int priority
2569
+ )
1505
2570
  {
1506
- handlersByContext ??= new Dictionary<InstanceId, Dictionary<U, int>>();
1507
-
1508
- if (!handlersByContext.TryGetValue(context, out Dictionary<U, int> handlers))
2571
+ handlersByContext ??=
2572
+ new Dictionary<InstanceId, Dictionary<int, Dictionary<U, int>>>();
2573
+
2574
+ if (
2575
+ !handlersByContext.TryGetValue(
2576
+ context,
2577
+ out Dictionary<int, Dictionary<U, int>> sortedHandlers
2578
+ )
2579
+ )
1509
2580
  {
1510
- handlers = new Dictionary<U, int>();
1511
- handlersByContext[context] = handlers;
2581
+ sortedHandlers = new Dictionary<int, Dictionary<U, int>>();
2582
+ handlersByContext[context] = sortedHandlers;
1512
2583
  }
1513
2584
 
1514
- if (!handlers.TryGetValue(handler, out int count))
2585
+ if (!sortedHandlers.TryGetValue(priority, out Dictionary<U, int> handlers))
1515
2586
  {
1516
- count = 0;
2587
+ handlers = new Dictionary<U, int>();
2588
+ sortedHandlers[priority] = handlers;
1517
2589
  }
1518
2590
 
2591
+ int count = handlers.GetValueOrDefault(handler, 0);
2592
+
1519
2593
  handlers[handler] = count + 1;
1520
2594
 
1521
- Dictionary<InstanceId, Dictionary<U, int>> localHandlersByContext = handlersByContext;
2595
+ Dictionary<InstanceId, Dictionary<int, Dictionary<U, int>>> localHandlersByContext =
2596
+ handlersByContext;
2597
+
1522
2598
  return () =>
1523
2599
  {
1524
- if (!localHandlersByContext.TryGetValue(context, out handlers))
2600
+ if (!localHandlersByContext.TryGetValue(context, out sortedHandlers))
2601
+ {
2602
+ return;
2603
+ }
2604
+
2605
+ if (!sortedHandlers.TryGetValue(priority, out handlers))
1525
2606
  {
1526
2607
  return;
1527
2608
  }
@@ -1549,22 +2630,62 @@
1549
2630
  };
1550
2631
  }
1551
2632
 
1552
- private static Action AddHandler<U>(ref Dictionary<U, int> handlers, U handler, Action deregistration)
2633
+ private static Action AddHandler<U>(
2634
+ ref Dictionary<U, int> handlersByPriority,
2635
+ U handler,
2636
+ Action deregistration
2637
+ )
1553
2638
  {
1554
- int count;
1555
- if (handlers == null)
2639
+ handlersByPriority ??= new Dictionary<U, int>();
2640
+
2641
+ int count = handlersByPriority.GetValueOrDefault(handler, 0);
2642
+
2643
+ handlersByPriority[handler] = count + 1;
2644
+
2645
+ Dictionary<U, int> localHandlers = handlersByPriority;
2646
+
2647
+ return () =>
1556
2648
  {
1557
- handlers = new Dictionary<U, int>();
1558
- count = 0;
1559
- }
1560
- else if (!handlers.TryGetValue(handler, out count))
2649
+ if (!localHandlers.TryGetValue(handler, out count))
2650
+ {
2651
+ return;
2652
+ }
2653
+
2654
+ // Always invoke deregistration action, as MessageBus dedupes this as well
2655
+ deregistration?.Invoke();
2656
+ if (count <= 1)
2657
+ {
2658
+ _ = localHandlers.Remove(handler);
2659
+ return;
2660
+ }
2661
+
2662
+ localHandlers[handler] = count - 1;
2663
+ };
2664
+ }
2665
+
2666
+ private static Action AddHandler<U>(
2667
+ ref Dictionary<int, Dictionary<U, int>> sortedHandlers,
2668
+ U handler,
2669
+ Action deregistration,
2670
+ int priority
2671
+ )
2672
+ {
2673
+ sortedHandlers ??= new Dictionary<int, Dictionary<U, int>>();
2674
+
2675
+ if (
2676
+ !sortedHandlers.TryGetValue(priority, out Dictionary<U, int> handlersByPriority)
2677
+ )
1561
2678
  {
1562
- count = 0;
2679
+ handlersByPriority = new Dictionary<U, int>();
2680
+ sortedHandlers[priority] = handlersByPriority;
1563
2681
  }
1564
2682
 
1565
- handlers[handler] = count + 1;
2683
+ int count = handlersByPriority.GetValueOrDefault(handler, 0);
2684
+
2685
+ handlersByPriority[handler] = count + 1;
1566
2686
 
1567
- Dictionary<U, int> localHandlers = handlers;
2687
+ Dictionary<int, Dictionary<U, int>> localSortedHandlers = sortedHandlers;
2688
+ Dictionary<U, int> localHandlers = handlersByPriority;
1568
2689
 
1569
2690
  return () =>
1570
2691
  {
@@ -1578,6 +2699,12 @@
1578
2699
  if (count <= 1)
1579
2700
  {
1580
2701
  _ = localHandlers.Remove(handler);
2702
+
2703
+ if (localHandlers.Count <= 0)
2704
+ {
2705
+ _ = localSortedHandlers.Remove(priority);
2706
+ }
2707
+
1581
2708
  return;
1582
2709
  }
1583
2710