Skip to content
Open
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
2 changes: 2 additions & 0 deletions InertiaCore/Inertia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public static class Inertia

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

public static void FlushShared() => _factory.FlushShared();

public static AlwaysProp Always(string value) => _factory.Always(value);

public static AlwaysProp Always(Func<string> callback) => _factory.Always(callback);
Expand Down
12 changes: 12 additions & 0 deletions InertiaCore/ResponseFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ internal interface IResponseFactory
public LocationResult Location(string url);
public void Share(string key, object? value);
public void Share(IDictionary<string, object?> data);
public void FlushShared();
public AlwaysProp Always(object? value);
public AlwaysProp Always(Func<object?> callback);
public AlwaysProp Always(Func<Task<object?>> callback);
Expand Down Expand Up @@ -131,6 +132,17 @@ public void Share(IDictionary<string, object?> data)
context.Features.Set(sharedData);
}

public void FlushShared()
{
var context = _contextAccessor.HttpContext!;

var sharedData = context.Features.Get<InertiaSharedProps>();
if (sharedData != null)
{
sharedData.Clear();
}
}

public LazyProp Lazy(Func<object?> callback) => new(callback);
public LazyProp Lazy(Func<Task<object?>> callback) => new(callback);
public AlwaysProp Always(object? value) => new(value);
Expand Down
5 changes: 5 additions & 0 deletions InertiaCore/Utils/InertiaSharedProps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,9 @@ public void Set(string key, object? value)
Data ??= new Dictionary<string, object?>();
Data[key.ToCamelCase()] = value;
}

public void Clear()
{
Data = null;
}
}
49 changes: 49 additions & 0 deletions InertiaCoreTests/UnitTestSharedData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,53 @@ public async Task TestSharedProps()
{ "errors", new Dictionary<string, string>(0) }
}));
}

[Test]
[Description("Test if FlushShared clears all shared data properly.")]
public async Task TestFlushShared()
{
var response = _factory.Render("Test/Page", new
{
Test = "Test"
});

var sharedProps = new InertiaSharedProps();
sharedProps.Set("TestShared", "Shared");
sharedProps.Set("AnotherShared", "AnotherValue");

var context = PrepareContext(null, sharedProps);

response.SetContext(context);
await response.ProcessResponse();

var page = response.GetJson().Value as Page;

Assert.That(page?.Props, Is.EqualTo(new Dictionary<string, object?>
{
{ "test", "Test" },
{ "testShared", "Shared" },
{ "anotherShared", "AnotherValue" },
{ "errors", new Dictionary<string, string>(0) }
}));

sharedProps.Clear();

var responseAfterFlush = _factory.Render("Test/Page", new
{
Test = "Test"
});

var contextAfterFlush = PrepareContext(null, sharedProps);

responseAfterFlush.SetContext(contextAfterFlush);
await responseAfterFlush.ProcessResponse();

var pageAfterFlush = responseAfterFlush.GetJson().Value as Page;

Assert.That(pageAfterFlush?.Props, Is.EqualTo(new Dictionary<string, object?>
{
{ "test", "Test" },
{ "errors", new Dictionary<string, string>(0) }
}));
}
}