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

Commit

Permalink
LogToConsole on context and storage sets
Browse files Browse the repository at this point in the history
  • Loading branch information
chanan committed Jun 2, 2018
1 parent df32a04 commit 22de520
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/BlazorDB/BlazorDB.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<BlazorLinkOnBuild>false</BlazorLinkOnBuild>
<LangVersion>7.3</LangVersion>
<PackageId>BlazorDB</PackageId>
<Version>0.1.1</Version>
<Version>0.1.2</Version>
<Authors>Chanan Braunstein</Authors>
<Title>Blazor localStorage Database</Title>
<Description>In memory, persisted to localstorage, database for .net Blazor browser framework</Description>
Expand Down
1 change: 1 addition & 0 deletions src/BlazorDB/IStorageContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
public interface IStorageContext
{
int SaveChanges();
void LogToConsole();
}
}
11 changes: 9 additions & 2 deletions src/BlazorDB/Storage/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,17 @@ internal static class Logger
private const string Normal = "color: black; font-style: normal;";
internal static bool LogDebug { get; set; } = true;

internal static void StartContextType(Type contextType)
internal static void LogStorageSetToConsole(Type type, object list)
{
if (!LogDebug) return;
BlazorLogger.Logger.GroupCollapsed($"Context loaded: %c{contextType.Namespace}.{contextType.Name}", Blue);
BlazorLogger.Logger.Log($"StorageSet<{type.GetGenericArguments()[0].Name}>: %o", list);
}

internal static void StartContextType(Type contextType, bool loading = true)
{
if (!LogDebug) return;
var message = loading ? " loading" : " log";
BlazorLogger.Logger.GroupCollapsed($"Context{message}: %c{contextType.Namespace}.{contextType.Name}", Blue);
}

internal static void ContextSaved(Type contextType)
Expand Down
13 changes: 13 additions & 0 deletions src/BlazorDB/StorageContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ public class StorageContext : IStorageContext
{
private IStorageManager StorageManager { get; set; } = new StorageManager(); // Dep injection not working right now

public void LogToConsole()
{
Logger.StartContextType(GetType(), false);
var storageSets = StorageManagerUtil.GetStorageSets(GetType());
foreach (var prop in storageSets)
{
var storageSet = prop.GetValue(this);
var method = storageSet.GetType().GetMethod("LogToConsole");
method.Invoke(storageSet, new object[]{});
}
Logger.EndGroup();
}

public int SaveChanges()
{
return StorageManager.SaveContextToLocalStorage(this);
Expand Down
6 changes: 5 additions & 1 deletion src/BlazorDB/StorageSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

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

public void LogToConsole()
{
Logger.LogStorageSetToConsole(GetType(), List);
}

public TModel this[int index]
{
get => List[index];
Expand Down
9 changes: 8 additions & 1 deletion src/FluxorIntegration/Pages/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@

<h2>Movies</h2>

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

@if (State.Current.Movies.Count > 0)
{
Expand Down Expand Up @@ -148,4 +150,9 @@
await Dispatcher.DispatchAsync(new InsertSeedMoviesAction());
StateHasChanged();
}

async void OnDebug()
{
await Dispatcher.DispatchAsync(new LogContextAction());
}
}
8 changes: 8 additions & 0 deletions src/FluxorIntegration/Store/Movies/LogContextAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Blazor.Fluxor;

namespace FluxorIntegration.Store.Movies
{
public class LogContextAction : IAction
{
}
}
20 changes: 20 additions & 0 deletions src/FluxorIntegration/Store/Movies/LogContextReducer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Blazor.Fluxor;
using FluxorIntegration.Models;

namespace FluxorIntegration.Store.Movies
{
public class LogContextReducer : IReducer<MovieState, LogContextAction>
{
private Context Context { get; set; }

public LogContextReducer(Context context)
{
Context = context;
}
public MovieState Reduce(MovieState state, LogContextAction action)
{
Context.LogToConsole();
return state;
}
}
}

0 comments on commit 22de520

Please sign in to comment.