Skip to content

Commit 6dd5d85

Browse files
authored
Merge pull request #172 from LoveCommunity/refactor/make-states-auto-cache
refactor - apply cache behavior to `States`
2 parents ed1b3c5 + 6f3b09d commit 6dd5d85

File tree

5 files changed

+43
-61
lines changed

5 files changed

+43
-61
lines changed

lib/src/dart_observable/states/observable_as_states_x.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,8 @@ import 'states.dart';
66
extension ObservableAsStatesX<T> on Observable<T> {
77

88
/// Cast an Observable to States.
9-
States<T> asStates() => States<T>.from(this);
9+
States<T> asStates() {
10+
final observable = multicastReplay(1);
11+
return States<T>.from(observable);
12+
}
1013
}

lib/src/dart_observable/states/states.dart

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,10 @@ class States<T> {
4949
/// });
5050
/// ```
5151
///
52-
States(
52+
factory States(
5353
Observe<T> observe
54-
): this.from(Observable(observe));
54+
) => Observable(observe)
55+
.asStates();
5556

5657
/// Combine multiple `States` into one `States`.
5758
///
@@ -106,7 +107,7 @@ class States<T> {
106107
combiner: combiner,
107108
).asStates();
108109

109-
/// Cast an `Observable` to `States`.
110+
/// Create `States` from raw `Observable`.
110111
const States.from(this.observable);
111112

112113
/// The underlining raw `observable` that is promised to
@@ -188,18 +189,6 @@ extension StatesX<T> on States<T> {
188189
.asStates();
189190
}
190191

191-
/// Cache the computed state.
192-
///
193-
/// Instead of computing state very time when observed,
194-
/// `Cache` will cache latest state and replay it to new observer.
195-
/// This cache will keep in sync with source `States`.
196-
///
197-
States<T> cache() {
198-
return observable
199-
.multicastReplay(1)
200-
.asStates();
201-
}
202-
203192
/// Suppress the first n items emitted by States.
204193
///
205194
/// You can ignore the first n items emitted by States
@@ -242,4 +231,4 @@ extension StatesX<T> on States<T> {
242231
states: this
243232
);
244233
}
245-
}
234+
}

test/dart_observable/dart_observable_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

2+
import 'states/observable_as_states_test.dart' as observable_as_states_test;
23
import 'states/states_activated_test.dart' as states_activated_test;
3-
import 'states/states_cache_test.dart' as states_cache_test;
44
import 'states/states_cast_test.dart' as states_cast_test;
55
import 'states/states_combine_test.dart' as states_combine_test;
66
import 'states/states_default_constructor_test.dart' as states_default_constructor_test;
@@ -29,8 +29,8 @@ import 'models/async_test.dart' as async_test;
2929

3030

3131
void main() {
32+
observable_as_states_test.main();
3233
states_activated_test.main();
33-
states_cache_test.main();
3434
states_cast_test.main();
3535
states_combine_test.main();
3636
states_default_constructor_test.main();
Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,50 @@
11

2+
23
import 'package:test/test.dart';
34
import 'package:dart_scope/dart_scope.dart';
45

56
import '../shared/states_tester.dart';
67

78
void main() {
89

9-
test('`states.cache` connect when observers increase to one', () {
10+
test('`observable.asStates` connect when observers increase to one', () {
1011

1112
int invokes = 0;
1213

13-
final states = States<String>((setState) {
14-
setState('a');
14+
final observable = Observable<String>((onData) {
15+
onData('a');
1516
invokes += 1;
1617
return Disposable.empty;
1718
});
1819

19-
final cache = states.cache();
20+
final states = observable.asStates();
2021

2122
expect(invokes, 0);
22-
final observation1 = cache.observe((_) {});
23+
final observation1 = states.observe((_) {});
2324
expect(invokes, 1);
24-
final observation2 = cache.observe((_) {});
25+
final observation2 = states.observe((_) {});
2526
expect(invokes, 1);
2627

2728
observation1.dispose();
2829
observation2.dispose();
2930

3031
});
3132

32-
test('`states.cache` disconnect when observers decrease to zero', () {
33+
test('`observable.asStates` disconnect when observers decrease to zero', () {
3334

3435
int invokes = 0;
3536

36-
final states = States<String>((setState) {
37-
setState('a');
37+
final observable = Observable<String>((onData) {
38+
onData('a');
3839
return Disposable(() {
3940
invokes += 1;
4041
});
4142
});
4243

43-
final cache = states.cache();
44+
final states = observable.asStates();
4445

45-
final observation1 = cache.observe((_) {});
46-
final observation2 = cache.observe((_) {});
46+
final observation1 = states.observe((_) {});
47+
final observation2 = states.observe((_) {});
4748

4849
expect(invokes, 0);
4950
observation1.dispose();
@@ -53,28 +54,28 @@ void main() {
5354

5455
});
5556

56-
test('`states.cache` forward data to observers', () async {
57+
test('`observable.asStates` forward data to observers', () async {
5758

58-
final states = States<String>((setState) {
59-
setState('a');
60-
Future(() => setState('b'));
59+
final observable = Observable<String>((onData) {
60+
onData('a');
61+
Future(() => onData('b'));
6162
return Disposable.empty;
6263
});
6364

64-
final cache = states.cache();
65+
final states = observable.asStates();
6566

6667
final tester1 = StatesTester(
67-
cache,
68-
);
68+
states,
69+
);
6970

7071
final tester2 = StatesTester(
71-
cache,
72-
);
72+
states,
73+
);
7374

7475

7576
tester1.startObserve();
7677
tester2.startObserve();
77-
78+
7879
expect(tester1.recorded, [
7980
'a',
8081
]);
@@ -96,22 +97,22 @@ void main() {
9697

9798
});
9899

99-
test('`states.cache` replay data to observers', () {
100+
test('`observable.asStates` replay data to observers', () {
100101

101-
final states = States<String>((setState) {
102-
setState('a');
103-
setState('b');
104-
setState('c');
102+
final observable = Observable<String>((onData) {
103+
onData('a');
104+
onData('b');
105+
onData('c');
105106
return Disposable.empty;
106107
});
107108

108-
final cache = states.cache();
109+
final states = observable.asStates();
109110

110111
final tester = StatesTester(
111-
cache,
112+
states,
112113
);
113114

114-
final observation = cache.observe((_) {});
115+
final observation = states.observe((_) {});
115116

116117
expect(tester.recorded, <String>[]);
117118
tester.startObserve();
@@ -125,3 +126,4 @@ void main() {
125126
});
126127

127128
}
129+

test/dart_observable/states/states_from_test.dart

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,6 @@ void main() {
1515
expect(states.observable, observable);
1616

1717
});
18-
19-
test("`observable.asStates`'s observable is identical to origin observable", () {
20-
21-
final observable = Observable<String>((onData) {
22-
return Disposable.empty;
23-
});
24-
25-
final states = observable.asStates();
26-
27-
expect(states.observable, observable);
28-
29-
});
3018

3119
test("`states.observe` cause observable observe", () {
3220

0 commit comments

Comments
 (0)