-
-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix #948 [doc] PersistentComponentState while prerendering: There is …
…already a persisted object under the same key 'ColorMode'
- Loading branch information
Showing
15 changed files
with
172 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
27 changes: 0 additions & 27 deletions
27
Havit.Blazor.Documentation.Server/DocColorModeServerResolver.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,5 +6,6 @@ | |
/// </summary> | ||
public interface IHttpContextProxy | ||
{ | ||
bool IsSupported(); | ||
string GetCookieValue(string key); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
...t.Blazor.Documentation/Shared/Components/DocColorMode/DocColorModeCascadingValueSource.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
namespace Havit.Blazor.Documentation.Shared.Components.DocColorMode; | ||
|
||
// Reference implementation in AuthenticationStateCascadingValueSource | ||
// https://github.com/dotnet/aspnetcore/blob/79d06db8a4be29165e24eb841054a337161bd09a/src/Components/Authorization/src/CascadingAuthenticationStateServiceCollectionExtensions.cs#L29-L56 | ||
public class DocColorModeCascadingValueSource : CascadingValueSource<ColorMode>, IDisposable | ||
{ | ||
private readonly IDocColorModeProvider _docColorModeStateProvider; | ||
|
||
public DocColorModeCascadingValueSource(IDocColorModeProvider docColorModeStateProvider) | ||
: base(docColorModeStateProvider.GetColorMode, isFixed: false) | ||
{ | ||
_docColorModeStateProvider = docColorModeStateProvider; | ||
_docColorModeStateProvider.ColorModeChanged += HandleColorModeChanged; | ||
} | ||
|
||
private void HandleColorModeChanged(ColorMode colorMode) | ||
{ | ||
// It's OK to discard the task because this only represents the duration of the dispatch to sync context. | ||
// It handles any exceptions internally by dispatching them to the renderer within the context of whichever | ||
// component threw when receiving the update. This is the same as how a CascadingValue doesn't get notified | ||
// about exceptions that happen inside the recipients of value notifications. | ||
_ = NotifyChangedAsync(colorMode); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_docColorModeStateProvider.ColorModeChanged -= HandleColorModeChanged; | ||
} | ||
} |
9 changes: 0 additions & 9 deletions
9
Havit.Blazor.Documentation/Shared/Components/DocColorMode/DocColorModeClientResolver.cs
This file was deleted.
Oops, something went wrong.
78 changes: 78 additions & 0 deletions
78
Havit.Blazor.Documentation/Shared/Components/DocColorMode/DocColorModeProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
using Havit.Blazor.Documentation.Services; | ||
|
||
namespace Havit.Blazor.Documentation.Shared.Components.DocColorMode; | ||
|
||
public class DocColorModeProvider : IDisposable, IDocColorModeProvider | ||
{ | ||
private readonly IHttpContextProxy _httpContextProxy; | ||
private readonly PersistentComponentState _persistentComponentState; | ||
private PersistingComponentStateSubscription _persistingComponentStateSubscription; | ||
private ColorMode? _colorMode; | ||
|
||
public DocColorModeProvider( | ||
IHttpContextProxy httpContextProxy, | ||
PersistentComponentState persistentComponentState) | ||
{ | ||
_httpContextProxy = httpContextProxy; | ||
_persistentComponentState = persistentComponentState; | ||
_persistingComponentStateSubscription = _persistentComponentState.RegisterOnPersisting(PersistMode); | ||
} | ||
|
||
public event ColorModeChangedHandler ColorModeChanged; | ||
|
||
/// <summary> | ||
/// Raises the <see cref="AuthenticationStateChanged"/> event. | ||
/// </summary> | ||
/// <param name="task">A <see cref="Task"/> that supplies the updated <see cref="AuthenticationState"/>.</param> | ||
public void SetColorMode(ColorMode colorMode) | ||
{ | ||
_colorMode = colorMode; | ||
ColorModeChanged?.Invoke(colorMode); | ||
} | ||
|
||
public ColorMode GetColorMode() | ||
{ | ||
if (_colorMode == null) | ||
{ | ||
ResolveInitialColorMode(); | ||
} | ||
return _colorMode.Value; | ||
} | ||
|
||
private void ResolveInitialColorMode() | ||
{ | ||
// prerendering | ||
if (_httpContextProxy.IsSupported() | ||
&& _httpContextProxy.GetCookieValue("ColorMode") is string cookie | ||
&& Enum.TryParse<ColorMode>(cookie, ignoreCase: true, out var mode)) | ||
{ | ||
_colorMode = mode; | ||
} | ||
else if (_persistentComponentState.TryTakeFromJson<ColorMode>("ColorMode", out var restored)) | ||
{ | ||
_colorMode = restored; | ||
} | ||
else | ||
{ | ||
_colorMode = ColorMode.Auto; | ||
} | ||
} | ||
|
||
private Task PersistMode() | ||
{ | ||
_persistentComponentState.PersistAsJson("ColorMode", GetColorMode()); | ||
return Task.CompletedTask; | ||
} | ||
|
||
void IDisposable.Dispose() | ||
{ | ||
_persistingComponentStateSubscription.Dispose(); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// A handler for the <see cref="AuthenticationStateProvider.AuthenticationStateChanged"/> event. | ||
/// </summary> | ||
/// <param name="task">A <see cref="Task"/> that supplies the updated <see cref="AuthenticationState"/>.</param> | ||
public delegate void ColorModeChangedHandler(ColorMode colorMode); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
Havit.Blazor.Documentation/Shared/Components/DocColorMode/IDocColorModeProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace Havit.Blazor.Documentation.Shared.Components.DocColorMode; | ||
|
||
public interface IDocColorModeProvider | ||
{ | ||
event ColorModeChangedHandler ColorModeChanged; | ||
|
||
ColorMode GetColorMode(); | ||
void SetColorMode(ColorMode colorMode); | ||
} |
6 changes: 0 additions & 6 deletions
6
Havit.Blazor.Documentation/Shared/Components/DocColorMode/IDocColorModeResolver.cs
This file was deleted.
Oops, something went wrong.