Skip to content

Commit

Permalink
Merge pull request #159 from kuhnroyal/fix/158-disposeProps
Browse files Browse the repository at this point in the history
Fix disposeProps clearing all props even if predicate is provided.
  • Loading branch information
marcglasberg authored May 13, 2024
2 parents 341afdf + e955727 commit 222176e
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 14 deletions.
27 changes: 13 additions & 14 deletions lib/src/store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import 'dart:collection';
import 'package:async_redux/async_redux.dart';
import 'package:async_redux/src/process_persistence.dart';
import 'package:collection/collection.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';

import 'connector_tester.dart';
Expand Down Expand Up @@ -155,6 +154,10 @@ class Store<St> {
/// See also: [prop] and [setProp].
Object? get env => _environment;

/// Gets the store properties.
@visibleForTesting
Map<Object?, Object?> get props => _props;

/// Gets a property from the store.
/// This can be used to save global values, but scoped to the store.
/// For example, you could save timers, streams or futures used by actions.
Expand Down Expand Up @@ -220,20 +223,16 @@ class Store<St> {
var keysToRemove = [];

for (var MapEntry(key: key, value: value) in _props.entries) {
bool removeIt = true;

if (predicate == null) {
bool ifClosed = _closeTimerFutureStream(value);
if (!ifClosed) removeIt = false;
final removeIt = predicate?.call(key: key, value: value) ?? true;

if (removeIt) {
final ifClosed = _closeTimerFutureStream(value);
// Skip removal if no predicate was provided
// and the value was no Future/Stream like
if (!ifClosed && predicate == null) continue;
// Otherwise remove the property
keysToRemove.add(key);
}
//
// A predicate was provided,
else {
var removeIt = predicate(key: key, value: value);
if (removeIt) _closeTimerFutureStream(value);
}

if (removeIt) keysToRemove.add(key);
}

// After the iteration, remove all keys at the same time.
Expand Down
66 changes: 66 additions & 0 deletions test/props_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import 'dart:async';

import 'package:async_redux/async_redux.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
group('Add and dispose store properties', () {
test('without dispose predicate', () async {
final store = Store<int>(initialState: 1);

final sub = Stream.periodic(
const Duration(milliseconds: 10),
(i) => i,
).listen((event) {});

store.setProp(
'future',
Future.delayed(
const Duration(seconds: 1),
() => 'foo',
),
);
store.setProp(
'subscription',
sub,
);
store.setProp('value', 'bar');

expect(store.props, hasLength(3));

store.disposeProps();

expect(store.props, hasLength(1));
expect(store.props.containsKey('value'), isTrue);
});

test('with dispose predicate', () async {
final store = Store<int>(initialState: 1);

final sub = Stream.periodic(
const Duration(milliseconds: 10),
(i) => i,
).listen((event) {});

store.setProp(
'future',
Future.delayed(
const Duration(seconds: 1),
() => 'foo',
),
);
store.setProp(
'subscription',
sub,
);
store.setProp('value', 'bar');

expect(store.props, hasLength(3));

store.disposeProps(({key, value}) => key == 'value');

expect(store.props, hasLength(2));
expect(store.props.containsKey('value'), isFalse);
});
});
}

0 comments on commit 222176e

Please sign in to comment.