ObservableObject.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. // This file is inspired from the MvvmLight library (lbugnion/MvvmLight),
  5. // more info in ThirdPartyNotices.txt in the root of the project.
  6. // ================================== NOTE ==================================
  7. // This file is mirrored in the trimmed-down INotifyPropertyChanged file in
  8. // the source generator project, to be used with the [INotifyPropertyChanged],
  9. // attribute, along with the ObservableObject annotated copy (for debugging info).
  10. // If any changes are made to this file, they should also be appropriately
  11. // ported to that file as well to keep the behavior consistent.
  12. // ==========================================================================
  13. using System;
  14. using System.Collections.Generic;
  15. using System.ComponentModel;
  16. using System.Diagnostics.CodeAnalysis;
  17. using System.Runtime.CompilerServices;
  18. using System.Threading.Tasks;
  19. using CommunityToolkit.Mvvm.ComponentModel.__Internals;
  20. #pragma warning disable CS0618
  21. namespace CommunityToolkit.Mvvm.ComponentModel;
  22. /// <summary>
  23. /// A base class for objects of which the properties must be observable.
  24. /// </summary>
  25. public abstract class ObservableObject : INotifyPropertyChanged, INotifyPropertyChanging
  26. {
  27. /// <inheritdoc cref="INotifyPropertyChanged.PropertyChanged"/>
  28. public event PropertyChangedEventHandler? PropertyChanged;
  29. /// <inheritdoc cref="INotifyPropertyChanging.PropertyChanging"/>
  30. public event PropertyChangingEventHandler? PropertyChanging;
  31. /// <summary>
  32. /// Raises the <see cref="PropertyChanged"/> event.
  33. /// </summary>
  34. /// <param name="e">The input <see cref="PropertyChangedEventArgs"/> instance.</param>
  35. /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="e"/> is <see langword="null"/>.</exception>
  36. protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
  37. {
  38. ArgumentNullException.ThrowIfNull(e);
  39. PropertyChanged?.Invoke(this, e);
  40. }
  41. /// <summary>
  42. /// Raises the <see cref="PropertyChanging"/> event.
  43. /// </summary>
  44. /// <param name="e">The input <see cref="PropertyChangingEventArgs"/> instance.</param>
  45. /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="e"/> is <see langword="null"/>.</exception>
  46. protected virtual void OnPropertyChanging(PropertyChangingEventArgs e)
  47. {
  48. ArgumentNullException.ThrowIfNull(e);
  49. // When support is disabled, just do nothing
  50. if (!FeatureSwitches.EnableINotifyPropertyChangingSupport)
  51. {
  52. return;
  53. }
  54. PropertyChanging?.Invoke(this, e);
  55. }
  56. /// <summary>
  57. /// Raises the <see cref="PropertyChanged"/> event.
  58. /// </summary>
  59. /// <param name="propertyName">(optional) The name of the property that changed.</param>
  60. protected void OnPropertyChanged([CallerMemberName] string? propertyName = null)
  61. {
  62. OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
  63. }
  64. /// <summary>
  65. /// Raises the <see cref="PropertyChanging"/> event.
  66. /// </summary>
  67. /// <param name="propertyName">(optional) The name of the property that changed.</param>
  68. protected void OnPropertyChanging([CallerMemberName] string? propertyName = null)
  69. {
  70. // When support is disabled, avoid instantiating the event args entirely
  71. if (!FeatureSwitches.EnableINotifyPropertyChangingSupport)
  72. {
  73. return;
  74. }
  75. OnPropertyChanging(new PropertyChangingEventArgs(propertyName));
  76. }
  77. /// <summary>
  78. /// Compares the current and new values for a given property. If the value has changed,
  79. /// raises the <see cref="PropertyChanging"/> event, updates the property with the new
  80. /// value, then raises the <see cref="PropertyChanged"/> event.
  81. /// </summary>
  82. /// <typeparam name="T">The type of the property that changed.</typeparam>
  83. /// <param name="field">The field storing the property's value.</param>
  84. /// <param name="newValue">The property's value after the change occurred.</param>
  85. /// <param name="propertyName">(optional) The name of the property that changed.</param>
  86. /// <returns><see langword="true"/> if the property was changed, <see langword="false"/> otherwise.</returns>
  87. /// <remarks>
  88. /// The <see cref="PropertyChanging"/> and <see cref="PropertyChanged"/> events are not raised
  89. /// if the current and new value for the target property are the same.
  90. /// </remarks>
  91. protected bool SetProperty<T>([NotNullIfNotNull(nameof(newValue))] ref T field, T newValue, [CallerMemberName] string? propertyName = null)
  92. {
  93. // We duplicate the code here instead of calling the overload because we can't
  94. // guarantee that the invoked SetProperty<T> will be inlined, and we need the JIT
  95. // to be able to see the full EqualityComparer<T>.Default.Equals call, so that
  96. // it'll use the intrinsics version of it and just replace the whole invocation
  97. // with a direct comparison when possible (eg. for primitive numeric types).
  98. // This is the fastest SetProperty<T> overload so we particularly care about
  99. // the codegen quality here, and the code is small and simple enough so that
  100. // duplicating it still doesn't make the whole class harder to maintain.
  101. if (EqualityComparer<T>.Default.Equals(field, newValue))
  102. {
  103. return false;
  104. }
  105. OnPropertyChanging(propertyName);
  106. field = newValue;
  107. OnPropertyChanged(propertyName);
  108. return true;
  109. }
  110. /// <summary>
  111. /// Compares the current and new values for a given property. If the value has changed,
  112. /// raises the <see cref="PropertyChanging"/> event, updates the property with the new
  113. /// value, then raises the <see cref="PropertyChanged"/> event.
  114. /// See additional notes about this overload in <see cref="SetProperty{T}(ref T,T,string)"/>.
  115. /// </summary>
  116. /// <typeparam name="T">The type of the property that changed.</typeparam>
  117. /// <param name="field">The field storing the property's value.</param>
  118. /// <param name="newValue">The property's value after the change occurred.</param>
  119. /// <param name="comparer">The <see cref="IEqualityComparer{T}"/> instance to use to compare the input values.</param>
  120. /// <param name="propertyName">(optional) The name of the property that changed.</param>
  121. /// <returns><see langword="true"/> if the property was changed, <see langword="false"/> otherwise.</returns>
  122. /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="comparer"/> is <see langword="null"/>.</exception>
  123. protected bool SetProperty<T>([NotNullIfNotNull(nameof(newValue))] ref T field, T newValue, IEqualityComparer<T> comparer, [CallerMemberName] string? propertyName = null)
  124. {
  125. ArgumentNullException.ThrowIfNull(comparer);
  126. if (comparer.Equals(field, newValue))
  127. {
  128. return false;
  129. }
  130. OnPropertyChanging(propertyName);
  131. field = newValue;
  132. OnPropertyChanged(propertyName);
  133. return true;
  134. }
  135. /// <summary>
  136. /// Compares the current and new values for a given property. If the value has changed,
  137. /// raises the <see cref="PropertyChanging"/> event, updates the property with the new
  138. /// value, then raises the <see cref="PropertyChanged"/> event.
  139. /// This overload is much less efficient than <see cref="SetProperty{T}(ref T,T,string)"/> and it
  140. /// should only be used when the former is not viable (eg. when the target property being
  141. /// updated does not directly expose a backing field that can be passed by reference).
  142. /// For performance reasons, it is recommended to use a stateful callback if possible through
  143. /// the <see cref="SetProperty{TModel,T}(T,T,TModel,Action{TModel,T},string?)"/> whenever possible
  144. /// instead of this overload, as that will allow the C# compiler to cache the input callback and
  145. /// reduce the memory allocations. More info on that overload are available in the related XML
  146. /// docs. This overload is here for completeness and in cases where that is not applicable.
  147. /// </summary>
  148. /// <typeparam name="T">The type of the property that changed.</typeparam>
  149. /// <param name="oldValue">The current property value.</param>
  150. /// <param name="newValue">The property's value after the change occurred.</param>
  151. /// <param name="callback">A callback to invoke to update the property value.</param>
  152. /// <param name="propertyName">(optional) The name of the property that changed.</param>
  153. /// <returns><see langword="true"/> if the property was changed, <see langword="false"/> otherwise.</returns>
  154. /// <remarks>
  155. /// The <see cref="PropertyChanging"/> and <see cref="PropertyChanged"/> events are not raised
  156. /// if the current and new value for the target property are the same.
  157. /// </remarks>
  158. /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="callback"/> is <see langword="null"/>.</exception>
  159. protected bool SetProperty<T>(T oldValue, T newValue, Action<T> callback, [CallerMemberName] string? propertyName = null)
  160. {
  161. ArgumentNullException.ThrowIfNull(callback);
  162. // We avoid calling the overload again to ensure the comparison is inlined
  163. if (EqualityComparer<T>.Default.Equals(oldValue, newValue))
  164. {
  165. return false;
  166. }
  167. OnPropertyChanging(propertyName);
  168. callback(newValue);
  169. OnPropertyChanged(propertyName);
  170. return true;
  171. }
  172. /// <summary>
  173. /// Compares the current and new values for a given property. If the value has changed,
  174. /// raises the <see cref="PropertyChanging"/> event, updates the property with the new
  175. /// value, then raises the <see cref="PropertyChanged"/> event.
  176. /// See additional notes about this overload in <see cref="SetProperty{T}(T,T,Action{T},string)"/>.
  177. /// </summary>
  178. /// <typeparam name="T">The type of the property that changed.</typeparam>
  179. /// <param name="oldValue">The current property value.</param>
  180. /// <param name="newValue">The property's value after the change occurred.</param>
  181. /// <param name="comparer">The <see cref="IEqualityComparer{T}"/> instance to use to compare the input values.</param>
  182. /// <param name="callback">A callback to invoke to update the property value.</param>
  183. /// <param name="propertyName">(optional) The name of the property that changed.</param>
  184. /// <returns><see langword="true"/> if the property was changed, <see langword="false"/> otherwise.</returns>
  185. /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="comparer"/> or <paramref name="callback"/> are <see langword="null"/>.</exception>
  186. protected bool SetProperty<T>(T oldValue, T newValue, IEqualityComparer<T> comparer, Action<T> callback, [CallerMemberName] string? propertyName = null)
  187. {
  188. ArgumentNullException.ThrowIfNull(comparer);
  189. ArgumentNullException.ThrowIfNull(callback);
  190. if (comparer.Equals(oldValue, newValue))
  191. {
  192. return false;
  193. }
  194. OnPropertyChanging(propertyName);
  195. callback(newValue);
  196. OnPropertyChanged(propertyName);
  197. return true;
  198. }
  199. /// <summary>
  200. /// Compares the current and new values for a given nested property. If the value has changed,
  201. /// raises the <see cref="PropertyChanging"/> event, updates the property and then raises the
  202. /// <see cref="PropertyChanged"/> event. The behavior mirrors that of <see cref="SetProperty{T}(ref T,T,string)"/>,
  203. /// with the difference being that this method is used to relay properties from a wrapped model in the
  204. /// current instance. This type is useful when creating wrapping, bindable objects that operate over
  205. /// models that lack support for notification (eg. for CRUD operations).
  206. /// Suppose we have this model (eg. for a database row in a table):
  207. /// <code>
  208. /// public class Person
  209. /// {
  210. /// public string Name { get; set; }
  211. /// }
  212. /// </code>
  213. /// We can then use a property to wrap instances of this type into our observable model (which supports
  214. /// notifications), injecting the notification to the properties of that model, like so:
  215. /// <code>
  216. /// public class BindablePerson : ObservableObject
  217. /// {
  218. /// public Model { get; }
  219. ///
  220. /// public BindablePerson(Person model)
  221. /// {
  222. /// Model = model;
  223. /// }
  224. ///
  225. /// public string Name
  226. /// {
  227. /// get => Model.Name;
  228. /// set => Set(Model.Name, value, Model, (model, name) => model.Name = name);
  229. /// }
  230. /// }
  231. /// </code>
  232. /// This way we can then use the wrapping object in our application, and all those "proxy" properties will
  233. /// also raise notifications when changed. Note that this method is not meant to be a replacement for
  234. /// <see cref="SetProperty{T}(ref T,T,string)"/>, and it should only be used when relaying properties to a model that
  235. /// doesn't support notifications, and only if you can't implement notifications to that model directly (eg. by having
  236. /// it inherit from <see cref="ObservableObject"/>). The syntax relies on passing the target model and a stateless callback
  237. /// to allow the C# compiler to cache the function, which results in much better performance and no memory usage.
  238. /// </summary>
  239. /// <typeparam name="TModel">The type of model whose property (or field) to set.</typeparam>
  240. /// <typeparam name="T">The type of property (or field) to set.</typeparam>
  241. /// <param name="oldValue">The current property value.</param>
  242. /// <param name="newValue">The property's value after the change occurred.</param>
  243. /// <param name="model">The model containing the property being updated.</param>
  244. /// <param name="callback">The callback to invoke to set the target property value, if a change has occurred.</param>
  245. /// <param name="propertyName">(optional) The name of the property that changed.</param>
  246. /// <returns><see langword="true"/> if the property was changed, <see langword="false"/> otherwise.</returns>
  247. /// <remarks>
  248. /// The <see cref="PropertyChanging"/> and <see cref="PropertyChanged"/> events are not
  249. /// raised if the current and new value for the target property are the same.
  250. /// </remarks>
  251. /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="model"/> or <paramref name="callback"/> are <see langword="null"/>.</exception>
  252. protected bool SetProperty<TModel, T>(T oldValue, T newValue, TModel model, Action<TModel, T> callback, [CallerMemberName] string? propertyName = null)
  253. where TModel : class
  254. {
  255. ArgumentNullException.ThrowIfNull(model);
  256. ArgumentNullException.ThrowIfNull(callback);
  257. if (EqualityComparer<T>.Default.Equals(oldValue, newValue))
  258. {
  259. return false;
  260. }
  261. OnPropertyChanging(propertyName);
  262. callback(model, newValue);
  263. OnPropertyChanged(propertyName);
  264. return true;
  265. }
  266. /// <summary>
  267. /// Compares the current and new values for a given nested property. If the value has changed,
  268. /// raises the <see cref="PropertyChanging"/> event, updates the property and then raises the
  269. /// <see cref="PropertyChanged"/> event. The behavior mirrors that of <see cref="SetProperty{T}(ref T,T,string)"/>,
  270. /// with the difference being that this method is used to relay properties from a wrapped model in the
  271. /// current instance. See additional notes about this overload in <see cref="SetProperty{TModel,T}(T,T,TModel,Action{TModel,T},string)"/>.
  272. /// </summary>
  273. /// <typeparam name="TModel">The type of model whose property (or field) to set.</typeparam>
  274. /// <typeparam name="T">The type of property (or field) to set.</typeparam>
  275. /// <param name="oldValue">The current property value.</param>
  276. /// <param name="newValue">The property's value after the change occurred.</param>
  277. /// <param name="comparer">The <see cref="IEqualityComparer{T}"/> instance to use to compare the input values.</param>
  278. /// <param name="model">The model containing the property being updated.</param>
  279. /// <param name="callback">The callback to invoke to set the target property value, if a change has occurred.</param>
  280. /// <param name="propertyName">(optional) The name of the property that changed.</param>
  281. /// <returns><see langword="true"/> if the property was changed, <see langword="false"/> otherwise.</returns>
  282. /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="comparer"/>, <paramref name="model"/> or <paramref name="callback"/> are <see langword="null"/>.</exception>
  283. protected bool SetProperty<TModel, T>(T oldValue, T newValue, IEqualityComparer<T> comparer, TModel model, Action<TModel, T> callback, [CallerMemberName] string? propertyName = null)
  284. where TModel : class
  285. {
  286. ArgumentNullException.ThrowIfNull(comparer);
  287. ArgumentNullException.ThrowIfNull(model);
  288. ArgumentNullException.ThrowIfNull(callback);
  289. if (comparer.Equals(oldValue, newValue))
  290. {
  291. return false;
  292. }
  293. OnPropertyChanging(propertyName);
  294. callback(model, newValue);
  295. OnPropertyChanged(propertyName);
  296. return true;
  297. }
  298. /// <summary>
  299. /// Compares the current and new values for a given field (which should be the backing
  300. /// field for a property). If the value has changed, raises the <see cref="PropertyChanging"/>
  301. /// event, updates the field and then raises the <see cref="PropertyChanged"/> event.
  302. /// The behavior mirrors that of <see cref="SetProperty{T}(ref T,T,string)"/>, with the difference being that
  303. /// this method will also monitor the new value of the property (a generic <see cref="Task"/>) and will also
  304. /// raise the <see cref="PropertyChanged"/> again for the target property when it completes.
  305. /// This can be used to update bindings observing that <see cref="Task"/> or any of its properties.
  306. /// This method and its overload specifically rely on the <see cref="TaskNotifier"/> type, which needs
  307. /// to be used in the backing field for the target <see cref="Task"/> property. The field doesn't need to be
  308. /// initialized, as this method will take care of doing that automatically. The <see cref="TaskNotifier"/>
  309. /// type also includes an implicit operator, so it can be assigned to any <see cref="Task"/> instance directly.
  310. /// Here is a sample property declaration using this method:
  311. /// <code>
  312. /// private TaskNotifier myTask;
  313. ///
  314. /// public Task MyTask
  315. /// {
  316. /// get => myTask;
  317. /// private set => SetAndNotifyOnCompletion(ref myTask, value);
  318. /// }
  319. /// </code>
  320. /// </summary>
  321. /// <param name="taskNotifier">The field notifier to modify.</param>
  322. /// <param name="newValue">The property's value after the change occurred.</param>
  323. /// <param name="propertyName">(optional) The name of the property that changed.</param>
  324. /// <returns><see langword="true"/> if the property was changed, <see langword="false"/> otherwise.</returns>
  325. /// <remarks>
  326. /// The <see cref="PropertyChanging"/> and <see cref="PropertyChanged"/> events are not raised if the current
  327. /// and new value for the target property are the same. The return value being <see langword="true"/> only
  328. /// indicates that the new value being assigned to <paramref name="taskNotifier"/> is different than the previous one,
  329. /// and it does not mean the new <see cref="Task"/> instance passed as argument is in any particular state.
  330. /// </remarks>
  331. protected bool SetPropertyAndNotifyOnCompletion([NotNull] ref TaskNotifier? taskNotifier, Task? newValue, [CallerMemberName] string? propertyName = null)
  332. {
  333. // We invoke the overload with a callback here to avoid code duplication, and simply pass an empty callback.
  334. // The lambda expression here is transformed by the C# compiler into an empty closure class with a
  335. // static singleton field containing a closure instance, and another caching the instantiated Action<TTask>
  336. // instance. This will result in no further allocations after the first time this method is called for a given
  337. // generic type. We only pay the cost of the virtual call to the delegate, but this is not performance critical
  338. // code and that overhead would still be much lower than the rest of the method anyway, so that's fine.
  339. return SetPropertyAndNotifyOnCompletion(taskNotifier ??= new TaskNotifier(), newValue, null, propertyName);
  340. }
  341. /// <summary>
  342. /// Compares the current and new values for a given field (which should be the backing
  343. /// field for a property). If the value has changed, raises the <see cref="PropertyChanging"/>
  344. /// event, updates the field and then raises the <see cref="PropertyChanged"/> event.
  345. /// This method is just like <see cref="SetPropertyAndNotifyOnCompletion(ref TaskNotifier,Task,string)"/>,
  346. /// with the difference being an extra <see cref="Action{T}"/> parameter with a callback being invoked
  347. /// either immediately, if the new task has already completed or is <see langword="null"/>, or upon completion.
  348. /// </summary>
  349. /// <param name="taskNotifier">The field notifier to modify.</param>
  350. /// <param name="newValue">The property's value after the change occurred.</param>
  351. /// <param name="callback">A callback to invoke to update the property value.</param>
  352. /// <param name="propertyName">(optional) The name of the property that changed.</param>
  353. /// <returns><see langword="true"/> if the property was changed, <see langword="false"/> otherwise.</returns>
  354. /// <remarks>
  355. /// The <see cref="PropertyChanging"/> and <see cref="PropertyChanged"/> events are not raised
  356. /// if the current and new value for the target property are the same.
  357. /// </remarks>
  358. /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="callback"/> is <see langword="null"/>.</exception>
  359. protected bool SetPropertyAndNotifyOnCompletion([NotNull] ref TaskNotifier? taskNotifier, Task? newValue, Action<Task?> callback, [CallerMemberName] string? propertyName = null)
  360. {
  361. ArgumentNullException.ThrowIfNull(callback);
  362. return SetPropertyAndNotifyOnCompletion(taskNotifier ??= new TaskNotifier(), newValue, callback, propertyName);
  363. }
  364. /// <summary>
  365. /// Compares the current and new values for a given field (which should be the backing
  366. /// field for a property). If the value has changed, raises the <see cref="PropertyChanging"/>
  367. /// event, updates the field and then raises the <see cref="PropertyChanged"/> event.
  368. /// The behavior mirrors that of <see cref="SetProperty{T}(ref T,T,string)"/>, with the difference being that
  369. /// this method will also monitor the new value of the property (a generic <see cref="Task"/>) and will also
  370. /// raise the <see cref="PropertyChanged"/> again for the target property when it completes.
  371. /// This can be used to update bindings observing that <see cref="Task"/> or any of its properties.
  372. /// This method and its overload specifically rely on the <see cref="TaskNotifier{T}"/> type, which needs
  373. /// to be used in the backing field for the target <see cref="Task"/> property. The field doesn't need to be
  374. /// initialized, as this method will take care of doing that automatically. The <see cref="TaskNotifier{T}"/>
  375. /// type also includes an implicit operator, so it can be assigned to any <see cref="Task"/> instance directly.
  376. /// Here is a sample property declaration using this method:
  377. /// <code>
  378. /// private TaskNotifier&lt;int&gt; myTask;
  379. ///
  380. /// public Task&lt;int&gt; MyTask
  381. /// {
  382. /// get => myTask;
  383. /// private set => SetAndNotifyOnCompletion(ref myTask, value);
  384. /// }
  385. /// </code>
  386. /// </summary>
  387. /// <typeparam name="T">The type of result for the <see cref="Task{TResult}"/> to set and monitor.</typeparam>
  388. /// <param name="taskNotifier">The field notifier to modify.</param>
  389. /// <param name="newValue">The property's value after the change occurred.</param>
  390. /// <param name="propertyName">(optional) The name of the property that changed.</param>
  391. /// <returns><see langword="true"/> if the property was changed, <see langword="false"/> otherwise.</returns>
  392. /// <remarks>
  393. /// The <see cref="PropertyChanging"/> and <see cref="PropertyChanged"/> events are not raised if the current
  394. /// and new value for the target property are the same. The return value being <see langword="true"/> only
  395. /// indicates that the new value being assigned to <paramref name="taskNotifier"/> is different than the previous one,
  396. /// and it does not mean the new <see cref="Task{TResult}"/> instance passed as argument is in any particular state.
  397. /// </remarks>
  398. protected bool SetPropertyAndNotifyOnCompletion<T>([NotNull] ref TaskNotifier<T>? taskNotifier, Task<T>? newValue, [CallerMemberName] string? propertyName = null)
  399. {
  400. return SetPropertyAndNotifyOnCompletion(taskNotifier ??= new TaskNotifier<T>(), newValue, null, propertyName);
  401. }
  402. /// <summary>
  403. /// Compares the current and new values for a given field (which should be the backing
  404. /// field for a property). If the value has changed, raises the <see cref="PropertyChanging"/>
  405. /// event, updates the field and then raises the <see cref="PropertyChanged"/> event.
  406. /// This method is just like <see cref="SetPropertyAndNotifyOnCompletion{T}(ref TaskNotifier{T},Task{T},string)"/>,
  407. /// with the difference being an extra <see cref="Action{T}"/> parameter with a callback being invoked
  408. /// either immediately, if the new task has already completed or is <see langword="null"/>, or upon completion.
  409. /// </summary>
  410. /// <typeparam name="T">The type of result for the <see cref="Task{TResult}"/> to set and monitor.</typeparam>
  411. /// <param name="taskNotifier">The field notifier to modify.</param>
  412. /// <param name="newValue">The property's value after the change occurred.</param>
  413. /// <param name="callback">A callback to invoke to update the property value.</param>
  414. /// <param name="propertyName">(optional) The name of the property that changed.</param>
  415. /// <returns><see langword="true"/> if the property was changed, <see langword="false"/> otherwise.</returns>
  416. /// <remarks>
  417. /// The <see cref="PropertyChanging"/> and <see cref="PropertyChanged"/> events are not raised
  418. /// if the current and new value for the target property are the same.
  419. /// </remarks>
  420. /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="callback"/> is <see langword="null"/>.</exception>
  421. protected bool SetPropertyAndNotifyOnCompletion<T>([NotNull] ref TaskNotifier<T>? taskNotifier, Task<T>? newValue, Action<Task<T>?> callback, [CallerMemberName] string? propertyName = null)
  422. {
  423. ArgumentNullException.ThrowIfNull(callback);
  424. return SetPropertyAndNotifyOnCompletion(taskNotifier ??= new TaskNotifier<T>(), newValue, callback, propertyName);
  425. }
  426. /// <summary>
  427. /// Implements the notification logic for the related methods.
  428. /// </summary>
  429. /// <typeparam name="TTask">The type of <see cref="Task"/> to set and monitor.</typeparam>
  430. /// <param name="taskNotifier">The field notifier.</param>
  431. /// <param name="newValue">The property's value after the change occurred.</param>
  432. /// <param name="callback">(optional) A callback to invoke to update the property value.</param>
  433. /// <param name="propertyName">(optional) The name of the property that changed.</param>
  434. /// <returns><see langword="true"/> if the property was changed, <see langword="false"/> otherwise.</returns>
  435. private bool SetPropertyAndNotifyOnCompletion<TTask>(ITaskNotifier<TTask> taskNotifier, TTask? newValue, Action<TTask?>? callback, [CallerMemberName] string? propertyName = null)
  436. where TTask : Task
  437. {
  438. if (ReferenceEquals(taskNotifier.Task, newValue))
  439. {
  440. return false;
  441. }
  442. // Check the status of the new task before assigning it to the
  443. // target field. This is so that in case the task is either
  444. // null or already completed, we can avoid the overhead of
  445. // scheduling the method to monitor its completion.
  446. bool isAlreadyCompletedOrNull = newValue?.IsCompleted ?? true;
  447. OnPropertyChanging(propertyName);
  448. taskNotifier.Task = newValue;
  449. OnPropertyChanged(propertyName);
  450. // If the input task is either null or already completed, we don't need to
  451. // execute the additional logic to monitor its completion, so we can just bypass
  452. // the rest of the method and return that the field changed here. The return value
  453. // does not indicate that the task itself has completed, but just that the property
  454. // value itself has changed (ie. the referenced task instance has changed).
  455. // This mirrors the return value of all the other synchronous Set methods as well.
  456. if (isAlreadyCompletedOrNull)
  457. {
  458. if (callback is not null)
  459. {
  460. callback(newValue);
  461. }
  462. return true;
  463. }
  464. // We use a local async function here so that the main method can
  465. // remain synchronous and return a value that can be immediately
  466. // used by the caller. This mirrors Set<T>(ref T, T, string).
  467. // We use an async void function instead of a Task-returning function
  468. // so that if a binding update caused by the property change notification
  469. // causes a crash, it is immediately reported in the application instead of
  470. // the exception being ignored (as the returned task wouldn't be awaited),
  471. // which would result in a confusing behavior for users.
  472. async void MonitorTask()
  473. {
  474. // Await the task and ignore any exceptions
  475. await newValue!.GetAwaitableWithoutEndValidation();
  476. // Only notify if the property hasn't changed
  477. if (ReferenceEquals(taskNotifier.Task, newValue))
  478. {
  479. OnPropertyChanged(propertyName);
  480. }
  481. if (callback is not null)
  482. {
  483. callback(newValue);
  484. }
  485. }
  486. MonitorTask();
  487. return true;
  488. }
  489. /// <summary>
  490. /// An interface for task notifiers of a specified type.
  491. /// </summary>
  492. /// <typeparam name="TTask">The type of value to store.</typeparam>
  493. private interface ITaskNotifier<TTask>
  494. where TTask : Task
  495. {
  496. /// <summary>
  497. /// Gets or sets the wrapped <typeparamref name="TTask"/> value.
  498. /// </summary>
  499. TTask? Task { get; set; }
  500. }
  501. /// <summary>
  502. /// A wrapping class that can hold a <see cref="Task"/> value.
  503. /// </summary>
  504. protected sealed class TaskNotifier : ITaskNotifier<Task>
  505. {
  506. /// <summary>
  507. /// Initializes a new instance of the <see cref="TaskNotifier"/> class.
  508. /// </summary>
  509. internal TaskNotifier()
  510. {
  511. }
  512. private Task? task;
  513. /// <inheritdoc/>
  514. Task? ITaskNotifier<Task>.Task
  515. {
  516. get => this.task;
  517. set => this.task = value;
  518. }
  519. /// <summary>
  520. /// Unwraps the <see cref="Task"/> value stored in the current instance.
  521. /// </summary>
  522. /// <param name="notifier">The input <see cref="TaskNotifier{TTask}"/> instance.</param>
  523. public static implicit operator Task?(TaskNotifier? notifier)
  524. {
  525. return notifier?.task;
  526. }
  527. }
  528. /// <summary>
  529. /// A wrapping class that can hold a <see cref="Task{T}"/> value.
  530. /// </summary>
  531. /// <typeparam name="T">The type of value for the wrapped <see cref="Task{T}"/> instance.</typeparam>
  532. protected sealed class TaskNotifier<T> : ITaskNotifier<Task<T>>
  533. {
  534. /// <summary>
  535. /// Initializes a new instance of the <see cref="TaskNotifier{TTask}"/> class.
  536. /// </summary>
  537. internal TaskNotifier()
  538. {
  539. }
  540. private Task<T>? task;
  541. /// <inheritdoc/>
  542. Task<T>? ITaskNotifier<Task<T>>.Task
  543. {
  544. get => this.task;
  545. set => this.task = value;
  546. }
  547. /// <summary>
  548. /// Unwraps the <see cref="Task{T}"/> value stored in the current instance.
  549. /// </summary>
  550. /// <param name="notifier">The input <see cref="TaskNotifier{TTask}"/> instance.</param>
  551. public static implicit operator Task<T>?(TaskNotifier<T>? notifier)
  552. {
  553. return notifier?.task;
  554. }
  555. }
  556. }