Skip to content

Commit bcab1ef

Browse files
committed
Add restorePosition method.
1 parent bfc629b commit bcab1ef

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 4.2.0
2+
3+
- Add `restorePosition` method.
4+
15
## 4.1.0
26

37
- Add `readInto` method.

lib/src/random_access_source.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,14 @@ abstract class RandomAccessSource {
5959

6060
/// Closes the source.
6161
Future<void> close();
62+
63+
/// Restores the position after executing the given [action].
64+
Future<void> restorePosition<T>(Future<T> Function() action) async {
65+
final currentPosition = await position();
66+
try {
67+
await action();
68+
} finally {
69+
await seek(currentPosition);
70+
}
71+
}
6272
}

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: random_access_source
22
description: A shared interface for common random access data.
3-
version: 4.1.0
3+
version: 4.2.0
44
repository: https://github.com/flutter-cavalry/random_access_source
55

66
environment:

test/raf_test.dart

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,29 @@ void main() {
109109
expect(await src.readByte(), -1);
110110
await src.close();
111111
});
112+
113+
test('RestorePosition', () async {
114+
final src = await rafSource();
115+
await src.seek(10);
116+
expect(await src.position(), 10);
117+
118+
await src.restorePosition(() async {
119+
expect(await src.position(), 10);
120+
expect(await src.readByte(), flutterIcon[10]);
121+
expect(await src.position(), 11);
122+
});
123+
expect(await src.position(), 10);
124+
125+
try {
126+
await src.restorePosition(() async {
127+
await src.seek(20);
128+
throw Exception('Test Exception');
129+
});
130+
} catch (e) {
131+
// Expected exception
132+
}
133+
expect(await src.position(), 10);
134+
135+
await src.close();
136+
});
112137
}

0 commit comments

Comments
 (0)