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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,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,102 @@
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
+ SortedDictionary<int, Dictionary<Action<T>, int>>
1337
+ > _targetedHandlers;
1338
+ private SortedDictionary<int, Dictionary<Action<T>, int>> _untargetedHandlers;
1339
+ private Dictionary<
1340
+ InstanceId,
1341
+ SortedDictionary<int, Dictionary<Action<T>, int>>
1342
+ > _broadcastHandlers;
1343
+ private Dictionary<
1344
+ InstanceId,
1345
+ SortedDictionary<int, Dictionary<Action<T>, int>>
1346
+ > _targetedPostProcessingHandlers;
1347
+ private SortedDictionary<
1348
+ int,
1349
+ Dictionary<Action<T>, int>
1350
+ > _untargetedPostProcessingHandlers;
1351
+ private Dictionary<
1352
+ InstanceId,
1353
+ SortedDictionary<int, Dictionary<Action<T>, int>>
1354
+ > _broadcastPostProcessingHandlers;
1355
+ private Dictionary<
1356
+ InstanceId,
1357
+ SortedDictionary<int, Dictionary<FastHandler<T>, int>>
1358
+ > _targetedFastHandlers;
1359
+ private SortedDictionary<int, Dictionary<FastHandler<T>, int>> _untargetedFastHandlers;
1360
+ private Dictionary<
1361
+ InstanceId,
1362
+ SortedDictionary<int, Dictionary<FastHandler<T>, int>>
1363
+ > _broadcastFastHandlers;
1364
+ private Dictionary<
1365
+ InstanceId,
1366
+ SortedDictionary<int, Dictionary<FastHandler<T>, int>>
1367
+ > _targetedPostProcessingFastHandlers;
1368
+ private SortedDictionary<
1369
+ int,
1370
+ Dictionary<FastHandler<T>, int>
1371
+ > _untargetedPostProcessingFastHandlers;
1372
+ private Dictionary<
1373
+ InstanceId,
1374
+ SortedDictionary<int, Dictionary<FastHandler<T>, int>>
1375
+ > _broadcastPostProcessingFastHandlers;
831
1376
  private Dictionary<Action<IUntargetedMessage>, int> _globalUntargetedHandlers;
832
1377
  private Dictionary<Action<InstanceId, ITargetedMessage>, int> _globalTargetedHandlers;
833
1378
  private Dictionary<Action<InstanceId, IBroadcastMessage>, int> _globalBroadcastHandlers;
834
1379
  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;
1380
+ private Dictionary<
1381
+ FastHandlerWithContext<ITargetedMessage>,
1382
+ int
1383
+ > _globalTargetedFastHandlers;
1384
+ private Dictionary<
1385
+ FastHandlerWithContext<IBroadcastMessage>,
1386
+ int
1387
+ > _globalBroadcastFastHandlers;
1388
+ private SortedDictionary<
1389
+ int,
1390
+ Dictionary<Action<InstanceId, T>, int>
1391
+ > _targetedWithoutTargetingHandlers;
1392
+ private SortedDictionary<
1393
+ int,
1394
+ Dictionary<FastHandlerWithContext<T>, int>
1395
+ > _fastTargetedWithoutTargetingHandlers;
1396
+ private SortedDictionary<
1397
+ int,
1398
+ Dictionary<Action<InstanceId, T>, int>
1399
+ > _broadcastWithoutSourceHandlers;
1400
+ private SortedDictionary<
1401
+ int,
1402
+ Dictionary<FastHandlerWithContext<T>, int>
1403
+ > _fastBroadcastWithoutSourceHandlers;
1404
+ private SortedDictionary<
1405
+ int,
1406
+ Dictionary<Action<InstanceId, T>, int>
1407
+ > _targetedWithoutTargetingPostProcessingHandlers;
1408
+ private SortedDictionary<
1409
+ int,
1410
+ Dictionary<FastHandlerWithContext<T>, int>
1411
+ > _fastTargetedWithoutTargetingPostProcessingHandlers;
1412
+ private SortedDictionary<
1413
+ int,
1414
+ Dictionary<Action<InstanceId, T>, int>
1415
+ > _broadcastWithoutSourcePostProcessingHandlers;
1416
+ private SortedDictionary<
1417
+ int,
1418
+ Dictionary<FastHandlerWithContext<T>, int>
1419
+ > _fastBroadcastWithoutSourcePostProcessingHandlers;
845
1420
 
846
1421
  /// <summary>
847
1422
  /// Emits the UntargetedMessage to all subscribed listeners.
848
1423
  /// </summary>
849
1424
  /// <param name="message">Message to emit.</param>
850
- public void HandleUntargeted(ref T message)
1425
+ /// <param name="priority">Priority at which to run the handlers.</param>
1426
+ public void HandleUntargeted(ref T message, int priority)
851
1427
  {
852
- RunFastHandlers(_untargetedFastHandlers, ref message);
853
- RunHandlers(_untargetedHandlers, ref message);
1428
+ RunFastHandlers(_untargetedFastHandlers, ref message, priority);
1429
+ RunHandlers(_untargetedHandlers, ref message, priority);
854
1430
  }
855
1431
 
856
1432
  /// <summary>
@@ -858,10 +1434,16 @@
858
1434
  /// </summary>
859
1435
  /// <param name="target">Target the message is for.</param>
860
1436
  /// <param name="message">Message to emit.</param>
861
- public void HandleTargeted(ref InstanceId target, ref T message)
1437
+ /// <param name="priority">Priority at which to run the handlers.</param>
1438
+ public void HandleTargeted(ref InstanceId target, ref T message, int priority)
862
1439
  {
863
- RunFastHandlersWithContext(ref target, _targetedFastHandlers, ref message);
864
- RunHandlersWithContext(ref target, _targetedHandlers, ref message);
1440
+ RunFastHandlersWithContext(
1441
+ ref target,
1442
+ _targetedFastHandlers,
1443
+ ref message,
1444
+ priority
1445
+ );
1446
+ RunHandlersWithContext(ref target, _targetedHandlers, ref message, priority);
865
1447
  }
866
1448
 
867
1449
  /// <summary>
@@ -869,10 +1451,21 @@
869
1451
  /// </summary>
870
1452
  /// <param name="target">Target the message is for.</param>
871
1453
  /// <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);
1454
+ /// <param name="priority">Priority at which to run the handlers.</param>
1455
+ public void HandleTargetedWithoutTargeting(
1456
+ ref InstanceId target,
1457
+ ref T message,
1458
+ int priority
1459
+ )
1460
+ {
1461
+ RunFastHandlers(
1462
+ ref target,
1463
+ ref FastHandlersWithContextStack,
1464
+ _fastTargetedWithoutTargetingHandlers,
1465
+ ref message,
1466
+ priority
1467
+ );
1468
+ RunHandlers(ref target, _targetedWithoutTargetingHandlers, ref message, priority);
876
1469
  }
877
1470
 
878
1471
  /// <summary>
@@ -880,10 +1473,16 @@
880
1473
  /// </summary>
881
1474
  /// <param name="source">Source the message is from.</param>
882
1475
  /// <param name="message">Message to emit.</param>
883
- public void HandleSourcedBroadcast(ref InstanceId source, ref T message)
1476
+ /// <param name="priority">Priority at which to run the handlers.</param>
1477
+ public void HandleSourcedBroadcast(ref InstanceId source, ref T message, int priority)
884
1478
  {
885
- RunFastHandlersWithContext(ref source, _broadcastFastHandlers, ref message);
886
- RunHandlersWithContext(ref source, _broadcastHandlers, ref message);
1479
+ RunFastHandlersWithContext(
1480
+ ref source,
1481
+ _broadcastFastHandlers,
1482
+ ref message,
1483
+ priority
1484
+ );
1485
+ RunHandlersWithContext(ref source, _broadcastHandlers, ref message, priority);
887
1486
  }
888
1487
 
889
1488
  /// <summary>
@@ -891,10 +1490,21 @@
891
1490
  /// </summary>
892
1491
  /// <param name="source">Source the message is from.</param>
893
1492
  /// <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);
1493
+ /// <param name="priority">Priority at which to run the handlers.</param>
1494
+ public void HandleSourcedBroadcastWithoutSource(
1495
+ ref InstanceId source,
1496
+ ref T message,
1497
+ int priority
1498
+ )
1499
+ {
1500
+ RunFastHandlers(
1501
+ ref source,
1502
+ ref FastHandlersWithContextStack,
1503
+ _fastBroadcastWithoutSourceHandlers,
1504
+ ref message,
1505
+ priority
1506
+ );
1507
+ RunHandlers(ref source, _broadcastWithoutSourceHandlers, ref message, priority);
898
1508
  }
899
1509
 
900
1510
  /// <summary>
@@ -903,14 +1513,22 @@
903
1513
  /// <param name="message">Message to emit.</param>
904
1514
  public void HandleGlobalUntargeted(ref IUntargetedMessage message)
905
1515
  {
906
- RunFastHandlers(GlobalUntargetedFastHandlersStack, _globalUntargetedFastHandlers, ref message);
1516
+ RunFastHandlers(
1517
+ GlobalUntargetedFastHandlersStack,
1518
+ _globalUntargetedFastHandlers,
1519
+ ref message
1520
+ );
907
1521
 
908
1522
  if (_globalUntargetedHandlers is not { Count: > 0 })
909
1523
  {
910
1524
  return;
911
1525
  }
912
1526
 
913
- if (GlobalUntargetedHandlersStack.TryPop(out List<Action<IUntargetedMessage>> handlers))
1527
+ if (
1528
+ GlobalUntargetedHandlersStack.TryPop(
1529
+ out List<Action<IUntargetedMessage>> handlers
1530
+ )
1531
+ )
914
1532
  {
915
1533
  handlers.Clear();
916
1534
  handlers.AddRange(_globalUntargetedHandlers.Keys);
@@ -940,21 +1558,32 @@
940
1558
  /// <param name="message">Message to emit.</param>
941
1559
  public void HandleGlobalTargeted(ref InstanceId target, ref ITargetedMessage message)
942
1560
  {
943
- RunFastHandlers(ref target, GlobalTargetedFastHandlersStack, _globalTargetedFastHandlers, ref message);
1561
+ RunFastHandlers(
1562
+ ref target,
1563
+ ref GlobalTargetedFastHandlersStack,
1564
+ _globalTargetedFastHandlers,
1565
+ ref message
1566
+ );
944
1567
 
945
1568
  if (_globalTargetedHandlers is not { Count: > 0 })
946
1569
  {
947
1570
  return;
948
1571
  }
949
1572
 
950
- if (GlobalTargetedHandlersStack.TryPop(out List<Action<InstanceId, ITargetedMessage>> handlers))
1573
+ if (
1574
+ GlobalTargetedHandlersStack.TryPop(
1575
+ out List<Action<InstanceId, ITargetedMessage>> handlers
1576
+ )
1577
+ )
951
1578
  {
952
1579
  handlers.Clear();
953
1580
  handlers.AddRange(_globalTargetedHandlers.Keys);
954
1581
  }
955
1582
  else
956
1583
  {
957
- handlers = new List<Action<InstanceId, ITargetedMessage>>(_globalTargetedHandlers.Keys);
1584
+ handlers = new List<Action<InstanceId, ITargetedMessage>>(
1585
+ _globalTargetedHandlers.Keys
1586
+ );
958
1587
  }
959
1588
 
960
1589
  try
@@ -977,21 +1606,32 @@
977
1606
  /// <param name="message">Message to emit.</param>
978
1607
  public void HandleGlobalBroadcast(ref InstanceId source, ref IBroadcastMessage message)
979
1608
  {
980
- RunFastHandlers(ref source, GlobalBroadcastFastHandlersStack, _globalBroadcastFastHandlers, ref message);
1609
+ RunFastHandlers(
1610
+ ref source,
1611
+ ref GlobalBroadcastFastHandlersStack,
1612
+ _globalBroadcastFastHandlers,
1613
+ ref message
1614
+ );
981
1615
 
982
1616
  if (_globalBroadcastHandlers is not { Count: > 0 })
983
1617
  {
984
1618
  return;
985
1619
  }
986
1620
 
987
- if (GlobalBroadcastHandlersStack.TryPop(out List<Action<InstanceId, IBroadcastMessage>> handlers))
1621
+ if (
1622
+ GlobalBroadcastHandlersStack.TryPop(
1623
+ out List<Action<InstanceId, IBroadcastMessage>> handlers
1624
+ )
1625
+ )
988
1626
  {
989
1627
  handlers.Clear();
990
1628
  handlers.AddRange(_globalBroadcastHandlers.Keys);
991
1629
  }
992
1630
  else
993
1631
  {
994
- handlers = new List<Action<InstanceId, IBroadcastMessage>>(_globalBroadcastHandlers.Keys);
1632
+ handlers = new List<Action<InstanceId, IBroadcastMessage>>(
1633
+ _globalBroadcastHandlers.Keys
1634
+ );
995
1635
  }
996
1636
 
997
1637
  try
@@ -1007,34 +1647,90 @@
1007
1647
  }
1008
1648
  }
1009
1649
 
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);
1650
+ public void HandleUntargetedPostProcessing(ref T message, int priority)
1651
+ {
1652
+ RunFastHandlers(_untargetedPostProcessingFastHandlers, ref message, priority);
1653
+ RunHandlers(_untargetedPostProcessingHandlers, ref message, priority);
1654
+ }
1655
+
1656
+ public void HandleTargetedPostProcessing(
1657
+ ref InstanceId target,
1658
+ ref T message,
1659
+ int priority
1660
+ )
1661
+ {
1662
+ RunFastHandlersWithContext(
1663
+ ref target,
1664
+ _targetedPostProcessingFastHandlers,
1665
+ ref message,
1666
+ priority
1667
+ );
1668
+ RunHandlersWithContext(
1669
+ ref target,
1670
+ _targetedPostProcessingHandlers,
1671
+ ref message,
1672
+ priority
1673
+ );
1674
+ }
1675
+
1676
+ public void HandleTargetedWithoutTargetingPostProcessing(
1677
+ ref InstanceId target,
1678
+ ref T message,
1679
+ int priority
1680
+ )
1681
+ {
1682
+ RunFastHandlersWithContext(
1683
+ ref target,
1684
+ _fastTargetedWithoutTargetingPostProcessingHandlers,
1685
+ ref message,
1686
+ priority
1687
+ );
1688
+ RunHandlers(
1689
+ ref target,
1690
+ _targetedWithoutTargetingPostProcessingHandlers,
1691
+ ref message,
1692
+ priority
1693
+ );
1694
+ }
1695
+
1696
+ public void HandleSourcedBroadcastPostProcessing(
1697
+ ref InstanceId source,
1698
+ ref T message,
1699
+ int priority
1700
+ )
1701
+ {
1702
+ RunFastHandlersWithContext(
1703
+ ref source,
1704
+ _broadcastPostProcessingFastHandlers,
1705
+ ref message,
1706
+ priority
1707
+ );
1708
+ RunHandlersWithContext(
1709
+ ref source,
1710
+ _broadcastPostProcessingHandlers,
1711
+ ref message,
1712
+ priority
1713
+ );
1714
+ }
1715
+
1716
+ public void HandleBroadcastWithoutSourcePostProcessing(
1717
+ ref InstanceId source,
1718
+ ref T message,
1719
+ int priority
1720
+ )
1721
+ {
1722
+ RunFastHandlersWithContext(
1723
+ ref source,
1724
+ _fastBroadcastWithoutSourcePostProcessingHandlers,
1725
+ ref message,
1726
+ priority
1727
+ );
1728
+ RunHandlers(
1729
+ ref source,
1730
+ _broadcastWithoutSourcePostProcessingHandlers,
1731
+ ref message,
1732
+ priority
1733
+ );
1038
1734
  }
1039
1735
 
1040
1736
  /// <summary>
@@ -1043,10 +1739,16 @@
1043
1739
  /// <param name="target">Target the handler is for.</param>
1044
1740
  /// <param name="handler">Relevant MessageHandler.</param>
1045
1741
  /// <param name="deregistration">Deregistration action for the handler.</param>
1742
+ /// <param name="priority">Priority at which to add the handler.</param>
1046
1743
  /// <returns>De-registration action to un-register the handler.</returns>
1047
- public Action AddTargetedHandler(InstanceId target, Action<T> handler, Action deregistration)
1744
+ public Action AddTargetedHandler(
1745
+ InstanceId target,
1746
+ Action<T> handler,
1747
+ Action deregistration,
1748
+ int priority
1749
+ )
1048
1750
  {
1049
- return AddHandler(target, ref _targetedHandlers, handler, deregistration);
1751
+ return AddHandler(target, ref _targetedHandlers, handler, deregistration, priority);
1050
1752
  }
1051
1753
 
1052
1754
  /// <summary>
@@ -1055,10 +1757,22 @@
1055
1757
  /// <param name="target">Target the handler is for.</param>
1056
1758
  /// <param name="handler">Relevant MessageHandler.</param>
1057
1759
  /// <param name="deregistration">Deregistration action for the handler.</param>
1760
+ /// <param name="priority">Priority at which to add the handler.</param>
1058
1761
  /// <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);
1762
+ public Action AddTargetedHandler(
1763
+ InstanceId target,
1764
+ FastHandler<T> handler,
1765
+ Action deregistration,
1766
+ int priority
1767
+ )
1768
+ {
1769
+ return AddHandler(
1770
+ target,
1771
+ ref _targetedFastHandlers,
1772
+ handler,
1773
+ deregistration,
1774
+ priority
1775
+ );
1062
1776
  }
1063
1777
 
1064
1778
  /// <summary>
@@ -1066,10 +1780,20 @@
1066
1780
  /// </summary>
1067
1781
  /// <param name="handler">Relevant MessageHandler.</param>
1068
1782
  /// <param name="deregistration">Deregistration action for the handler.</param>
1783
+ /// <param name="priority">Priority at which to add the handler.</param>
1069
1784
  /// <returns>De-registration action to un-register the handler.</returns>
1070
- public Action AddTargetedWithoutTargetingHandler(Action<InstanceId, T> handler, Action deregistration)
1785
+ public Action AddTargetedWithoutTargetingHandler(
1786
+ Action<InstanceId, T> handler,
1787
+ Action deregistration,
1788
+ int priority
1789
+ )
1071
1790
  {
1072
- return AddHandler(ref _targetedWithoutTargetingHandlers, handler, deregistration);
1791
+ return AddHandler(
1792
+ ref _targetedWithoutTargetingHandlers,
1793
+ handler,
1794
+ deregistration,
1795
+ priority
1796
+ );
1073
1797
  }
1074
1798
 
1075
1799
  /// <summary>
@@ -1077,10 +1801,20 @@
1077
1801
  /// </summary>
1078
1802
  /// <param name="handler">Relevant MessageHandler.</param>
1079
1803
  /// <param name="deregistration">Deregistration action for the handler.</param>
1804
+ /// <param name="priority">Priority at which to add the handler.</param>
1080
1805
  /// <returns>De-registration action to un-register the handler.</returns>
1081
- public Action AddTargetedWithoutTargetingHandler(FastHandlerWithContext<T> handler, Action deregistration)
1806
+ public Action AddTargetedWithoutTargetingHandler(
1807
+ FastHandlerWithContext<T> handler,
1808
+ Action deregistration,
1809
+ int priority
1810
+ )
1082
1811
  {
1083
- return AddHandler(ref _fastTargetedWithoutTargetingHandlers, handler, deregistration);
1812
+ return AddHandler(
1813
+ ref _fastTargetedWithoutTargetingHandlers,
1814
+ handler,
1815
+ deregistration,
1816
+ priority
1817
+ );
1084
1818
  }
1085
1819
 
1086
1820
  /// <summary>
@@ -1088,10 +1822,15 @@
1088
1822
  /// </summary>
1089
1823
  /// <param name="handler">Relevant MessageHandler.</param>
1090
1824
  /// <param name="deregistration">Deregistration action for the handler.</param>
1825
+ /// <param name="priority">Priority at which to add the handler.</param>
1091
1826
  /// <returns>De-registration action to un-register the handler.</returns>
1092
- public Action AddUntargetedHandler(Action<T> handler, Action deregistration)
1827
+ public Action AddUntargetedHandler(
1828
+ Action<T> handler,
1829
+ Action deregistration,
1830
+ int priority
1831
+ )
1093
1832
  {
1094
- return AddHandler(ref _untargetedHandlers, handler, deregistration);
1833
+ return AddHandler(ref _untargetedHandlers, handler, deregistration, priority);
1095
1834
  }
1096
1835
 
1097
1836
  /// <summary>
@@ -1099,11 +1838,15 @@
1099
1838
  /// </summary>
1100
1839
  /// <param name="handler">Relevant MessageHandler.</param>
1101
1840
  /// <param name="deregistration">Deregistration action for the handler.</param>
1841
+ /// <param name="priority">Priority at which to add the handler.</param>
1102
1842
  /// <returns>De-registration action to un-register the handler.</returns>
1103
-
1104
- public Action AddUntargetedHandler(FastHandler<T> handler, Action deregistration)
1843
+ public Action AddUntargetedHandler(
1844
+ FastHandler<T> handler,
1845
+ Action deregistration,
1846
+ int priority
1847
+ )
1105
1848
  {
1106
- return AddHandler(ref _untargetedFastHandlers, handler, deregistration);
1849
+ return AddHandler(ref _untargetedFastHandlers, handler, deregistration, priority);
1107
1850
  }
1108
1851
 
1109
1852
  /// <summary>
@@ -1112,10 +1855,22 @@
1112
1855
  /// <param name="source">Source of the handler is for.</param>
1113
1856
  /// <param name="handler">Relevant MessageHandler.</param>
1114
1857
  /// <param name="deregistration">Deregistration action for the handler.</param>
1858
+ /// <param name="priority">Priority at which to add the handler.</param>
1115
1859
  /// <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);
1860
+ public Action AddSourcedBroadcastHandler(
1861
+ InstanceId source,
1862
+ Action<T> handler,
1863
+ Action deregistration,
1864
+ int priority
1865
+ )
1866
+ {
1867
+ return AddHandler(
1868
+ source,
1869
+ ref _broadcastHandlers,
1870
+ handler,
1871
+ deregistration,
1872
+ priority
1873
+ );
1119
1874
  }
1120
1875
 
1121
1876
  /// <summary>
@@ -1124,10 +1879,22 @@
1124
1879
  /// <param name="source">Source of the handler is for.</param>
1125
1880
  /// <param name="handler">Relevant MessageHandler.</param>
1126
1881
  /// <param name="deregistration">Deregistration action for the handler.</param>
1882
+ /// <param name="priority">Priority at which to add the handler.</param>
1127
1883
  /// <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);
1884
+ public Action AddSourcedBroadcastHandler(
1885
+ InstanceId source,
1886
+ FastHandler<T> handler,
1887
+ Action deregistration,
1888
+ int priority
1889
+ )
1890
+ {
1891
+ return AddHandler(
1892
+ source,
1893
+ ref _broadcastFastHandlers,
1894
+ handler,
1895
+ deregistration,
1896
+ priority
1897
+ );
1131
1898
  }
1132
1899
 
1133
1900
  /// <summary>
@@ -1135,10 +1902,20 @@
1135
1902
  /// </summary>
1136
1903
  /// <param name="handler">Relevant MessageHandler.</param>
1137
1904
  /// <param name="deregistration">Deregistration action for the handler.</param>
1905
+ /// <param name="priority">Priority at which to add the handler.</param>
1138
1906
  /// <returns>De-registration action to un-register the handler.</returns>
1139
- public Action AddSourcedBroadcastWithoutSourceHandler(Action<InstanceId, T> handler, Action deregistration)
1907
+ public Action AddSourcedBroadcastWithoutSourceHandler(
1908
+ Action<InstanceId, T> handler,
1909
+ Action deregistration,
1910
+ int priority
1911
+ )
1140
1912
  {
1141
- return AddHandler(ref _broadcastWithoutSourceHandlers, handler, deregistration);
1913
+ return AddHandler(
1914
+ ref _broadcastWithoutSourceHandlers,
1915
+ handler,
1916
+ deregistration,
1917
+ priority
1918
+ );
1142
1919
  }
1143
1920
 
1144
1921
  /// <summary>
@@ -1146,10 +1923,20 @@
1146
1923
  /// </summary>
1147
1924
  /// <param name="handler">Relevant MessageHandler.</param>
1148
1925
  /// <param name="deregistration">Deregistration action for the handler.</param>
1926
+ /// <param name="priority">Priority at which to add the handler.</param>
1149
1927
  /// <returns>De-registration action to un-register the handler.</returns>
1150
- public Action AddSourcedBroadcastWithoutSourceHandler(FastHandlerWithContext<T> handler, Action deregistration)
1928
+ public Action AddSourcedBroadcastWithoutSourceHandler(
1929
+ FastHandlerWithContext<T> handler,
1930
+ Action deregistration,
1931
+ int priority
1932
+ )
1151
1933
  {
1152
- return AddHandler(ref _fastBroadcastWithoutSourceHandlers, handler, deregistration);
1934
+ return AddHandler(
1935
+ ref _fastBroadcastWithoutSourceHandlers,
1936
+ handler,
1937
+ deregistration,
1938
+ priority
1939
+ );
1153
1940
  }
1154
1941
 
1155
1942
  /// <summary>
@@ -1158,7 +1945,10 @@
1158
1945
  /// <param name="handler">Relevant MessageHandler.</param>
1159
1946
  /// <param name="deregistration">Deregistration action for the handler.</param>
1160
1947
  /// <returns>De-registration action to un-register the handler.</returns>
1161
- public Action AddGlobalUntargetedHandler(Action<IUntargetedMessage> handler, Action deregistration)
1948
+ public Action AddGlobalUntargetedHandler(
1949
+ Action<IUntargetedMessage> handler,
1950
+ Action deregistration
1951
+ )
1162
1952
  {
1163
1953
  return AddHandler(ref _globalUntargetedHandlers, handler, deregistration);
1164
1954
  }
@@ -1170,7 +1960,10 @@
1170
1960
  /// <param name="deregistration">Deregistration action for the handler.</param>
1171
1961
  /// <returns>De-registration action to un-register the handler.</returns>
1172
1962
 
1173
- public Action AddGlobalUntargetedHandler(FastHandler<IUntargetedMessage> handler, Action deregistration)
1963
+ public Action AddGlobalUntargetedHandler(
1964
+ FastHandler<IUntargetedMessage> handler,
1965
+ Action deregistration
1966
+ )
1174
1967
  {
1175
1968
  return AddHandler(ref _globalUntargetedFastHandlers, handler, deregistration);
1176
1969
  }
@@ -1181,7 +1974,10 @@
1181
1974
  /// <param name="handler">Relevant MessageHandler.</param>
1182
1975
  /// <param name="deregistration">Deregistration action for the handler.</param>
1183
1976
  /// <returns>De-registration action to un-register the handler.</returns>
1184
- public Action AddGlobalTargetedHandler(Action<InstanceId, ITargetedMessage> handler, Action deregistration)
1977
+ public Action AddGlobalTargetedHandler(
1978
+ Action<InstanceId, ITargetedMessage> handler,
1979
+ Action deregistration
1980
+ )
1185
1981
  {
1186
1982
  return AddHandler(ref _globalTargetedHandlers, handler, deregistration);
1187
1983
  }
@@ -1193,7 +1989,10 @@
1193
1989
  /// <param name="deregistration">Deregistration action for the handler.</param>
1194
1990
  /// <returns>De-registration action to un-register the handler.</returns>
1195
1991
 
1196
- public Action AddGlobalTargetedHandler(FastHandlerWithContext<ITargetedMessage> handler, Action deregistration)
1992
+ public Action AddGlobalTargetedHandler(
1993
+ FastHandlerWithContext<ITargetedMessage> handler,
1994
+ Action deregistration
1995
+ )
1197
1996
  {
1198
1997
  return AddHandler(ref _globalTargetedFastHandlers, handler, deregistration);
1199
1998
  }
@@ -1204,7 +2003,10 @@
1204
2003
  /// <param name="handler">Relevant MessageHandler.</param>
1205
2004
  /// <param name="deregistration">Deregistration action for the handler.</param>
1206
2005
  /// <returns>De-registration action to un-register the handler.</returns>
1207
- public Action AddGlobalBroadcastHandler(Action<InstanceId, IBroadcastMessage> handler, Action deregistration)
2006
+ public Action AddGlobalBroadcastHandler(
2007
+ Action<InstanceId, IBroadcastMessage> handler,
2008
+ Action deregistration
2009
+ )
1208
2010
  {
1209
2011
  return AddHandler(ref _globalBroadcastHandlers, handler, deregistration);
1210
2012
  }
@@ -1215,7 +2017,10 @@
1215
2017
  /// <param name="handler">Relevant MessageHandler.</param>
1216
2018
  /// <param name="deregistration">Deregistration action for the handler.</param>
1217
2019
  /// <returns>De-registration action to un-register the handler.</returns>
1218
- public Action AddGlobalBroadcastHandler(FastHandlerWithContext<IBroadcastMessage> handler, Action deregistration)
2020
+ public Action AddGlobalBroadcastHandler(
2021
+ FastHandlerWithContext<IBroadcastMessage> handler,
2022
+ Action deregistration
2023
+ )
1219
2024
  {
1220
2025
  return AddHandler(ref _globalBroadcastFastHandlers, handler, deregistration);
1221
2026
  }
@@ -1225,10 +2030,20 @@
1225
2030
  /// </summary>
1226
2031
  /// <param name="handler">Relevant MessageHandler.</param>
1227
2032
  /// <param name="deregistration">Deregistration action for the handler.</param>
2033
+ /// <param name="priority">Priority at which to add the handler.</param>
1228
2034
  /// <returns>De-registration action to un-register the handler.</returns>
1229
- public Action AddUntargetedPostProcessor(Action<T> handler, Action deregistration)
2035
+ public Action AddUntargetedPostProcessor(
2036
+ Action<T> handler,
2037
+ Action deregistration,
2038
+ int priority
2039
+ )
1230
2040
  {
1231
- return AddHandler(ref _untargetedPostProcessingHandlers, handler, deregistration);
2041
+ return AddHandler(
2042
+ ref _untargetedPostProcessingHandlers,
2043
+ handler,
2044
+ deregistration,
2045
+ priority
2046
+ );
1232
2047
  }
1233
2048
 
1234
2049
  /// <summary>
@@ -1236,10 +2051,20 @@
1236
2051
  /// </summary>
1237
2052
  /// <param name="handler">Relevant MessageHandler.</param>
1238
2053
  /// <param name="deregistration">Deregistration action for the handler.</param>
2054
+ /// <param name="priority">Priority at which to add the handler.</param>
1239
2055
  /// <returns>De-registration action to un-register the handler.</returns>
1240
- public Action AddUntargetedPostProcessor(FastHandler<T> handler, Action deregistration)
2056
+ public Action AddUntargetedPostProcessor(
2057
+ FastHandler<T> handler,
2058
+ Action deregistration,
2059
+ int priority
2060
+ )
1241
2061
  {
1242
- return AddHandler(ref _untargetedPostProcessingFastHandlers, handler, deregistration);
2062
+ return AddHandler(
2063
+ ref _untargetedPostProcessingFastHandlers,
2064
+ handler,
2065
+ deregistration,
2066
+ priority
2067
+ );
1243
2068
  }
1244
2069
 
1245
2070
  /// <summary>
@@ -1248,10 +2073,22 @@
1248
2073
  /// <param name="target">Target the handler is for.</param>
1249
2074
  /// <param name="handler">Relevant MessageHandler.</param>
1250
2075
  /// <param name="deregistration">Deregistration action for the handler.</param>
2076
+ /// <param name="priority">Priority at which to add the handler.</param>
1251
2077
  /// <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);
2078
+ public Action AddTargetedPostProcessor(
2079
+ InstanceId target,
2080
+ Action<T> handler,
2081
+ Action deregistration,
2082
+ int priority
2083
+ )
2084
+ {
2085
+ return AddHandler(
2086
+ target,
2087
+ ref _targetedPostProcessingHandlers,
2088
+ handler,
2089
+ deregistration,
2090
+ priority
2091
+ );
1255
2092
  }
1256
2093
 
1257
2094
  /// <summary>
@@ -1260,10 +2097,22 @@
1260
2097
  /// <param name="target">Target the handler is for.</param>
1261
2098
  /// <param name="handler">Relevant MessageHandler.</param>
1262
2099
  /// <param name="deregistration">Deregistration action for the handler.</param>
2100
+ /// <param name="priority">Priority at which to add the handler.</param>
1263
2101
  /// <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);
2102
+ public Action AddTargetedPostProcessor(
2103
+ InstanceId target,
2104
+ FastHandler<T> handler,
2105
+ Action deregistration,
2106
+ int priority
2107
+ )
2108
+ {
2109
+ return AddHandler(
2110
+ target,
2111
+ ref _targetedPostProcessingFastHandlers,
2112
+ handler,
2113
+ deregistration,
2114
+ priority
2115
+ );
1267
2116
  }
1268
2117
 
1269
2118
  /// <summary>
@@ -1271,10 +2120,20 @@
1271
2120
  /// </summary>
1272
2121
  /// <param name="handler">Relevant MessageHandler.</param>
1273
2122
  /// <param name="deregistration">Deregistration action for the handler.</param>
2123
+ /// <param name="priority">Priority at which to add the handler.</param>
1274
2124
  /// <returns>De-registration action to un-register the handler.</returns>
1275
- public Action AddTargetedWithoutTargetingPostProcessor(Action<InstanceId, T> handler, Action deregistration)
2125
+ public Action AddTargetedWithoutTargetingPostProcessor(
2126
+ Action<InstanceId, T> handler,
2127
+ Action deregistration,
2128
+ int priority
2129
+ )
1276
2130
  {
1277
- return AddHandler(ref _targetedWithoutTargetingPostProcessingHandlers, handler, deregistration);
2131
+ return AddHandler(
2132
+ ref _targetedWithoutTargetingPostProcessingHandlers,
2133
+ handler,
2134
+ deregistration,
2135
+ priority
2136
+ );
1278
2137
  }
1279
2138
 
1280
2139
  /// <summary>
@@ -1282,10 +2141,20 @@
1282
2141
  /// </summary>
1283
2142
  /// <param name="handler">Relevant MessageHandler.</param>
1284
2143
  /// <param name="deregistration">Deregistration action for the handler.</param>
2144
+ /// <param name="priority">Priority at which to add the handler.</param>
1285
2145
  /// <returns>De-registration action to un-register the handler.</returns>
1286
- public Action AddTargetedWithoutTargetingPostProcessor(FastHandlerWithContext<T> handler, Action deregistration)
2146
+ public Action AddTargetedWithoutTargetingPostProcessor(
2147
+ FastHandlerWithContext<T> handler,
2148
+ Action deregistration,
2149
+ int priority
2150
+ )
1287
2151
  {
1288
- return AddHandler(ref _fastTargetedWithoutTargetingPostProcessingHandlers, handler, deregistration);
2152
+ return AddHandler(
2153
+ ref _fastTargetedWithoutTargetingPostProcessingHandlers,
2154
+ handler,
2155
+ deregistration,
2156
+ priority
2157
+ );
1289
2158
  }
1290
2159
 
1291
2160
  /// <summary>
@@ -1294,10 +2163,22 @@
1294
2163
  /// <param name="source">Source the handler is for.</param>
1295
2164
  /// <param name="handler">Relevant MessageHandler.</param>
1296
2165
  /// <param name="deregistration">Deregistration action for the handler.</param>
2166
+ /// <param name="priority">Priority at which to add the handler.</param>
1297
2167
  /// <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);
2168
+ public Action AddBroadcastPostProcessor(
2169
+ InstanceId source,
2170
+ Action<T> handler,
2171
+ Action deregistration,
2172
+ int priority
2173
+ )
2174
+ {
2175
+ return AddHandler(
2176
+ source,
2177
+ ref _broadcastPostProcessingHandlers,
2178
+ handler,
2179
+ deregistration,
2180
+ priority
2181
+ );
1301
2182
  }
1302
2183
 
1303
2184
  /// <summary>
@@ -1306,10 +2187,22 @@
1306
2187
  /// <param name="source">Source the handler is for.</param>
1307
2188
  /// <param name="handler">Relevant MessageHandler.</param>
1308
2189
  /// <param name="deregistration">Deregistration action for the handler.</param>
2190
+ /// <param name="priority">Priority at which to add the handler.</param>
1309
2191
  /// <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);
2192
+ public Action AddBroadcastPostProcessor(
2193
+ InstanceId source,
2194
+ FastHandler<T> handler,
2195
+ Action deregistration,
2196
+ int priority
2197
+ )
2198
+ {
2199
+ return AddHandler(
2200
+ source,
2201
+ ref _broadcastPostProcessingFastHandlers,
2202
+ handler,
2203
+ deregistration,
2204
+ priority
2205
+ );
1313
2206
  }
1314
2207
 
1315
2208
  /// <summary>
@@ -1317,54 +2210,121 @@
1317
2210
  /// </summary>
1318
2211
  /// <param name="handler">Relevant MessageHandler.</param>
1319
2212
  /// <param name="deregistration">Deregistration action for the handler.</param>
2213
+ /// <param name="priority">Priority at which to add the handler.</param>
1320
2214
  /// <returns>De-registration action to un-register the handler.</returns>
1321
- public Action AddBroadcastWithoutSourcePostProcessor(Action<InstanceId, T> handler, Action deregistration)
2215
+ public Action AddBroadcastWithoutSourcePostProcessor(
2216
+ Action<InstanceId, T> handler,
2217
+ Action deregistration,
2218
+ int priority
2219
+ )
1322
2220
  {
1323
- return AddHandler(ref _broadcastWithoutSourcePostProcessingHandlers, handler, deregistration);
2221
+ return AddHandler(
2222
+ ref _broadcastWithoutSourcePostProcessingHandlers,
2223
+ handler,
2224
+ deregistration,
2225
+ priority
2226
+ );
1324
2227
  }
1325
2228
 
1326
2229
  /// <summary>
1327
2230
  /// Adds a fast Broadcast post processor to be called after all other handlers have been called.
1328
2231
  /// </summary>
1329
- /// <param name="source">Source the handler is for.</param>
1330
2232
  /// <param name="handler">Relevant MessageHandler.</param>
1331
2233
  /// <param name="deregistration">Deregistration action for the handler.</param>
2234
+ /// <param name="priority">Priority at which to add the handler.</param>
1332
2235
  /// <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
2236
+ public Action AddBroadcastWithoutSourcePostProcessor(
2237
+ FastHandlerWithContext<T> handler,
2238
+ Action deregistration,
2239
+ int priority
2240
+ )
2241
+ {
2242
+ return AddHandler(
2243
+ ref _fastBroadcastWithoutSourcePostProcessingHandlers,
2244
+ handler,
2245
+ deregistration,
2246
+ priority
2247
+ );
2248
+ }
2249
+
2250
+ private static void RunFastHandlersWithContext<TMessage>(
2251
+ ref InstanceId context,
2252
+ SortedDictionary<
2253
+ int,
2254
+ Dictionary<FastHandlerWithContext<T>, int>
2255
+ > fastHandlersByContext,
2256
+ ref TMessage message,
2257
+ int priority
2258
+ )
2259
+ where TMessage : IMessage
1339
2260
  {
1340
2261
  if (fastHandlersByContext is not { Count: > 0 })
1341
2262
  {
1342
2263
  return;
1343
2264
  }
1344
2265
 
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))
2266
+ RunFastHandlers(
2267
+ ref context,
2268
+ ref FastHandlersWithContextStack,
2269
+ fastHandlersByContext,
2270
+ ref message,
2271
+ priority
2272
+ );
2273
+ }
2274
+
2275
+ private static void RunFastHandlersWithContext<TMessage>(
2276
+ ref InstanceId context,
2277
+ Dictionary<
2278
+ InstanceId,
2279
+ SortedDictionary<int, Dictionary<FastHandler<T>, int>>
2280
+ > fastHandlersByContext,
2281
+ ref TMessage message,
2282
+ int priority
2283
+ )
2284
+ where TMessage : IMessage
2285
+ {
2286
+ if (
2287
+ fastHandlersByContext is not { Count: > 0 }
2288
+ || !fastHandlersByContext.TryGetValue(
2289
+ context,
2290
+ out SortedDictionary<int, Dictionary<FastHandler<T>, int>> fastHandlers
2291
+ )
2292
+ )
1351
2293
  {
1352
2294
  return;
1353
2295
  }
1354
2296
 
1355
- RunFastHandlers(fastHandlers, ref message);
2297
+ RunFastHandlers(fastHandlers, ref message, priority);
1356
2298
  }
