Skip to content

Commit

Permalink
feat(single): add Single.asNullable() operator
Browse files Browse the repository at this point in the history
  • Loading branch information
hoc081098 committed Sep 2, 2024
1 parent 818fef1 commit e7763ef
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
- **operators**:
- Add `Stream.asNullable()` operator.

- **single**:
- Add `Single.asNullable()` operator.

## 0.3.0 - Jun 16, 2024

- Accept Dart SDK versions above 3.0 (`sdk: '>=2.12.0 <4.0.0'`).
Expand Down
9 changes: 9 additions & 0 deletions lib/src/single/as_nullable.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import 'single.dart';

/// Adapt this `Single<T>` to be a `Single<T?>`.
extension AsNullableSingleExtension<T extends Object> on Single<T> {
/// Adapt this `Single<T>` to be a `Single<T?>`.
@pragma('vm:prefer-inline')
@pragma('dart2js:tryInline')
Single<T?> asNullable() => this;
}
39 changes: 39 additions & 0 deletions test/single/as_nullable_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import 'dart:async';

import 'package:rxdart_ext/rxdart_ext.dart';
import 'package:test/expect.dart';
import 'package:test/scaffolding.dart';

void main() {
group('Single.asNullable', () {
test('works', () {
final single = Single.value(1);
final asNullable = single.asNullable();

expect(asNullable, isA<Single<int?>>());
expect(asNullable, same(single));
});

test('asNullable with publishState', () async {
final single = Single.value(1);
final stateStream = single.asNullable().publishState(null);

expect(stateStream, isA<StateConnectableStream<int?>>());
expect(stateStream.value, null);

scheduleMicrotask(() => stateStream.connect());
await expectLater(stateStream, emitsInOrder([1]));
});

test('asNullable with publishValueSeeded', () async {
final single = Single.value(1);
final valueStream = single.asNullable().publishValueSeeded(null);

expect(valueStream, isA<ValueConnectableStream<int?>>());
expect(valueStream.value, null);

scheduleMicrotask(() => valueStream.connect());
await expectLater(valueStream, emitsInOrder([null, 1]));
});
});
}

0 comments on commit e7763ef

Please sign in to comment.