File tree Expand file tree Collapse file tree 3 files changed +68
-0
lines changed
packages/signals/examples/dart Expand file tree Collapse file tree 3 files changed +68
-0
lines changed Original file line number Diff line number Diff line change
1
+ # Signals demonstration using pure dart
2
+
3
+ 1 - Pipes
Original file line number Diff line number Diff line change
1
+ import 'dart:math' ;
2
+
3
+ import 'package:signals/signals.dart' ;
4
+
5
+ void main () {
6
+ /*
7
+ Signal<bool> from a periodic Stream
8
+ */
9
+ final timer =
10
+ Stream .periodic (const Duration (milliseconds: 2000 ), (ms) {}).toSignal ();
11
+
12
+ const poolSize = 5 ;
13
+
14
+ final candidat = signal (0 );
15
+
16
+ /*
17
+ Signal<int> from an Iterable
18
+ */
19
+ final source = List .generate (poolSize, (index) => generate ()).toSignal ();
20
+
21
+ /// Each time the timer move on we take the first element of the list
22
+ effect (() {
23
+ // Subscribe to timer event
24
+ timer.value;
25
+
26
+ // We use peak to avoid subscription to source
27
+ final [head, ...tail] = source.peek ();
28
+
29
+ // Batch multiple Signals at once.
30
+ batch (() {
31
+ source.value = tail;
32
+ candidat.value = head;
33
+ });
34
+ });
35
+
36
+ effect (() => print ("Qualified to ${candidat .value }" ));
37
+
38
+ /// Effect on source => refill
39
+ effect (() {
40
+ // Refill our source if required
41
+ while (source.value.length < poolSize) {
42
+ final complement = generate ();
43
+ source.value = source.value..add (complement);
44
+ print ("Complemented with $complement : ${source .value }\n " );
45
+ }
46
+ });
47
+ }
48
+
49
+ final rdm = Random ();
50
+
51
+ int generate () => rdm.nextInt (9999 );
Original file line number Diff line number Diff line change
1
+ name : signals_dart_examples
2
+ description : Demonstrates Signals
3
+ version : 1.0.0
4
+
5
+ environment :
6
+ sdk : ^3.1.0
7
+
8
+ dependencies :
9
+ signals :
10
+ path : ../..
11
+
12
+ dev_dependencies :
13
+ lints : ^2.0.0
14
+ test : ^1.21.0
You can’t perform that action at this time.
0 commit comments