From c2e76b31b9ead80027c068b1549dddace888c796 Mon Sep 17 00:00:00 2001 From: Kunal22shah Date: Tue, 3 Sep 2024 10:08:18 -0400 Subject: [PATCH] docs: removing reference samples --- doc/Reference/Reactive/listfeed.md | 40 +++---------- doc/Reference/Reactive/state.md | 90 ++++-------------------------- 2 files changed, 21 insertions(+), 109 deletions(-) diff --git a/doc/Reference/Reactive/listfeed.md b/doc/Reference/Reactive/listfeed.md index 23be25d5b..812fb3935 100644 --- a/doc/Reference/Reactive/listfeed.md +++ b/doc/Reference/Reactive/listfeed.md @@ -25,35 +25,24 @@ A couple of points to note about list-feeds: To create an `IListFeed`, use the static class `ListFeed` to call one of the following methods: **Async**: Creates a `ListFeed` using a method that returns a `Task>`. - -- Service code: ```csharp -public ValueTask> GetNames(CancellationToken ct = default); +public static IListFeed Async(Func>> valueProvider, Signal? refresh = null); ``` -- Model code: - -```csharp -public IListFeed Names => ListFeed.Async(service.GetNames); -``` **AsyncEnumerable**: Creates a `ListFeed` using an `IAsyncEnumerable>`. -- Service code: ```csharp -public IAsyncEnumerable> GetNames( - [EnumeratorCancellation] CancellationToken ct = default); +public static IListFeed AsyncEnumerable(Func>> enumerableProvider); ``` -- Model code: - -```csharp -public IListFeed Names => ListFeed.AsyncEnumerable(service.GetNames); -``` Pull and push are explained more in the [feeds page](xref:Uno.Extensions.Mvux.Feeds#creation-of-feeds). **Create**: Provides custom initialization for a `ListFeed`. +```csharp +public static IListFeed Create(Func>>> sourceProvider); +``` There are also 2 helpers that allow you to convert from/to a _feed_ to/from a _list feed_. ## Operators: How to interact with a list feed @@ -68,7 +57,7 @@ Used among the generated view models and a `ListView`, when the user scroll and which will trigger the load of the next page using the delegate that you provided. ```csharp -public IListFeed Cities => ListFeed.AsyncPaginated(async (page, ct) => _service.GetCities(pageIndex: page.Index, perPage: 20)); +public static IListFeed PaginatedAsync(Func>> getPage); ``` > [!CAUTION] @@ -87,7 +76,7 @@ This operator allows the filtering of _items_. > If all _items_ of the collection are filtered out, the resulting feed will go in _none_ state. ```csharp -public IListFeed LongNames => Names.Where(name => name.Length >= 10); +public static IListFeed Where(this IListFeed source, Predicate predicate); ``` ### AsFeed @@ -95,11 +84,7 @@ public IListFeed LongNames => Names.Where(name => name.Length >= 10); This does the opposite of `AsListFeed` and converts a _list feed_ to a _feed of list_. ```csharp -public void SetUp() -{ - IListFeed stringsListFeed = ...; - IFeed> stringsFeed = stringsListFeed.AsFeed(); -} +public static IFeed> AsFeed(this IListFeed source); ``` ### AsListFeed @@ -107,12 +92,5 @@ public void SetUp() A `ListFeed` can also be created from a `Feed` when the `Feed` exposes a collection (`IFeed>`): ```csharp -public IListFeed Forecast => Feed - .Async(async ct => new [] - { - await _weatherService.GetWeatherForecast(DateTime.Today.AddDays(1), ct), - await _weatherService.GetWeatherForecast(DateTime.Today.AddDays(2), ct), - }) - .Select(list => list.ToImmutableList()) - .AsListFeed(); +public static IListFeed AsListFeed(this IFeed> source); ``` \ No newline at end of file diff --git a/doc/Reference/Reactive/state.md b/doc/Reference/Reactive/state.md index 1acf3c823..e90e417bb 100644 --- a/doc/Reference/Reactive/state.md +++ b/doc/Reference/Reactive/state.md @@ -26,7 +26,7 @@ You can create a _state_ using one of the following: Creates a state without any initial value. ```csharp -public IState City => State.Empty(this); +public static IState Empty(object owner); ``` ### Value @@ -34,7 +34,7 @@ public IState City => State.Empty(this); Creates a state with a synchronous initial value. ```csharp -public IState City => State.Value(this, () => "Montréal"); +public static IState Value(object owner, Func valueProvider); ``` ### Async @@ -42,7 +42,7 @@ public IState City => State.Value(this, () => "Montréal"); Creates a state with an asynchronous initial value. ```csharp -public IState City => State.Async(this, async ct => await _locationService.GetCurrentCity(ct)); +public static IState Async(object owner, Func> asyncFunc, Signal? refreshSignal = null); ``` ### AsyncEnumerable @@ -50,16 +50,7 @@ public IState City => State.Async(this, async ct => await _locationServi Like for `Feed.AsyncEnumerable`, this allows you to adapt an `IAsyncEnumerable` into a _state_. ```csharp -public IState City => State.AsyncEnumerable(this, () => GetCurrentCity()); - -public async IAsyncEnumerable GetCurrentCity([EnumeratorCancellation] CancellationToken ct = default) -{ - while (!ct.IsCancellationRequested) - { - yield return await _locationService.GetCurrentCity(ct); - await Task.Delay(TimeSpan.FromMinutes(15), ct); - } -} +public static IState AsyncEnumerable(object owner, Func> asyncEnumerableFunc); ``` ### Create @@ -69,37 +60,14 @@ This gives you the ability to create your own _state_ by dealing directly with _ > This is designed for advanced usage and should probably not be used directly in apps. ```csharp -public IState City => State.Create(this, GetCurrentCity); - -public async IAsyncEnumerable> GetCurrentCity([EnumeratorCancellation] CancellationToken ct = default) -{ - var message = Message.Initial; - var city = Option.Undefined(); - var error = default(Exception); - while (!ct.IsCancellationRequested) - { - try - { - city = await _locationService.GetCurrentCity(ct); - error = default; - } - catch (Exception ex) - { - error = ex; - } - - yield return message = message.With().Data(city).Error(error); - await Task.Delay(TimeSpan.FromHours(1), ct); - } -} +public static IState Create(object owner, Func>> messageFunc); ``` ### From a feed A state can easily be converted from a feed as follows: ```csharp -public IFeed MyFeed => ... -public IState MyState => State.FromFeed(this, MyFeed); +public static IState FromFeed(object owner, IFeed feed); ``` ## Update: How to update a state @@ -117,13 +85,7 @@ This makes sure that you are working with the latest version of the data. This allows you to update the value only of the state. ```csharp -public IState City => State.Empty(this); - -public async ValueTask SetCurrent(CancellationToken ct) -{ - var city = await _locationService.GetCurrentCity(ct); - await City.UpdateValue(_ => city, ct); -} +public static Task UpdateValue(this IState state, Func updater, CancellationToken ct = default); ``` ### Set @@ -131,20 +93,7 @@ public async ValueTask SetCurrent(CancellationToken ct) For value types and strings, you also have a `Set` **which does not ensure the respect of the ACID properties**. ```csharp -public IState Error => State.Empty(this); - -public async ValueTask Share(CancellationToken ct) -{ - try - { - ../.. - await Error.Set(string.Empty, ct); - } - catch (Exception error) - { - await Error.Set("Share failed.", ct); - } -} +public static Task Set(this IState state, T value, CancellationToken ct = default); ``` > [!CAUTION] @@ -182,7 +131,7 @@ This gives you the ability to update a _state_, including the metadata. States are advanced Feeds. As such, they can also be awaited directly: ```csharp -City currentCity = await this.CurrentCity; +public static TaskAwaiter GetAwaiter(this IState state); ``` ### Binding the View to a State @@ -269,25 +218,10 @@ public async ValueTask SetSliderMiddle(CancellationToken ct = default) The `ForEach` enables executing a callback each time the value of the `IState` is updated. This extension-method takes a single parameter which is a async callback that takes two parameters. The first parameter is of type `T?`, where `T` is type of the `IState`, and represents the new value of the state. The second parameter is a `CancellationToken` which can be used to cancel a long running action. - -For example: - + ```csharp - public partial record Model - { - public IState MyState => ... - - public async ValueTask EnableChangeTracking() - { - MyState.ForEach(PerformAction); - } - - public async ValueTask PerformAction(string item, CancellationToken ct) - { - ... - } - } - ``` +public static IDisposable ForEach(this IState state, Func action); + ``` Additionally, the `ForEach` method can be set using the Fluent API: