Skip to content

Commit c068081

Browse files
committed
typed stream
support passing async functions to setState() make sure stream gets updated after every setState() 0.2.0
1 parent eaa0d17 commit c068081

File tree

4 files changed

+70
-127
lines changed

4 files changed

+70
-127
lines changed

example/pubspec.lock

Lines changed: 27 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,62 @@
11
# Generated by pub
22
# See https://dart.dev/tools/pub/glossary#lockfile
33
packages:
4-
archive:
5-
dependency: transitive
6-
description:
7-
name: archive
8-
url: "https://pub.dartlang.org"
9-
source: hosted
10-
version: "2.0.13"
11-
args:
12-
dependency: transitive
13-
description:
14-
name: args
15-
url: "https://pub.dartlang.org"
16-
source: hosted
17-
version: "1.6.0"
184
async:
195
dependency: transitive
206
description:
217
name: async
228
url: "https://pub.dartlang.org"
239
source: hosted
24-
version: "2.4.1"
10+
version: "2.4.2"
2511
boolean_selector:
2612
dependency: transitive
2713
description:
2814
name: boolean_selector
2915
url: "https://pub.dartlang.org"
3016
source: hosted
3117
version: "2.0.0"
32-
charcode:
18+
characters:
3319
dependency: transitive
3420
description:
35-
name: charcode
21+
name: characters
3622
url: "https://pub.dartlang.org"
3723
source: hosted
38-
version: "1.1.3"
39-
collection:
24+
version: "1.0.0"
25+
charcode:
4026
dependency: transitive
4127
description:
42-
name: collection
28+
name: charcode
4329
url: "https://pub.dartlang.org"
4430
source: hosted
45-
version: "1.14.12"
46-
convert:
31+
version: "1.1.3"
32+
clock:
4733
dependency: transitive
4834
description:
49-
name: convert
35+
name: clock
5036
url: "https://pub.dartlang.org"
5137
source: hosted
52-
version: "2.1.1"
53-
crypto:
38+
version: "1.0.1"
39+
collection:
5440
dependency: transitive
5541
description:
56-
name: crypto
42+
name: collection
5743
url: "https://pub.dartlang.org"
5844
source: hosted
59-
version: "2.1.4"
45+
version: "1.14.13"
6046
cupertino_icons:
6147
dependency: "direct main"
6248
description:
6349
name: cupertino_icons
6450
url: "https://pub.dartlang.org"
6551
source: hosted
6652
version: "0.1.2"
53+
fake_async:
54+
dependency: transitive
55+
description:
56+
name: fake_async
57+
url: "https://pub.dartlang.org"
58+
source: hosted
59+
version: "1.1.0"
6760
flutter:
6861
dependency: "direct main"
6962
description: flutter
@@ -79,20 +72,13 @@ packages:
7972
description: flutter
8073
source: sdk
8174
version: "0.0.0"
82-
image:
83-
dependency: transitive
84-
description:
85-
name: image
86-
url: "https://pub.dartlang.org"
87-
source: hosted
88-
version: "2.1.12"
8975
matcher:
9076
dependency: transitive
9177
description:
9278
name: matcher
9379
url: "https://pub.dartlang.org"
9480
source: hosted
95-
version: "0.12.6"
81+
version: "0.12.8"
9682
meta:
9783
dependency: transitive
9884
description:
@@ -106,21 +92,7 @@ packages:
10692
name: path
10793
url: "https://pub.dartlang.org"
10894
source: hosted
109-
version: "1.6.4"
110-
petitparser:
111-
dependency: transitive
112-
description:
113-
name: petitparser
114-
url: "https://pub.dartlang.org"
115-
source: hosted
116-
version: "2.4.0"
117-
quiver:
118-
dependency: transitive
119-
description:
120-
name: quiver
121-
url: "https://pub.dartlang.org"
122-
source: hosted
123-
version: "2.1.3"
95+
version: "1.7.0"
12496
shared_preferences:
12597
dependency: transitive
12698
description:
@@ -155,7 +127,7 @@ packages:
155127
path: ".."
156128
relative: true
157129
source: path
158-
version: "0.1.0"
130+
version: "0.1.5"
159131
sky_engine:
160132
dependency: transitive
161133
description: flutter
@@ -174,7 +146,7 @@ packages:
174146
name: stack_trace
175147
url: "https://pub.dartlang.org"
176148
source: hosted
177-
version: "1.9.3"
149+
version: "1.9.5"
178150
stream_channel:
179151
dependency: transitive
180152
description:
@@ -202,28 +174,21 @@ packages:
202174
name: test_api
203175
url: "https://pub.dartlang.org"
204176
source: hosted
205-
version: "0.2.15"
177+
version: "0.2.17"
206178
typed_data:
207179
dependency: transitive
208180
description:
209181
name: typed_data
210182
url: "https://pub.dartlang.org"
211183
source: hosted
212-
version: "1.1.6"
184+
version: "1.2.0"
213185
vector_math:
214186
dependency: transitive
215187
description:
216188
name: vector_math
217189
url: "https://pub.dartlang.org"
218190
source: hosted
219191
version: "2.0.8"
220-
xml:
221-
dependency: transitive
222-
description:
223-
name: xml
224-
url: "https://pub.dartlang.org"
225-
source: hosted
226-
version: "3.6.1"
227192
sdks:
228-
dart: ">=2.6.0 <3.0.0"
193+
dart: ">=2.9.0-14.0.dev <3.0.0"
229194
flutter: ">=1.12.13+hotfix.4 <2.0.0"

lib/shared_value.dart

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,14 @@ class SharedValue<T> {
3434

3535
SharedValue({this.key, T value}) {
3636
_value = value;
37-
_updateNonce();
37+
_update(rebuild: false);
3838
}
3939

4040
/// The value held by this state.
4141
T get value => _value;
4242

4343
/// Update [value] and rebuild the dependent widgets if it changed.
4444
set value(T newValue) {
45-
_controller?.add(newValue);
4645
if (newValue == _value) return;
4746
setState(() {
4847
_value = newValue;
@@ -66,8 +65,13 @@ class SharedValue<T> {
6665

6766
var ret = fn?.call();
6867

69-
_updateNonce();
70-
stateManager.rebuild();
68+
if (ret is Future) {
69+
ret.then((_) {
70+
_update();
71+
});
72+
} else {
73+
_update();
74+
}
7175

7276
return ret;
7377
}
@@ -83,7 +87,7 @@ class SharedValue<T> {
8387
}
8488

8589
/// A stream of [value]s that gets updated everytime the internal value is changed.
86-
Stream get stream {
90+
Stream<T> get stream {
8791
_controller ??= StreamController.broadcast();
8892
return _controller.stream;
8993
}
@@ -94,9 +98,18 @@ class SharedValue<T> {
9498
value = fn(_value);
9599
}
96100

97-
void _updateNonce() {
101+
void _update({rebuild: true}) {
102+
// update the nonce
98103
nonce = random.nextDouble();
99104
stateNonceMap[this] = nonce;
105+
106+
if (rebuild) {
107+
// rebuild state manger widget
108+
stateManager.rebuild();
109+
}
110+
111+
// add value to stream
112+
_controller?.add(value);
100113
}
101114

102115
/// Try to load the value stored at [key] in shared preferences.

0 commit comments

Comments
 (0)