Skip to content

Commit

Permalink
feat(UIComponent): add InitializationTask property
Browse files Browse the repository at this point in the history
  • Loading branch information
jonisavo committed Sep 3, 2022
1 parent 09274bd commit 5596a42
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
6 changes: 3 additions & 3 deletions Assets/UIComponents.Benchmarks/BenchmarkUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace UIComponents.Benchmarks
{
public static class BenchmarkUtils
{
public const string Version = "0.21.0.0";
public const string Version = "0.22.0.0";

private static SampleGroup[] GetProfilerMarkers()
{
Expand All @@ -22,7 +22,7 @@ public static void MeasureComponentInitWithColdCache<TComponent>() where TCompon
Measure.Method(async () =>
{
var component = new TComponent();
await component.WaitForInitialization();
await component.InitializationTask;
})
.SetUp(() =>
{
Expand All @@ -42,7 +42,7 @@ public static void MeasureComponentInitWithWarmCache<TComponent>() where TCompon
Measure.Method(async () =>
{
var component = new TComponent();
await component.WaitForInitialization();
await component.InitializationTask;
})
.SampleGroup(new SampleGroup("Warm Cache Time"))
.MeasurementCount(50)
Expand Down
7 changes: 6 additions & 1 deletion Assets/UIComponents/Core/UIComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public abstract class UIComponent : VisualElement
/// </summary>
public bool Initialized { get; private set; }

/// <summary>
/// A Task that completes when the UIComponent has been fully initialized.
/// </summary>
public Task<UIComponent> InitializationTask => _initCompletionSource.Task;

/// <summary>
/// The IUIComponentLogger used by this UIComponent.
/// Defaults to <see cref="UIComponentDebugLogger"/>.
Expand Down Expand Up @@ -186,7 +191,7 @@ public Task<UIComponent> WaitForInitialization()
/// <returns>An enumerator which yields when the component has initialized</returns>
public IEnumerator WaitForInitializationEnumerator()
{
yield return WaitForInitialization().AsEnumerator();
yield return _initCompletionSource.Task.AsEnumerator();
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Assets/UIComponents/Testing/TestBed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public async Task<TComponent> CreateComponentAsync<TComponent>(Func<TComponent>
{
var component = CreateComponent(factoryPredicate);

var initTask = component.WaitForInitialization();
var initTask = component.InitializationTask;
var timeoutTask = Task.Delay(AsyncTimeout);

var task = await Task.WhenAny(initTask, timeoutTask);
Expand Down

0 comments on commit 5596a42

Please sign in to comment.