1357
2299
 
1358
- private static void RunFastHandlers<TMessage>(Dictionary<FastHandler<T>, int> fastHandlers, ref TMessage message) where TMessage : IMessage
2300
+ private static void RunFastHandlers<TMessage>(
2301
+ SortedDictionary<int, Dictionary<FastHandler<T>, int>> fastHandlers,
2302
+ ref TMessage message,
2303
+ int priority
2304
+ )
2305
+ where TMessage : IMessage
1359
2306
  {
1360
2307
  if (fastHandlers is not { Count: > 0 })
1361
2308
  {
1362
2309
  return;
1363
2310
  }
1364
2311
 
2312
+ if (
2313
+ !fastHandlers.TryGetValue(
2314
+ priority,
2315
+ out Dictionary<FastHandler<T>, int> fastHandlersByPriority
2316
+ )
2317
+ )
2318
+ {
2319
+ return;
2320
+ }
2321
+
1365
2322
  ref T typedMessage = ref Unsafe.As<TMessage, T>(ref message);
1366
2323
 
1367
- List<FastHandler<T>> handlers = GetOrAddNewHandlerStack(ref FastHandlersStack, fastHandlers.Keys);
2324
+ List<FastHandler<T>> handlers = GetOrAddNewHandlerStack(
2325
+ ref FastHandlersStack,
2326
+ fastHandlersByPriority.Keys
2327
+ );
1368
2328
  try
1369
2329
  {
1370
2330
  foreach (FastHandler<T> fastHandler in handlers)
@@ -1378,7 +2338,13 @@
1378
2338
  }
1379
2339
  }
