com.wallstop-studios.dxmessaging 1.0.0-rc10

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.
Files changed (96) hide show
  1. package/.gitattributes +63 -0
  2. package/CHANGELOG.md +0 -0
  3. package/CHANGELOG.md.meta +7 -0
  4. package/Editor.meta +8 -0
  5. package/LICENSE.md +7 -0
  6. package/LICENSE.md.meta +7 -0
  7. package/README.md +76 -0
  8. package/README.md.meta +7 -0
  9. package/Runtime/Core/Extensions/MessageExtensions.cs +275 -0
  10. package/Runtime/Core/Extensions/MessageExtensions.cs.meta +11 -0
  11. package/Runtime/Core/Extensions.meta +8 -0
  12. package/Runtime/Core/IMessage.cs +12 -0
  13. package/Runtime/Core/IMessage.cs.meta +11 -0
  14. package/Runtime/Core/InstanceId.cs +117 -0
  15. package/Runtime/Core/InstanceId.cs.meta +11 -0
  16. package/Runtime/Core/MessageBus/IMessageBus.cs +249 -0
  17. package/Runtime/Core/MessageBus/IMessageBus.cs.meta +11 -0
  18. package/Runtime/Core/MessageBus/MessageBus.cs +961 -0
  19. package/Runtime/Core/MessageBus/MessageBus.cs.meta +11 -0
  20. package/Runtime/Core/MessageBus/MessagingRegistration.cs +100 -0
  21. package/Runtime/Core/MessageBus/MessagingRegistration.cs.meta +11 -0
  22. package/Runtime/Core/MessageBus/RegistrationLog.cs +111 -0
  23. package/Runtime/Core/MessageBus/RegistrationLog.cs.meta +11 -0
  24. package/Runtime/Core/MessageBus.meta +8 -0
  25. package/Runtime/Core/MessageHandler.cs +1589 -0
  26. package/Runtime/Core/MessageHandler.cs.meta +11 -0
  27. package/Runtime/Core/MessageRegistrationHandle.cs +55 -0
  28. package/Runtime/Core/MessageRegistrationHandle.cs.meta +11 -0
  29. package/Runtime/Core/MessageRegistrationToken.cs +885 -0
  30. package/Runtime/Core/MessageRegistrationToken.cs.meta +11 -0
  31. package/Runtime/Core/Messages/IBroadcastMessage.cs +27 -0
  32. package/Runtime/Core/Messages/IBroadcastMessage.cs.meta +11 -0
  33. package/Runtime/Core/Messages/ITargetedMessage.cs +26 -0
  34. package/Runtime/Core/Messages/ITargetedMessage.cs.meta +11 -0
  35. package/Runtime/Core/Messages/IUntargetedMessage.cs +26 -0
  36. package/Runtime/Core/Messages/IUntargetedMessage.cs.meta +11 -0
  37. package/Runtime/Core/Messages.meta +8 -0
  38. package/Runtime/Core/MessagingDebug.cs +71 -0
  39. package/Runtime/Core/MessagingDebug.cs.meta +11 -0
  40. package/Runtime/Core.meta +8 -0
  41. package/Runtime/Unity/MessageAwareComponent.cs +77 -0
  42. package/Runtime/Unity/MessageAwareComponent.cs.meta +11 -0
  43. package/Runtime/Unity/MessagingComponent.cs +73 -0
  44. package/Runtime/Unity/MessagingComponent.cs.meta +11 -0
  45. package/Runtime/Unity.meta +8 -0
  46. package/Runtime/WallstopStudios.DxMessaging.asmdef +18 -0
  47. package/Runtime/WallstopStudios.DxMessaging.asmdef.meta +7 -0
  48. package/Runtime.meta +8 -0
  49. package/Tests/Editor/WallstopStudios.DxMessaging.Tests.Editor.asmdef +23 -0
  50. package/Tests/Editor/WallstopStudios.DxMessaging.Tests.Editor.asmdef.meta +7 -0
  51. package/Tests/Editor.meta +8 -0
  52. package/Tests/Runtime/Benchmarks/PerformanceTests.cs +196 -0
  53. package/Tests/Runtime/Benchmarks/PerformanceTests.cs.meta +11 -0
  54. package/Tests/Runtime/Benchmarks.meta +8 -0
  55. package/Tests/Runtime/Core/BroadcastTests.cs +600 -0
  56. package/Tests/Runtime/Core/BroadcastTests.cs.meta +11 -0
  57. package/Tests/Runtime/Core/GlobalAcceptAllTests.cs +169 -0
  58. package/Tests/Runtime/Core/GlobalAcceptAllTests.cs.meta +11 -0
  59. package/Tests/Runtime/Core/MessagingTestBase.cs +164 -0
  60. package/Tests/Runtime/Core/MessagingTestBase.cs.meta +11 -0
  61. package/Tests/Runtime/Core/NominalTests.cs +1468 -0
  62. package/Tests/Runtime/Core/NominalTests.cs.meta +11 -0
  63. package/Tests/Runtime/Core/PostProcessorTests.cs +849 -0
  64. package/Tests/Runtime/Core/PostProcessorTests.cs.meta +11 -0
  65. package/Tests/Runtime/Core/RegistrationTests.cs +1045 -0
  66. package/Tests/Runtime/Core/RegistrationTests.cs.meta +11 -0
  67. package/Tests/Runtime/Core/TargetedTests.cs +596 -0
  68. package/Tests/Runtime/Core/TargetedTests.cs.meta +11 -0
  69. package/Tests/Runtime/Core/UntargetedTests.cs +149 -0
  70. package/Tests/Runtime/Core/UntargetedTests.cs.meta +11 -0
  71. package/Tests/Runtime/Core.meta +8 -0
  72. package/Tests/Runtime/Scripts/Components/EmptyMessageAwareComponent.cs +9 -0
  73. package/Tests/Runtime/Scripts/Components/EmptyMessageAwareComponent.cs.meta +11 -0
  74. package/Tests/Runtime/Scripts/Components/SimpleMessageAwareComponent.cs +140 -0
  75. package/Tests/Runtime/Scripts/Components/SimpleMessageAwareComponent.cs.meta +11 -0
  76. package/Tests/Runtime/Scripts/Components.meta +8 -0
  77. package/Tests/Runtime/Scripts/Messages/ComplexTargetedMessage.cs +28 -0
  78. package/Tests/Runtime/Scripts/Messages/ComplexTargetedMessage.cs.meta +11 -0
  79. package/Tests/Runtime/Scripts/Messages/SimpleBroadcastMessage.cs +8 -0
  80. package/Tests/Runtime/Scripts/Messages/SimpleBroadcastMessage.cs.meta +11 -0
  81. package/Tests/Runtime/Scripts/Messages/SimpleTargetedMessage.cs +8 -0
  82. package/Tests/Runtime/Scripts/Messages/SimpleTargetedMessage.cs.meta +11 -0
  83. package/Tests/Runtime/Scripts/Messages/SimpleUntargetedMessage.cs +8 -0
  84. package/Tests/Runtime/Scripts/Messages/SimpleUntargetedMessage.cs.meta +11 -0
  85. package/Tests/Runtime/Scripts/Messages.meta +8 -0
  86. package/Tests/Runtime/Scripts.meta +8 -0
  87. package/Tests/Runtime/WallstopStudios.DxMessaging.Tests.Runtime.asmdef +22 -0
  88. package/Tests/Runtime/WallstopStudios.DxMessaging.Tests.Runtime.asmdef.meta +7 -0
  89. package/Tests/Runtime/WallstopStudios.DxMessaging.Tests.Runtime.csproj +1 -0
  90. package/Tests/Runtime/WallstopStudios.DxMessaging.Tests.Runtime.csproj.meta +7 -0
  91. package/Tests/Runtime.meta +8 -0
  92. package/Tests.meta +8 -0
  93. package/Third Party Notices.md +1 -0
  94. package/Third Party Notices.md.meta +7 -0
  95. package/package.json +36 -0
  96. package/package.json.meta +7 -0
