Skip to content

Commit f64770f

Browse files
authored
[1.x] Add flushShared() method for clearing all shared data (#44)
inertiajs/inertia-laravel#244
1 parent f8e7b85 commit f64770f

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed

InertiaCore/Inertia.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public static BackResult Back(string? fallbackUrl = null, HttpStatusCode statusC
3535

3636
public static void Share(IDictionary<string, object?> data) => _factory.Share(data);
3737

38+
public static void FlushShared() => _factory.FlushShared();
39+
3840
public static AlwaysProp Always(string value) => _factory.Always(value);
3941

4042
public static AlwaysProp Always(Func<string> callback) => _factory.Always(callback);

InertiaCore/ResponseFactory.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ internal interface IResponseFactory
2121
public BackResult Back(string? fallbackUrl = null, HttpStatusCode statusCode = HttpStatusCode.SeeOther);
2222
public void Share(string key, object? value);
2323
public void Share(IDictionary<string, object?> data);
24+
public void FlushShared();
2425
public AlwaysProp Always(object? value);
2526
public AlwaysProp Always(Func<object?> callback);
2627
public AlwaysProp Always(Func<Task<object?>> callback);
@@ -129,6 +130,17 @@ public void Share(IDictionary<string, object?> data)
129130
context.Features.Set(sharedData);
130131
}
131132

133+
public void FlushShared()
134+
{
135+
var context = _contextAccessor.HttpContext!;
136+
137+
var sharedData = context.Features.Get<InertiaSharedProps>();
138+
if (sharedData != null)
139+
{
140+
sharedData.Clear();
141+
}
142+
}
143+
132144
public LazyProp Lazy(Func<object?> callback) => new(callback);
133145
public LazyProp Lazy(Func<Task<object?>> callback) => new(callback);
134146
public AlwaysProp Always(object? value) => new(value);

InertiaCore/Utils/InertiaSharedProps.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,9 @@ public void Set(string key, object? value)
2626
Data ??= new Dictionary<string, object?>();
2727
Data[key.ToCamelCase()] = value;
2828
}
29+
30+
public void Clear()
31+
{
32+
Data = null;
33+
}
2934
}

InertiaCoreTests/UnitTestSharedData.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,53 @@ public async Task TestSharedProps()
3131
{ "errors", new Dictionary<string, string>(0) }
3232
}));
3333
}
34+
35+
[Test]
36+
[Description("Test if FlushShared clears all shared data properly.")]
37+
public async Task TestFlushShared()
38+
{
39+
var response = _factory.Render("Test/Page", new
40+
{
41+
Test = "Test"
42+
});
43+
44+
var sharedProps = new InertiaSharedProps();
45+
sharedProps.Set("TestShared", "Shared");
46+
sharedProps.Set("AnotherShared", "AnotherValue");
47+
48+
var context = PrepareContext(null, sharedProps);
49+
50+
response.SetContext(context);
51+
await response.ProcessResponse();
52+
53+
var page = response.GetJson().Value as Page;
54+
55+
Assert.That(page?.Props, Is.EqualTo(new Dictionary<string, object?>
56+
{
57+
{ "test", "Test" },
58+
{ "testShared", "Shared" },
59+
{ "anotherShared", "AnotherValue" },
60+
{ "errors", new Dictionary<string, string>(0) }
61+
}));
62+
63+
sharedProps.Clear();
64+
65+
var responseAfterFlush = _factory.Render("Test/Page", new
66+
{
67+
Test = "Test"
68+
});
69+
70+
var contextAfterFlush = PrepareContext(null, sharedProps);
71+
72+
responseAfterFlush.SetContext(contextAfterFlush);
73+
await responseAfterFlush.ProcessResponse();
74+
75+
var pageAfterFlush = responseAfterFlush.GetJson().Value as Page;
76+
77+
Assert.That(pageAfterFlush?.Props, Is.EqualTo(new Dictionary<string, object?>
78+
{
79+
{ "test", "Test" },
80+
{ "errors", new Dictionary<string, string>(0) }
81+
}));
82+
}
3483
}

0 commit comments

Comments
 (0)