Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions src/Uno.Extensions.Reactive/Core/ListState.T.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,36 @@ public static IListState<T> Empty<TOwner>(TOwner owner, [CallerMemberName] strin
/// <typeparam name="TOwner">Type of the owner of the state.</typeparam>
/// <param name="owner">The owner of the state.</param>
/// <param name="valueProvider">The provider of the initial value of the state.</param>
/// <param name="name">The caller member name, used as a stable cache key.</param>
/// <param name="line">The caller line number, used as a stable cache key.</param>
/// <returns>A feed that encapsulate the source.</returns>
public static IListState<T> Value<TOwner>(TOwner owner, Func<IImmutableList<T>> valueProvider)
public static IListState<T> Value<TOwner>(TOwner owner, Func<IImmutableList<T>> valueProvider, [CallerMemberName] string? name = null, [CallerLineNumber] int line = -1)
where TOwner : class
// Note: We force the usage of delegate so 2 properties which are doing State.Value(this, () => 42) will effectively have 2 distinct states.
=> AttachedProperty.GetOrCreate(owner, valueProvider, static (o, v) => SourceContext.GetOrCreate(o).CreateListState(Option<IImmutableList<T>>.Some(v())));
// Use CallerMemberName+line as stable cache key instead of delegate reference identity.
// Delegate instances can be recreated after MetadataUpdater.ApplyUpdate on WASM,
// which would cause cache misses and state recreation (spec 033).
=> AttachedProperty.GetOrCreate<TOwner, (string, int), Func<IImmutableList<T>>, IListState<T>>(
owner,
(name ?? throw new InvalidOperationException("The name of the list state must not be null"), line < 0 ? throw new InvalidOperationException("The provided line number is invalid.") : line),
valueProvider,
static (o, _, v) => SourceContext.GetOrCreate(o).CreateListState(Option<IImmutableList<T>>.Some(v())));

/// <summary>
/// Gets or creates a list state from a static initial list of items.
/// </summary>
/// <typeparam name="TOwner">Type of the owner of the state.</typeparam>
/// <param name="owner">The owner of the state.</param>
/// <param name="valueProvider">The provider of the initial value of the state.</param>
/// <param name="name">The caller member name, used as a stable cache key.</param>
/// <param name="line">The caller line number, used as a stable cache key.</param>
/// <returns>A feed that encapsulate the source.</returns>
public static IListState<T> Value<TOwner>(TOwner owner, Func<ImmutableList<T>> valueProvider)
public static IListState<T> Value<TOwner>(TOwner owner, Func<ImmutableList<T>> valueProvider, [CallerMemberName] string? name = null, [CallerLineNumber] int line = -1)
where TOwner : class
// Note: We force the usage of delegate so 2 properties which are doing State.Value(this, () => 42) will effectively have 2 distinct states.
=> AttachedProperty.GetOrCreate(owner, valueProvider, static (o, v) => SourceContext.GetOrCreate(o).CreateListState(Option<IImmutableList<T>>.Some(v())));
=> AttachedProperty.GetOrCreate<TOwner, (string, int), Func<ImmutableList<T>>, IListState<T>>(
owner,
(name ?? throw new InvalidOperationException("The name of the list state must not be null"), line < 0 ? throw new InvalidOperationException("The provided line number is invalid.") : line),
valueProvider,
static (o, _, v) => SourceContext.GetOrCreate(o).CreateListState(Option<IImmutableList<T>>.Some(v())));

/// <summary>
/// Gets or creates a list state from a static initial list of items.
Expand Down
15 changes: 9 additions & 6 deletions src/Uno.Extensions.Reactive/Core/ListState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;

namespace Uno.Extensions.Reactive;
Expand Down Expand Up @@ -34,11 +35,12 @@ public static IListState<TValue> Create<TOwner, TValue>(TOwner owner, Func<Cance
/// <typeparam name="TValue">The type of the value of the resulting feed.</typeparam>
/// <param name="owner">The owner of the state.</param>
/// <param name="valueProvider">The provider of the initial value of the state.</param>
/// <param name="name">The caller member name, used as a stable cache key.</param>
/// <param name="line">The caller line number, used as a stable cache key.</param>
/// <returns>A state that encapsulate the source.</returns>
public static IListState<TValue> Value<TOwner, TValue>(TOwner owner, Func<IImmutableList<TValue>> valueProvider)
public static IListState<TValue> Value<TOwner, TValue>(TOwner owner, Func<IImmutableList<TValue>> valueProvider, [CallerMemberName] string? name = null, [CallerLineNumber] int line = -1)
where TOwner : class
// Note: We force the usage of delegate so 2 properties which are doing State.Value(this, () => 42) will effectively have 2 distinct states.
=> ListState<TValue>.Value(owner, valueProvider);
=> ListState<TValue>.Value(owner, valueProvider, name, line);

/// <summary>
/// Gets or creates a list state from a static initial list of items.
Expand All @@ -47,11 +49,12 @@ public static IListState<TValue> Value<TOwner, TValue>(TOwner owner, Func<IImmut
/// <typeparam name="TValue">The type of the value of the resulting feed.</typeparam>
/// <param name="owner">The owner of the state.</param>
/// <param name="valueProvider">The provider of the initial value of the state.</param>
/// <param name="name">The caller member name, used as a stable cache key.</param>
/// <param name="line">The caller line number, used as a stable cache key.</param>
/// <returns>A state that encapsulate the source.</returns>
public static IListState<TValue> Value<TOwner, TValue>(TOwner owner, Func<ImmutableList<TValue>> valueProvider)
public static IListState<TValue> Value<TOwner, TValue>(TOwner owner, Func<ImmutableList<TValue>> valueProvider, [CallerMemberName] string? name = null, [CallerLineNumber] int line = -1)
where TOwner : class
// Note: We force the usage of delegate so 2 properties which are doing State.Value(this, () => 42) will effectively have 2 distinct states.
=> ListState<TValue>.Value(owner, valueProvider);
=> ListState<TValue>.Value(owner, valueProvider, name, line);

/// <summary>
/// Gets or creates a list state from an async method.
Expand Down
Loading