Skip to content

Commit 21e4eaf

Browse files
authored
Clean up RecordingFile ResultReference classes (flutter#20)
1 parent e30f99c commit 21e4eaf

File tree

1 file changed

+71
-81
lines changed

1 file changed

+71
-81
lines changed

lib/src/backends/record_replay/recording_file.dart

Lines changed: 71 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'dart:async';
66
import 'dart:convert';
77

8+
import 'package:meta/meta.dart';
89
import 'package:file/file.dart';
910
import 'package:file/src/io.dart' as io;
1011

@@ -139,6 +140,52 @@ class RecordingFile extends RecordingFileSystemEntity<File, io.File>
139140
.then(wrap);
140141
}
141142

143+
abstract class _ExternalReference<T> extends ResultReference<T> {
144+
final File file;
145+
final T _value;
146+
147+
_ExternalReference(MutableRecording recording, String name, this._value)
148+
: file = recording.newFile(name);
149+
150+
@protected
151+
void writeDataToFile(T value);
152+
153+
@override
154+
T get value {
155+
writeDataToFile(_value);
156+
return _value;
157+
}
158+
159+
@override
160+
T get recordedValue => _value;
161+
162+
@override
163+
dynamic get serializedValue => '!${file.basename}';
164+
}
165+
166+
abstract class _ExternalFutureReference<T> extends FutureReference<T> {
167+
final File file;
168+
169+
_ExternalFutureReference(
170+
MutableRecording recording, String name, Future<T> future)
171+
: file = recording.newFile(name),
172+
super(future);
173+
174+
@protected
175+
Future<Null> writeDataToFile(T value);
176+
177+
@override
178+
Future<T> get value {
179+
return super.value.then((T value) async {
180+
await writeDataToFile(value);
181+
return value;
182+
});
183+
}
184+
185+
@override
186+
dynamic get serializedValue => '!${file.basename}';
187+
}
188+
142189
class _ByteArrayStreamReference extends StreamReference<List<int>> {
143190
final File file;
144191
IOSink _sink;
@@ -172,122 +219,65 @@ class _ByteArrayStreamReference extends StreamReference<List<int>> {
172219
Future.wait(<Future<dynamic>>[super.complete, _sink.done]).then((_) {});
173220
}
174221

175-
class _ByteArrayFutureReference extends FutureReference<List<int>> {
176-
final File file;
177-
222+
class _ByteArrayFutureReference extends _ExternalFutureReference<List<int>> {
178223
_ByteArrayFutureReference(
179224
MutableRecording recording, String name, Future<List<int>> future)
180-
: file = recording.newFile(name),
181-
super(future);
225+
: super(recording, name, future);
182226

183227
@override
184-
Future<List<int>> get value {
185-
return super.value.then((List<int> bytes) async {
186-
await file.writeAsBytes(bytes, flush: true);
187-
return bytes;
188-
});
228+
Future<Null> writeDataToFile(List<int> bytes) async {
229+
await file.writeAsBytes(bytes, flush: true);
189230
}
190-
191-
@override
192-
dynamic get serializedValue => '!${file.basename}';
193231
}
194232

195-
class _ByteArrayReference extends ResultReference<List<int>> {
196-
final File file;
197-
final List<int> bytes;
198-
199-
_ByteArrayReference(MutableRecording recording, String name, this.bytes)
200-
: file = recording.newFile(name);
233+
class _ByteArrayReference extends _ExternalReference<List<int>> {
234+
_ByteArrayReference(MutableRecording recording, String name, List<int> bytes)
235+
: super(recording, name, bytes);
201236

202237
@override
203-
List<int> get value {
238+
void writeDataToFile(List<int> bytes) {
204239
file.writeAsBytesSync(bytes, flush: true);
205-
return bytes;
206240
}
207-
208-
@override
209-
List<int> get recordedValue => bytes;
210-
211-
@override
212-
dynamic get serializedValue => '!${file.basename}';
213241
}
214242

215-
class _FileContentFutureReference extends FutureReference<String> {
216-
final File file;
217-
243+
class _FileContentFutureReference extends _ExternalFutureReference<String> {
218244
_FileContentFutureReference(
219245
MutableRecording recording, String name, Future<String> future)
220-
: file = recording.newFile(name),
221-
super(future);
246+
: super(recording, name, future);
222247

223248
@override
224-
Future<String> get value {
225-
return super.value.then((String content) async {
226-
await file.writeAsString(content, flush: true);
227-
return content;
228-
});
249+
Future<Null> writeDataToFile(String content) async {
250+
await file.writeAsString(content, flush: true);
229251
}
230-
231-
@override
232-
dynamic get serializedValue => '!${file.basename}';
233252
}
234253

235-
class _FileContentReference extends ResultReference<String> {
236-
final File file;
237-
final String content;
238-
239-
_FileContentReference(MutableRecording recording, String name, this.content)
240-
: file = recording.newFile(name);
254+
class _FileContentReference extends _ExternalReference<String> {
255+
_FileContentReference(MutableRecording recording, String name, String content)
256+
: super(recording, name, content);
241257

242258
@override
243-
String get value {
259+
void writeDataToFile(String content) {
244260
file.writeAsStringSync(content, flush: true);
245-
return content;
246261
}
247-
248-
@override
249-
String get recordedValue => content;
250-
251-
@override
252-
dynamic get serializedValue => '!${file.basename}';
253262
}
254263

255-
class _LinesFutureReference extends FutureReference<List<String>> {
256-
final File file;
257-
264+
class _LinesFutureReference extends _ExternalFutureReference<List<String>> {
258265
_LinesFutureReference(
259266
MutableRecording recording, String name, Future<List<String>> future)
260-
: file = recording.newFile(name),
261-
super(future);
267+
: super(recording, name, future);
262268

263269
@override
264-
Future<List<String>> get value {
265-
return super.value.then((List<String> lines) async {
266-
await file.writeAsString(lines.join('\n'), flush: true);
267-
return lines;
268-
});
270+
Future<Null> writeDataToFile(List<String> lines) async {
271+
await file.writeAsString(lines.join('\n'), flush: true);
269272
}
270-
271-
@override
272-
dynamic get serializedValue => '!${file.basename}';
273273
}
274274

275-
class _LinesReference extends ResultReference<List<String>> {
276-
final File file;
277-
final List<String> lines;
278-
279-
_LinesReference(MutableRecording recording, String name, this.lines)
280-
: file = recording.newFile(name);
275+
class _LinesReference extends _ExternalReference<List<String>> {
276+
_LinesReference(MutableRecording recording, String name, List<String> lines)
277+
: super(recording, name, lines);
281278

282279
@override
283-
List<String> get value {
280+
void writeDataToFile(List<String> lines) {
284281
file.writeAsStringSync(lines.join('\n'), flush: true);
285-
return lines;
286282
}
287-
288-
@override
289-
List<String> get recordedValue => lines;
290-
291-
@override
292-
dynamic get serializedValue => '!${file.basename}';
293283
}

0 commit comments

Comments
 (0)