com.wallstop-studios.dxmessaging 2.0.0-rc20 → 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.
@@ -2,8 +2,8 @@
2
2
  {
3
3
  using System;
4
4
  using System.Collections.Generic;
5
- using System.Linq;
6
5
  using System.Runtime.CompilerServices;
6
+ using Helper;
7
7
  using MessageBus;
8
8
  using Messages;
9
9
 
@@ -18,6 +18,7 @@
18
18
  {
19
19
  public delegate void FastHandler<TMessage>(ref TMessage message)
20
20
  where TMessage : IMessage;
21
+
21
22
  public delegate void FastHandlerWithContext<TMessage>(
22
23
  ref InstanceId context,
23
24
  ref TMessage message
@@ -29,17 +30,6 @@
29
30
  /// </summary>
30
31
  public static readonly MessageBus.MessageBus MessageBus = new();
31
32
 
32
- /// <summary>
33
- /// Maps Types to the corresponding Handler of that type.
34
- /// </summary>
35
- /// <note>
36
- /// Ideally, this would be something like a Dictionary[T, Handler[T]], but that can't be done with C#s type system.
37
- /// </note>
38
- private readonly Dictionary<
39
- IMessageBus,
40
- Dictionary<Type, object>
41
- > _handlersByTypeByMessageBus;
42
-
43
33
  /// <summary>
44
34
  /// Whether this MessageHandler will process messages.
45
35
  /// </summary>
@@ -50,10 +40,26 @@
50
40
  /// </summary>
51
41
  public readonly InstanceId owner;
52
42
 
43
+ /// <summary>
44
+ /// Maps Types to the corresponding Handler of that type.
45
+ /// </summary>
46
+ /// <note>
47
+ /// Ideally, this would be something like a Dictionary[T, Handler[T]], but that can't be done with C#s type system.
48
+ /// </note>
49
+ private readonly List<MessageCache<object>> _handlersByTypeByMessageBus;
50
+
51
+ static MessageHandler()
52
+ {
53
+ if (!DxMessagingRuntime.Initialized)
54
+ {
55
+ DxMessagingRuntime.Initialize();
56
+ }
57
+ }
58
+
53
59
  public MessageHandler(InstanceId owner)
54
60
  {
55
61
  this.owner = owner;
56
- _handlersByTypeByMessageBus = new Dictionary<IMessageBus, Dictionary<Type, object>>();
62
+ _handlersByTypeByMessageBus = new List<MessageCache<object>>();
57
63
  }
58
64
 
59
65
  /// <summary>
@@ -77,13 +83,7 @@
77
83
  return;
78
84
  }
79
85
 
80
- if (
81
- GetHandlerForType(
82
- message.MessageType,
83
- messageBus,
84
- out TypedHandler<TMessage> handler
85
- )
86
- )
86
+ if (GetHandlerForType(messageBus, out TypedHandler<TMessage> handler))
87
87
  {
88
88
  handler.HandleUntargeted(ref message, priority);
89
89
  }
@@ -110,13 +110,7 @@
110
110
  return;
111
111
  }
112
112
 
113
- if (
114
- GetHandlerForType(
115
- message.MessageType,
116
- messageBus,
117
- out TypedHandler<TMessage> handler
118
- )
119
- )
113
+ if (GetHandlerForType(messageBus, out TypedHandler<TMessage> handler))
120
114
  {
121
115
  handler.HandleUntargetedPostProcessing(ref message, priority);
122
116
  }
@@ -145,13 +139,7 @@
145
139
  return;
146
140
  }
147
141
 
148
- if (
149
- GetHandlerForType(
150
- message.MessageType,
151
- messageBus,
152
- out TypedHandler<TMessage> handler
153
- )
154
- )
142
+ if (GetHandlerForType(messageBus, out TypedHandler<TMessage> handler))
155
143
  {
156
144
  handler.HandleTargeted(ref target, ref message, priority);
157
145
  }
@@ -180,13 +168,7 @@
180
168
  return;
181
169
  }
182
170
 
183
- if (
184
- GetHandlerForType(
185
- message.MessageType,
186
- messageBus,
187
- out TypedHandler<TMessage> handler
188
- )
189
- )
171
+ if (GetHandlerForType(messageBus, out TypedHandler<TMessage> handler))
190
172
  {
191
173
  handler.HandleTargetedWithoutTargeting(ref target, ref message, priority);
192
174
  }
