-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathFakeSyncSource.cs
More file actions
90 lines (79 loc) · 2.83 KB
/
Copy pathFakeSyncSource.cs
File metadata and controls
90 lines (79 loc) · 2.83 KB
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
using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
using LcmCrdt;
using SIL.Harmony;
using SIL.Harmony.Changes;
using SIL.Harmony.Core;
namespace LcmDebugger;
internal class FakeCommit : Commit
{
[SetsRequiredMembers]
public FakeCommit(Guid id, HybridDateTime hybridDateTime) : base(id, "", NullParentHash, hybridDateTime)
{
HybridDateTime = hybridDateTime;
SetParentHash(NullParentHash);
}
}
/// <summary>
///
/// </summary>
/// <param name="commits"></param>
/// <param name="currentSyncState">when null it will just report to the client that it has the same sync state, that way the client doesn't try to push any changes</param>
public class FakeSyncSource(Commit[] commits, SyncState? currentSyncState = null) : ISyncable
{
public static FakeSyncSource FromSingleChangeJson(
[StringSyntax(StringSyntaxAttribute.Json)] string json,
DateTimeOffset commitDate,
JsonSerializerOptions? options = null)
{
var change = JsonSerializer.Deserialize<ChangeEntity<IChange>>(json, options);
ArgumentNullException.ThrowIfNull(change);
return new FakeSyncSource([new FakeCommit(change.CommitId, new HybridDateTime(commitDate, 0))
{
ClientId = Guid.NewGuid(),
ChangeEntities = [change]
}]);
}
public static FakeSyncSource FromJsonFile(string path, JsonSerializerOptions? options = null)
{
if (options is null)
{
var config = new CrdtConfig();
LcmCrdtKernel.ConfigureCrdt(config);
options = new JsonSerializerOptions(JsonSerializerDefaults.Web)
{
TypeInfoResolver = config.MakeLcmCrdtExternalJsonTypeResolver(),
};
}
using var file = File.OpenRead(path);
var changes = JsonSerializer.Deserialize<ChangesResult<Commit>>(file, options);
ArgumentNullException.ThrowIfNull(changes);
ArgumentNullException.ThrowIfNull(changes.MissingFromClient);
ArgumentNullException.ThrowIfNull(changes.ServerSyncState);
return new FakeSyncSource(changes.MissingFromClient, changes.ServerSyncState);
}
public Task AddRangeFromSync(IEnumerable<Commit> commits)
{
return Task.CompletedTask;
}
public Task<SyncState> GetSyncState()
{
return Task.FromResult(new SyncState([]));
}
public Task<ChangesResult<Commit>> GetChanges(SyncState otherHeads)
{
return Task.FromResult(new ChangesResult<Commit>(commits, currentSyncState ?? otherHeads));
}
public Task<SyncResults> SyncWith(ISyncable remoteModel)
{
throw new NotImplementedException();
}
public Task SyncMany(ISyncable[] remotes)
{
throw new NotImplementedException();
}
public ValueTask<bool> ShouldSync()
{
return new ValueTask<bool>(true);
}
}