-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: Add record base C# 9 (#2751)
* Create ReactiveRecord.cs * Add IsExternalInit * Add version file bump * Fix tests
- Loading branch information
1 parent
9f05e4a
commit b84fb67
Showing
7 changed files
with
225 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright (c) 2021 .NET Foundation and Contributors. All rights reserved. | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for full license information. | ||
|
||
using System.ComponentModel; | ||
|
||
namespace System.Runtime.CompilerServices | ||
{ | ||
/// <summary> | ||
/// Reserved to be used by the compiler for tracking metadata. | ||
/// This class should not be used by developers in source code. | ||
/// </summary> | ||
[EditorBrowsable(EditorBrowsableState.Never)] | ||
internal static class IsExternalInit | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
// Copyright (c) 2021 .NET Foundation and Contributors. All rights reserved. | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.ComponentModel; | ||
using System.Reactive; | ||
using System.Runtime.Serialization; | ||
using System.Threading; | ||
|
||
namespace ReactiveUI | ||
{ | ||
/// <summary> | ||
/// ReactiveObject is the base object for ViewModel classes, and it | ||
/// implements INotifyPropertyChanged. In addition, ReactiveObject provides | ||
/// Changing and Changed Observables to monitor object changes. | ||
/// </summary> | ||
[DataContract] | ||
public record ReactiveRecord : IReactiveNotifyPropertyChanged<IReactiveObject>, IHandleObservableErrors, IReactiveObject | ||
{ | ||
private readonly Lazy<IObservable<IReactivePropertyChangedEventArgs<IReactiveObject>>> _changing; | ||
private readonly Lazy<IObservable<IReactivePropertyChangedEventArgs<IReactiveObject>>> _changed; | ||
private readonly Lazy<Unit> _propertyChangingEventsSubscribed; | ||
private readonly Lazy<Unit> _propertyChangedEventsSubscribed; | ||
private readonly Lazy<IObservable<Exception>> _thrownExceptions; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ReactiveRecord"/> class. | ||
/// </summary> | ||
public ReactiveRecord() | ||
{ | ||
_changing = new Lazy<IObservable<IReactivePropertyChangedEventArgs<IReactiveObject>>>(() => ((IReactiveObject)this).GetChangingObservable(), LazyThreadSafetyMode.PublicationOnly); | ||
_changed = new Lazy<IObservable<IReactivePropertyChangedEventArgs<IReactiveObject>>>(() => ((IReactiveObject)this).GetChangedObservable(), LazyThreadSafetyMode.PublicationOnly); | ||
_propertyChangingEventsSubscribed = new Lazy<Unit>( | ||
() => | ||
{ | ||
this.SubscribePropertyChangingEvents(); | ||
return Unit.Default; | ||
}, | ||
LazyThreadSafetyMode.PublicationOnly); | ||
_propertyChangedEventsSubscribed = new Lazy<Unit>( | ||
() => | ||
{ | ||
this.SubscribePropertyChangedEvents(); | ||
return Unit.Default; | ||
}, | ||
LazyThreadSafetyMode.PublicationOnly); | ||
_thrownExceptions = new Lazy<IObservable<Exception>>(this.GetThrownExceptionsObservable, LazyThreadSafetyMode.PublicationOnly); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public event PropertyChangingEventHandler? PropertyChanging | ||
{ | ||
add | ||
{ | ||
_ = _propertyChangingEventsSubscribed.Value; | ||
PropertyChangingHandler += value; | ||
} | ||
remove => PropertyChangingHandler -= value; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public event PropertyChangedEventHandler? PropertyChanged | ||
{ | ||
add | ||
{ | ||
_ = _propertyChangedEventsSubscribed.Value; | ||
PropertyChangedHandler += value; | ||
} | ||
remove => PropertyChangedHandler -= value; | ||
} | ||
|
||
private event PropertyChangingEventHandler? PropertyChangingHandler; | ||
|
||
private event PropertyChangedEventHandler? PropertyChangedHandler; | ||
|
||
/// <inheritdoc /> | ||
[IgnoreDataMember] | ||
public IObservable<IReactivePropertyChangedEventArgs<IReactiveObject>> Changing => _changing.Value; | ||
|
||
/// <inheritdoc /> | ||
[IgnoreDataMember] | ||
public IObservable<IReactivePropertyChangedEventArgs<IReactiveObject>> Changed => _changed.Value; | ||
|
||
/// <inheritdoc/> | ||
[IgnoreDataMember] | ||
public IObservable<Exception> ThrownExceptions => _thrownExceptions.Value; | ||
|
||
/// <inheritdoc/> | ||
void IReactiveObject.RaisePropertyChanging(PropertyChangingEventArgs args) => PropertyChangingHandler?.Invoke(this, args); | ||
|
||
/// <inheritdoc/> | ||
void IReactiveObject.RaisePropertyChanged(PropertyChangedEventArgs args) => PropertyChangedHandler?.Invoke(this, args); | ||
|
||
/// <inheritdoc/> | ||
public IDisposable SuppressChangeNotifications() => IReactiveObjectExtensions.SuppressChangeNotifications(this); | ||
|
||
/// <summary> | ||
/// Determines if change notifications are enabled or not. | ||
/// </summary> | ||
/// <returns>A value indicating whether change notifications are enabled.</returns> | ||
public bool AreChangeNotificationsEnabled() => IReactiveObjectExtensions.AreChangeNotificationsEnabled(this); | ||
|
||
/// <summary> | ||
/// Delays notifications until the return IDisposable is disposed. | ||
/// </summary> | ||
/// <returns>A disposable which when disposed will send delayed notifications.</returns> | ||
public IDisposable DelayChangeNotifications() => IReactiveObjectExtensions.DelayChangeNotifications(this); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters