Skip to content

Commit

Permalink
Modified context.read so that it can take any watchable
Browse files Browse the repository at this point in the history
  • Loading branch information
letsar committed Nov 9, 2020
1 parent 97f0dc1 commit 378e992
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 5 deletions.
7 changes: 6 additions & 1 deletion packages/binder/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.2.1]
### Modified
- `BuildContext.read()` can be called with any watchable, not only a `StateRef`.

## [0.2.0]
### Added
- `Consumer` widget used to watch one watchable.
Expand Down Expand Up @@ -38,7 +42,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Initial release.

[Unreleased]: https://github.com/letsar/binder/compare/v0.2.0...HEAD
[Unreleased]: https://github.com/letsar/binder/compare/v0.2.1...HEAD
[0.2.1]: https://github.com/letsar/binder/compare/releases/tag/v0.2.1
[0.2.0]: https://github.com/letsar/binder/compare/releases/tag/v0.2.0
[0.1.5]: https://github.com/letsar/binder/compare/releases/tag/v0.1.5
[0.1.4]: https://github.com/letsar/binder/compare/releases/tag/v0.1.4
Expand Down
6 changes: 3 additions & 3 deletions packages/binder/lib/src/build_context_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ extension BinderBuildContextExtensions on BuildContext {
return watchScope(watchable).read(watchable);
}

/// Reads the current state of the [ref].
/// Reads the current state of the [watchable].
///
/// Cannot be called while building a widget.
T read<T>(StateRef<T> ref) {
T read<T>(Watchable<T> watchable) {
assert(!debugDoingBuild, 'Cannot call read() while building a widget.');
return readScope().read(ref);
return readScope().read(watchable);
}

/// Gets the instance of the business logic component referenced by [ref].
Expand Down
2 changes: 1 addition & 1 deletion packages/binder/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: binder
description: A lightweight, yet powerful way to bind your application state with your business logic.
version: 0.2.0
version: 0.2.1
homepage: https://github.com/letsar/binder

environment:
Expand Down
64 changes: 64 additions & 0 deletions packages/binder/test/binder_scope_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,70 @@ void main() {
expect(state0.states.containsKey(a.key), isTrue);
});

group('read', () {
testWidgets('can be used with StateRef', (tester) async {
final a = StateRef(0);

BuildContext ctx;
await tester.pumpWidget(
BinderScope(
child: Builder(
builder: (c) {
ctx = c;
return const SizedBox();
},
),
),
);

expect(ctx.read(a), 0);
ctx.write(a, 2);
expect(ctx.read(a), 2);
});

testWidgets('can be used with Selector', (tester) async {
final a = StateRef(0);
final selector = a.select((state) => state + 3);

BuildContext ctx;
await tester.pumpWidget(
BinderScope(
child: Builder(
builder: (c) {
ctx = c;
return const SizedBox();
},
),
),
);

expect(ctx.read(selector), 3);
ctx.write(a, 2);
expect(ctx.read(selector), 5);
});

testWidgets('can be used with Computed', (tester) async {
final a = StateRef(0);
final computed = Computed((watch) => watch(a) + 3);

BuildContext ctx;
await tester.pumpWidget(
BinderScope(
child: Builder(
builder: (c) {
ctx = c;
return const SizedBox();
},
),
),
);

expect(ctx.read(computed), 3);
ctx.write(a, 2);
expect(ctx.read(computed), 5);
});
});

group('watch', () {
testWidgets(
'throws when called outside of a build method',
Expand Down
8 changes: 8 additions & 0 deletions packages/binder/test/logic_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ void main() {
test('read calls binder.read', () {
myLogic.read(stateRef);
verify(mockScope.read(stateRef));

final selector = stateRef.select((state) => state + 3);
myLogic.read(selector);
verify(mockScope.read(selector));

final computed = Computed((watch) => watch(stateRef) + 3);
myLogic.read(computed);
verify(mockScope.read(computed));
});

test('redo calls binder.redo', () {
Expand Down

0 comments on commit 378e992

Please sign in to comment.