|
| 1 | +import 'sync_status.dart'; |
| 2 | + |
| 3 | +/// An internal instruction emitted by the sync client in the core extension in |
| 4 | +/// response to the Dart SDK passing sync data into the extension. |
| 5 | +sealed class Instruction { |
| 6 | + factory Instruction.fromJson(Map<String, Object?> json) { |
| 7 | + return switch (json) { |
| 8 | + {'LogLine': final logLine} => |
| 9 | + LogLine.fromJson(logLine as Map<String, Object?>), |
| 10 | + {'UpdateSyncStatus': final updateStatus} => |
| 11 | + UpdateSyncStatus.fromJson(updateStatus as Map<String, Object?>), |
| 12 | + {'EstablishSyncStream': final establish} => |
| 13 | + EstablishSyncStream.fromJson(establish as Map<String, Object?>), |
| 14 | + {'FetchCredentials': final creds} => |
| 15 | + FetchCredentials.fromJson(creds as Map<String, Object?>), |
| 16 | + {'CloseSyncStream': _} => const CloseSyncStream(), |
| 17 | + {'FlushFileSystem': _} => const FlushFileSystem(), |
| 18 | + {'DidCompleteSync': _} => const DidCompleteSync(), |
| 19 | + _ => UnknownSyncInstruction(json) |
| 20 | + }; |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +final class LogLine implements Instruction { |
| 25 | + final String severity; |
| 26 | + final String line; |
| 27 | + |
| 28 | + LogLine({required this.severity, required this.line}); |
| 29 | + |
| 30 | + factory LogLine.fromJson(Map<String, Object?> json) { |
| 31 | + return LogLine( |
| 32 | + severity: json['severity'] as String, |
| 33 | + line: json['line'] as String, |
| 34 | + ); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +final class EstablishSyncStream implements Instruction { |
| 39 | + final Map<String, Object?> request; |
| 40 | + |
| 41 | + EstablishSyncStream(this.request); |
| 42 | + |
| 43 | + factory EstablishSyncStream.fromJson(Map<String, Object?> json) { |
| 44 | + return EstablishSyncStream(json['request'] as Map<String, Object?>); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +final class UpdateSyncStatus implements Instruction { |
| 49 | + final CoreSyncStatus status; |
| 50 | + |
| 51 | + UpdateSyncStatus({required this.status}); |
| 52 | + |
| 53 | + factory UpdateSyncStatus.fromJson(Map<String, Object?> json) { |
| 54 | + return UpdateSyncStatus( |
| 55 | + status: |
| 56 | + CoreSyncStatus.fromJson(json['status'] as Map<String, Object?>)); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +final class CoreSyncStatus { |
| 61 | + final bool connected; |
| 62 | + final bool connecting; |
| 63 | + final List<SyncPriorityStatus> priorityStatus; |
| 64 | + final DownloadProgress? downloading; |
| 65 | + |
| 66 | + CoreSyncStatus({ |
| 67 | + required this.connected, |
| 68 | + required this.connecting, |
| 69 | + required this.priorityStatus, |
| 70 | + required this.downloading, |
| 71 | + }); |
| 72 | + |
| 73 | + factory CoreSyncStatus.fromJson(Map<String, Object?> json) { |
| 74 | + return CoreSyncStatus( |
| 75 | + connected: json['connected'] as bool, |
| 76 | + connecting: json['connecting'] as bool, |
| 77 | + priorityStatus: [ |
| 78 | + for (final entry in json['priority_status'] as List) |
| 79 | + _priorityStatusFromJson(entry as Map<String, Object?>) |
| 80 | + ], |
| 81 | + downloading: switch (json['downloading']) { |
| 82 | + null => null, |
| 83 | + final raw as Map<String, Object?> => DownloadProgress.fromJson(raw), |
| 84 | + }, |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + static SyncPriorityStatus _priorityStatusFromJson(Map<String, Object?> json) { |
| 89 | + return ( |
| 90 | + priority: BucketPriority(json['priority'] as int), |
| 91 | + hasSynced: json['has_synced'] as bool?, |
| 92 | + lastSyncedAt: switch (json['last_synced_at']) { |
| 93 | + null => null, |
| 94 | + final lastSyncedAt as int => |
| 95 | + DateTime.fromMillisecondsSinceEpoch(lastSyncedAt * 1000), |
| 96 | + }, |
| 97 | + ); |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +final class DownloadProgress { |
| 102 | + final Map<String, BucketProgress> buckets; |
| 103 | + |
| 104 | + DownloadProgress(this.buckets); |
| 105 | + |
| 106 | + factory DownloadProgress.fromJson(Map<String, Object?> line) { |
| 107 | + final rawBuckets = line['buckets'] as Map<String, Object?>; |
| 108 | + |
| 109 | + return DownloadProgress(rawBuckets.map((k, v) { |
| 110 | + return MapEntry( |
| 111 | + k, |
| 112 | + _bucketProgressFromJson(v as Map<String, Object?>), |
| 113 | + ); |
| 114 | + })); |
| 115 | + } |
| 116 | + |
| 117 | + static BucketProgress _bucketProgressFromJson(Map<String, Object?> json) { |
| 118 | + return ( |
| 119 | + priority: BucketPriority(json['priority'] as int), |
| 120 | + atLast: json['at_last'] as int, |
| 121 | + sinceLast: json['since_last'] as int, |
| 122 | + targetCount: json['target_count'] as int, |
| 123 | + ); |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +final class FetchCredentials implements Instruction { |
| 128 | + final bool didExpire; |
| 129 | + |
| 130 | + FetchCredentials(this.didExpire); |
| 131 | + |
| 132 | + factory FetchCredentials.fromJson(Map<String, Object?> line) { |
| 133 | + return FetchCredentials(line['did_expire'] as bool); |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +final class CloseSyncStream implements Instruction { |
| 138 | + const CloseSyncStream(); |
| 139 | +} |
| 140 | + |
| 141 | +final class FlushFileSystem implements Instruction { |
| 142 | + const FlushFileSystem(); |
| 143 | +} |
| 144 | + |
| 145 | +final class DidCompleteSync implements Instruction { |
| 146 | + const DidCompleteSync(); |
| 147 | +} |
| 148 | + |
| 149 | +final class UnknownSyncInstruction implements Instruction { |
| 150 | + final Map<String, Object?> source; |
| 151 | + |
| 152 | + UnknownSyncInstruction(this.source); |
| 153 | +} |
0 commit comments