1380
2340
 
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
2341
+ private static void RunFastHandlers<TMessage, U>(
2342
+ Stack<List<FastHandler<U>>> stack,
2343
+ Dictionary<FastHandler<U>, int> fastHandlers,
2344
+ ref TMessage message
2345
+ )
2346
+ where TMessage : IMessage
2347
+ where U : IMessage
1382
2348
  {
1383
2349
  if (fastHandlers is not { Count: > 0 })
1384
2350
  {
@@ -1387,7 +2353,10 @@
1387
2353
 
1388
2354
  ref U typedMessage = ref Unsafe.As<TMessage, U>(ref message);
1389
2355
 
1390
- List<FastHandler<U>> handlers = GetOrAddNewHandlerStack(ref stack, fastHandlers.Keys);
2356
+ List<FastHandler<U>> handlers = GetOrAddNewHandlerStack(
2357
+ ref stack,
2358
+ fastHandlers.Keys
2359
+ );
1391
2360
  try
1392
2361
  {
1393
2362
  foreach (FastHandler<U> fastHandler in handlers)
@@ -1402,23 +2371,69 @@
1402
2371
  }
1403
2372
 
1404
2373
  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);
2374
+ ref InstanceId context,
2375
+ ref Stack<List<FastHandlerWithContext<U>>> stack,
2376
+ Dictionary<FastHandlerWithContext<U>, int> priorityHandlers,
2377
+ ref TMessage message
2378
+ )
2379
+ where TMessage : IMessage
2380
+ where U : IMessage
2381
+ {
2382
+ if (priorityHandlers is not { Count: > 0 })
2383
+ {
2384
+ return;
2385
+ }
2386
+
2387
+ ref U typedMessage = ref Unsafe.As<TMessage, U>(ref message);
2388
+
2389
+ List<FastHandlerWithContext<U>> handlers = GetOrAddNewHandlerStack(
2390
+ ref stack,
2391
+ priorityHandlers.Keys
2392
+ );
2393
+ try
2394
+ {
2395
+ foreach (FastHandlerWithContext<U> fastHandler in handlers)
2396
+ {
2397
+ fastHandler(ref context, ref typedMessage);
2398
+ }
2399
+ }
2400
+ finally
2401
+ {
2402
+ stack.Push(handlers);
2403
+ }
1410
2404
  }
1411
2405
 
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
2406
+ private static void RunFastHandlers<TMessage, U>(
2407
+ ref InstanceId context,
2408
+ ref Stack<List<FastHandlerWithContext<U>>> stack,
2409
+ SortedDictionary<int, Dictionary<FastHandlerWithContext<U>, int>> fastHandlers,
2410
+ ref TMessage message,
2411
+ int priority
2412
+ )
2413
+ where TMessage : IMessage
2414
+ where U : IMessage
1413
2415
  {
1414
2416
  if (fastHandlers is not { Count: > 0 })
1415
2417
  {
1416
2418
  return;
1417
2419
  }
1418
2420
 
2421
+ if (
2422
+ !fastHandlers.TryGetValue(
2423
+ priority,
2424
+ out Dictionary<FastHandlerWithContext<U>, int> priorityHandlers
2425
+ )
2426
+ )
2427
+ {
2428
+ return;
2429
+ }
2430
+
1419
2431
  ref U typedMessage = ref Unsafe.As<TMessage, U>(ref message);
1420
2432
 
1421
- List<FastHandlerWithContext<U>> handlers = GetOrAddNewHandlerStack(ref stack, fastHandlers.Keys);
2433
+ List<FastHandlerWithContext<U>> handlers = GetOrAddNewHandlerStack(
2434
+ ref stack,
2435
+ priorityHandlers.Keys
2436
+ );
1422
2437
  try
1423
2438
  {
1424
2439
  foreach (FastHandlerWithContext<U> fastHandler in handlers)
@@ -1431,25 +2446,53 @@
1431
2446
  stack.Push(handlers);
1432
2447
  }
1433
2448
  }
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))
2449
+
2450
+ private static void RunHandlersWithContext<TMessage>(
2451
+ ref InstanceId context,
2452
+ Dictionary<
2453
+ InstanceId,
2454
+ SortedDictionary<int, Dictionary<Action<T>, int>>
2455
+ > handlersByContext,
2456
+ ref TMessage message,
2457
+ int priority
2458
+ )
2459
+ where TMessage : IMessage
2460
+ {
2461
+ if (
2462
+ handlersByContext is not { Count: > 0 }
2463
+ || !handlersByContext.TryGetValue(
2464
+ context,
2465
+ out SortedDictionary<int, Dictionary<Action<T>, int>> handlers
2466
+ )
2467
+ )
1438
2468
  {
1439
2469
  return;
1440
2470
  }
1441
2471
 
1442
- RunHandlers(handlers, ref message);
2472
+ RunHandlers(handlers, ref message, priority);
1443
2473
  }
