com.wallstop-studios.dxmessaging 2.0.0-rc21 → 2.0.0-rc22

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.
@@ -3,6 +3,8 @@
3
3
  using System;
4
4
  using System.Collections.Generic;
5
5
  using System.Reflection;
6
+ using System.Threading;
7
+ using Helper;
6
8
  using Messages;
7
9
  using static IMessageBus;
8
10
 
@@ -34,13 +36,15 @@
34
36
  int count = 0;
35
37
  foreach (var entry in _targetedSinks)
36
38
  {
37
- count += entry.Value.Count;
39
+ count += entry.Count;
38
40
  }
39
41
 
40
42
  return count;
41
43
  }
42
44
  }
43
45
 
46
+ public int RegisteredGlobalSequentialIndex { get; private set; }
47
+
44
48
  public int RegisteredBroadcast
45
49
  {
46
50
  get
@@ -48,7 +52,7 @@
48
52
  int count = 0;
49
53
  foreach (var entry in _broadcastSinks)
50
54
  {
51
- count += entry.Value.Count;
55
+ count += entry.Count;
52
56
  }
53
57
 
54
58
  return count;
@@ -62,7 +66,7 @@
62
66
  int count = 0;
63
67
  foreach (var entry in _sinks)
64
68
  {
65
- count += entry.Value.handlers.Count;
69
+ count += entry.handlers.Count;
66
70
  }
67
71
 
68
72
  return count;
@@ -86,36 +90,28 @@
86
90
 
87
91
  public RegistrationLog Log => _log;
88
92
 
89
- private readonly Dictionary<Type, HandlerCache<int, HandlerCache>> _sinks = new();
90
- private readonly Dictionary<
91
- Type,
93
+ private readonly MessageCache<HandlerCache<int, HandlerCache>> _sinks = new();
94
+ private readonly MessageCache<
92
95
  Dictionary<InstanceId, HandlerCache<int, HandlerCache>>
93
96
  > _targetedSinks = new();
94
- private readonly Dictionary<
95
- Type,
97
+ private readonly MessageCache<
96
98
  Dictionary<InstanceId, HandlerCache<int, HandlerCache>>
97
99
  > _broadcastSinks = new();
98
- private readonly Dictionary<Type, HandlerCache<int, HandlerCache>> _postProcessingSinks =
99
- new();
100
- private readonly Dictionary<
101
- Type,
100
+ private readonly MessageCache<HandlerCache<int, HandlerCache>> _postProcessingSinks = new();
101
+ private readonly MessageCache<
102
102
  Dictionary<InstanceId, HandlerCache<int, HandlerCache>>
103
103
  > _postProcessingTargetedSinks = new();
104
- private readonly Dictionary<
105
- Type,
104
+ private readonly MessageCache<
106
105
  Dictionary<InstanceId, HandlerCache<int, HandlerCache>>
107
106
  > _postProcessingBroadcastSinks = new();
108
- private readonly Dictionary<
109
- Type,
107
+ private readonly MessageCache<
110
108
  HandlerCache<int, HandlerCache>
111
109
  > _postProcessingTargetedWithoutTargetingSinks = new();
112
- private readonly Dictionary<
113
- Type,
110
+ private readonly MessageCache<
114
111
  HandlerCache<int, HandlerCache>
115
112
  > _postProcessingBroadcastWithoutSourceSinks = new();
116
113
  private readonly HandlerCache _globalSinks = new();
117
- private readonly Dictionary<Type, HandlerCache<int, List<object>>> _interceptsByType =
118
- new();
114
+ private readonly MessageCache<HandlerCache<int, List<object>>> _interceptsByType = new();
119
115
  private readonly Dictionary<object, Dictionary<int, int>> _uniqueInterceptorsAndPriorities =
120
116
  new();
121
117
 
@@ -124,6 +120,19 @@
124
120
 
125
121
  private readonly RegistrationLog _log = new();
126
122
 
123
+ static MessageBus()
124
+ {
125
+ if (!DxMessagingRuntime.Initialized)
126
+ {
127
+ DxMessagingRuntime.Initialize();
128
+ }
129
+ }
130
+
131
+ public MessageBus()
132
+ {
133
+ RegisteredGlobalSequentialIndex = Interlocked.Increment(ref GlobalSequentialIndex);
134
+ }
135
+
127
136
  public Action RegisterUntargeted<T>(MessageHandler messageHandler, int priority = 0)
128
137
  where T : IUntargetedMessage
129
138
  {
@@ -352,16 +361,8 @@
352
361
  where T : IMessage
353
362
  {
354
363
  Type type = typeof(T);
355
- if (
356
- !_interceptsByType.TryGetValue(
357
- type,
358
- out HandlerCache<int, List<object>> prioritizedInterceptors
359
- )
360
- )
361
- {
362
- prioritizedInterceptors = new HandlerCache<int, List<object>>();
363
- _interceptsByType[type] = prioritizedInterceptors;
364
- }
364
+ HandlerCache<int, List<object>> prioritizedInterceptors =
365
+ _interceptsByType.GetOrAdd<T>();
365
366
 
366
367
  if (
367
368
  !prioritizedInterceptors.handlers.TryGetValue(
@@ -446,7 +447,7 @@
446
447
  bool complete = false;
447
448
  if (removed)
448
449
  {
449
- if (_interceptsByType.TryGetValue(type, out prioritizedInterceptors))
450
+ if (_interceptsByType.TryGetValue<T>(out prioritizedInterceptors))
450
451
  {
451
452
  prioritizedInterceptors.version++;
452
453
  if (
@@ -499,8 +500,7 @@
499
500
  public void UntargetedBroadcast<TMessage>(ref TMessage typedMessage)
500
501
  where TMessage : IUntargetedMessage
501
502
  {
502
- Type type = typeof(TMessage);
503
- if (!RunUntargetedInterceptors(type, ref typedMessage))
503
+ if (!RunUntargetedInterceptors(ref typedMessage))
504
504
  {
505
505
  return;
506
506
  }
@@ -511,11 +511,10 @@
511
511
  BroadcastGlobalUntargeted(ref untargetedMessage);
512
512
  }
513
513
 
514
- bool foundAnyHandlers = InternalUntargetedBroadcast(ref typedMessage, type);
514
+ bool foundAnyHandlers = InternalUntargetedBroadcast(ref typedMessage);
515
515
 
516
516
  if (
517
- _postProcessingSinks.TryGetValue(
518
- type,
517
+ _postProcessingSinks.TryGetValue<TMessage>(
519
518
  out HandlerCache<int, HandlerCache> sortedHandlers
520
519
  )
521
520
  && 0 < sortedHandlers.handlers.Count
@@ -697,8 +696,7 @@
697
696
  public void TargetedBroadcast<TMessage>(ref InstanceId target, ref TMessage typedMessage)
698
697
  where TMessage : ITargetedMessage
699
698
  {
700
- Type type = typeof(TMessage);
701
- if (!RunTargetedInterceptors(type, ref typedMessage, ref target))
699
+ if (!RunTargetedInterceptors(ref typedMessage, ref target))
702
700
  {
703
701
  return;
704
702
  }
@@ -711,8 +709,7 @@
711
709
 
712
710
  bool foundAnyHandlers = false;
713
711
  if (
714
- _targetedSinks.TryGetValue(
715
- type,
712
+ _targetedSinks.TryGetValue<TMessage>(
716
713
  out Dictionary<InstanceId, HandlerCache<int, HandlerCache>> targetedHandlers
717
714
  )
718
715
  && targetedHandlers.TryGetValue(
@@ -795,10 +792,10 @@
795
792
  }
796
793
  }
797
794
 
798
- _ = InternalTargetedWithoutTargetingBroadcast(ref target, ref typedMessage, type);
795
+ _ = InternalTargetedWithoutTargetingBroadcast(ref target, ref typedMessage);
799
796
 
800
797
  if (
801
- _postProcessingTargetedSinks.TryGetValue(type, out targetedHandlers)
798
+ _postProcessingTargetedSinks.TryGetValue<TMessage>(out targetedHandlers)
802
799
  && targetedHandlers.TryGetValue(target, out sortedHandlers)
803
800
  && 0 < sortedHandlers.handlers.Count
804
801
  )
@@ -952,7 +949,9 @@
952
949
  }
953
950
 
954
951
  if (
955
- _postProcessingTargetedWithoutTargetingSinks.TryGetValue(type, out sortedHandlers)
952
+ _postProcessingTargetedWithoutTargetingSinks.TryGetValue<TMessage>(
953
+ out sortedHandlers
954
+ )
956
955
  && 0 < sortedHandlers.handlers.Count
957
956
  )
958
957
  {
@@ -1435,8 +1434,7 @@
1435
1434
  public void SourcedBroadcast<TMessage>(ref InstanceId source, ref TMessage typedMessage)
1436
1435
  where TMessage : IBroadcastMessage
1437
1436
  {
1438
- Type type = typeof(TMessage);
1439
- if (!RunBroadcastInterceptors(type, ref typedMessage, ref source))
1437
+ if (!RunBroadcastInterceptors(ref typedMessage, ref source))
1440
1438
  {
1441
1439
  return;
1442
1440
  }
@@ -1449,8 +1447,7 @@
1449
1447
 
1450
1448
  bool foundAnyHandlers = false;
1451
1449
  if (
1452
- _broadcastSinks.TryGetValue(
1453
- type,
1450
+ _broadcastSinks.TryGetValue<TMessage>(
1454
1451
  out Dictionary<InstanceId, HandlerCache<int, HandlerCache>> broadcastHandlers
1455
1452
  )
1456
1453
  && broadcastHandlers.TryGetValue(
@@ -1528,10 +1525,10 @@
1528
1525
  }
1529
1526
  }
1530
1527
 
1531
- _ = InternalBroadcastWithoutSource(ref source, ref typedMessage, type);
1528
+ _ = InternalBroadcastWithoutSource(ref source, ref typedMessage);
1532
1529
 
1533
1530
  if (
1534
- _postProcessingBroadcastSinks.TryGetValue(type, out broadcastHandlers)
1531
+ _postProcessingBroadcastSinks.TryGetValue<TMessage>(out broadcastHandlers)
1535
1532
  && broadcastHandlers.TryGetValue(source, out sortedHandlers)
1536
1533
  && 0 < sortedHandlers.handlers.Count
1537
1534
  )
@@ -1685,7 +1682,7 @@
1685
1682
  }
1686
1683
 
1687
1684
  if (
1688
- _postProcessingBroadcastWithoutSourceSinks.TryGetValue(type, out sortedHandlers)
1685
+ _postProcessingBroadcastWithoutSourceSinks.TryGetValue<TMessage>(out sortedHandlers)
1689
1686
  && 0 < sortedHandlers.handlers.Count
1690
1687
  )
1691
1688
  {
@@ -2138,15 +2135,14 @@
2138
2135
  }
2139
2136
  }
2140
2137
 
2141
- private bool TryGetInterceptorCaches(
2142
- Type type,
2138
+ private bool TryGetInterceptorCaches<TMessage>(
2143
2139
  out List<KeyValuePair<int, List<object>>> interceptorStack,
2144
2140
  out List<object> interceptorObjects
2145
2141
  )
2142
+ where TMessage : IMessage
2146
2143
  {
2147
2144
  if (
2148
- !_interceptsByType.TryGetValue(
2149
- type,
2145
+ !_interceptsByType.TryGetValue<TMessage>(
2150
2146
  out HandlerCache<int, List<object>> interceptors
2151
2147
  )
2152
2148
  || interceptors.handlers.Count == 0
@@ -2179,12 +2175,11 @@
2179
2175
  return true;
2180
2176
  }
2181
2177
 
2182
- private bool RunUntargetedInterceptors<T>(Type type, ref T message)
2178
+ private bool RunUntargetedInterceptors<T>(ref T message)
2183
2179
  where T : IUntargetedMessage
2184
2180
  {
2185
2181
  if (
2186
- !TryGetInterceptorCaches(
2187
- type,
2182
+ !TryGetInterceptorCaches<T>(
2188
2183
  out List<KeyValuePair<int, List<object>>> interceptorStack,
2189
2184
  out List<object> interceptorObjects
2190
2185
  )
@@ -2225,12 +2220,11 @@
2225
2220
  return true;
2226
2221
  }
2227
2222
 
2228
- private bool RunTargetedInterceptors<T>(Type type, ref T message, ref InstanceId target)
2223
+ private bool RunTargetedInterceptors<T>(ref T message, ref InstanceId target)
2229
2224
  where T : ITargetedMessage
2230
2225
  {
2231
2226
  if (
2232
- !TryGetInterceptorCaches(
2233
- type,
2227
+ !TryGetInterceptorCaches<T>(
2234
2228
  out List<KeyValuePair<int, List<object>>> interceptorStack,
2235
2229
  out List<object> interceptorObjects
2236
2230
  )
@@ -2271,12 +2265,11 @@
2271
2265
  return true;
2272
2266
  }
2273
2267
 
2274
- private bool RunBroadcastInterceptors<T>(Type type, ref T message, ref InstanceId source)
2268
+ private bool RunBroadcastInterceptors<T>(ref T message, ref InstanceId source)
2275
2269
  where T : IBroadcastMessage
2276
2270
  {
2277
2271
  if (
2278
- !TryGetInterceptorCaches(
2279
- type,
2272
+ !TryGetInterceptorCaches<T>(
2280
2273
  out List<KeyValuePair<int, List<object>>> interceptorStack,
2281
2274
  out List<object> interceptorObjects
2282
2275
  )
@@ -2317,11 +2310,11 @@
2317
2310
  return true;
2318
2311
  }
2319
2312
 
2320
- private bool InternalUntargetedBroadcast<TMessage>(ref TMessage message, Type type)
2313
+ private bool InternalUntargetedBroadcast<TMessage>(ref TMessage message)
2321
2314
  where TMessage : IMessage
2322
2315
  {
2323
2316
  if (
2324
- !_sinks.TryGetValue(type, out HandlerCache<int, HandlerCache> sortedHandlers)
2317
+ !_sinks.TryGetValue<TMessage>(out HandlerCache<int, HandlerCache> sortedHandlers)
2325
2318
  || sortedHandlers.handlers.Count == 0
2326
2319
  )
2327
2320
  {
@@ -2452,13 +2445,12 @@
2452
2445
 
2453
2446
  private bool InternalTargetedWithoutTargetingBroadcast<TMessage>(
2454
2447
  ref InstanceId target,
2455
- ref TMessage message,
2456
- Type type
2448
+ ref TMessage message
2457
2449
  )
2458
2450
  where TMessage : ITargetedMessage
2459
2451
  {
2460
2452
  if (
2461
- !_sinks.TryGetValue(type, out HandlerCache<int, HandlerCache> sortedHandlers)
2453
+ !_sinks.TryGetValue<TMessage>(out HandlerCache<int, HandlerCache> sortedHandlers)
2462
2454
  || sortedHandlers.handlers.Count == 0
2463
2455
  )
2464
2456
  {
@@ -2607,13 +2599,12 @@
2607
2599
 
2608
2600
  private bool InternalBroadcastWithoutSource<TMessage>(
2609
2601
  ref InstanceId source,
2610
- ref TMessage message,
2611
- Type type
2602
+ ref TMessage message
2612
2603
  )
2613
2604
  where TMessage : IBroadcastMessage
2614
2605
  {
2615
2606
  if (
2616
- !_sinks.TryGetValue(type, out HandlerCache<int, HandlerCache> sortedHandlers)
2607
+ !_sinks.TryGetValue<TMessage>(out HandlerCache<int, HandlerCache> sortedHandlers)
2617
2608
  || sortedHandlers.handlers.Count == 0
2618
2609
  )
2619
2610
  {
@@ -2841,7 +2832,7 @@
2841
2832
 
2842
2833
  private Action InternalRegisterUntargeted<T>(
2843
2834
  MessageHandler messageHandler,
2844
- Dictionary<Type, HandlerCache<int, HandlerCache>> sinks,
2835
+ MessageCache<HandlerCache<int, HandlerCache>> sinks,
2845
2836
  RegistrationMethod registrationMethod,
2846
2837
  int priority
2847
2838
  )
@@ -2855,11 +2846,7 @@
2855
2846
  InstanceId handlerOwnerId = messageHandler.owner;
2856
2847
  Type type = typeof(T);
2857
2848
 
2858
- if (!sinks.TryGetValue(type, out HandlerCache<int, HandlerCache> handlers))
2859
- {
2860
- handlers = new HandlerCache<int, HandlerCache>();
2861
- sinks[type] = handlers;
2862
- }
2849
+ HandlerCache<int, HandlerCache> handlers = sinks.GetOrAdd<T>();
2863
2850
 
2864
2851
  if (!handlers.handlers.TryGetValue(priority, out HandlerCache cache))
2865
2852
  {
@@ -2894,7 +2881,7 @@
2894
2881
  )
2895
2882
  );
2896
2883
  if (
2897
- !sinks.TryGetValue(type, out handlers)
2884
+ !sinks.TryGetValue<T>(out handlers)
2898
2885
  || !handlers.handlers.TryGetValue(priority, out cache)
2899
2886
  || !cache.handlers.TryGetValue(messageHandler, out count)
2900
2887
  )
@@ -2925,7 +2912,7 @@
2925
2912
 
2926
2913
  if (handlers.handlers.Count == 0)
2927
2914
  {
2928
- _ = sinks.Remove(type);
2915
+ sinks.Remove<T>();
2929
2916
  }
2930
2917
 
2931
2918
  if (!complete && MessagingDebug.enabled)
@@ -2948,10 +2935,11 @@
2948
2935
  private Action InternalRegisterWithContext<T>(
2949
2936
  InstanceId context,
2950
2937
  MessageHandler messageHandler,
2951
- Dictionary<Type, Dictionary<InstanceId, HandlerCache<int, HandlerCache>>> sinks,
2938
+ MessageCache<Dictionary<InstanceId, HandlerCache<int, HandlerCache>>> sinks,
2952
2939
  RegistrationMethod registrationMethod,
2953
2940
  int priority
2954
2941
  )
2942
+ where T : IMessage
2955
2943
  {
2956
2944
  if (messageHandler == null)
2957
2945
  {
@@ -2959,16 +2947,8 @@
2959
2947
  }
2960
2948
 
2961
2949
  Type type = typeof(T);
2962
- if (
2963
- !sinks.TryGetValue(
2964
- type,
2965
- out Dictionary<InstanceId, HandlerCache<int, HandlerCache>> broadcastHandlers
2966
- )
2967
- )
2968
- {
2969
- broadcastHandlers = new Dictionary<InstanceId, HandlerCache<int, HandlerCache>>();
2970
- sinks[type] = broadcastHandlers;
2971
- }
2950
+ Dictionary<InstanceId, HandlerCache<int, HandlerCache>> broadcastHandlers =
2951
+ sinks.GetOrAdd<T>();
2972
2952
 
2973
2953
  if (
2974
2954
  !broadcastHandlers.TryGetValue(
@@ -3014,7 +2994,7 @@
3014
2994
  )
3015
2995
  );
3016
2996
  if (
3017
- !sinks.TryGetValue(type, out broadcastHandlers)
2997
+ !sinks.TryGetValue<T>(out broadcastHandlers)
3018
2998
  || !broadcastHandlers.TryGetValue(context, out handlers)
3019
2999
  || !handlers.handlers.TryGetValue(priority, out cache)
3020
3000
  || !cache.handlers.TryGetValue(messageHandler, out count)
@@ -3050,7 +3030,7 @@
3050
3030
 
3051
3031
  if (broadcastHandlers.Count == 0)
3052
3032
  {
3053
- _ = sinks.Remove(type);
3033
+ sinks.Remove<T>();
3054
3034
  }
3055
3035
 
3056
3036
  if (!complete && MessagingDebug.enabled)