-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathCoreInstructions.cs
More file actions
129 lines (104 loc) · 3.54 KB
/
CoreInstructions.cs
File metadata and controls
129 lines (104 loc) · 3.54 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
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
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
namespace PowerSync.Common.Client.Sync.Stream;
/// <summary>
/// An internal instruction emitted by the sync client in the core extension in response to the
/// SDK passing sync data into the extension.
/// </summary>
public abstract class Instruction
{
public static Instruction[] ParseInstructions(string rawResponse)
{
var jsonArray = JArray.Parse(rawResponse);
List<Instruction> instructions = [];
foreach (JObject item in jsonArray)
{
var instruction = ParseInstruction(item);
if (instruction == null)
{
throw new JsonSerializationException("Failed to parse instruction from JSON.");
}
instructions.Add(instruction);
}
return instructions.ToArray();
}
public static Instruction? ParseInstruction(JObject json)
{
if (json.ContainsKey("LogLine"))
return json["LogLine"]!.ToObject<LogLine>();
if (json.ContainsKey("UpdateSyncStatus"))
return json["UpdateSyncStatus"]!.ToObject<UpdateSyncStatus>();
if (json.ContainsKey("EstablishSyncStream"))
return json["EstablishSyncStream"]!.ToObject<EstablishSyncStream>();
if (json.ContainsKey("FetchCredentials"))
return json["FetchCredentials"]!.ToObject<FetchCredentials>();
if (json.ContainsKey("CloseSyncStream"))
return new CloseSyncStream();
if (json.ContainsKey("FlushFileSystem"))
return new FlushFileSystem();
if (json.ContainsKey("DidCompleteSync"))
return new DidCompleteSync();
throw new JsonSerializationException("Unknown Instruction type.");
}
}
public class LogLine : Instruction
{
[JsonProperty("severity")]
public string Severity { get; set; } = null!; // "DEBUG", "INFO", "WARNING"
[JsonProperty("line")]
public string Line { get; set; } = null!;
}
public class EstablishSyncStream : Instruction
{
[JsonProperty("request")]
public StreamingSyncRequest Request { get; set; } = null!;
}
public class UpdateSyncStatus : Instruction
{
[JsonProperty("status")]
public CoreSyncStatus Status { get; set; } = null!;
}
public class CoreSyncStatus
{
[JsonProperty("connected")]
public bool Connected { get; set; }
[JsonProperty("connecting")]
public bool Connecting { get; set; }
[JsonProperty("priority_status")]
public List<SyncPriorityStatus> PriorityStatus { get; set; } = [];
[JsonProperty("downloading")]
public DownloadProgress? Downloading { get; set; }
}
public class SyncPriorityStatus
{
[JsonProperty("priority")]
public int Priority { get; set; }
[JsonProperty("last_synced_at")]
public long LastSyncedAt { get; set; }
[JsonProperty("has_synced")]
public bool? HasSynced { get; set; }
}
public class DownloadProgress
{
[JsonProperty("buckets")]
public Dictionary<string, BucketProgress> Buckets { get; set; } = null!;
}
public class BucketProgress
{
[JsonProperty("priority")]
public int Priority { get; set; }
[JsonProperty("at_last")]
public int AtLast { get; set; }
[JsonProperty("since_last")]
public int SinceLast { get; set; }
[JsonProperty("target_count")]
public int TargetCount { get; set; }
}
public class FetchCredentials : Instruction
{
[JsonProperty("did_expire")]
public bool DidExpire { get; set; }
}
public class CloseSyncStream : Instruction { }
public class FlushFileSystem : Instruction { }
public class DidCompleteSync : Instruction { }