Skip to content
This repository was archived by the owner on May 10, 2026. It is now read-only.

Commit c04d2bb

Browse files
committed
Change: NavigationOptions/Context to Immutable
1 parent 209e37e commit c04d2bb

4 files changed

Lines changed: 22 additions & 55 deletions

File tree

src/NavStack/Assets/NavStack/Runtime/Internal/NavigationSheetCore.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,10 @@ public UniTask RemoveAllAsync(CancellationToken cancellationToken = default)
6161

6262
public async UniTask ShowAsync(INavigation navigation, int index, NavigationContext context, CancellationToken cancellationToken = default)
6363
{
64-
var copiedContext = context.CreateCopy();
65-
copiedContext.Options = context.Options ?? navigation.DefaultOptions;
64+
var copiedContext = context with
65+
{
66+
Options = context.Options ?? navigation.DefaultOptions
67+
};
6668

6769
if (isRunning)
6870
{
@@ -101,8 +103,10 @@ public async UniTask ShowAsync(INavigation navigation, int index, NavigationCont
101103

102104
public async UniTask HideAsync(INavigation navigation, NavigationContext context, CancellationToken cancellationToken = default)
103105
{
104-
var copiedContext = context.CreateCopy();
105-
copiedContext.Options = context.Options ?? navigation.DefaultOptions;
106+
var copiedContext = context with
107+
{
108+
Options = context.Options ?? navigation.DefaultOptions
109+
};
106110

107111
if (activePage == null)
108112
{

src/NavStack/Assets/NavStack/Runtime/Internal/NavigationStackCore.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ public sealed class NavigationStackCore
2121

2222
public async UniTask PopAsync(INavigation navigation, NavigationContext context, CancellationToken cancellationToken = default)
2323
{
24-
var copiedContext = context.CreateCopy();
25-
copiedContext.Options = context.Options ?? navigation.DefaultOptions;
24+
var copiedContext = context with
25+
{
26+
Options = context.Options ?? navigation.DefaultOptions
27+
};
2628

2729
if (isRunning)
2830
{
@@ -69,8 +71,10 @@ public async UniTask PopAsync(INavigation navigation, NavigationContext context,
6971

7072
public async UniTask PushAsync(INavigation navigation, Func<UniTask<IPage>> pageFactory, NavigationContext context, CancellationToken cancellationToken = default)
7173
{
72-
var copiedContext = context.CreateCopy();
73-
copiedContext.Options = context.Options ?? navigation.DefaultOptions;
74+
var copiedContext = context with
75+
{
76+
Options = context.Options ?? navigation.DefaultOptions
77+
};
7478

7579
if (isRunning)
7680
{

src/NavStack/Assets/NavStack/Runtime/NavigationContext.cs

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,9 @@
22

33
namespace NavStack
44
{
5-
public class NavigationContext
5+
public record NavigationContext
66
{
7-
public NavigationOptions Options { get; set; }
8-
public IDictionary<object, object> Parameters { get; set; } = new Dictionary<object, object>();
9-
10-
public virtual NavigationContext CreateCopy()
11-
{
12-
var context = new NavigationContext()
13-
{
14-
Options = new(Options),
15-
Parameters = new Dictionary<object, object>()
16-
};
17-
18-
foreach (var kv in Parameters)
19-
{
20-
context.Parameters.Add(kv);
21-
}
22-
23-
return context;
24-
}
7+
public NavigationOptions Options { get; init; }
8+
public IDictionary<object, object> Parameters { get; init; } = new Dictionary<object, object>();
259
}
2610
}
Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,8 @@
1-
using System;
2-
using UnityEngine;
3-
41
namespace NavStack
52
{
6-
[Serializable]
7-
public sealed class NavigationOptions
3+
public record NavigationOptions
84
{
9-
public NavigationOptions() { }
10-
public NavigationOptions(NavigationOptions source)
11-
{
12-
if (source == null) return;
13-
14-
animated = source.animated;
15-
awaitOperation = source.awaitOperation;
16-
}
17-
18-
[SerializeField] bool animated = true;
19-
[SerializeField] NavigationAwaitOperation awaitOperation = NavigationAwaitOperation.Error;
20-
21-
public bool Animated
22-
{
23-
get => animated;
24-
set => animated = value;
25-
}
26-
27-
public NavigationAwaitOperation AwaitOperation
28-
{
29-
get => awaitOperation;
30-
set => awaitOperation = value;
31-
}
5+
public bool Animated { get; init; } = true;
6+
public NavigationAwaitOperation AwaitOperation { get; init; } = NavigationAwaitOperation.Error;
327
}
338
}

0 commit comments

Comments
 (0)