-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathinstruction.dart
More file actions
153 lines (126 loc) · 4.35 KB
/
instruction.dart
File metadata and controls
153 lines (126 loc) · 4.35 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import 'sync_status.dart';
/// An internal instruction emitted by the sync client in the core extension in
/// response to the Dart SDK passing sync data into the extension.
sealed class Instruction {
factory Instruction.fromJson(Map<String, Object?> json) {
return switch (json) {
{'LogLine': final logLine} =>
LogLine.fromJson(logLine as Map<String, Object?>),
{'UpdateSyncStatus': final updateStatus} =>
UpdateSyncStatus.fromJson(updateStatus as Map<String, Object?>),
{'EstablishSyncStream': final establish} =>
EstablishSyncStream.fromJson(establish as Map<String, Object?>),
{'FetchCredentials': final creds} =>
FetchCredentials.fromJson(creds as Map<String, Object?>),
{'CloseSyncStream': _} => const CloseSyncStream(),
{'FlushFileSystem': _} => const FlushFileSystem(),
{'DidCompleteSync': _} => const DidCompleteSync(),
_ => UnknownSyncInstruction(json)
};
}
}
final class LogLine implements Instruction {
final String severity;
final String line;
LogLine({required this.severity, required this.line});
factory LogLine.fromJson(Map<String, Object?> json) {
return LogLine(
severity: json['severity'] as String,
line: json['line'] as String,
);
}
}
final class EstablishSyncStream implements Instruction {
final Map<String, Object?> request;
EstablishSyncStream(this.request);
factory EstablishSyncStream.fromJson(Map<String, Object?> json) {
return EstablishSyncStream(json['request'] as Map<String, Object?>);
}
}
final class UpdateSyncStatus implements Instruction {
final CoreSyncStatus status;
UpdateSyncStatus({required this.status});
factory UpdateSyncStatus.fromJson(Map<String, Object?> json) {
return UpdateSyncStatus(
status:
CoreSyncStatus.fromJson(json['status'] as Map<String, Object?>));
}
}
final class CoreSyncStatus {
final bool connected;
final bool connecting;
final List<SyncPriorityStatus> priorityStatus;
final DownloadProgress? downloading;
CoreSyncStatus({
required this.connected,
required this.connecting,
required this.priorityStatus,
required this.downloading,
});
factory CoreSyncStatus.fromJson(Map<String, Object?> json) {
return CoreSyncStatus(
connected: json['connected'] as bool,
connecting: json['connecting'] as bool,
priorityStatus: [
for (final entry in json['priority_status'] as List)
_priorityStatusFromJson(entry as Map<String, Object?>)
],
downloading: switch (json['downloading']) {
null => null,
final raw as Map<String, Object?> => DownloadProgress.fromJson(raw),
},
);
}
static SyncPriorityStatus _priorityStatusFromJson(Map<String, Object?> json) {
return (
priority: BucketPriority(json['priority'] as int),
hasSynced: json['has_synced'] as bool?,
lastSyncedAt: switch (json['last_synced_at']) {
null => null,
final lastSyncedAt as int =>
DateTime.fromMillisecondsSinceEpoch(lastSyncedAt * 1000),
},
);
}
}
final class DownloadProgress {
final Map<String, BucketProgress> buckets;
DownloadProgress(this.buckets);
factory DownloadProgress.fromJson(Map<String, Object?> line) {
final rawBuckets = line['buckets'] as Map<String, Object?>;
return DownloadProgress(rawBuckets.map((k, v) {
return MapEntry(
k,
_bucketProgressFromJson(v as Map<String, Object?>),
);
}));
}
static BucketProgress _bucketProgressFromJson(Map<String, Object?> json) {
return (
priority: BucketPriority(json['priority'] as int),
atLast: json['at_last'] as int,
sinceLast: json['since_last'] as int,
targetCount: json['target_count'] as int,
);
}
}
final class FetchCredentials implements Instruction {
final bool didExpire;
FetchCredentials(this.didExpire);
factory FetchCredentials.fromJson(Map<String, Object?> line) {
return FetchCredentials(line['did_expire'] as bool);
}
}
final class CloseSyncStream implements Instruction {
const CloseSyncStream();
}
final class FlushFileSystem implements Instruction {
const FlushFileSystem();
}
final class DidCompleteSync implements Instruction {
const DidCompleteSync();
}
final class UnknownSyncInstruction implements Instruction {
final Map<String, Object?> source;
UnknownSyncInstruction(this.source);
}