@@ -572,8 +572,62 @@ Use C<migrate> to join the values into a single supply again.
572
572
573
573
method migrate(Supply:D: --> Supply:D)
574
574
575
- Creates a supply that acts like the last supply emitted to the given supply
576
- of supplies (usually created with C < start > ).
575
+ Takes a Supply which itself has values that are of type Supply as input. Each
576
+ time the outer Supply emits a new Supply, this will be tapped and its values
577
+ emitted. Any previously tapped Supply will be closed. This is useful for migrating
578
+ between different data sources, and only paying attention to the latest one.
579
+
580
+ For example, imagine an application where the user can switch between different
581
+ stocks. When they switch to a new one, a connection is established to a web socket
582
+ to get the latest values, and any previous connection should be closed. Each stream
583
+ of values coming over the web socket would be represented as a Supply, which
584
+ themselves are emitted into a Supply of latest data sources to watch.
585
+ The migrate method could be used to flatten this supply of supplies into a single
586
+ Supply of the current values that the user cares about.
587
+
588
+ Here is a simple simulation of such a program:
589
+
590
+ = begin code :skip-test
591
+ my Supplier $stock-sources .= new;
592
+
593
+ sub watch-stock($symbol) {
594
+ $stock-sources.emit: supply {
595
+ say "Starting to watch $symbol";
596
+ whenever Supply.interval(1) {
597
+ emit "$symbol: 111." ~ 99.rand.Int;
598
+ }
599
+ CLOSE say "Lost interest in $symbol";
600
+ }
601
+ }
602
+
603
+ $stock-sources.Supply.migrate.tap: *.say;
604
+
605
+ watch-stock('GOOG');
606
+ sleep 3;
607
+ watch-stock('AAPL');
608
+ sleep 3;
609
+
610
+ $stock-sources.Supply.migrate.tap: *.say;
611
+
612
+ watch-stock('GOOG');
613
+ sleep 3;
614
+ watch-stock('AAPL');
615
+ sleep 3;
616
+ = end code
617
+
618
+ Which produces output like:
619
+
620
+ = begin code
621
+ Starting to watch GOOG
622
+ GOOG: 111.67
623
+ GOOG: 111.20
624
+ GOOG: 111.37
625
+ Lost interest in GOOG
626
+ Starting to watch AAPL
627
+ AAPL: 111.55
628
+ AAPL: 111.6
629
+ AAPL: 111.6
630
+ = end code
577
631
578
632
= head1 Methods that combine supplies
579
633
0 commit comments