Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UIP-2614 Update UiComponent to implement DisposableManagerV6 #104

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions lib/src/component_declaration/component_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ typedef TProps BuilderOnlyUiFactory<TProps extends UiProps>();
/// }
///
/// > Related: [UiStatefulComponent]
abstract class UiComponent<TProps extends UiProps> extends react.Component implements DisposableManagerV3 {
abstract class UiComponent<TProps extends UiProps> extends react.Component implements DisposableManagerV6 {
Disposable _disposableProxy;

/// The props for the non-forwarding props defined in this component.
Expand Down Expand Up @@ -291,6 +291,9 @@ abstract class UiComponent<TProps extends UiProps> extends react.Component imple
@override
Future<T> getManagedDelayedFuture<T>(Duration duration, T callback()) =>
_getDisposableProxy().getManagedDelayedFuture<T>(duration, callback);

@override
ManagedDisposer getManagedDisposer(Disposer disposer) => _getDisposableProxy().getManagedDisposer(disposer);

@override
Timer getManagedPeriodicTimer(Duration duration, void callback(Timer timer)) =>
Expand All @@ -300,6 +303,17 @@ abstract class UiComponent<TProps extends UiProps> extends react.Component imple
Timer getManagedTimer(Duration duration, void callback()) =>
_getDisposableProxy().getManagedTimer(duration, callback);

@override
StreamSubscription<T> listenToStream<T>(
Stream<T> stream, void onData(T event),
{Function onError, void onDone(), bool cancelOnError}) =>
_getDisposableProxy().listenToStream(
stream, onData, onError: onError, onDone: onDone, cancelOnError: cancelOnError);

@override
Disposable manageAndReturnDisposable(Disposable disposable) =>
_getDisposableProxy().manageAndReturnDisposable(disposable);

@override
Completer<T> manageCompleter<T>(Completer<T> completer) =>
_getDisposableProxy().manageCompleter<T>(completer);
Expand All @@ -308,20 +322,24 @@ abstract class UiComponent<TProps extends UiProps> extends react.Component imple
void manageDisposable(Disposable disposable) =>
_getDisposableProxy().manageDisposable(disposable);

/// DEPRECATED. Use [getManagedDisposer] instead.
@Deprecated('2.0.0')
@override
void manageDisposer(Disposer disposer) =>
_getDisposableProxy().manageDisposer(disposer);

@override
void manageStreamController(StreamController controller) =>
_getDisposableProxy().manageStreamController(controller);

/// DEPRECATED. Use [listenToStream] instead.
@Deprecated('2.0.0')
@override
void manageStreamSubscription(StreamSubscription subscription) =>
_getDisposableProxy().manageStreamSubscription(subscription);

/// Instantiates a new [Disposable] instance on the first call to the
/// [DisposableManagerV3] method.
/// [DisposableManagerV6] method.
Disposable _getDisposableProxy() {
if (_disposableProxy == null) {
_disposableProxy = new Disposable();
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ dependencies:
react: "^3.4.3"
source_span: "^1.4.0"
transformer_utils: "^0.1.1"
w_common: "^1.6.0"
w_common: "^1.8.0"
w_flux: "^2.7.1"
platform_detect: "^1.3.2"
quiver: ">=0.21.4 <0.26.0"
Expand Down
34 changes: 34 additions & 0 deletions test/over_react/component_declaration/component_base_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,17 @@ main() {
await unmountAndDisposal();
});

test('should call managed disposer returned by getManagedDisposer', () async {
var disposerCalled = false;
var disposer = component.getManagedDisposer(() async => disposerCalled = true);
expect(disposer, new isInstanceOf<ManagedDisposer>());

expect(disposerCalled, isFalse);
await unmountAndDisposal();
expect(disposerCalled, isTrue);
expect(disposer.isDisposed, isTrue);
});

test('should cancel periodic timer', () async {
var timer = component.getManagedPeriodicTimer(shortDuration,
expectAsync1((_) {}, count: 0, reason: 'Did not expect callback to be invoked.'));
Expand All @@ -728,6 +739,29 @@ main() {
expect(timer.isActive, isFalse);
});

test('should cancel stream subscription returned by listenToStream', () async{
var streamController = new StreamController<Null>.broadcast();
// ignore: cancel_subscriptions
var streamSubscription = component.listenToStream(streamController.stream, expectAsync1((_) {},
count: 0,
reason: 'Did not expect event after cancelling subscription'));
expect(streamSubscription, new isInstanceOf<StreamSubscription>());

await unmountAndDisposal();

streamController
..add(null)
..close();
});

test('should dispose managed Disposable returned by manageAndReturnDisposable', () async {
var disposable = new Disposable();
expect(component.manageAndReturnDisposable(disposable), same(disposable));
expect(disposable.isDisposed, isFalse);
await unmountAndDisposal();
expect(disposable.isDisposed, isTrue);
});

test('should complete uncompleted managed Completer with ObjectDisposedException', () async {
var completer = new Completer<Null>();
component.manageCompleter(completer);
Expand Down