1444
2474
 
1445
- private static void RunHandlers<TMessage>(Dictionary<Action<T>, int> handlers, ref TMessage message) where TMessage : IMessage
2475
+ private static void RunHandlers<TMessage>(
2476
+ SortedDictionary<int, Dictionary<Action<T>, int>> sortedHandlers,
2477
+ ref TMessage message,
2478
+ int priority
2479
+ )
2480
+ where TMessage : IMessage
1446
2481
  {
1447
- if (handlers is not { Count: > 0 })
2482
+ if (sortedHandlers is not { Count: > 0 })
1448
2483
  {
1449
2484
  return;
1450
2485
  }
1451
2486
 
1452
- List<Action<T>> typedHandlers = GetOrAddNewHandlerStack(ref HandlersStack, handlers.Keys);
2487
+ if (!sortedHandlers.TryGetValue(priority, out Dictionary<Action<T>, int> handlers))
2488
+ {
2489
+ return;
2490
+ }
2491
+
2492
+ List<Action<T>> typedHandlers = GetOrAddNewHandlerStack(
2493
+ ref HandlersStack,
2494
+ handlers.Keys
2495
+ );
1453
2496
  try
1454
2497
  {
1455
2498
  ref T typedMessage = ref Unsafe.As<TMessage, T>(ref message);
@@ -1465,14 +2508,33 @@
1465
2508
  }
1466
2509
  }
1467
2510
 
1468
- private static void RunHandlers<TMessage>(ref InstanceId context, Dictionary<Action<InstanceId, T>, int> handlers, ref TMessage message) where TMessage : IMessage
2511
+ private static void RunHandlers<TMessage>(
2512
+ ref InstanceId context,
2513
+ SortedDictionary<int, Dictionary<Action<InstanceId, T>, int>> handlers,
2514
+ ref TMessage message,
2515
+ int priority
2516
+ )
2517
+ where TMessage : IMessage
1469
2518
  {
1470
2519
  if (handlers is not { Count: > 0 })
1471
2520
  {
1472
2521
  return;
1473
2522
  }
1474
2523
 
1475
- List<Action<InstanceId, T>> typedHandlers = GetOrAddNewHandlerStack(ref HandlersWithoutContextStack, handlers.Keys);
2524
+ if (
2525
+ !handlers.TryGetValue(
2526
+ priority,
2527
+ out Dictionary<Action<InstanceId, T>, int> handlersByPriority
2528
+ )
2529
+ )
2530
+ {
2531
+ return;
2532
+ }
2533
+
2534
+ List<Action<InstanceId, T>> typedHandlers = GetOrAddNewHandlerStack(
2535
+ ref HandlersWithoutContextStack,
2536
+ handlersByPriority.Keys
2537
+ );
1476
2538
  try
1477
2539
  {
1478
2540
  ref T typedMessage = ref Unsafe.As<TMessage, T>(ref message);
@@ -1488,40 +2550,70 @@
1488
2550
  }
1489
2551
  }
