From d37644387fdbdc8ad7f9a034fc0fa8655148ecfc Mon Sep 17 00:00:00 2001 From: Andres Pineda Date: Wed, 25 Sep 2024 12:57:49 -0400 Subject: [PATCH 1/5] docs: mvux messaging observe overloads --- doc/Learn/Mvux/Advanced/Messaging.md | 75 ++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/doc/Learn/Mvux/Advanced/Messaging.md b/doc/Learn/Mvux/Advanced/Messaging.md index f6169e0320..94340af051 100644 --- a/doc/Learn/Mvux/Advanced/Messaging.md +++ b/doc/Learn/Mvux/Advanced/Messaging.md @@ -302,6 +302,81 @@ The `SelectedPersonPhone` state will only be refreshed if it meets the predicate This overload is the same as the previous one, except it watches a single-item state rather than a `ListState`, as in the last example. +#### Fluent API Observe overloads + +These extension methods are available for both `IState` and `IListState` objects. + +- `IState Observe(this IState state, IMessenger messenger, Func keySelector)` + +This overload is a fluent API extension method that can be used to observe entity-change messages from the messenger for a specific entity type and apply them to the state. It returns the state itself, so it can be chained with other methods. + +```csharp +public partial record MyModel +{ + protected IUserService UserService { get; } + + public MyModel(IUserService userService, IMessenger messenger) + { + UserService = userService; + + CurrentUser + .Observe(messenger, user => user.Id) + .Observe(messenger, user => user.Name); + } + + public IState CurrentUser => State.Async(this, UserService.GetCurrentUser); +} +``` + +or in a more Fluent API way: + +```csharp +public partial record MyModel(IUserService UserService, IMessenger Messenger) +{ + public IState CurrentUser => State.Async(this, UserService.GetCurrentUser) + .Observe(Messenger, user => user.Id) + .Observe(Messenger, user => user.Name); +} +``` + +> [!NOTE] +> Please not that in this example we are using C# Primary Constructors, which is a feature available in C# 9.0. + +- `IState Observe(this IState state, IMessenger messenger, Func keySelector, out IDisposable disposable)` + +This overload is the same as the previous one, except it returns an `IDisposable` that can be used to dispose of the subscription. This will stop the state from observing entity-change messages from the messenger. + +```csharp +public partial record MyModel +{ + protected IUserService UserService { get; } + private IDisposable subscriptions; + + public MyModel(IUserService userService, IMessenger messenger) + { + UserService = userService; + + CurrentUser + .Observe(messenger, user => user.Id, out disposable); + } + + public IState CurrentUser => State.Async(this, UserService.GetCurrentUser); + + // Call this method to cancel the subscription + private void CancelSubscriptions() + { + subscriptions.Dispose(); + } +} +``` + +Two more overloads extensions are available for `IListState` and they behave the same as the `IState` overloads. + +- `IListState Observe(this IListState listState, IMessenger messenger, Func keySelector, out IDisposable disposable)` + +- `IListState Observe(this IListState listState, IMessenger messenger, Func keySelector)` + + ### Update The MVUX messaging package also includes a pair of `Update` methods that enable updating an `IState` or an `IListState` from an `EntityMessage`. From 409df4eeff90b39decd3a3fc336dd33aa63ff649 Mon Sep 17 00:00:00 2001 From: Andres Pineda <1900897+ajpinedam@users.noreply.github.com> Date: Wed, 25 Sep 2024 17:17:31 -0400 Subject: [PATCH 2/5] chore: apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Xiaotian Gu Co-authored-by: Agnès ZITTE <16295702+agneszitte@users.noreply.github.com> --- doc/Learn/Mvux/Advanced/Messaging.md | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/doc/Learn/Mvux/Advanced/Messaging.md b/doc/Learn/Mvux/Advanced/Messaging.md index 94340af051..2ac08698b5 100644 --- a/doc/Learn/Mvux/Advanced/Messaging.md +++ b/doc/Learn/Mvux/Advanced/Messaging.md @@ -334,17 +334,17 @@ or in a more Fluent API way: public partial record MyModel(IUserService UserService, IMessenger Messenger) { public IState CurrentUser => State.Async(this, UserService.GetCurrentUser) - .Observe(Messenger, user => user.Id) - .Observe(Messenger, user => user.Name); + .Observe(Messenger, user => user.Id) + .Observe(Messenger, user => user.Name); } ``` > [!NOTE] -> Please not that in this example we are using C# Primary Constructors, which is a feature available in C# 9.0. +> Please note that in this example we are using C# Primary Constructors, which is a feature available in C# 9.0. - `IState Observe(this IState state, IMessenger messenger, Func keySelector, out IDisposable disposable)` -This overload is the same as the previous one, except it returns an `IDisposable` that can be used to dispose of the subscription. This will stop the state from observing entity-change messages from the messenger. +This overload is the same as the previous one, except it returns an `IDisposable` that can be used to dispose of the subscription. When disposed, it will stop the state from observing further entity-change messages from the messenger. ```csharp public partial record MyModel @@ -370,13 +370,12 @@ public partial record MyModel } ``` -Two more overloads extensions are available for `IListState` and they behave the same as the `IState` overloads. +Two more overload extensions are available for `IListState` and they behave the same as the `IState` overloads. - `IListState Observe(this IListState listState, IMessenger messenger, Func keySelector, out IDisposable disposable)` - `IListState Observe(this IListState listState, IMessenger messenger, Func keySelector)` - ### Update The MVUX messaging package also includes a pair of `Update` methods that enable updating an `IState` or an `IListState` from an `EntityMessage`. From 7d6a01d4593fec647827bb3a2ae5344247eb4da1 Mon Sep 17 00:00:00 2001 From: Andres Pineda Date: Wed, 25 Sep 2024 17:22:11 -0400 Subject: [PATCH 3/5] docs: fix messaging identation linkter issues --- doc/Learn/Mvux/Advanced/Messaging.md | 90 ++++++++++++++-------------- 1 file changed, 45 insertions(+), 45 deletions(-) diff --git a/doc/Learn/Mvux/Advanced/Messaging.md b/doc/Learn/Mvux/Advanced/Messaging.md index 2ac08698b5..0f5ac04aa3 100644 --- a/doc/Learn/Mvux/Advanced/Messaging.md +++ b/doc/Learn/Mvux/Advanced/Messaging.md @@ -308,67 +308,67 @@ These extension methods are available for both `IState` and `IListState - `IState Observe(this IState state, IMessenger messenger, Func keySelector)` -This overload is a fluent API extension method that can be used to observe entity-change messages from the messenger for a specific entity type and apply them to the state. It returns the state itself, so it can be chained with other methods. + This overload is a fluent API extension method that can be used to observe entity-change messages from the messenger for a specific entity type and apply them to the state. It returns the state itself, so it can be chained with other methods. -```csharp -public partial record MyModel -{ - protected IUserService UserService { get; } - - public MyModel(IUserService userService, IMessenger messenger) + ```csharp + public partial record MyModel { - UserService = userService; + protected IUserService UserService { get; } - CurrentUser - .Observe(messenger, user => user.Id) - .Observe(messenger, user => user.Name); - } + public MyModel(IUserService userService, IMessenger messenger) + { + UserService = userService; - public IState CurrentUser => State.Async(this, UserService.GetCurrentUser); -} -``` + CurrentUser + .Observe(messenger, user => user.Id) + .Observe(messenger, user => user.Name); + } -or in a more Fluent API way: + public IState CurrentUser => State.Async(this, UserService.GetCurrentUser); + } + ``` -```csharp -public partial record MyModel(IUserService UserService, IMessenger Messenger) -{ - public IState CurrentUser => State.Async(this, UserService.GetCurrentUser) - .Observe(Messenger, user => user.Id) - .Observe(Messenger, user => user.Name); -} -``` + or in a more Fluent API way: -> [!NOTE] -> Please note that in this example we are using C# Primary Constructors, which is a feature available in C# 9.0. + ```csharp + public partial record MyModel(IUserService UserService, IMessenger Messenger) + { + public IState CurrentUser => State.Async(this, UserService.GetCurrentUser) + .Observe(Messenger, user => user.Id) + .Observe(Messenger, user => user.Name); + } + ``` + + > [!NOTE] + > Please note that in this example we are using C# Primary Constructors, which is a feature available in C# 9.0. - `IState Observe(this IState state, IMessenger messenger, Func keySelector, out IDisposable disposable)` -This overload is the same as the previous one, except it returns an `IDisposable` that can be used to dispose of the subscription. When disposed, it will stop the state from observing further entity-change messages from the messenger. + This overload is the same as the previous one, except it returns an `IDisposable` that can be used to dispose of the subscription. When disposed, it will stop the state from observing further entity-change messages from the messenger. -```csharp -public partial record MyModel -{ - protected IUserService UserService { get; } - private IDisposable subscriptions; - - public MyModel(IUserService userService, IMessenger messenger) + ```csharp + public partial record MyModel { - UserService = userService; + protected IUserService UserService { get; } + private IDisposable subscriptions; - CurrentUser - .Observe(messenger, user => user.Id, out disposable); - } + public MyModel(IUserService userService, IMessenger messenger) + { + UserService = userService; + + CurrentUser + .Observe(messenger, user => user.Id, out disposable); + } - public IState CurrentUser => State.Async(this, UserService.GetCurrentUser); + public IState CurrentUser => State.Async(this, UserService.GetCurrentUser); - // Call this method to cancel the subscription - private void CancelSubscriptions() - { - subscriptions.Dispose(); + // Call this method to cancel the subscription + private void CancelSubscriptions() + { + subscriptions.Dispose(); + } } -} -``` + ``` Two more overload extensions are available for `IListState` and they behave the same as the `IState` overloads. From d5c14d80c20e522a6150916aca73bd6b84aa61b8 Mon Sep 17 00:00:00 2001 From: Andres Pineda <1900897+ajpinedam@users.noreply.github.com> Date: Thu, 26 Sep 2024 08:10:35 -0400 Subject: [PATCH 4/5] chore: apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Érik Lima <114886335+eriklimakc@users.noreply.github.com> --- doc/Learn/Mvux/Advanced/Messaging.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/Learn/Mvux/Advanced/Messaging.md b/doc/Learn/Mvux/Advanced/Messaging.md index 0f5ac04aa3..4de4c6f898 100644 --- a/doc/Learn/Mvux/Advanced/Messaging.md +++ b/doc/Learn/Mvux/Advanced/Messaging.md @@ -344,7 +344,7 @@ These extension methods are available for both `IState` and `IListState - `IState Observe(this IState state, IMessenger messenger, Func keySelector, out IDisposable disposable)` - This overload is the same as the previous one, except it returns an `IDisposable` that can be used to dispose of the subscription. When disposed, it will stop the state from observing further entity-change messages from the messenger. + This overload is the same as the previous one, except it returns an `IDisposable`, which can be used to dispose of the subscription. When disposed, the state will stop observing further entity-change messages from the messenger. ```csharp public partial record MyModel @@ -370,7 +370,7 @@ These extension methods are available for both `IState` and `IListState } ``` -Two more overload extensions are available for `IListState` and they behave the same as the `IState` overloads. +Two more overload extensions are available for `IListState`, and they behave the same as the `IState` overloads. - `IListState Observe(this IListState listState, IMessenger messenger, Func keySelector, out IDisposable disposable)` From 61e2635d74f9067522e9e7e032fdf2d2a989c83a Mon Sep 17 00:00:00 2001 From: Andres Pineda Date: Thu, 26 Sep 2024 08:14:16 -0400 Subject: [PATCH 5/5] docs: fix variable reference name --- doc/Learn/Mvux/Advanced/Messaging.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/Learn/Mvux/Advanced/Messaging.md b/doc/Learn/Mvux/Advanced/Messaging.md index 4de4c6f898..7f44c0a2a0 100644 --- a/doc/Learn/Mvux/Advanced/Messaging.md +++ b/doc/Learn/Mvux/Advanced/Messaging.md @@ -357,7 +357,7 @@ These extension methods are available for both `IState` and `IListState UserService = userService; CurrentUser - .Observe(messenger, user => user.Id, out disposable); + .Observe(messenger, user => user.Id, out subscriptions); } public IState CurrentUser => State.Async(this, UserService.GetCurrentUser);