@@ -0,0 +1,885 @@
1
+ namespace DxMessaging.Core
2
+ {
3
+ using System;
4
+ using System.Collections.Generic;
5
+ using MessageBus;
6
+ using Messages;
7
+
8
+ /// <summary>
9
+ /// Maintains all of the [de]registration logic for MessagingComponents. Wraps registrations up for lazy registration, which are executed on Enable() call.
10
+ /// </summary>
11
+ /// <note>
12
+ /// General usage should be to create one of these on awake or start (probably start), and bind all messaging functions there.
13
+ /// Then, on OnEnable(), call .Enable(), OnDisable(), call .Disable()
14
+ /// </note>
15
+ public sealed class MessageRegistrationToken
16
+ {
17
+ public bool Enabled => _enabled;
18
+
19
+ private readonly MessageHandler _messageHandler;
20
+
21
+ private readonly Dictionary<MessageRegistrationHandle, Action> _registrations = new();
22
+ private readonly Dictionary<MessageRegistrationHandle, Action> _deregistrations = new();
23
+
24
+ private readonly IMessageBus _messageBus;
25
+ private bool _enabled;
26
+
27
+ private MessageRegistrationToken(MessageHandler messageHandler, IMessageBus messageBus)
28
+ {
29
+ _enabled = false;
30
+ _messageHandler = messageHandler ?? throw new ArgumentNullException(nameof(messageHandler));
31
+ _messageBus = messageBus;
32
+ }
33
+
34
+ private MessageRegistrationHandle RegisterTargetedInternal<T>(InstanceId target, Action<T> targetedHandler)
35
+ where T : ITargetedMessage
36
+ {
37
+ if (_messageHandler == null) // Unity has a bug
38
+ {
39
+ return MessageRegistrationHandle.CreateMessageRegistrationHandle();
40
+ }
41
+ return InternalRegister(targetedHandler, () => _messageHandler.RegisterTargetedMessageHandler(target, targetedHandler, _messageBus));
42
+ }
43
+
44
+ private MessageRegistrationHandle RegisterTargetedInternal<T>(InstanceId target, MessageHandler.FastHandler<T> targetedHandler) where T : ITargetedMessage
45
+ {
46
+ if (_messageHandler == null) // Unity has a bug
47
+ {
48
+ return MessageRegistrationHandle.CreateMessageRegistrationHandle();
49
+ }
50
+ return InternalRegister(targetedHandler, () => _messageHandler.RegisterTargetedMessageHandler(target, targetedHandler, _messageBus));
51
+ }
52
+
53
+ #if UNITY_2017_1_OR_NEWER
54
+ /// <summary>
55
+ /// Stages a registration of the provided MessageHandler to accept TargetedMessages of the given type targeted towards the provided target.
56
+ /// </summary>
57
+ /// <note>
58
+ /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
59
+ /// </note>
60
+ /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
61
+ /// <param name="target">Target of the TargetedMessages to consume.</param>
62
+ /// <param name="targetedHandler">Actual handler functionality.</param>
63
+ /// <returns>A handle that allows for registration and de-registration.</returns>
64
+ public MessageRegistrationHandle RegisterGameObjectTargeted<T>(UnityEngine.GameObject target, Action<T> targetedHandler)
65
+ where T : ITargetedMessage
66
+ {
67
+ return RegisterTargetedInternal(target, targetedHandler);
68
+ }
69
+
70
+ /// <summary>
71
+ /// Stages a registration of the provided MessageHandler to accept TargetedMessages of the given type targeted towards the provided target.
72
+ /// </summary>
73
+ /// <note>
74
+ /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
75
+ /// </note>
76
+ /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
77
+ /// <param name="target">Target of the TargetedMessages to consume.</param>
78
+ /// <param name="targetedHandler">Actual handler functionality.</param>
79
+ /// <returns>A handle that allows for registration and de-registration.</returns>
80
+ public MessageRegistrationHandle RegisterGameObjectTargeted<T>(UnityEngine.GameObject target, MessageHandler.FastHandler<T> targetedHandler)
81
+ where T : ITargetedMessage
82
+ {
83
+ return RegisterTargetedInternal(target, targetedHandler);
84
+ }
85
+
86
+ /// <summary>
87
+ /// Stages a registration of the provided MessageHandler to accept TargetedMessages of the given type targeted towards the provided target.
88
+ /// </summary>
89
+ /// <note>
90
+ /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
91
+ /// </note>
92
+ /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
93
+ /// <param name="target">Target of the TargetedMessages to consume.</param>
94
+ /// <param name="targetedHandler">Actual handler functionality.</param>
95
+ /// <returns>A handle that allows for registration and de-registration.</returns>
96
+ public MessageRegistrationHandle RegisterComponentTargeted<T>(UnityEngine.Component target, Action<T> targetedHandler)
97
+ where T : ITargetedMessage
98
+ {
99
+ return RegisterTargetedInternal(target, targetedHandler);
100
+ }
101
+
102
+ /// <summary>
103
+ /// Stages a registration of the provided MessageHandler to accept TargetedMessages of the given type targeted towards the provided target.
104
+ /// </summary>
105
+ /// <note>
106
+ /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
107
+ /// </note>
108
+ /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
109
+ /// <param name="target">Target of the TargetedMessages to consume.</param>
110
+ /// <param name="targetedHandler">Actual handler functionality.</param>
111
+ /// <returns>A handle that allows for registration and de-registration.</returns>
112
+ public MessageRegistrationHandle RegisterComponentTargeted<T>(UnityEngine.Component target, MessageHandler.FastHandler<T> targetedHandler)
113
+ where T : ITargetedMessage
114
+ {
115
+ return RegisterTargetedInternal(target, targetedHandler);
116
+ }
117
+
118
+ /// <summary>
119
+ /// Stages a registration of the provided PostProcessor to post process TargetedMessages of the given type for the provided target.
120
+ /// </summary>
121
+ /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
122
+ /// <param name="target">Target to post process messages for.</param>
123
+ /// <param name="targetedPostProcessor">Actual post processor functionality.</param>
124
+ /// <returns>A handle that allows for registration and de-registration.</returns>
125
+ public MessageRegistrationHandle RegisterGameObjectTargetedPostProcessor<T>(
126
+ UnityEngine.GameObject target, MessageHandler.FastHandler<T> targetedPostProcessor) where T : ITargetedMessage
127
+ {
128
+ return InternalRegister(targetedPostProcessor, () => _messageHandler.RegisterTargetedPostProcessor(target, targetedPostProcessor, _messageBus));
129
+ }
130
+
131
+ /// <summary>
132
+ /// Stages a registration of the provided PostProcessor to post process TargetedMessages of the given type for the provided target.
133
+ /// </summary>
134
+ /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
135
+ /// <param name="target">Target to post process messages for.</param>
136
+ /// <param name="targetedPostProcessor">Actual post processor functionality.</param>
137
+ /// <returns>A handle that allows for registration and de-registration.</returns>
138
+ public MessageRegistrationHandle RegisterComponentTargetedPostProcessor<T>(
139
+ UnityEngine.Component target, MessageHandler.FastHandler<T> targetedPostProcessor) where T : ITargetedMessage
140
+ {
141
+ return InternalRegister(targetedPostProcessor, () => _messageHandler.RegisterTargetedPostProcessor(target, targetedPostProcessor, _messageBus));
142
+ }
143
+ #else
144
+ /// <summary>
145
+ /// Stages a registration of the provided MessageHandler to accept TargetedMessages of the given type targeted towards the provided target.
146
+ /// </summary>
147
+ /// <note>
148
+ /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
149
+ /// </note>
150
+ /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
151
+ /// <param name="target">Target of the TargetedMessages to consume.</param>
152
+ /// <param name="targetedHandler">Actual handler functionality.</param>
153
+ /// <returns>A handle that allows for registration and de-registration.</returns>
154
+ public MessageRegistrationHandle RegisterTargeted<T>(InstanceId target, Action<T> targetedHandler)
155
+ where T : ITargetedMessage
156
+ {
157
+ return RegisterTargetedInternal(target, targetedHandler);
158
+ }
159
+
160
+ /// <summary>
161
+ /// Stages a registration of the provided MessageHandler to accept TargetedMessages of the given type targeted towards the provided target.
162
+ /// </summary>
163
+ /// <note>
164
+ /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
165
+ /// </note>
166
+ /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
167
+ /// <param name="target">Target of the TargetedMessages to consume.</param>
168
+ /// <param name="targetedHandler">Actual handler functionality.</param>
169
+ /// <returns>A handle that allows for registration and de-registration.</returns>
170
+ public MessageRegistrationHandle RegisterTargeted<T>(InstanceId target, MessageHandler.FastHandler<T> targetedHandler)
171
+ where T : ITargetedMessage
172
+ {
173
+ return RegisterTargetedInternal(target, targetedHandler);
174
+ }
175
+
176
+ /// <summary>
177
+ /// Stages a registration of the provided PostProcessor to post process TargetedMessages of the given type for the provided target.
178
+ /// </summary>
179
+ /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
180
+ /// <param name="target">Target to post process messages for.</param>
181
+ /// <param name="targetedPostProcessor">Actual post processor functionality.</param>
182
+ /// <returns>A handle that allows for registration and de-registration.</returns>
183
+ public MessageRegistrationHandle RegisterTargetedPostProcessor<T>(
184
+ InstanceId target, MessageHandler.FastHandler<T> targetedPostProcessor) where T : ITargetedMessage
185
+ {
186
+ return InternalRegister(targetedPostProcessor, () => _messageHandler.RegisterTargetedPostProcessor(target, targetedPostProcessor, _messageBus));
187
+ }
188
+
189
+ /// <summary>
190
+ /// Stages a registration of the provided PostProcessor to post process TargetedMessages of the given type for the provided target.
191
+ /// </summary>
192
+ /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
193
+ /// <param name="target">Target to post process messages for.</param>
194
+ /// <param name="targetedPostProcessor">Actual post processor functionality.</param>
195
+ /// <returns>A handle that allows for registration and de-registration.</returns>
196
+ public MessageRegistrationHandle RegisterTargetedPostProcessor<T>(
197
+ InstanceId target, Action<T> targetedPostProcessor) where T : ITargetedMessage
198
+ {
199
+ return InternalRegister(targetedPostProcessor, () => _messageHandler.RegisterTargetedPostProcessor(target, targetedPostProcessor, _messageBus));
200
+ }
201
+ #endif
202
+
203
+ /// <summary>
204
+ /// Stages a registration of the provided MessageHandler to accept TargetedMessages of the given type targeted towards anything (including itself).
205
+ /// </summary>
206
+ /// <note>
207
+ /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
208
+ /// </note>
209
+ /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
210
+ /// <param name="messageHandler">Actual handler functionality.</param>
211
+ /// <returns>A handle that allows for registration and de-registration.</returns>
212
+ public MessageRegistrationHandle RegisterTargetedWithoutTargeting<T>(Action<InstanceId, T> messageHandler) where T : ITargetedMessage
213
+ {
214
+ if (_messageHandler == null) // Unity has a bug
215
+ {
216
+ return MessageRegistrationHandle.CreateMessageRegistrationHandle();
217
+ }
218
+ return InternalRegister(messageHandler, () => _messageHandler.RegisterTargetedWithoutTargeting(messageHandler, _messageBus));
219
+ }
220
+
221
+ /// <summary>
222
+ /// Stages a registration of the provided MessageHandler to accept TargetedMessages of the given type targeted towards anything (including itself).
223
+ /// </summary>
224
+ /// <note>
225
+ /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
226
+ /// </note>
227
+ /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
228
+ /// <param name="messageHandler">Actual handler functionality.</param>
229
+ /// <returns>A handle that allows for registration and de-registration.</returns>
230
+ public MessageRegistrationHandle RegisterTargetedWithoutTargeting<T>(MessageHandler.FastHandlerWithContext<T> messageHandler) where T : ITargetedMessage
231
+ {
232
+ if (_messageHandler == null) // Unity has a bug
233
+ {
234
+ return MessageRegistrationHandle.CreateMessageRegistrationHandle();
235
+ }
236
+ return InternalRegister(messageHandler, () => _messageHandler.RegisterTargetedWithoutTargeting(messageHandler, _messageBus));
237
+ }
238
+
239
+ /// <summary>
240
+ /// Stages a registration of the provided MessageHandler to post process TargetedMessages of the given type targeted towards anything (including itself).
241
+ /// </summary>
242
+ /// <note>
243
+ /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
244
+ /// </note>
245
+ /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
246
+ /// <param name="postProcessor">Actual handler functionality.</param>
247
+ /// <returns>A handle that allows for registration and de-registration.</returns>
248
+ public MessageRegistrationHandle RegisterTargetedWithoutTargetingPostProcessor<T>(Action<InstanceId, T> postProcessor) where T : ITargetedMessage
249
+ {
250
+ if (_messageHandler == null) // Unity has a bug
251
+ {
252
+ return MessageRegistrationHandle.CreateMessageRegistrationHandle();
253
+ }
254
+ return InternalRegister(postProcessor, () => _messageHandler.RegisterTargetedWithoutTargetingPostProcessor(postProcessor, _messageBus));
255
+ }
256
+
257
+ /// <summary>
258
+ /// Stages a registration of the provided MessageHandler to post process TargetedMessages of the given type targeted towards anything (including itself).
259
+ /// </summary>
260
+ /// <note>
261
+ /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
262
+ /// </note>
263
+ /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
264
+ /// <param name="postProcessor">Actual post processor functionality.</param>
265
+ /// <returns>A handle that allows for registration and de-registration.</returns>
266
+ public MessageRegistrationHandle RegisterTargetedWithoutTargetingPostProcessor<T>(MessageHandler.FastHandlerWithContext<T> postProcessor) where T : ITargetedMessage
267
+ {
268
+ if (_messageHandler == null) // Unity has a bug
269
+ {
270
+ return MessageRegistrationHandle.CreateMessageRegistrationHandle();
271
+ }
272
+ return InternalRegister(postProcessor, () => _messageHandler.RegisterTargetedWithoutTargetingPostProcessor(postProcessor, _messageBus));
273
+ }
274
+
275
+ /// <summary>
276
+ /// Stages a registration of the provided MessageHandler to accept UntargetedMessages of the given type.
277
+ /// </summary>
278
+ /// <note>
279
+ /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
280
+ /// </note>
281
+ /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
282
+ /// <param name="untargetedHandler">Actual handler functionality.</param>
283
+ /// <returns>A handle that allows for registration and de-registration.</returns>
284
+ public MessageRegistrationHandle RegisterUntargeted<T>(Action<T> untargetedHandler) where T : IUntargetedMessage
285
+ {
286
+ if (_messageHandler == null) // Unity has a bug
287
+ {
288
+ return MessageRegistrationHandle.CreateMessageRegistrationHandle();
289
+ }
290
+ return InternalRegister(untargetedHandler, () => _messageHandler.RegisterUntargetedMessageHandler(untargetedHandler, _messageBus));
291
+ }
292
+
293
+ /// <summary>
294
+ /// Stages a registration of the provided MessageHandler to accept UntargetedMessages of the given type.
295
+ /// </summary>
296
+ /// <note>
297
+ /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
298
+ /// </note>
299
+ /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
300
+ /// <param name="untargetedHandler">Actual handler functionality.</param>
301
+ /// <returns>A handle that allows for registration and de-registration.</returns>
302
+ public MessageRegistrationHandle RegisterUntargeted<T>(MessageHandler.FastHandler<T> untargetedHandler) where T : IUntargetedMessage
303
+ {
304
+ if (_messageHandler == null) // Unity has a bug
305
+ {
306
+ return MessageRegistrationHandle.CreateMessageRegistrationHandle();
307
+ }
308
+ return InternalRegister(untargetedHandler, () => _messageHandler.RegisterUntargetedMessageHandler(untargetedHandler, _messageBus));
309
+ }
310
+
311
+ /// <summary>
312
+ /// Stages a registration of the provided PostProcessor to post process UntargetedMessages of the given type.
313
+ /// </summary>
314
+ /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
315
+ /// <param name="untargetedPostProcessor">Actual post processor functionality.</param>
316
+ /// <returns>A handle that allows for registration and de-registration.</returns>
317
+ public MessageRegistrationHandle RegisterUntargetedPostProcessor<T>(MessageHandler.FastHandler<T> untargetedPostProcessor) where T : IUntargetedMessage
318
+ {
319
+ if (_messageHandler == null)
320
+ {
321
+ return MessageRegistrationHandle.CreateMessageRegistrationHandle();
322
+ }
323
+ return InternalRegister(untargetedPostProcessor, () => _messageHandler.RegisterUntargetedPostProcessor(untargetedPostProcessor, _messageBus));
324
+ }
325
+
326
+ private MessageRegistrationHandle RegisterBroadcastInternal<T>(InstanceId source, Action<T> broadcastHandler) where T : IBroadcastMessage
327
+ {
328
+ if (_messageHandler == null) // Unity has a bug
329
+ {
330
+ return MessageRegistrationHandle.CreateMessageRegistrationHandle();
331
+ }
332
+ return InternalRegister(broadcastHandler, () => _messageHandler.RegisterSourcedBroadcastMessageHandler(source, broadcastHandler, _messageBus));
333
+ }
334
+
335
+ private MessageRegistrationHandle RegisterBroadcastInternal<T>(InstanceId source, MessageHandler.FastHandler<T> broadcastHandler) where T : IBroadcastMessage
336
+ {
337
+ if (_messageHandler == null) // Unity has a bug
338
+ {
339
+ return MessageRegistrationHandle.CreateMessageRegistrationHandle();
340
+ }
341
+ return InternalRegister(broadcastHandler, () => _messageHandler.RegisterSourcedBroadcastMessageHandler(source, broadcastHandler, _messageBus));
342
+ }
343
+
344
+ private MessageRegistrationHandle RegisterBroadcastPostProcessorInternal<T>(InstanceId source, Action<T> broadcastPostProcessor)
345
+ where T : IBroadcastMessage
346
+ {
347
+ if (_messageHandler == null)
348
+ {
349
+ return MessageRegistrationHandle.CreateMessageRegistrationHandle();
350
+ }
351
+ return InternalRegister(broadcastPostProcessor, () => _messageHandler.RegisterSourcedBroadcastPostProcessor(source, broadcastPostProcessor, _messageBus));
352
+ }
353
+
354
+ private MessageRegistrationHandle RegisterBroadcastPostProcessorInternal<T>(InstanceId source, MessageHandler.FastHandler<T> broadcastPostProcessor)
355
+ where T : IBroadcastMessage
356
+ {
357
+ if (_messageHandler == null)
358
+ {
359
+ return MessageRegistrationHandle.CreateMessageRegistrationHandle();
360
+ }
361
+ return InternalRegister(broadcastPostProcessor, () => _messageHandler.RegisterSourcedBroadcastPostProcessor(source, broadcastPostProcessor, _messageBus));
362
+ }
363
+
364
+ #if UNITY_2017_1_OR_NEWER
365
+ /// <summary>
366
+ /// Stages a registration of the provided MessageHandler to accept BroadcastMessages of the given type.
367
+ /// </summary>
368
+ /// <note>
369
+ /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
370
+ /// </note>
371
+ /// <typeparam name="T">Type of the message that the handler accepts.</typeparam>
372
+ /// <param name="source">Id of the source for BroadcastMessages to listen for.</param>
373
+ /// <param name="broadcastHandler">Actual handler functionality.</param>
374
+ /// <returns>A handle that allows for registration and de-registration.</returns>
375
+ public MessageRegistrationHandle RegisterGameObjectBroadcast<T>(UnityEngine.GameObject source, Action<T> broadcastHandler) where T : IBroadcastMessage
376
+ {
377
+ return RegisterBroadcastInternal(source, broadcastHandler);
378
+ }
379
+
380
+ /// <summary>
381
+ /// Stages a registration of the provided MessageHandler to accept BroadcastMessages of the given type.
382
+ /// </summary>
383
+ /// <note>
384
+ /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
385
+ /// </note>
386
+ /// <typeparam name="T">Type of the message that the handler accepts.</typeparam>
387
+ /// <param name="source">Id of the source for BroadcastMessages to listen for.</param>
388
+ /// <param name="broadcastHandler">Actual handler functionality.</param>
389
+ /// <returns>A handle that allows for registration and de-registration.</returns>
390
+ public MessageRegistrationHandle RegisterGameObjectBroadcast<T>(UnityEngine.GameObject source, MessageHandler.FastHandler<T> broadcastHandler) where T : IBroadcastMessage
391
+ {
392
+ return RegisterBroadcastInternal(source, broadcastHandler);
393
+ }
394
+
395
+ /// <summary>
396
+ /// Stages a registration of the provided PostProcessor to post process BroadcastMessages of the given type for the given GameObject.
397
+ /// </summary>
398
+ /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
399
+ /// <param name="source">Source of the messages.</param>
400
+ /// <param name="broadcastPostProcesor">Actual post processor logic.</param>
401
+ /// <returns>A handle that allows for registration and de-registration.</returns>
402
+ public MessageRegistrationHandle RegisterGameObjectBroadcastPostProcessor<T>(UnityEngine.GameObject source, Action<T> broadcastPostProcesor) where T : IBroadcastMessage
403
+ {
404
+ return RegisterBroadcastPostProcessorInternal(source, broadcastPostProcesor);
405
+ }
406
+
407
+ /// <summary>
408
+ /// Stages a registration of the provided PostProcessor to post process BroadcastMessages of the given type for the given GameObject.
409
+ /// </summary>
410
+ /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
411
+ /// <param name="source">Source of the messages.</param>
412
+ /// <param name="broadcastPostProcesor">Actual post processor logic.</param>
413
+ /// <returns>A handle that allows for registration and de-registration.</returns>
414
+ public MessageRegistrationHandle RegisterGameObjectBroadcastPostProcessor<T>(UnityEngine.GameObject source, MessageHandler.FastHandler<T> broadcastPostProcesor) where T : IBroadcastMessage
415
+ {
416
+ return RegisterBroadcastPostProcessorInternal(source, broadcastPostProcesor);
417
+ }
418
+
419
+ /// <summary>
420
+ /// Stages a registration of the provided MessageHandler to accept BroadcastMessages of the given type.
421
+ /// </summary>
422
+ /// <note>
423
+ /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
424
+ /// </note>
425
+ /// <typeparam name="T">Type of the message that the handler accepts.</typeparam>
426
+ /// <param name="source">The component source for BroadcastMessages to listen for.</param>
427
+ /// <param name="broadcastHandler">Actual handler functionality.</param>
428
+ /// <returns>A handle that allows for registration and de-registration.</returns>
429
+ public MessageRegistrationHandle RegisterComponentBroadcast<T>(UnityEngine.Component source, Action<T> broadcastHandler) where T : IBroadcastMessage
430
+ {
431
+ return RegisterBroadcastInternal(source, broadcastHandler);
432
+ }
433
+
434
+ /// <summary>
435
+ /// Stages a registration of the provided MessageHandler to accept BroadcastMessages of the given type.
436
+ /// </summary>
437
+ /// <note>
438
+ /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
439
+ /// </note>
440
+ /// <typeparam name="T">Type of the message that the handler accepts.</typeparam>
441
+ /// <param name="source">The component source for BroadcastMessages to listen for.</param>
442
+ /// <param name="broadcastHandler">Actual handler functionality.</param>
443
+ /// <returns>A handle that allows for registration and de-registration.</returns>
444
+ public MessageRegistrationHandle RegisterComponentBroadcast<T>(UnityEngine.Component source, MessageHandler.FastHandler<T> broadcastHandler) where T : IBroadcastMessage
445
+ {
446
+ return RegisterBroadcastInternal(source, broadcastHandler);
447
+ }
448
+
449
+ /// <summary>
450
+ /// Stages a registration of the provided PostProcessor to post process BroadcastMessages of the given type for the given component.
451
+ /// </summary>
452
+ /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
453
+ /// <param name="source">Source of the messages.</param>
454
+ /// <param name="broadcastPostProcesor">Actual post processor logic.</param>
455
+ /// <returns>A handle that allows for registration and de-registration.</returns>
456
+ public MessageRegistrationHandle RegisterComponentBroadcastPostProcessor<T>(UnityEngine.Component source, Action<T> broadcastPostProcesor) where T : IBroadcastMessage
457
+ {
458
+ if (_messageHandler == null)
459
+ {
460
+ return MessageRegistrationHandle.CreateMessageRegistrationHandle();
461
+ }
462
+ return InternalRegister(broadcastPostProcesor, () => _messageHandler.RegisterSourcedBroadcastPostProcessor(source, broadcastPostProcesor, _messageBus));
463
+ }
464
+
465
+ /// <summary>
466
+ /// Stages a registration of the provided PostProcessor to post process BroadcastMessages of the given type for the given component.
467
+ /// </summary>
468
+ /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
469
+ /// <param name="source">Source of the messages.</param>
470
+ /// <param name="broadcastPostProcesor">Actual post processor logic.</param>
471
+ /// <returns>A handle that allows for registration and de-registration.</returns>
472
+ public MessageRegistrationHandle RegisterComponentBroadcastPostProcessor<T>(UnityEngine.Component source, MessageHandler.FastHandler<T> broadcastPostProcesor) where T : IBroadcastMessage
473
+ {
474
+ if (_messageHandler == null)
475
+ {
476
+ return MessageRegistrationHandle.CreateMessageRegistrationHandle();
477
+ }
478
+ return InternalRegister(broadcastPostProcesor, () => _messageHandler.RegisterSourcedBroadcastPostProcessor(source, broadcastPostProcesor, _messageBus));
479
+ }
480
+ #else
481
+ /// <summary>
482
+ /// Stages a registration of the provided MessageHandler to accept BroadcastMessages of the given type.
483
+ /// </summary>
484
+ /// <note>
485
+ /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
486
+ /// </note>
487
+ /// <typeparam name="T">Type of the message that the handler accepts.</typeparam>
488
+ /// <param name="source">Source of the messages.</param>
489
+ /// <param name="broadcastHandler">Actual handler functionality.</param>
490
+ /// <returns>A handle that allows for registration and de-registration.</returns>
491
+ public MessageRegistrationHandle RegisterBroadcast<T>(InstanceId source, Action<T> broadcastHandler) where T : IBroadcastMessage
492
+ {
493
+ return RegisterBroadcastInternal(source, broadcastHandler);
494
+ }
495
+
496
+ /// <summary>
497
+ /// Stages a registration of the provided MessageHandler to accept BroadcastMessages of the given type.
498
+ /// </summary>
499
+ /// <note>
500
+ /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
501
+ /// </note>
502
+ /// <typeparam name="T">Type of the message that the handler accepts.</typeparam>
503
+ /// <param name="source">Source of the messages.</param>
504
+ /// <param name="broadcastHandler">Actual handler functionality.</param>
505
+ /// <returns>A handle that allows for registration and de-registration.</returns>
506
+ public MessageRegistrationHandle RegisterBroadcast<T>(InstanceId source, MessageHandler.FastHandler<T> broadcastHandler) where T : IBroadcastMessage
507
+ {
508
+ return RegisterBroadcastInternal(source, broadcastHandler);
509
+ }
510
+
511
+ /// <summary>
512
+ /// Stages a registration of the provided PostProcessor to post process BroadcastMessages of the given type for the given source.
513
+ /// </summary>
514
+ /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
515
+ /// <param name="source">Source of the messages.</param>
516
+ /// <param name="broadcastPostProcesor">Actual post processor logic.</param>
517
+ /// <returns>A handle that allows for registration and de-registration.</returns>
518
+ public MessageRegistrationHandle RegisterBroadcastPostProcessor<T>(InstanceId source, Action<T> broadcastPostProcesor) where T : IBroadcastMessage
519
+ {
520
+ return RegisterBroadcastPostProcessorInternal(source, broadcastPostProcesor);
521
+ }
522
+
523
+ /// <summary>
524
+ /// Stages a registration of the provided PostProcessor to post process BroadcastMessages of the given type for the given source.
525
+ /// </summary>
526
+ /// <typeparam name="T">Type of message that the handler accepts.</typeparam>
527
+ /// <param name="source">Source of the messages.</param>
528
+ /// <param name="broadcastPostProcesor">Actual post processor logic.</param>
529
+ /// <returns>A handle that allows for registration and de-registration.</returns>
530
+ public MessageRegistrationHandle RegisterBroadcastPostProcessor<T>(InstanceId source, MessageHandler.FastHandler<T> broadcastPostProcesor) where T : IBroadcastMessage
531
+ {
532
+ return RegisterBroadcastPostProcessorInternal(source, broadcastPostProcesor);
533
+ }
534
+ #endif
535
+
536
+ /// <summary>
537
+ /// Stages a registration of the provided MessageHandler to accept BroadcastMessages of the given type.
538
+ /// </summary>
539
+ /// <note>
540
+ /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
541
+ /// </note>
542
+ /// <typeparam name="T">Type of the message that the handler accepts.</typeparam>
543
+ /// <param name="broadcastHandler">Action handler functionality.</param>
544
+ /// <returns>A handle that allows for registration and de-registration.</returns>
545
+ public MessageRegistrationHandle RegisterBroadcastWithoutSource<T>(Action<InstanceId, T> broadcastHandler)
546
+ where T : IBroadcastMessage
547
+ {
548
+ if (_messageHandler == null) // Unity has a bug
549
+ {
550
+ return MessageRegistrationHandle.CreateMessageRegistrationHandle();
551
+ }
552
+
553
+ return InternalRegister(broadcastHandler, () => _messageHandler.RegisterSourcedBroadcastWithoutSource(broadcastHandler, _messageBus));
554
+ }
555
+
556
+ /// <summary>
557
+ /// Stages a registration of the provided MessageHandler to accept BroadcastMessages of the given type.
558
+ /// </summary>
559
+ /// <note>
560
+ /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
561
+ /// </note>
562
+ /// <typeparam name="T">Type of the message that the handler accepts.</typeparam>
563
+ /// <param name="broadcastHandler">Action handler functionality.</param>
564
+ /// <returns>A handle that allows for registration and de-registration.</returns>
565
+ public MessageRegistrationHandle RegisterBroadcastWithoutSource<T>(MessageHandler.FastHandlerWithContext<T> broadcastHandler)
566
+ where T : IBroadcastMessage
567
+ {
568
+ if (_messageHandler == null) // Unity has a bug
569
+ {
570
+ return MessageRegistrationHandle.CreateMessageRegistrationHandle();
571
+ }
572
+
573
+ return InternalRegister(broadcastHandler, () => _messageHandler.RegisterSourcedBroadcastWithoutSource(broadcastHandler, _messageBus));
574
+ }
575
+
576
+ /// <summary>
577
+ /// Stages a registration of the provided MessageHandler to post process BroadcastMessages of the given type.
578
+ /// </summary>
579
+ /// <note>
580
+ /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
581
+ /// </note>
582
+ /// <typeparam name="T">Type of the message that the handler accepts.</typeparam>
583
+ /// <param name="broadcastHandler">Actual post process functionality.</param>
584
+ /// <returns>A handle that allows for registration and de-registration.</returns>
585
+ public MessageRegistrationHandle RegisterBroadcastWithoutSourcePostProcessor<T>(Action<InstanceId, T> broadcastHandler)
586
+ where T : IBroadcastMessage
587
+ {
588
+ if (_messageHandler == null) // Unity has a bug
589
+ {
590
+ return MessageRegistrationHandle.CreateMessageRegistrationHandle();
591
+ }
592
+
593
+ return InternalRegister(broadcastHandler, () => _messageHandler.RegisterSourcedBroadcastWithoutSourcePostProcessor(broadcastHandler, _messageBus));
594
+ }
595
+
596
+ /// <summary>
597
+ /// Stages a registration of the provided MessageHandler to post post process BroadcastMessages of the given type.
598
+ /// </summary>
599
+ /// <note>
600
+ /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
601
+ /// </note>
602
+ /// <typeparam name="T">Type of the message that the handler accepts.</typeparam>
603
+ /// <param name="broadcastHandler">Actual post process functionality.</param>
604
+ /// <returns>A handle that allows for registration and de-registration.</returns>
605
+ public MessageRegistrationHandle RegisterBroadcastWithoutSourcePostProcessor<T>(MessageHandler.FastHandlerWithContext<T> broadcastHandler)
606
+ where T : IBroadcastMessage
607
+ {
608
+ if (_messageHandler == null) // Unity has a bug
609
+ {
610
+ return MessageRegistrationHandle.CreateMessageRegistrationHandle();
611
+ }
612
+
613
+ return InternalRegister(broadcastHandler, () => _messageHandler.RegisterSourcedBroadcastWithoutSourcePostProcessor(broadcastHandler, _messageBus));
614
+ }
615
+
616
+
617
+ /// <summary>
618
+ /// Stages a registration of the provided MessageHandler to accept every message that is broadcast.
619
+ /// </summary>
620
+ /// <note>
621
+ /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
622
+ /// </note>
623
+ /// <param name="acceptAllUntargeted">Action handler functionality for UntargetedMessages.</param>
624
+ /// <param name="acceptAllTargeted">Action handler functionality for TargetedMessages.</param>
625
+ /// <param name="acceptAllBroadcast">Action handler functionality for BroadcastMessages.</param>
626
+ /// <returns>A handle that allows for registration and de-registration.</returns>
627
+ public MessageRegistrationHandle RegisterGlobalAcceptAll(Action<IUntargetedMessage> acceptAllUntargeted, Action<InstanceId, ITargetedMessage> acceptAllTargeted, Action<InstanceId, IBroadcastMessage> acceptAllBroadcast)
628
+ {
629
+ if (_messageHandler == null) // Unity has a bug
630
+ {
631
+ return MessageRegistrationHandle.CreateMessageRegistrationHandle();
632
+ }
633
+ return InternalRegister<IMessage>(() => _messageHandler.RegisterGlobalAcceptAll(acceptAllUntargeted, acceptAllTargeted, acceptAllBroadcast, _messageBus));
634
+ }
635
+
636
+ /// <summary>
637
+ /// Stages a registration of the provided MessageHandler to accept every message that is broadcast.
638
+ /// </summary>
639
+ /// <note>
640
+ /// DOES NOT ACTUALLY REGISTER THE HANDLER IF NOT ENABLED. To register, a call to Enable() is needed.
641
+ /// </note>
642
+ /// <param name="acceptAllUntargeted">Action handler functionality for UntargetedMessages.</param>
643
+ /// <param name="acceptAllTargeted">Action handler functionality for TargetedMessages.</param>
644
+ /// <param name="acceptAllBroadcast">Action handler functionality for BroadcastMessages.</param>
645
+ /// <returns>A handle that allows for registration and de-registration.</returns>
646
+ public MessageRegistrationHandle RegisterGlobalAcceptAll(MessageHandler.FastHandler<IUntargetedMessage> acceptAllUntargeted, MessageHandler.FastHandlerWithContext<ITargetedMessage> acceptAllTargeted, MessageHandler.FastHandlerWithContext<IBroadcastMessage> acceptAllBroadcast)
647
+ {
648
+ if (_messageHandler == null) // Unity has a bug
649
+ {
650
+ return MessageRegistrationHandle.CreateMessageRegistrationHandle();
651
+ }
652
+ return InternalRegister<IMessage>(() => _messageHandler.RegisterGlobalAcceptAll(acceptAllUntargeted, acceptAllTargeted, acceptAllBroadcast, _messageBus));
653
+ }
654
+
655
+ public MessageRegistrationHandle RegisterUntargetedInterceptor<T>(IMessageBus.UntargetedInterceptor<T> interceptor, int priority = 0) where T : IUntargetedMessage
656
+ {
657
+ if (_messageHandler == null)
658
+ {
659
+ return MessageRegistrationHandle.CreateMessageRegistrationHandle();
660
+ }
661
+
662
+ return InternalRegister<T>(() => _messageHandler.RegisterUntargetedInterceptor(interceptor, priority));
663
+ }
664
+
665
+ public MessageRegistrationHandle RegisterBroadcastInterceptor<T>(IMessageBus.BroadcastInterceptor<T> interceptor, int priority = 0) where T : IBroadcastMessage
666
+ {
667
+ if (_messageHandler == null)
668
+ {
669
+ return MessageRegistrationHandle.CreateMessageRegistrationHandle();
670
+ }
671
+
672
+ return InternalRegister<T>(() => _messageHandler.RegisterBroadcastInterceptor(interceptor, priority));
673
+ }
674
+
675
+ public MessageRegistrationHandle RegisterTargetedInterceptor<T>(IMessageBus.TargetedInterceptor<T> interceptor, int priority = 0) where T : ITargetedMessage
676
+ {
677
+ if (_messageHandler == null)
678
+ {
679
+ return MessageRegistrationHandle.CreateMessageRegistrationHandle();
680
+ }
681
+
682
+ return InternalRegister<T>(() => _messageHandler.RegisterTargetedInterceptor(interceptor, priority));
683
+ }
684
+
685
+ /// <summary>
686
+ /// Handles the actual [de]registration wrapping and (potential) lazy execution.
687
+ /// </summary>
688
+ /// <typeparam name="T">Type of message being registered.</typeparam>
689
+ /// <param name="handler">Handler being registered (mainly used for type info).</param>
690
+ /// <param name="registerAndGetDeregistration">Proxied registration function that returns a de-registration function.</param>
691
+ /// <returns>A handle that allows for registration and de-registration.</returns>
692
+ // ReSharper disable once ParameterOnlyUsedForPreconditionCheck.Local
693
+ private MessageRegistrationHandle InternalRegister<T>(Action<T> handler, Func<Action> registerAndGetDeregistration)
694
+ where T : IMessage
695
+ {
696
+ if (handler == null)
697
+ {
698
+ throw new ArgumentNullException(nameof(handler));
699
+ }
700
+
701
+ return InternalRegister<T>(registerAndGetDeregistration);
702
+ }
703
+
704
+ /// <summary>
705
+ /// Handles the actual [de]registration wrapping and (potential) lazy execution.
706
+ /// </summary>
707
+ /// <typeparam name="T">Type of message being registered.</typeparam>
708
+ /// <param name="handler">Handler being registered (mainly used for type info).</param>
709
+ /// <param name="registerAndGetDeregistration">Proxied registration function that returns a de-registration function.</param>
710
+ /// <returns>A handle that allows for registration and de-registration.</returns>
711
+ // ReSharper disable once ParameterOnlyUsedForPreconditionCheck.Local
712
+ private MessageRegistrationHandle InternalRegister<T>(Action<InstanceId, T> handler, Func<Action> registerAndGetDeregistration)
713
+ where T : IMessage
714
+ {
715
+ if (handler == null)
716
+ {
717
+ throw new ArgumentNullException(nameof(handler));
718
+ }
719
+
720
+ return InternalRegister<T>(registerAndGetDeregistration);
721
+ }
722
+
723
+ /// <summary>
724
+ /// Handles the actual [de]registration wrapping and (potential) lazy execution.
725
+ /// </summary>
726
+ /// <typeparam name="T">Type of message being registered.</typeparam>
727
+ /// <param name="handler">Handler being registered (mainly used for type info).</param>
728
+ /// <param name="registerAndGetDeregistration">Proxied registration function that returns a de-registration function.</param>
729
+ /// <returns>A handle that allows for registration and de-registration.</returns>
730
+ // ReSharper disable once ParameterOnlyUsedForPreconditionCheck.Local
731
+ private MessageRegistrationHandle InternalRegister<T>(MessageHandler.FastHandler<T> handler, Func<Action> registerAndGetDeregistration)
732
+ where T : IMessage
733
+ {
734
+ if (handler == null)
735
+ {
736
+ throw new ArgumentNullException(nameof(handler));
737
+ }
738
+
739
+ return InternalRegister<T>(registerAndGetDeregistration);
740
+ }
741
+
742
+ /// <summary>
743
+ /// Handles the actual [de]registration wrapping and (potential) lazy execution.
744
+ /// </summary>
745
+ /// <typeparam name="T">Type of message being registered.</typeparam>
746
+ /// <param name="handler">Handler being registered (mainly used for type info).</param>
747
+ /// <param name="registerAndGetDeregistration">Proxied registration function that returns a de-registration function.</param>
748
+ /// <returns>A handle that allows for registration and de-registration.</returns>
749
+ // ReSharper disable once ParameterOnlyUsedForPreconditionCheck.Local
750
+ private MessageRegistrationHandle InternalRegister<T>(MessageHandler.FastHandlerWithContext<T> handler, Func<Action> registerAndGetDeregistration)
751
+ where T : IMessage
752
+ {
753
+ if (handler == null)
754
+ {
755
+ throw new ArgumentNullException(nameof(handler));
756
+ }
757
+
758
+ return InternalRegister<T>(registerAndGetDeregistration);
759
+ }
760
+
761
+ /// <summary>
762
+ /// Handles the actual [de]registration wrapping and (potential) lazy execution.
763
+ /// </summary>
764
+ /// <typeparam name="T">Type of message being registered.</typeparam>
765
+ /// <param name="registerAndGetDeregistration">Proxied registration function that returns a de-registration function.</param>
766
+ /// <returns>A handle that allows for registration and de-registration.</returns>
767
+ private MessageRegistrationHandle InternalRegister<T>(Func<Action> registerAndGetDeregistration)
768
+ {
769
+ MessageRegistrationHandle handle = MessageRegistrationHandle.CreateMessageRegistrationHandle();
770
+
771
+ // We don't want to actually register at this time (might not be awake/enabled) - so we wrap that shit up, to lazy register when we're enabled.
772
+ void Registration()
773
+ {
774
+ Action actualDeregistration = registerAndGetDeregistration();
775
+ _deregistrations[handle] = actualDeregistration;
776
+ }
777
+
778
+ _registrations[handle] = Registration;
779
+
780
+ // Generally, registrations should take place before all calls to enable. Just in case, though...
781
+ if (_enabled)
782
+ {
783
+ Registration();
784
+ }
785
+
786
+ return handle;
787
+ }
788
+
789
+ /// <summary>
790
+ /// Enables the token if not already enabled. Executes all staged registrations.
791
+ /// </summary>
792
+ /// <note>
793
+ /// Idempotent.
794
+ /// </note>
795
+ public void Enable()
796
+ {
797
+ if (_enabled)
798
+ {
799
+ return;
800
+ }
801
+
802
+ if (_registrations is { Count: > 0 })
803
+ {
804
+ foreach (Action registrationAction in _registrations.Values)
805
+ {
806
+ registrationAction();
807
+ }
808
+ }
809
+
810
+ _enabled = true;
811
+ }
812
+
813
+ /// <summary>
814
+ /// Disables the token if not already disabled. Executes all staged de-registrations.
815
+ /// </summary>
816
+ /// <note>
817
+ /// Idempotent.
818
+ /// </note>
819
+ public void Disable()
820
+ {
821
+ if (!_enabled)
822
+ {
823
+ return;
824
+ }
825
+
826
+ if (_deregistrations is { Count: > 0 })
827
+ {
828
+ foreach (Action deregistrationAction in _deregistrations.Values)
829
+ {
830
+ deregistrationAction();
831
+ }
832
+ }
833
+
834
+ // ReSharper disable once ForCanBeConvertedToForeach
835
+
836
+
837
+ _enabled = false;
838
+ }
839
+
840
+ /// <summary>
841
+ /// Disables the token and clears all registrations + de-registrations
842
+ /// </summary>
843
+ public void UnregisterAll()
844
+ {
845
+ if (_enabled && _deregistrations is { Count: > 0 })
846
+ {
847
+ foreach (Action deregistrationAction in _deregistrations.Values)
848
+ {
849
+ deregistrationAction();
850
+ }
851
+ }
852
+
853
+ _enabled = false;
854
+ _registrations?.Clear();
855
+ _deregistrations?.Clear();
856
+ }
857
+
858
+ public void RemoveRegistration(MessageRegistrationHandle handle)
859
+ {
860
+ if (_deregistrations != null && _deregistrations.TryGetValue(handle, out Action deregistrationAction))
861
+ {
862
+ deregistrationAction();
863
+ _ = _deregistrations.Remove(handle);
864
+ }
865
+
866
+ _ = _registrations?.Remove(handle);
867
+ }
868
+
869
+ /// <summary>
870
+ /// Creates a MessagingRegistrationToken that operates on the given handler.
871
+ /// </summary>
872
+ /// <param name="messageHandler">Message handler to register handlers to.</param>
873
+ /// <param name="messageBus">MessageBus to use for this MessageRegistrationToken. Uses the GlobalMessageBus if left null.</param>
874
+ /// <returns>MessagingRegistrationToken bound to the MessageHandler.</returns>
875
+ public static MessageRegistrationToken Create(MessageHandler messageHandler, IMessageBus messageBus = null)
876
+ {
877
+ if (messageHandler == null)
878
+ {
879
+ throw new ArgumentNullException(nameof(messageHandler));
880
+ }
881
+
882
+ return new MessageRegistrationToken(messageHandler, messageBus);
883
+ }
884
+ }
885
+ }