@@ -215,13 +197,7 @@
215
197
  return;
216
198
  }
217
199
 
218
- if (
219
- GetHandlerForType(
220
- message.MessageType,
221
- messageBus,
222
- out TypedHandler<TMessage> handler
223
- )
224
- )
200
+ if (GetHandlerForType(messageBus, out TypedHandler<TMessage> handler))
225
201
  {
226
202
  handler.HandleTargetedPostProcessing(ref target, ref message, priority);
227
203
  }
@@ -250,13 +226,7 @@
250
226
  return;
251
227
  }
252
228
 
253
- if (
254
- GetHandlerForType(
255
- message.MessageType,
256
- messageBus,
257
- out TypedHandler<TMessage> handler
258
- )
259
- )
229
+ if (GetHandlerForType(messageBus, out TypedHandler<TMessage> handler))
260
230
  {
261
231
  handler.HandleTargetedWithoutTargetingPostProcessing(
262
232
  ref target,
@@ -289,13 +259,7 @@
289
259
  return;
290
260
  }
291
261
 
292
- if (
293
- GetHandlerForType(
294
- message.MessageType,
295
- messageBus,
296
- out TypedHandler<TMessage> handler
297
- )
298
- )
262
+ if (GetHandlerForType(messageBus, out TypedHandler<TMessage> handler))
299
263
  {
300
264
  handler.HandleSourcedBroadcast(ref source, ref message, priority);
301
265
  }
@@ -324,13 +288,7 @@
324
288
  return;
325
289
  }
326
290
 
327
- if (
328
- GetHandlerForType(
329
- message.MessageType,
330
- messageBus,
331
- out TypedHandler<TMessage> handler
332
- )
333
- )
291
+ if (GetHandlerForType(messageBus, out TypedHandler<TMessage> handler))
334
292
  {
335
293
  handler.HandleSourcedBroadcastWithoutSource(ref source, ref message, priority);
336
294
  }
@@ -359,13 +317,7 @@
359
317
  return;
360
318
  }
361
319
 
362
- if (
363
- GetHandlerForType(
364
- message.MessageType,
365
- messageBus,
366
- out TypedHandler<TMessage> handler
367
- )
368
- )
320
+ if (GetHandlerForType(messageBus, out TypedHandler<TMessage> handler))
369
321
  {
370
322
  handler.HandleSourcedBroadcastPostProcessing(ref source, ref message, priority);
371
323
  }
@@ -394,13 +346,7 @@
394
346
  return;
395
347
  }
396
348
 
397
- if (
398
- GetHandlerForType(
399
- message.MessageType,
400
- messageBus,
401
- out TypedHandler<TMessage> handler
402
- )
403
- )
349
+ if (GetHandlerForType(messageBus, out TypedHandler<TMessage> handler))
404
350
  {
405
351
  handler.HandleBroadcastWithoutSourcePostProcessing(
406
352
  ref source,
@@ -426,7 +372,7 @@
426
372
  }
427
373
 
428
374
  // Use the "IMessage" explicitly to indicate global messages, allowing us to multipurpose a single dictionary
429
- if (GetHandlerForType(typeof(IMessage), messageBus, out TypedHandler<IMessage> handler))
375
+ if (GetHandlerForType(messageBus, out TypedHandler<IMessage> handler))
430
376
  {
431
377
  handler.HandleGlobalUntargeted(ref message);
432
378
  }
@@ -450,7 +396,7 @@
450
396
  }
451
397
 
452
398
  // Use the "IMessage" explicitly to indicate global messages, allowing us to multipurpose a single dictionary
453
- if (GetHandlerForType(typeof(IMessage), messageBus, out TypedHandler<IMessage> handler))
399
+ if (GetHandlerForType(messageBus, out TypedHandler<IMessage> handler))
454
400
  {
455
401
  handler.HandleGlobalTargeted(ref target, ref message);
456
402
  }
@@ -474,7 +420,7 @@
474
420
  }
475
421
 
476
422
  // Use the "IMessage" explicitly to indicate global messages, allowing us to multipurpose a single dictionary
