-
Notifications
You must be signed in to change notification settings - Fork 3
/
IApp.cs
136 lines (112 loc) · 2.99 KB
/
IApp.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
using System;
using System.IO;
using System.IO.Compression;
using Microsoft.JSInterop;
using Microsoft.Extensions.DependencyInjection;
using IBoxDB.LocalServer;
/*
dotnet new blazorwasm
<PackageReference Include="iBoxDB" Version="3.9.1" />
builder.Services.AddDatabase();
public void ConfigureServices(IServiceCollection services)
services.AddDatabase();
*/
/*
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
<p role="status">Current count: @currentCount</p>
<p role="status">Database size: @App.Msg</p>
@code
{
[Inject]
IApp App { get; set; } = default!;
private int currentCount = 0;
private void IncrementCount()
{
using var box = App.Auto.Cube();
var record = box["Table", 0L].Replace<Record>();
record.Value++;
box.Commit();
currentCount = record.Value;
}
protected override void OnInitialized()
{
base.OnInitialized();
IncrementCount();
}
}
*/
public static class AppClientExtension
{
public static IServiceCollection AddDatabase(this IServiceCollection services)
{
return services.AddSingleton<IApp, AppClient>();
}
}
public interface IApp
{
AutoBox Auto { get; }
IBox Cube();
string Msg { get; }
}
public class AppClient : IApp
{
string msg = String.Empty;
IJSInProcessRuntime runtime;
DB db;
AutoBox auto;
public AppClient(IJSRuntime _runtime)
{
runtime = (IJSInProcessRuntime)_runtime;
var s = runtime.Invoke<string>("localStorage.getItem", typeof(DB).FullName);
var bs = s != null ? Convert.FromBase64String(s) : null;
if (bs != null)
{
using (var mm = new MemoryStream())
{
using (var zip = new GZipStream(new MemoryStream(bs), CompressionMode.Decompress))
{
zip.CopyTo(mm);
}
bs = mm.ToArray();
}
}
db = new DB(bs ?? new byte[0]);
var cfg = db.GetConfig();
cfg.EnsureTable<Record>("Table", "Id");
db.MinConfig().FileIncSize = 1;
db.SetBoxRecycler((socket, outBox, normal) =>
{
time++;
msg = $"{Save()}, Time:({time})";
});
auto = db.Open();
}
public AutoBox Auto => auto;
public string Msg => msg;
public IBox Cube()
{
return Auto.Cube();
}
private int Save()
{
var bs = db.GetBuffer();
using (var mm = new MemoryStream())
{
using (var zip = new GZipStream(mm, CompressionMode.Compress))
{
zip.Write(bs, 0, bs.Length);
zip.Flush();
}
bs = mm.ToArray();
}
runtime.Invoke<string>("localStorage.setItem", typeof(DB).FullName, Convert.ToBase64String(bs));
return bs.Length;
}
private long time = 0;
}
public class Record
{
public long Id;
public string Name = String.Empty;
public int Value = 0;
}