1490
2552
 
1491
- private static List<U> GetOrAddNewHandlerStack<U>(ref Stack<List<U>> stack, IEnumerable<U> handlers)
2553
+ private static List<U> GetOrAddNewHandlerStack<U>(
2554
+ ref Stack<List<U>> stack,
2555
+ IEnumerable<U> handlers
2556
+ )
1492
2557
  {
1493
2558
  stack ??= new Stack<List<U>>();
1494
- if (stack.TryPop(out List<U> typedHandlerStack))
2559
+ if (!stack.TryPop(out List<U> typedHandlerStack))
1495
2560
  {
1496
- typedHandlerStack.Clear();
1497
- typedHandlerStack.AddRange(handlers);
1498
- return typedHandlerStack;
2561
+ return new List<U>(handlers);
1499
2562
  }
1500
2563
 
1501
- return new List<U>(handlers);
1502
- }
1503
-
1504
- private static Action AddHandler<U>(InstanceId context, ref Dictionary<InstanceId, Dictionary<U, int>> handlersByContext, U handler, Action deregistration)
1505
- {
1506
- handlersByContext ??= new Dictionary<InstanceId, Dictionary<U, int>>();
1507
-
1508
- if (!handlersByContext.TryGetValue(context, out Dictionary<U, int> handlers))
2564
+ typedHandlerStack.Clear();
2565
+ typedHandlerStack.AddRange(handlers);
2566
+ return typedHandlerStack;
2567
+ }
2568
+
2569
+ private static Action AddHandler<U>(
2570
+ InstanceId context,
2571
+ ref Dictionary<
2572
+ InstanceId,
2573
+ SortedDictionary<int, Dictionary<U, int>>
2574
+ > handlersByContext,
2575
+ U handler,
2576
+ Action deregistration,
2577
+ int priority
2578
+ )
2579
+ {
2580
+ handlersByContext ??=
2581
+ new Dictionary<InstanceId, SortedDictionary<int, Dictionary<U, int>>>();
2582
+
2583
+ if (
2584
+ !handlersByContext.TryGetValue(
2585
+ context,
2586
+ out SortedDictionary<int, Dictionary<U, int>> sortedHandlers
2587
+ )
2588
+ )
1509
2589
  {
1510
- handlers = new Dictionary<U, int>();
1511
- handlersByContext[context] = handlers;
2590
+ sortedHandlers = new SortedDictionary<int, Dictionary<U, int>>();
2591
+ handlersByContext[context] = sortedHandlers;
1512
2592
  }
1513
2593
 
1514
- if (!handlers.TryGetValue(handler, out int count))
2594
+ if (!sortedHandlers.TryGetValue(priority, out Dictionary<U, int> handlers))
1515
2595
  {
1516
- count = 0;
2596
+ handlers = new Dictionary<U, int>();
2597
+ sortedHandlers[priority] = handlers;
1517
2598
  }
1518
2599
 
2600
+ int count = handlers.GetValueOrDefault(handler, 0);
2601
+
1519
2602
  handlers[handler] = count + 1;
1520
2603
 
1521
- Dictionary<InstanceId, Dictionary<U, int>> localHandlersByContext = handlersByContext;
2604
+ Dictionary<
2605
+ InstanceId,
2606
+ SortedDictionary<int, Dictionary<U, int>>
2607
+ > localHandlersByContext = handlersByContext;
2608
+
1522
2609
  return () =>
1523
2610
  {
1524
- if (!localHandlersByContext.TryGetValue(context, out handlers))
2611
+ if (!localHandlersByContext.TryGetValue(context, out sortedHandlers))
2612
+ {
2613
+ return;
2614
+ }
2615
+
2616
+ if (!sortedHandlers.TryGetValue(priority, out handlers))
1525
2617
  {
1526
2618
  return;
1527
2619
  }
@@ -1549,22 +2641,62 @@
1549
2641
  };
1550
2642
  }
1551
2643
 
1552
- private static Action AddHandler<U>(ref Dictionary<U, int> handlers, U handler, Action deregistration)
2644
+ private static Action AddHandler<U>(
2645
+ ref Dictionary<U, int> handlersByPriority,
2646
+ U handler,
2647
+ Action deregistration
2648
+ )
1553
2649
  {
1554
- int count;
1555
- if (handlers == null)
2650
+ handlersByPriority ??= new Dictionary<U, int>();
2651
+
2652
+ int count = handlersByPriority.GetValueOrDefault(handler, 0);
2653
+
2654
+ handlersByPriority[handler] = count + 1;
2655
+
2656
+ Dictionary<U, int> localHandlers = handlersByPriority;
2657
+
2658
+ return () =>
1556
2659
  {
1557
- handlers = new Dictionary<U, int>();
1558
- count = 0;
1559
- }
1560
- else if (!handlers.TryGetValue(handler, out count))
2660
+ if (!localHandlers.TryGetValue(handler, out count))
2661
+ {
2662
+ return;
2663
+ }
2664
+
2665
+ // Always invoke deregistration action, as MessageBus dedupes this as well
2666
+ deregistration?.Invoke();
2667
+ if (count <= 1)
2668
+ {
2669
+ _ = localHandlers.Remove(handler);
2670
+ return;
2671
+ }
2672
+
2673
+ localHandlers[handler] = count - 1;
2674
+ };
2675
+ }
2676
+
2677
+ private static Action AddHandler<U>(
2678
+ ref SortedDictionary<int, Dictionary<U, int>> sortedHandlers,
2679
+ U handler,
2680
+ Action deregistration,
2681
+ int priority
2682
+ )
2683
+ {
2684
+ sortedHandlers ??= new SortedDictionary<int, Dictionary<U, int>>();
2685
+
2686
+ if (
2687
+ !sortedHandlers.TryGetValue(priority, out Dictionary<U, int> handlersByPriority)
2688
+ )
1561
2689
  {
1562
- count = 0;
2690
+ handlersByPriority = new Dictionary<U, int>();
2691
+ sortedHandlers[priority] = handlersByPriority;
1563
2692
  }
1564
2693
 
1565
- handlers[handler] = count + 1;
2694
+ int count = handlersByPriority.GetValueOrDefault(handler, 0);
2695
+
2696
+ handlersByPriority[handler] = count + 1;
1566
2697
 
1567
- Dictionary<U, int> localHandlers = handlers;
2698
+ SortedDictionary<int, Dictionary<U, int>> localSortedHandlers = sortedHandlers;
2699
+ Dictionary<U, int> localHandlers = handlersByPriority;
1568
2700
 
1569
2701
  return () =>
1570
2702
  {
@@ -1578,6 +2710,12 @@
1578
2710
  if (count <= 1)
1579
2711
  {
1580
2712
  _ = localHandlers.Remove(handler);
2713
+
2714
+ if (localHandlers.Count <= 0)
2715
+ {
2716
+ _ = localSortedHandlers.Remove(priority);
2717
+ }
2718
+
1581
2719
  return;
1582
2720
  }
1583
2721