477
- if (GetHandlerForType(typeof(IMessage), messageBus, out TypedHandler<IMessage> handler))
423
+ if (GetHandlerForType(messageBus, out TypedHandler<IMessage> handler))
478
424
  {
479
425
  handler.HandleGlobalBroadcast(ref source, ref message);
480
426
  }
@@ -1265,18 +1211,7 @@
1265
1211
 
1266
1212
  public override string ToString()
1267
1213
  {
1268
- return new
1269
- {
1270
- OwnerId = owner,
1271
- HandlerTypes = string.Join(
1272
- ",",
1273
- _handlersByTypeByMessageBus
1274
- .Values.SelectMany(handlers => handlers.Keys)
1275
- .Distinct()
1276
- .Select(type => type.Name)
1277
- .OrderBy(x => x)
1278
- ),
1279
- }.ToString();
1214
+ return new { OwnerId = owner }.ToString();
1280
1215
  }
1281
1216
 
1282
1217
  /// <summary>
@@ -1287,48 +1222,45 @@
1287
1222
  private TypedHandler<T> GetOrCreateHandlerForType<T>(IMessageBus messageBus)
1288
1223
  where T : IMessage
1289
1224
  {
1290
- Type type = typeof(T);
1291
-
1292
- if (
1293
- !_handlersByTypeByMessageBus.TryGetValue(
1294
- messageBus,
1295
- out Dictionary<Type, object> handlersByType
1296
- )
1297
- )
1225
+ int messageBusIndex = messageBus.RegisteredGlobalSequentialIndex;
1226
+ while (_handlersByTypeByMessageBus.Count <= messageBusIndex)
1298
1227
  {
1299
- handlersByType = new Dictionary<Type, object>();
1300
- _handlersByTypeByMessageBus[messageBus] = handlersByType;
1228
+ _handlersByTypeByMessageBus.Add(new MessageCache<object>());
1301
1229
  }
1302
1230
 
1303
- if (handlersByType.TryGetValue(type, out object existingTypedHandler))
1231
+ MessageCache<object> handlersByType = _handlersByTypeByMessageBus[messageBusIndex];
1232
+ if (handlersByType.TryGetValue<T>(out object untypedHandler))
1304
1233
  {
1305
- return (TypedHandler<T>)existingTypedHandler;
1234
+ return (TypedHandler<T>)untypedHandler;
1306
1235
  }
1307
1236
 
1308
- TypedHandler<T> newTypedHandler = new();
1309
- handlersByType[type] = newTypedHandler;
1310
- return newTypedHandler;
1237
+ TypedHandler<T> typedHandler = new();
1238
+ handlersByType.Set<T>(typedHandler);
1239
+ return typedHandler;
1311
1240
  }
1312
1241
 
1313
1242
  /// <summary>
1314
1243
  /// Gets an existing Handler for the specific type if it exists.
1315
1244
  /// </summary>
1316
- /// <param name="type">Message type to get the handler for.</param>
1317
1245
  /// <param name="messageBus">The specific MessageBus to use.</param>
1318
1246
  /// <param name="existingTypedHandler">Existing typed message handler, if one exists.</param>
1319
1247
  /// <returns>Existing handler for the specific type, or null if none exists.</returns>
1320
1248
  private bool GetHandlerForType<T>(
1321
- Type type,
1322
1249
  IMessageBus messageBus,
1323
1250
  out TypedHandler<T> existingTypedHandler
1324
1251
  )
1325
1252
  where T : IMessage
1326
1253
  {
1254
+ int messageBusIndex = messageBus.RegisteredGlobalSequentialIndex;
1255
+ if (_handlersByTypeByMessageBus.Count <= messageBusIndex)
1256
+ {
1257
+ existingTypedHandler = default;
1258
+ return false;
1259
+ }
1260
+
1327
1261
  if (
1328
- _handlersByTypeByMessageBus.TryGetValue(
1329
- messageBus,
1330
- out Dictionary<Type, object> handlersByType
1331
- ) && handlersByType.TryGetValue(type, out object untypedHandler)
1262
+ _handlersByTypeByMessageBus[messageBusIndex]
1263
+ .TryGetValue<T>(out object untypedHandler)
1332
1264
  )
1333
1265
  {
1334
1266
  existingTypedHandler = (TypedHandler<T>)untypedHandler;