Toggle navigation
Toggle navigation
This project
Loading...
Sign in
flutter_package
/
fluttertpc_get
Go to a project
Toggle navigation
Projects
Groups
Snippets
Help
Toggle navigation pinning
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Authored by
Rafa Ruiz
2021-03-17 10:32:40 +0000
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
01547e2025e4b3ce93cc733852a6db96febdbf93
01547e20
1 parent
66db4f14
Updates on _RxImpl.trigger
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
38 additions
and
44 deletions
lib/get_rx/src/rx_types/rx_core/rx_impl.dart
test/rx/rx_workers_test.dart
lib/get_rx/src/rx_types/rx_core/rx_impl.dart
View file @
01547e2
...
...
@@ -199,6 +199,41 @@ abstract class _RxImpl<T> extends RxNotifier<T> with RxObjectMixin<T> {
fn
(
_value
);
subject
.
add
(
_value
);
}
/// Following certain practices on Rx data, we might want to react to certain
/// listeners when a value has been provided, even if the value is the same.
/// At the moment, we ignore part of the process if we `.call(value)` with
/// the same value since it holds the value and there's no real
/// need triggering the entire process for the same value inside, but
/// there are other situations where we might be interested in
/// triggering this.
///
/// For example, supposed we have a `int seconds = 2` and we want to animate
/// from invisible to visible a widget in two seconds:
/// RxEvent<int>.call(seconds);
/// then after a click happens, you want to call a RxEvent<int>.call(seconds).
/// By doing `call(seconds)`, if the value being held is the same,
/// the listeners won't trigger, hence we need this new `trigger` function.
/// This will refresh the listener of an AnimatedWidget and will keep
/// the value if the Rx is kept in memory.
/// Sample:
/// ```
/// Rx<Int> secondsRx = RxInt();
/// secondsRx.listen((value) => print("$value seconds set"));
///
/// secondsRx.call(2); // This won't trigger any listener, since the value is the same
/// secondsRx.trigger(2); // This will trigger the listener independently from the value.
/// ```
///
void
trigger
([
T
v
])
{
var
firstRebuild
=
this
.
firstRebuild
;
value
=
v
;
// If it's not the first rebuild, the listeners have been called already
// So we won't call them again.
if
(!
firstRebuild
)
{
subject
.
add
(
v
);
}
}
}
/// Rx class for `bool` Type.
...
...
@@ -379,30 +414,6 @@ class Rx<T> extends _RxImpl<T> {
}
}
/// Similar class to Rx<T> but this also will refresh the listeners if the same
/// value has been provided. This is useful when maintaining a state with the
/// same user = User("foo").
/// For example, supposed we have a `int seconds = 2` and we want to animate
/// from invisible to visible a widget in two seconds:
/// RxEvent<int>.call(seconds);
/// then after a click happens, you want to call a RxEvent<int>.call(seconds).
/// This will refresh the listener of an AnimatedWidget and will keep the value
/// if the Rx is kept in memory.
///
class
RxEvent
<
T
>
extends
Rx
<
T
>
{
RxEvent
([
T
initial
])
:
super
(
initial
);
void
trigger
([
T
v
])
{
var
firstRebuild
=
this
.
firstRebuild
;
value
=
v
;
// If it's not the first rebuild, the listeners have been called already
// So we won't call them again.
if
(!
firstRebuild
)
{
subject
.
add
(
v
);
}
}
}
extension
StringExtension
on
String
{
/// Returns a `RxString` with [this] `String` as initial value.
RxString
get
obs
=>
RxString
(
this
);
...
...
test/rx/rx_workers_test.dart
View file @
01547e2
...
...
@@ -96,7 +96,7 @@ void main() {
expect
(
count
,
555
);
});
test
(
'Rx same value will not call the same listener'
,
()
async
{
test
(
'Rx same value will not call the same listener
when `call`
'
,
()
async
{
var
reactiveInteger
=
RxInt
(
2
);
var
timesCalled
=
0
;
reactiveInteger
.
listen
((
newInt
)
{
...
...
@@ -113,8 +113,8 @@ void main() {
expect
(
1
,
timesCalled
);
});
test
(
'RxEvent same value will trigger the listener when trigger'
,
()
async
{
var
reactiveInteger
=
RxEvent
<
int
>(
2
);
test
(
'Rx same value will call the listener when `trigger`'
,
()
async
{
var
reactiveInteger
=
RxInt
(
2
);
var
timesCalled
=
0
;
reactiveInteger
.
listen
((
newInt
)
{
timesCalled
++;
...
...
@@ -129,21 +129,4 @@ void main() {
await
Future
.
delayed
(
Duration
(
milliseconds:
100
));
expect
(
3
,
timesCalled
);
});
test
(
'RxEvent same value will not trigger the listener when call'
,
()
async
{
var
reactiveInteger
=
RxEvent
<
int
>(
2
);
var
timesCalled
=
0
;
reactiveInteger
.
listen
((
newInt
)
{
timesCalled
++;
});
// we call 3
reactiveInteger
.
call
(
3
);
// then repeat twice
reactiveInteger
.
call
(
3
);
reactiveInteger
.
call
(
3
);
await
Future
.
delayed
(
Duration
(
milliseconds:
100
));
expect
(
1
,
timesCalled
);
});
}
...
...
Please
register
or
login
to post a comment