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

Commit 22de520

Browse files
committed
LogToConsole on context and storage sets
1 parent df32a04 commit 22de520

File tree

8 files changed

+65
-5
lines changed

8 files changed

+65
-5
lines changed

src/BlazorDB/BlazorDB.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<BlazorLinkOnBuild>false</BlazorLinkOnBuild>
88
<LangVersion>7.3</LangVersion>
99
<PackageId>BlazorDB</PackageId>
10-
<Version>0.1.1</Version>
10+
<Version>0.1.2</Version>
1111
<Authors>Chanan Braunstein</Authors>
1212
<Title>Blazor localStorage Database</Title>
1313
<Description>In memory, persisted to localstorage, database for .net Blazor browser framework</Description>

src/BlazorDB/IStorageContext.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
public interface IStorageContext
44
{
55
int SaveChanges();
6+
void LogToConsole();
67
}
78
}

src/BlazorDB/Storage/Logger.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,17 @@ internal static class Logger
1010
private const string Normal = "color: black; font-style: normal;";
1111
internal static bool LogDebug { get; set; } = true;
1212

13-
internal static void StartContextType(Type contextType)
13+
internal static void LogStorageSetToConsole(Type type, object list)
1414
{
1515
if (!LogDebug) return;
16-
BlazorLogger.Logger.GroupCollapsed($"Context loaded: %c{contextType.Namespace}.{contextType.Name}", Blue);
16+
BlazorLogger.Logger.Log($"StorageSet<{type.GetGenericArguments()[0].Name}>: %o", list);
17+
}
18+
19+
internal static void StartContextType(Type contextType, bool loading = true)
20+
{
21+
if (!LogDebug) return;
22+
var message = loading ? " loading" : " log";
23+
BlazorLogger.Logger.GroupCollapsed($"Context{message}: %c{contextType.Namespace}.{contextType.Name}", Blue);
1724
}
1825

1926
internal static void ContextSaved(Type contextType)

src/BlazorDB/StorageContext.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,19 @@ public class StorageContext : IStorageContext
66
{
77
private IStorageManager StorageManager { get; set; } = new StorageManager(); // Dep injection not working right now
88

9+
public void LogToConsole()
10+
{
11+
Logger.StartContextType(GetType(), false);
12+
var storageSets = StorageManagerUtil.GetStorageSets(GetType());
13+
foreach (var prop in storageSets)
14+
{
15+
var storageSet = prop.GetValue(this);
16+
var method = storageSet.GetType().GetMethod("LogToConsole");
17+
method.Invoke(storageSet, new object[]{});
18+
}
19+
Logger.EndGroup();
20+
}
21+
922
public int SaveChanges()
1023
{
1124
return StorageManager.SaveContextToLocalStorage(this);

src/BlazorDB/StorageSet.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System;
33
using System.Collections;
44
using System.Collections.Generic;
5-
using System.Linq;
65

76
namespace BlazorDB
87
{
@@ -11,6 +10,11 @@ public class StorageSet<TModel> : IList<TModel> where TModel : class
1110
private string StorageContextTypeName { get; set; }
1211
private IList<TModel> List { get; set; } = new List<TModel>();
1312

13+
public void LogToConsole()
14+
{
15+
Logger.LogStorageSetToConsole(GetType(), List);
16+
}
17+
1418
public TModel this[int index]
1519
{
1620
get => List[index];

src/FluxorIntegration/Pages/Index.cshtml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323

2424
<h2>Movies</h2>
2525

26-
<button class="btn btn-info" onclick="@OnLoadMovies">Load Movies</button><button class="btn btn-info" onclick="@OnInsertSeedData">Insert Seed Data</button>
26+
<button class="btn btn-info" onclick="@OnLoadMovies">Load Movies</button>
27+
<button class="btn btn-info" onclick="@OnInsertSeedData">Insert Seed Data</button>
28+
<button class="btn btn-info" onclick="@OnDebug">Print to Console</button>
2729

2830
@if (State.Current.Movies.Count > 0)
2931
{
@@ -148,4 +150,9 @@
148150
await Dispatcher.DispatchAsync(new InsertSeedMoviesAction());
149151
StateHasChanged();
150152
}
153+
154+
async void OnDebug()
155+
{
156+
await Dispatcher.DispatchAsync(new LogContextAction());
157+
}
151158
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using Blazor.Fluxor;
2+
3+
namespace FluxorIntegration.Store.Movies
4+
{
5+
public class LogContextAction : IAction
6+
{
7+
}
8+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Blazor.Fluxor;
2+
using FluxorIntegration.Models;
3+
4+
namespace FluxorIntegration.Store.Movies
5+
{
6+
public class LogContextReducer : IReducer<MovieState, LogContextAction>
7+
{
8+
private Context Context { get; set; }
9+
10+
public LogContextReducer(Context context)
11+
{
12+
Context = context;
13+
}
14+
public MovieState Reduce(MovieState state, LogContextAction action)
15+
{
16+
Context.LogToConsole();
17+
return state;
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)