1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
|
"use strict";
(self["webpackChunkbrowser_extension"] = self["webpackChunkbrowser_extension"] || []).push([["433"], {
50649(__unused_webpack_module, __webpack_exports__, __webpack_require__) {
__webpack_require__.d(__webpack_exports__, {
Sx: () => (fromPromise)
});
/* import */ var _dist_raise_c17ec2bc_esm_js__rspack_import_0 = __webpack_require__(92287);
/**
* Represents an actor created by `fromTransition`.
*
* The type of `self` within the actor's logic.
*
* @example
*
* ```ts
* import {
* fromTransition,
* createActor,
* type AnyActorSystem
* } from 'xstate';
*
* //* The actor's stored context.
* type Context = {
* // The current count.
* count: number;
* // The amount to increase `count` by.
* step: number;
* };
* // The events the actor receives.
* type Event = { type: 'increment' };
* // The actor's input.
* type Input = { step?: number };
*
* // Actor logic that increments `count` by `step` when it receives an event of
* // type `increment`.
* const logic = fromTransition<Context, Event, AnyActorSystem, Input>(
* (state, event, actorScope) => {
* actorScope.self;
* // ^? TransitionActorRef<Context, Event>
*
* if (event.type === 'increment') {
* return {
* ...state,
* count: state.count + state.step
* };
* }
* return state;
* },
* ({ input, self }) => {
* self;
* // ^? TransitionActorRef<Context, Event>
*
* return {
* count: 0,
* step: input.step ?? 1
* };
* }
* );
*
* const actor = createActor(logic, { input: { step: 10 } });
* // ^? TransitionActorRef<Context, Event>
* ```
*
* @see {@link fromTransition}
*/
/**
* Returns actor logic given a transition function and its initial state.
*
* A “transition function” is a function that takes the current `state` and
* received `event` object as arguments, and returns the next state, similar to
* a reducer.
*
* Actors created from transition logic (“transition actors”) can:
*
* - Receive events
* - Emit snapshots of its state
*
* The transition function’s `state` is used as its transition actor’s
* `context`.
*
* Note that the "state" for a transition function is provided by the initial
* state argument, and is not the same as the State object of an actor or a
* state within a machine configuration.
*
* @example
*
* ```ts
* const transitionLogic = fromTransition(
* (state, event) => {
* if (event.type === 'increment') {
* return {
* ...state,
* count: state.count + 1
* };
* }
* return state;
* },
* { count: 0 }
* );
*
* const transitionActor = createActor(transitionLogic);
* transitionActor.subscribe((snapshot) => {
* console.log(snapshot);
* });
* transitionActor.start();
* // => {
* // status: 'active',
* // context: { count: 0 },
* // ...
* // }
*
* transitionActor.send({ type: 'increment' });
* // => {
* // status: 'active',
* // context: { count: 1 },
* // ...
* // }
* ```
*
* @param transition The transition function used to describe the transition
* logic. It should return the next state given the current state and event.
* It receives the following arguments:
*
* - `state` - the current state.
* - `event` - the received event.
* - `actorScope` - the actor scope object, with properties like `self` and
* `system`.
*
* @param initialContext The initial state of the transition function, either an
* object representing the state, or a function which returns a state object.
* If a function, it will receive as its only argument an object with the
* following properties:
*
* - `input` - the `input` provided to its parent transition actor.
* - `self` - a reference to its parent transition actor.
*
* @returns Actor logic
* @see {@link https://stately.ai/docs/input | Input docs} for more information about how input is passed
*/
function fromTransition(transition, initialContext) {
return {
config: transition,
transition: (snapshot, event, actorScope) => {
return {
...snapshot,
context: transition(snapshot.context, event, actorScope)
};
},
getInitialSnapshot: (_, input) => {
return {
status: 'active',
output: undefined,
error: undefined,
context: typeof initialContext === 'function' ? initialContext({
input
}) : initialContext
};
},
getPersistedSnapshot: snapshot => snapshot,
restoreSnapshot: snapshot => snapshot
};
}
const instanceStates = /* #__PURE__ */new WeakMap();
/**
* Represents an actor created by `fromCallback`.
*
* The type of `self` within the actor's logic.
*
* @example
*
* ```ts
* import { fromCallback, createActor } from 'xstate';
*
* // The events the actor receives.
* type Event = { type: 'someEvent' };
* // The actor's input.
* type Input = { name: string };
*
* // Actor logic that logs whenever it receives an event of type `someEvent`.
* const logic = fromCallback<Event, Input>(({ self, input, receive }) => {
* self;
* // ^? CallbackActorRef<Event, Input>
*
* receive((event) => {
* if (event.type === 'someEvent') {
* console.log(`${input.name}: received "someEvent" event`);
* // logs 'myActor: received "someEvent" event'
* }
* });
* });
*
* const actor = createActor(logic, { input: { name: 'myActor' } });
* // ^? CallbackActorRef<Event, Input>
* ```
*
* @see {@link fromCallback}
*/
/**
* An actor logic creator which returns callback logic as defined by a callback
* function.
*
* @remarks
* Useful for subscription-based or other free-form logic that can send events
* back to the parent actor.
*
* Actors created from callback logic (“callback actors”) can:
*
* - Receive events via the `receive` function
* - Send events to the parent actor via the `sendBack` function
*
* Callback actors are a bit different from other actors in that they:
*
* - Do not work with `onDone`
* - Do not produce a snapshot using `.getSnapshot()`
* - Do not emit values when used with `.subscribe()`
* - Can not be stopped with `.stop()`
*
* @example
*
* ```typescript
* const callbackLogic = fromCallback(({ sendBack, receive }) => {
* let lockStatus = 'unlocked';
*
* const handler = (event) => {
* if (lockStatus === 'locked') {
* return;
* }
* sendBack(event);
* };
*
* receive((event) => {
* if (event.type === 'lock') {
* lockStatus = 'locked';
* } else if (event.type === 'unlock') {
* lockStatus = 'unlocked';
* }
* });
*
* document.body.addEventListener('click', handler);
*
* return () => {
* document.body.removeEventListener('click', handler);
* };
* });
* ```
*
* @param callback - The callback function used to describe the callback logic
* The callback function is passed an object with the following properties:
*
* - `receive` - A function that can send events back to the parent actor; the
* listener is then called whenever events are received by the callback
* actor
* - `sendBack` - A function that can send events back to the parent actor
* - `input` - Data that was provided to the callback actor
* - `self` - The parent actor of the callback actor
* - `system` - The actor system to which the callback actor belongs The callback
* function can (optionally) return a cleanup function, which is called
* when the actor is stopped.
*
* @returns Callback logic
* @see {@link CallbackLogicFunction} for more information about the callback function and its object argument
* @see {@link https://stately.ai/docs/input | Input docs} for more information about how input is passed
*/
function fromCallback(callback) {
const logic = {
config: callback,
start: (state, actorScope) => {
const {
self,
system,
emit
} = actorScope;
const callbackState = {
receivers: undefined,
dispose: undefined
};
instanceStates.set(self, callbackState);
callbackState.dispose = callback({
input: state.input,
system,
self,
sendBack: event => {
if (self.getSnapshot().status === 'stopped') {
return;
}
if (self._parent) {
system._relay(self, self._parent, event);
}
},
receive: listener => {
callbackState.receivers ??= new Set();
callbackState.receivers.add(listener);
},
emit
});
},
transition: (state, event, actorScope) => {
const callbackState = instanceStates.get(actorScope.self);
if (event.type === XSTATE_STOP) {
state = {
...state,
status: 'stopped',
error: undefined
};
callbackState.dispose?.();
return state;
}
callbackState.receivers?.forEach(receiver => receiver(event));
return state;
},
getInitialSnapshot: (_, input) => {
return {
status: 'active',
output: undefined,
error: undefined,
input
};
},
getPersistedSnapshot: snapshot => snapshot,
restoreSnapshot: snapshot => snapshot
};
return logic;
}
const XSTATE_OBSERVABLE_NEXT = 'xstate.observable.next';
const XSTATE_OBSERVABLE_ERROR = 'xstate.observable.error';
const XSTATE_OBSERVABLE_COMPLETE = 'xstate.observable.complete';
/**
* Represents an actor created by `fromObservable` or `fromEventObservable`.
*
* The type of `self` within the actor's logic.
*
* @example
*
* ```ts
* import { fromObservable, createActor } from 'xstate';
* import { interval } from 'rxjs';
*
* // The type of the value observed by the actor's logic.
* type Context = number;
* // The actor's input.
* type Input = { period?: number };
*
* // Actor logic that observes a number incremented every `input.period`
* // milliseconds (default: 1_000).
* const logic = fromObservable<Context, Input>(({ input, self }) => {
* self;
* // ^? ObservableActorRef<Event, Input>
*
* return interval(input.period ?? 1_000);
* });
*
* const actor = createActor(logic, { input: { period: 2_000 } });
* // ^? ObservableActorRef<Event, Input>
* ```
*
* @see {@link fromObservable}
* @see {@link fromEventObservable}
*/
/**
* Observable actor logic is described by an observable stream of values. Actors
* created from observable logic (“observable actors”) can:
*
* - Emit snapshots of the observable’s emitted value
*
* The observable’s emitted value is used as its observable actor’s `context`.
*
* Sending events to observable actors will have no effect.
*
* @example
*
* ```ts
* import { fromObservable, createActor } from 'xstate';
* import { interval } from 'rxjs';
*
* const logic = fromObservable((obj) => interval(1000));
*
* const actor = createActor(logic);
*
* actor.subscribe((snapshot) => {
* console.log(snapshot.context);
* });
*
* actor.start();
* // At every second:
* // Logs 0
* // Logs 1
* // Logs 2
* // ...
* ```
*
* @param observableCreator A function that creates an observable. It receives
* one argument, an object with the following properties:
*
* - `input` - Data that was provided to the observable actor
* - `self` - The parent actor
* - `system` - The actor system to which the observable actor belongs
*
* It should return a {@link Subscribable}, which is compatible with an RxJS
* Observable, although RxJS is not required to create them.
* @see {@link https://rxjs.dev} for documentation on RxJS Observable and observable creators.
* @see {@link Subscribable} interface in XState, which is based on and compatible with RxJS Observable.
*/
function fromObservable(observableCreator) {
// TODO: add event types
const logic = {
config: observableCreator,
transition: (snapshot, event) => {
if (snapshot.status !== 'active') {
return snapshot;
}
switch (event.type) {
case XSTATE_OBSERVABLE_NEXT:
{
const newSnapshot = {
...snapshot,
context: event.data
};
return newSnapshot;
}
case XSTATE_OBSERVABLE_ERROR:
return {
...snapshot,
status: 'error',
error: event.data,
input: undefined,
_subscription: undefined
};
case XSTATE_OBSERVABLE_COMPLETE:
return {
...snapshot,
status: 'done',
input: undefined,
_subscription: undefined
};
case XSTATE_STOP:
snapshot._subscription.unsubscribe();
return {
...snapshot,
status: 'stopped',
input: undefined,
_subscription: undefined
};
default:
return snapshot;
}
},
getInitialSnapshot: (_, input) => {
return {
status: 'active',
output: undefined,
error: undefined,
context: undefined,
input,
_subscription: undefined
};
},
start: (state, {
self,
system,
emit
}) => {
if (state.status === 'done') {
// Do not restart a completed observable
return;
}
state._subscription = observableCreator({
input: state.input,
system,
self,
emit
}).subscribe({
next: value => {
system._relay(self, self, {
type: XSTATE_OBSERVABLE_NEXT,
data: value
});
},
error: err => {
system._relay(self, self, {
type: XSTATE_OBSERVABLE_ERROR,
data: err
});
},
complete: () => {
system._relay(self, self, {
type: XSTATE_OBSERVABLE_COMPLETE
});
}
});
},
getPersistedSnapshot: ({
_subscription,
...state
}) => state,
restoreSnapshot: state => ({
...state,
_subscription: undefined
})
};
return logic;
}
/**
* Creates event observable logic that listens to an observable that delivers
* event objects.
*
* Event observable actor logic is described by an observable stream of
* {@link https://stately.ai/docs/transitions#event-objects | event objects}.
* Actors created from event observable logic (“event observable actors”) can:
*
* - Implicitly send events to its parent actor
* - Emit snapshots of its emitted event objects
*
* Sending events to event observable actors will have no effect.
*
* @example
*
* ```ts
* import {
* fromEventObservable,
* Subscribable,
* EventObject,
* createMachine,
* createActor
* } from 'xstate';
* import { fromEvent } from 'rxjs';
*
* const mouseClickLogic = fromEventObservable(
* () => fromEvent(document.body, 'click') as Subscribable<EventObject>
* );
*
* const canvasMachine = createMachine({
* invoke: {
* // Will send mouse `click` events to the canvas actor
* src: mouseClickLogic
* }
* });
*
* const canvasActor = createActor(canvasMachine);
* canvasActor.start();
* ```
*
* @param lazyObservable A function that creates an observable that delivers
* event objects. It receives one argument, an object with the following
* properties:
*
* - `input` - Data that was provided to the event observable actor
* - `self` - The parent actor
* - `system` - The actor system to which the event observable actor belongs.
*
* It should return a {@link Subscribable}, which is compatible with an RxJS
* Observable, although RxJS is not required to create them.
*/
function fromEventObservable(lazyObservable) {
// TODO: event types
const logic = {
config: lazyObservable,
transition: (state, event) => {
if (state.status !== 'active') {
return state;
}
switch (event.type) {
case XSTATE_OBSERVABLE_ERROR:
return {
...state,
status: 'error',
error: event.data,
input: undefined,
_subscription: undefined
};
case XSTATE_OBSERVABLE_COMPLETE:
return {
...state,
status: 'done',
input: undefined,
_subscription: undefined
};
case XSTATE_STOP:
state._subscription.unsubscribe();
return {
...state,
status: 'stopped',
input: undefined,
_subscription: undefined
};
default:
return state;
}
},
getInitialSnapshot: (_, input) => {
return {
status: 'active',
output: undefined,
error: undefined,
context: undefined,
input,
_subscription: undefined
};
},
start: (state, {
self,
system,
emit
}) => {
if (state.status === 'done') {
// Do not restart a completed observable
return;
}
state._subscription = lazyObservable({
input: state.input,
system,
self,
emit
}).subscribe({
next: value => {
if (self._parent) {
system._relay(self, self._parent, value);
}
},
error: err => {
system._relay(self, self, {
type: XSTATE_OBSERVABLE_ERROR,
data: err
});
},
complete: () => {
system._relay(self, self, {
type: XSTATE_OBSERVABLE_COMPLETE
});
}
});
},
getPersistedSnapshot: ({
_subscription,
...snapshot
}) => snapshot,
restoreSnapshot: snapshot => ({
...snapshot,
_subscription: undefined
})
};
return logic;
}
const XSTATE_PROMISE_RESOLVE = 'xstate.promise.resolve';
const XSTATE_PROMISE_REJECT = 'xstate.promise.reject';
/**
* Represents an actor created by `fromPromise`.
*
* The type of `self` within the actor's logic.
*
* @example
*
* ```ts
* import { fromPromise, createActor } from 'xstate';
*
* // The actor's resolved output
* type Output = string;
* // The actor's input.
* type Input = { message: string };
*
* // Actor logic that fetches the url of an image of a cat saying `input.message`.
* const logic = fromPromise<Output, Input>(async ({ input, self }) => {
* self;
* // ^? PromiseActorRef<Output, Input>
*
* const data = await fetch(
* `https://cataas.com/cat/says/${input.message}`
* );
* const url = await data.json();
* return url;
* });
*
* const actor = createActor(logic, { input: { message: 'hello world' } });
* // ^? PromiseActorRef<Output, Input>
* ```
*
* @see {@link fromPromise}
*/
const controllerMap = new WeakMap();
/**
* An actor logic creator which returns promise logic as defined by an async
* process that resolves or rejects after some time.
*
* Actors created from promise actor logic (“promise actors”) can:
*
* - Emit the resolved value of the promise
* - Output the resolved value of the promise
*
* Sending events to promise actors will have no effect.
*
* @example
*
* ```ts
* const promiseLogic = fromPromise(async () => {
* const result = await fetch('https://example.com/...').then((data) =>
* data.json()
* );
*
* return result;
* });
*
* const promiseActor = createActor(promiseLogic);
* promiseActor.subscribe((snapshot) => {
* console.log(snapshot);
* });
* promiseActor.start();
* // => {
* // output: undefined,
* // status: 'active'
* // ...
* // }
*
* // After promise resolves
* // => {
* // output: { ... },
* // status: 'done',
* // ...
* // }
* ```
*
* @param promiseCreator A function which returns a Promise, and accepts an
* object with the following properties:
*
* - `input` - Data that was provided to the promise actor
* - `self` - The parent actor of the promise actor
* - `system` - The actor system to which the promise actor belongs
*
* @see {@link https://stately.ai/docs/input | Input docs} for more information about how input is passed
*/
function fromPromise(promiseCreator) {
const logic = {
config: promiseCreator,
transition: (state, event, scope) => {
if (state.status !== 'active') {
return state;
}
switch (event.type) {
case XSTATE_PROMISE_RESOLVE:
{
const resolvedValue = event.data;
return {
...state,
status: 'done',
output: resolvedValue,
input: undefined
};
}
case XSTATE_PROMISE_REJECT:
return {
...state,
status: 'error',
error: event.data,
input: undefined
};
case _dist_raise_c17ec2bc_esm_js__rspack_import_0.X:
{
controllerMap.get(scope.self)?.abort();
return {
...state,
status: 'stopped',
input: undefined
};
}
default:
return state;
}
},
start: (state, {
self,
system,
emit
}) => {
// TODO: determine how to allow customizing this so that promises
// can be restarted if necessary
if (state.status !== 'active') {
return;
}
const controller = new AbortController();
controllerMap.set(self, controller);
const resolvedPromise = Promise.resolve(promiseCreator({
input: state.input,
system,
self,
signal: controller.signal,
emit
}));
resolvedPromise.then(response => {
if (self.getSnapshot().status !== 'active') {
return;
}
controllerMap.delete(self);
system._relay(self, self, {
type: XSTATE_PROMISE_RESOLVE,
data: response
});
}, errorData => {
if (self.getSnapshot().status !== 'active') {
return;
}
controllerMap.delete(self);
system._relay(self, self, {
type: XSTATE_PROMISE_REJECT,
data: errorData
});
});
},
getInitialSnapshot: (_, input) => {
return {
status: 'active',
output: undefined,
error: undefined,
input
};
},
getPersistedSnapshot: snapshot => snapshot,
restoreSnapshot: snapshot => snapshot
};
return logic;
}
const emptyLogic = fromTransition(_ => undefined, undefined);
function createEmptyActor() {
return createActor(emptyLogic);
}
},
45801(__unused_webpack_module, __webpack_exports__, __webpack_require__) {
__webpack_require__.d(__webpack_exports__, {
a: () => (assign)
});
/* import */ var _raise_c17ec2bc_esm_js__rspack_import_0 = __webpack_require__(92287);
function createSpawner(actorScope, {
machine,
context
}, event, spawnedChildren) {
const spawn = (src, options = {}) => {
const {
systemId,
input
} = options;
if (typeof src === 'string') {
const logic = (0,_raise_c17ec2bc_esm_js__rspack_import_0.z)(machine, src);
if (!logic) {
throw new Error(`Actor logic '${src}' not implemented in machine '${machine.id}'`);
}
const actorRef = (0,_raise_c17ec2bc_esm_js__rspack_import_0.A)(logic, {
id: options.id,
parent: actorScope.self,
syncSnapshot: options.syncSnapshot,
input: typeof input === 'function' ? input({
context,
event,
self: actorScope.self
}) : input,
src,
systemId
});
spawnedChildren[actorRef.id] = actorRef;
return actorRef;
} else {
const actorRef = (0,_raise_c17ec2bc_esm_js__rspack_import_0.A)(src, {
id: options.id,
parent: actorScope.self,
syncSnapshot: options.syncSnapshot,
input: options.input,
src,
systemId
});
return actorRef;
}
};
return (src, options) => {
const actorRef = spawn(src, options); // TODO: fix types
spawnedChildren[actorRef.id] = actorRef;
actorScope.defer(() => {
if (actorRef._processingStatus === _raise_c17ec2bc_esm_js__rspack_import_0.T.Stopped) {
return;
}
actorRef.start();
});
return actorRef;
};
}
function resolveAssign(actorScope, snapshot, actionArgs, actionParams, {
assignment
}) {
if (!snapshot.context) {
throw new Error('Cannot assign to undefined `context`. Ensure that `context` is defined in the machine config.');
}
const spawnedChildren = {};
const assignArgs = {
context: snapshot.context,
event: actionArgs.event,
spawn: createSpawner(actorScope, snapshot, actionArgs.event, spawnedChildren),
self: actorScope.self,
system: actorScope.system
};
let partialUpdate = {};
if (typeof assignment === 'function') {
partialUpdate = assignment(assignArgs, actionParams);
} else {
for (const key of Object.keys(assignment)) {
const propAssignment = assignment[key];
partialUpdate[key] = typeof propAssignment === 'function' ? propAssignment(assignArgs, actionParams) : propAssignment;
}
}
const updatedContext = Object.assign({}, snapshot.context, partialUpdate);
return [(0,_raise_c17ec2bc_esm_js__rspack_import_0.U)(snapshot, {
context: updatedContext,
children: Object.keys(spawnedChildren).length ? {
...snapshot.children,
...spawnedChildren
} : snapshot.children
}), undefined, undefined];
}
/**
* Updates the current context of the machine.
*
* @example
*
* ```ts
* import { createMachine, assign } from 'xstate';
*
* const countMachine = createMachine({
* context: {
* count: 0,
* message: ''
* },
* on: {
* inc: {
* actions: assign({
* count: ({ context }) => context.count + 1
* })
* },
* updateMessage: {
* actions: assign(({ context, event }) => {
* return {
* message: event.message.trim()
* };
* })
* }
* }
* });
* ```
*
* @param assignment An object that represents the partial context to update, or
* a function that returns an object that represents the partial context to
* update.
*/
function assign(assignment) {
function assign(_args, _params) {
}
assign.type = 'xstate.assign';
assign.assignment = assignment;
assign.resolve = resolveAssign;
return assign;
}
function resolveEmit(_, snapshot, args, actionParams, {
event: eventOrExpr
}) {
const resolvedEvent = typeof eventOrExpr === 'function' ? eventOrExpr(args, actionParams) : eventOrExpr;
return [snapshot, {
event: resolvedEvent
}, undefined];
}
function executeEmit(actorScope, {
event
}) {
actorScope.defer(() => actorScope.emit(event));
}
/**
* Emits an event to event handlers registered on the actor via `actor.on(event,
* handler)`.
*
* @example
*
* ```ts
* import { emit } from 'xstate';
*
* const machine = createMachine({
* // ...
* on: {
* something: {
* actions: emit({
* type: 'emitted',
* some: 'data'
* })
* }
* }
* // ...
* });
*
* const actor = createActor(machine).start();
*
* actor.on('emitted', (event) => {
* console.log(event);
* });
*
* actor.send({ type: 'something' });
* // logs:
* // {
* // type: 'emitted',
* // some: 'data'
* // }
* ```
*/
function emit(/** The event to emit, or an expression that returns an event to emit. */
eventOrExpr) {
function emit(_args, _params) {
}
emit.type = 'xstate.emit';
emit.event = eventOrExpr;
emit.resolve = resolveEmit;
emit.execute = executeEmit;
return emit;
}
/**
* @remarks
* `T | unknown` reduces to `unknown` and that can be problematic when it comes
* to contextual typing. It especially is a problem when the union has a
* function member, like here:
*
* ```ts
* declare function test(
* cbOrVal: ((arg: number) => unknown) | unknown
* ): void;
* test((arg) => {}); // oops, implicit any
* ```
*
* This type can be used to avoid this problem. This union represents the same
* value space as `unknown`.
*/
// https://github.com/microsoft/TypeScript/issues/23182#issuecomment-379091887
// @TODO: Replace with native `NoInfer` when TS issue gets fixed:
// https://github.com/microsoft/TypeScript/pull/57673
/** @deprecated Use the built-in `NoInfer` type instead */
/** The full definition of an event, with a string `type`. */
/**
* The string or object representing the state value relative to the parent
* state node.
*
* @remarks
* - For a child atomic state node, this is a string, e.g., `"pending"`.
* - For complex state nodes, this is an object, e.g., `{ success:
* "someChildState" }`.
*/
/** @deprecated Use `AnyMachineSnapshot` instead */
// TODO: possibly refactor this somehow, use even a simpler type, and maybe even make `machine.options` private or something
/** @ignore */
let SpecialTargets = /*#__PURE__*/(/* unused pure expression or super */ null && (function (SpecialTargets) {
SpecialTargets["Parent"] = "#_parent";
SpecialTargets["Internal"] = "#_internal";
return SpecialTargets;
}({})));
/** @deprecated Use `AnyActor` instead. */
// Based on RxJS types
// TODO: in v6, this should only accept AnyActorLogic, like ActorRefFromLogic
/** @deprecated Use `Actor<T>` instead. */
/**
* Represents logic which can be used by an actor.
*
* @template TSnapshot - The type of the snapshot.
* @template TEvent - The type of the event object.
* @template TInput - The type of the input.
* @template TSystem - The type of the actor system.
*/
/** @deprecated */
// TODO: cover all that can be actually returned
function resolveSendTo(actorScope, snapshot, args, actionParams, {
to,
event: eventOrExpr,
id,
delay
}, extra) {
const delaysMap = snapshot.machine.implementations.delays;
if (typeof eventOrExpr === 'string') {
throw new Error(
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
`Only event objects may be used with sendTo; use sendTo({ type: "${eventOrExpr}" }) instead`);
}
const resolvedEvent = typeof eventOrExpr === 'function' ? eventOrExpr(args, actionParams) : eventOrExpr;
let resolvedDelay;
if (typeof delay === 'string') {
const configDelay = delaysMap && delaysMap[delay];
resolvedDelay = typeof configDelay === 'function' ? configDelay(args, actionParams) : configDelay;
} else {
resolvedDelay = typeof delay === 'function' ? delay(args, actionParams) : delay;
}
const resolvedTarget = typeof to === 'function' ? to(args, actionParams) : to;
let targetActorRef;
if (typeof resolvedTarget === 'string') {
// eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison
if (resolvedTarget === SpecialTargets.Parent) {
targetActorRef = actorScope.self._parent;
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison
else if (resolvedTarget === SpecialTargets.Internal) {
targetActorRef = actorScope.self;
} else if (resolvedTarget.startsWith('#_')) {
// SCXML compatibility: https://www.w3.org/TR/scxml/#SCXMLEventProcessor
// #_invokeid. If the target is the special term '#_invokeid', where invokeid is the invokeid of an SCXML session that the sending session has created by <invoke>, the Processor must add the event to the external queue of that session.
targetActorRef = snapshot.children[resolvedTarget.slice(2)];
} else {
targetActorRef = extra.deferredActorIds?.includes(resolvedTarget) ? resolvedTarget : snapshot.children[resolvedTarget];
}
if (!targetActorRef) {
throw new Error(`Unable to send event to actor '${resolvedTarget}' from machine '${snapshot.machine.id}'.`);
}
} else {
targetActorRef = resolvedTarget || actorScope.self;
}
return [snapshot, {
to: targetActorRef,
targetId: typeof resolvedTarget === 'string' ? resolvedTarget : undefined,
event: resolvedEvent,
id,
delay: resolvedDelay
}, undefined];
}
function retryResolveSendTo(_, snapshot, params) {
if (typeof params.to === 'string') {
params.to = snapshot.children[params.to];
}
}
function executeSendTo(actorScope, params) {
// this forms an outgoing events queue
// thanks to that the recipient actors are able to read the *updated* snapshot value of the sender
actorScope.defer(() => {
const {
to,
event,
delay,
id
} = params;
if (typeof delay === 'number') {
actorScope.system.scheduler.schedule(actorScope.self, to, event, delay, id);
return;
}
actorScope.system._relay(actorScope.self,
// at this point, in a deferred task, it should already be mutated by retryResolveSendTo
// if it initially started as a string
to, event.type === XSTATE_ERROR ? createErrorActorEvent(actorScope.self.id, event.data) : event);
});
}
/**
* Sends an event to an actor.
*
* @param actor The `ActorRef` to send the event to.
* @param event The event to send, or an expression that evaluates to the event
* to send
* @param options Send action options
*
* - `id` - The unique send event identifier (used with `cancel()`).
* - `delay` - The number of milliseconds to delay the sending of the event.
*/
function sendTo(to, eventOrExpr, options) {
function sendTo(_args, _params) {
}
sendTo.type = 'xstate.sendTo';
sendTo.to = to;
sendTo.event = eventOrExpr;
sendTo.id = options?.id;
sendTo.delay = options?.delay;
sendTo.resolve = resolveSendTo;
sendTo.retryResolve = retryResolveSendTo;
sendTo.execute = executeSendTo;
return sendTo;
}
/**
* Sends an event to this machine's parent.
*
* @param event The event to send to the parent machine.
* @param options Options to pass into the send event.
*/
function sendParent(event, options) {
return sendTo(SpecialTargets.Parent, event, options);
}
/**
* Forwards (sends) an event to the `target` actor.
*
* @param target The target actor to forward the event to.
* @param options Options to pass into the send action creator.
*/
function forwardTo(target, options) {
return sendTo(target, ({
event
}) => event, options);
}
function resolveEnqueueActions(actorScope, snapshot, args, actionParams, {
collect
}) {
const actions = [];
const enqueue = function enqueue(action) {
actions.push(action);
};
enqueue.assign = (...args) => {
actions.push(assign(...args));
};
enqueue.cancel = (...args) => {
actions.push(cancel(...args));
};
enqueue.raise = (...args) => {
// for some reason it fails to infer `TDelay` from `...args` here and picks its default (`never`)
// then it fails to typecheck that because `...args` use `string` in place of `TDelay`
actions.push(raise(...args));
};
enqueue.sendTo = (...args) => {
// for some reason it fails to infer `TDelay` from `...args` here and picks its default (`never`)
// then it fails to typecheck that because `...args` use `string` in place of `TDelay
actions.push(sendTo(...args));
};
enqueue.sendParent = (...args) => {
actions.push(sendParent(...args));
};
enqueue.spawnChild = (...args) => {
actions.push(spawnChild(...args));
};
enqueue.stopChild = (...args) => {
actions.push(stopChild(...args));
};
enqueue.emit = (...args) => {
actions.push(emit(...args));
};
collect({
context: args.context,
event: args.event,
enqueue,
check: guard => evaluateGuard(guard, snapshot.context, args.event, snapshot),
self: actorScope.self,
system: actorScope.system
}, actionParams);
return [snapshot, undefined, actions];
}
/**
* Creates an action object that will execute actions that are queued by the
* `enqueue(action)` function.
*
* @example
*
* ```ts
* import { createMachine, enqueueActions } from 'xstate';
*
* const machine = createMachine({
* entry: enqueueActions(({ enqueue, check }) => {
* enqueue.assign({ count: 0 });
*
* if (check('someGuard')) {
* enqueue.assign({ count: 1 });
* }
*
* enqueue('someAction');
* })
* });
* ```
*/
function enqueueActions(collect) {
function enqueueActions(_args, _params) {
}
enqueueActions.type = 'xstate.enqueueActions';
enqueueActions.collect = collect;
enqueueActions.resolve = resolveEnqueueActions;
return enqueueActions;
}
function resolveLog(_, snapshot, actionArgs, actionParams, {
value,
label
}) {
return [snapshot, {
value: typeof value === 'function' ? value(actionArgs, actionParams) : value,
label
}, undefined];
}
function executeLog({
logger
}, {
value,
label
}) {
if (label) {
logger(label, value);
} else {
logger(value);
}
}
/**
* @param expr The expression function to evaluate which will be logged. Takes
* in 2 arguments:
*
* - `ctx` - the current state context
* - `event` - the event that caused this action to be executed.
*
* @param label The label to give to the logged expression.
*/
function log(value = ({
context,
event
}) => ({
context,
event
}), label) {
function log(_args, _params) {
}
log.type = 'xstate.log';
log.value = value;
log.label = label;
log.resolve = resolveLog;
log.execute = executeLog;
return log;
}
},
92287(__unused_webpack_module, __webpack_exports__, __webpack_require__) {
// EXPORTS
__webpack_require__.d(__webpack_exports__, {
x: () => (/* binding */ getStateNodeByPath),
T: () => (/* binding */ raise_c17ec2bc_esm_ProcessingStatus),
j: () => (/* binding */ getStateNodes),
u: () => (/* binding */ getInitialStateNodes),
g: () => (/* binding */ getDelayedTransitions),
r: () => (/* binding */ resolveStateValue),
d: () => (/* binding */ formatInitialTransition),
N: () => (/* binding */ NULL_EVENT),
o: () => (/* binding */ transitionNode),
z: () => (/* binding */ resolveReferencedActor),
a: () => (/* binding */ toTransitionConfigArray),
l: () => (/* binding */ isInFinalState),
w: () => (/* binding */ isStateId),
S: () => (/* binding */ STATE_DELIMITER),
i: () => (/* binding */ getAllStateNodes),
t: () => (/* binding */ toArray),
$: () => (/* binding */ $$ACTOR_TYPE),
f: () => (/* binding */ formatTransitions),
q: () => (/* binding */ createInitEvent),
X: () => (/* binding */ XSTATE_STOP),
c: () => (/* binding */ createInvokeId),
n: () => (/* binding */ macrostep),
y: () => (/* binding */ getPersistedSnapshot),
U: () => (/* binding */ cloneMachineSnapshot),
k: () => (/* binding */ createMachineSnapshot),
v: () => (/* binding */ toStatePath),
h: () => (/* binding */ getCandidates),
s: () => (/* binding */ microstep),
e: () => (/* binding */ evaluateGuard),
p: () => (/* binding */ resolveActionsAndContext),
A: () => (/* binding */ createActor),
b: () => (/* binding */ formatTransition),
m: () => (/* binding */ mapValues)
});
// UNUSED EXPORTS: E, P, I, B, M, F, Q, J, C, G, R, K, V, D, O, H, L, W
;// CONCATENATED MODULE: ./node_modules/.pnpm/xstate@5.19.0/node_modules/xstate/dev/dist/xstate-dev.esm.js
// From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis
function getGlobal() {
if (typeof globalThis !== 'undefined') {
return globalThis;
}
if (typeof self !== 'undefined') {
return self;
}
if (typeof window !== 'undefined') {
return window;
}
if (typeof __webpack_require__.g !== 'undefined') {
return __webpack_require__.g;
}
}
function getDevTools() {
const w = getGlobal();
if (w.__xstate__) {
return w.__xstate__;
}
return undefined;
}
function registerService(service) {
if (typeof window === 'undefined') {
return;
}
const devTools = getDevTools();
if (devTools) {
devTools.register(service);
}
}
const devToolsAdapter = service => {
if (typeof window === 'undefined') {
return;
}
const devTools = getDevTools();
if (devTools) {
devTools.register(service);
}
};
;// CONCATENATED MODULE: ./node_modules/.pnpm/xstate@5.19.0/node_modules/xstate/dist/raise-c17ec2bc.esm.js
class Mailbox {
constructor(_process) {
this._process = _process;
this._active = false;
this._current = null;
this._last = null;
}
start() {
this._active = true;
this.flush();
}
clear() {
// we can't set _current to null because we might be currently processing
// and enqueue following clear shouldnt start processing the enqueued item immediately
if (this._current) {
this._current.next = null;
this._last = this._current;
}
}
enqueue(event) {
const enqueued = {
value: event,
next: null
};
if (this._current) {
this._last.next = enqueued;
this._last = enqueued;
return;
}
this._current = enqueued;
this._last = enqueued;
if (this._active) {
this.flush();
}
}
flush() {
while (this._current) {
// atm the given _process is responsible for implementing proper try/catch handling
// we assume here that this won't throw in a way that can affect this mailbox
const consumed = this._current;
this._process(consumed.value);
this._current = consumed.next;
}
this._last = null;
}
}
const STATE_DELIMITER = '.';
const TARGETLESS_KEY = '';
const NULL_EVENT = '';
const STATE_IDENTIFIER = '#';
const WILDCARD = '*';
const XSTATE_INIT = 'xstate.init';
const XSTATE_ERROR = 'xstate.error';
const XSTATE_STOP = 'xstate.stop';
/**
* Returns an event that represents an implicit event that is sent after the
* specified `delay`.
*
* @param delayRef The delay in milliseconds
* @param id The state node ID where this event is handled
*/
function createAfterEvent(delayRef, id) {
return {
type: `xstate.after.${delayRef}.${id}`
};
}
/**
* Returns an event that represents that a final state node has been reached in
* the parent state node.
*
* @param id The final state node's parent state node `id`
* @param output The data to pass into the event
*/
function createDoneStateEvent(id, output) {
return {
type: `xstate.done.state.${id}`,
output
};
}
/**
* Returns an event that represents that an invoked service has terminated.
*
* An invoked service is terminated when it has reached a top-level final state
* node, but not when it is canceled.
*
* @param invokeId The invoked service ID
* @param output The data to pass into the event
*/
function createDoneActorEvent(invokeId, output) {
return {
type: `xstate.done.actor.${invokeId}`,
output,
actorId: invokeId
};
}
function createErrorActorEvent(id, error) {
return {
type: `xstate.error.actor.${id}`,
error,
actorId: id
};
}
function createInitEvent(input) {
return {
type: XSTATE_INIT,
input
};
}
/**
* This function makes sure that unhandled errors are thrown in a separate
* macrotask. It allows those errors to be detected by global error handlers and
* reported to bug tracking services without interrupting our own stack of
* execution.
*
* @param err Error to be thrown
*/
function reportUnhandledError(err) {
setTimeout(() => {
throw err;
});
}
const symbolObservable = (() => typeof Symbol === 'function' && Symbol.observable || '@@observable')();
function matchesState(parentStateId, childStateId) {
const parentStateValue = toStateValue(parentStateId);
const childStateValue = toStateValue(childStateId);
if (typeof childStateValue === 'string') {
if (typeof parentStateValue === 'string') {
return childStateValue === parentStateValue;
}
// Parent more specific than child
return false;
}
if (typeof parentStateValue === 'string') {
return parentStateValue in childStateValue;
}
return Object.keys(parentStateValue).every(key => {
if (!(key in childStateValue)) {
return false;
}
return matchesState(parentStateValue[key], childStateValue[key]);
});
}
function toStatePath(stateId) {
if (isArray(stateId)) {
return stateId;
}
const result = [];
let segment = '';
for (let i = 0; i < stateId.length; i++) {
const char = stateId.charCodeAt(i);
switch (char) {
// \
case 92:
// consume the next character
segment += stateId[i + 1];
// and skip over it
i++;
continue;
// .
case 46:
result.push(segment);
segment = '';
continue;
}
segment += stateId[i];
}
result.push(segment);
return result;
}
function toStateValue(stateValue) {
if (isMachineSnapshot(stateValue)) {
return stateValue.value;
}
if (typeof stateValue !== 'string') {
return stateValue;
}
const statePath = toStatePath(stateValue);
return pathToStateValue(statePath);
}
function pathToStateValue(statePath) {
if (statePath.length === 1) {
return statePath[0];
}
const value = {};
let marker = value;
for (let i = 0; i < statePath.length - 1; i++) {
if (i === statePath.length - 2) {
marker[statePath[i]] = statePath[i + 1];
} else {
const previous = marker;
marker = {};
previous[statePath[i]] = marker;
}
}
return value;
}
function mapValues(collection, iteratee) {
const result = {};
const collectionKeys = Object.keys(collection);
for (let i = 0; i < collectionKeys.length; i++) {
const key = collectionKeys[i];
result[key] = iteratee(collection[key], key, collection, i);
}
return result;
}
function toArrayStrict(value) {
if (isArray(value)) {
return value;
}
return [value];
}
function toArray(value) {
if (value === undefined) {
return [];
}
return toArrayStrict(value);
}
function resolveOutput(mapper, context, event, self) {
if (typeof mapper === 'function') {
return mapper({
context,
event,
self
});
}
return mapper;
}
function isArray(value) {
return Array.isArray(value);
}
function isErrorActorEvent(event) {
return event.type.startsWith('xstate.error.actor');
}
function toTransitionConfigArray(configLike) {
return toArrayStrict(configLike).map(transitionLike => {
if (typeof transitionLike === 'undefined' || typeof transitionLike === 'string') {
return {
target: transitionLike
};
}
return transitionLike;
});
}
function normalizeTarget(target) {
if (target === undefined || target === TARGETLESS_KEY) {
return undefined;
}
return toArray(target);
}
function toObserver(nextHandler, errorHandler, completionHandler) {
const isObserver = typeof nextHandler === 'object';
const self = isObserver ? nextHandler : undefined;
return {
next: (isObserver ? nextHandler.next : nextHandler)?.bind(self),
error: (isObserver ? nextHandler.error : errorHandler)?.bind(self),
complete: (isObserver ? nextHandler.complete : completionHandler)?.bind(self)
};
}
function createInvokeId(stateNodeId, index) {
return `${index}.${stateNodeId}`;
}
function resolveReferencedActor(machine, src) {
const match = src.match(/^xstate\.invoke\.(\d+)\.(.*)/);
if (!match) {
return machine.implementations.actors[src];
}
const [, indexStr, nodeId] = match;
const node = machine.getStateNodeById(nodeId);
const invokeConfig = node.config.invoke;
return (Array.isArray(invokeConfig) ? invokeConfig[indexStr] : invokeConfig).src;
}
function getAllOwnEventDescriptors(snapshot) {
return [...new Set([...snapshot._nodes.flatMap(sn => sn.ownEvents)])];
}
function createScheduledEventId(actorRef, id) {
return `${actorRef.sessionId}.${id}`;
}
let idCounter = 0;
function createSystem(rootActor, options) {
const children = new Map();
const keyedActors = new Map();
const reverseKeyedActors = new WeakMap();
const inspectionObservers = new Set();
const timerMap = {};
const {
clock,
logger
} = options;
const scheduler = {
schedule: (source, target, event, delay, id = Math.random().toString(36).slice(2)) => {
const scheduledEvent = {
source,
target,
event,
delay,
id,
startedAt: Date.now()
};
const scheduledEventId = createScheduledEventId(source, id);
system._snapshot._scheduledEvents[scheduledEventId] = scheduledEvent;
const timeout = clock.setTimeout(() => {
delete timerMap[scheduledEventId];
delete system._snapshot._scheduledEvents[scheduledEventId];
system._relay(source, target, event);
}, delay);
timerMap[scheduledEventId] = timeout;
},
cancel: (source, id) => {
const scheduledEventId = createScheduledEventId(source, id);
const timeout = timerMap[scheduledEventId];
delete timerMap[scheduledEventId];
delete system._snapshot._scheduledEvents[scheduledEventId];
if (timeout !== undefined) {
clock.clearTimeout(timeout);
}
},
cancelAll: actorRef => {
for (const scheduledEventId in system._snapshot._scheduledEvents) {
const scheduledEvent = system._snapshot._scheduledEvents[scheduledEventId];
if (scheduledEvent.source === actorRef) {
scheduler.cancel(actorRef, scheduledEvent.id);
}
}
}
};
const sendInspectionEvent = event => {
if (!inspectionObservers.size) {
return;
}
const resolvedInspectionEvent = {
...event,
rootId: rootActor.sessionId
};
inspectionObservers.forEach(observer => observer.next?.(resolvedInspectionEvent));
};
const system = {
_snapshot: {
_scheduledEvents: (options?.snapshot && options.snapshot.scheduler) ?? {}
},
_bookId: () => `x:${idCounter++}`,
_register: (sessionId, actorRef) => {
children.set(sessionId, actorRef);
return sessionId;
},
_unregister: actorRef => {
children.delete(actorRef.sessionId);
const systemId = reverseKeyedActors.get(actorRef);
if (systemId !== undefined) {
keyedActors.delete(systemId);
reverseKeyedActors.delete(actorRef);
}
},
get: systemId => {
return keyedActors.get(systemId);
},
_set: (systemId, actorRef) => {
const existing = keyedActors.get(systemId);
if (existing && existing !== actorRef) {
throw new Error(`Actor with system ID '${systemId}' already exists.`);
}
keyedActors.set(systemId, actorRef);
reverseKeyedActors.set(actorRef, systemId);
},
inspect: observerOrFn => {
const observer = toObserver(observerOrFn);
inspectionObservers.add(observer);
return {
unsubscribe() {
inspectionObservers.delete(observer);
}
};
},
_sendInspectionEvent: sendInspectionEvent,
_relay: (source, target, event) => {
system._sendInspectionEvent({
type: '@xstate.event',
sourceRef: source,
actorRef: target,
event
});
target._send(event);
},
scheduler,
getSnapshot: () => {
return {
_scheduledEvents: {
...system._snapshot._scheduledEvents
}
};
},
start: () => {
const scheduledEvents = system._snapshot._scheduledEvents;
system._snapshot._scheduledEvents = {};
for (const scheduledId in scheduledEvents) {
const {
source,
target,
event,
delay,
id
} = scheduledEvents[scheduledId];
scheduler.schedule(source, target, event, delay, id);
}
},
_clock: clock,
_logger: logger
};
return system;
}
let executingCustomAction = false;
const $$ACTOR_TYPE = 1;
// those values are currently used by @xstate/react directly so it's important to keep the assigned values in sync
let raise_c17ec2bc_esm_ProcessingStatus = /*#__PURE__*/function (ProcessingStatus) {
ProcessingStatus[ProcessingStatus["NotStarted"] = 0] = "NotStarted";
ProcessingStatus[ProcessingStatus["Running"] = 1] = "Running";
ProcessingStatus[ProcessingStatus["Stopped"] = 2] = "Stopped";
return ProcessingStatus;
}({});
const defaultOptions = {
clock: {
setTimeout: (fn, ms) => {
return setTimeout(fn, ms);
},
clearTimeout: id => {
return clearTimeout(id);
}
},
logger: console.log.bind(console),
devTools: false
};
/**
* An Actor is a running process that can receive events, send events and change
* its behavior based on the events it receives, which can cause effects outside
* of the actor. When you run a state machine, it becomes an actor.
*/
class Actor {
/**
* Creates a new actor instance for the given logic with the provided options,
* if any.
*
* @param logic The logic to create an actor from
* @param options Actor options
*/
constructor(logic, options) {
this.logic = logic;
/** The current internal state of the actor. */
this._snapshot = void 0;
/**
* The clock that is responsible for setting and clearing timeouts, such as
* delayed events and transitions.
*/
this.clock = void 0;
this.options = void 0;
/** The unique identifier for this actor relative to its parent. */
this.id = void 0;
this.mailbox = new Mailbox(this._process.bind(this));
this.observers = new Set();
this.eventListeners = new Map();
this.logger = void 0;
/** @internal */
this._processingStatus = raise_c17ec2bc_esm_ProcessingStatus.NotStarted;
// Actor Ref
this._parent = void 0;
/** @internal */
this._syncSnapshot = void 0;
this.ref = void 0;
// TODO: add typings for system
this._actorScope = void 0;
this._systemId = void 0;
/** The globally unique process ID for this invocation. */
this.sessionId = void 0;
/** The system to which this actor belongs. */
this.system = void 0;
this._doneEvent = void 0;
this.src = void 0;
// array of functions to defer
this._deferred = [];
const resolvedOptions = {
...defaultOptions,
...options
};
const {
clock,
logger,
parent,
syncSnapshot,
id,
systemId,
inspect
} = resolvedOptions;
this.system = parent ? parent.system : createSystem(this, {
clock,
logger
});
if (inspect && !parent) {
// Always inspect at the system-level
this.system.inspect(toObserver(inspect));
}
this.sessionId = this.system._bookId();
this.id = id ?? this.sessionId;
this.logger = options?.logger ?? this.system._logger;
this.clock = options?.clock ?? this.system._clock;
this._parent = parent;
this._syncSnapshot = syncSnapshot;
this.options = resolvedOptions;
this.src = resolvedOptions.src ?? logic;
this.ref = this;
this._actorScope = {
self: this,
id: this.id,
sessionId: this.sessionId,
logger: this.logger,
defer: fn => {
this._deferred.push(fn);
},
system: this.system,
stopChild: child => {
if (child._parent !== this) {
throw new Error(`Cannot stop child actor ${child.id} of ${this.id} because it is not a child`);
}
child._stop();
},
emit: emittedEvent => {
const listeners = this.eventListeners.get(emittedEvent.type);
const wildcardListener = this.eventListeners.get('*');
if (!listeners && !wildcardListener) {
return;
}
const allListeners = [...(listeners ? listeners.values() : []), ...(wildcardListener ? wildcardListener.values() : [])];
for (const handler of allListeners) {
handler(emittedEvent);
}
},
actionExecutor: action => {
const exec = () => {
this._actorScope.system._sendInspectionEvent({
type: '@xstate.action',
actorRef: this,
action: {
type: action.type,
params: action.params
}
});
if (!action.exec) {
return;
}
const saveExecutingCustomAction = executingCustomAction;
try {
executingCustomAction = true;
action.exec(action.info, action.params);
} finally {
executingCustomAction = saveExecutingCustomAction;
}
};
if (this._processingStatus === raise_c17ec2bc_esm_ProcessingStatus.Running) {
exec();
} else {
this._deferred.push(exec);
}
}
};
// Ensure that the send method is bound to this Actor instance
// if destructured
this.send = this.send.bind(this);
this.system._sendInspectionEvent({
type: '@xstate.actor',
actorRef: this
});
if (systemId) {
this._systemId = systemId;
this.system._set(systemId, this);
}
this._initState(options?.snapshot ?? options?.state);
if (systemId && this._snapshot.status !== 'active') {
this.system._unregister(this);
}
}
_initState(persistedState) {
try {
this._snapshot = persistedState ? this.logic.restoreSnapshot ? this.logic.restoreSnapshot(persistedState, this._actorScope) : persistedState : this.logic.getInitialSnapshot(this._actorScope, this.options?.input);
} catch (err) {
// if we get here then it means that we assign a value to this._snapshot that is not of the correct type
// we can't get the true `TSnapshot & { status: 'error'; }`, it's impossible
// so right now this is a lie of sorts
this._snapshot = {
status: 'error',
output: undefined,
error: err
};
}
}
update(snapshot, event) {
// Update state
this._snapshot = snapshot;
// Execute deferred effects
let deferredFn;
while (deferredFn = this._deferred.shift()) {
try {
deferredFn();
} catch (err) {
// this error can only be caught when executing *initial* actions
// it's the only time when we call actions provided by the user through those deferreds
// when the actor is already running we always execute them synchronously while transitioning
// no "builtin deferred" should actually throw an error since they are either safe
// or the control flow is passed through the mailbox and errors should be caught by the `_process` used by the mailbox
this._deferred.length = 0;
this._snapshot = {
...snapshot,
status: 'error',
error: err
};
}
}
switch (this._snapshot.status) {
case 'active':
for (const observer of this.observers) {
try {
observer.next?.(snapshot);
} catch (err) {
reportUnhandledError(err);
}
}
break;
case 'done':
// next observers are meant to be notified about done snapshots
// this can be seen as something that is different from how observable work
// but with observables `complete` callback is called without any arguments
// it's more ergonomic for XState to treat a done snapshot as a "next" value
// and the completion event as something that is separate,
// something that merely follows emitting that done snapshot
for (const observer of this.observers) {
try {
observer.next?.(snapshot);
} catch (err) {
reportUnhandledError(err);
}
}
this._stopProcedure();
this._complete();
this._doneEvent = createDoneActorEvent(this.id, this._snapshot.output);
if (this._parent) {
this.system._relay(this, this._parent, this._doneEvent);
}
break;
case 'error':
this._error(this._snapshot.error);
break;
}
this.system._sendInspectionEvent({
type: '@xstate.snapshot',
actorRef: this,
event,
snapshot
});
}
/**
* Subscribe an observer to an actor’s snapshot values.
*
* @remarks
* The observer will receive the actor’s snapshot value when it is emitted.
* The observer can be:
*
* - A plain function that receives the latest snapshot, or
* - An observer object whose `.next(snapshot)` method receives the latest
* snapshot
*
* @example
*
* ```ts
* // Observer as a plain function
* const subscription = actor.subscribe((snapshot) => {
* console.log(snapshot);
* });
* ```
*
* @example
*
* ```ts
* // Observer as an object
* const subscription = actor.subscribe({
* next(snapshot) {
* console.log(snapshot);
* },
* error(err) {
* // ...
* },
* complete() {
* // ...
* }
* });
* ```
*
* The return value of `actor.subscribe(observer)` is a subscription object
* that has an `.unsubscribe()` method. You can call
* `subscription.unsubscribe()` to unsubscribe the observer:
*
* @example
*
* ```ts
* const subscription = actor.subscribe((snapshot) => {
* // ...
* });
*
* // Unsubscribe the observer
* subscription.unsubscribe();
* ```
*
* When the actor is stopped, all of its observers will automatically be
* unsubscribed.
*
* @param observer - Either a plain function that receives the latest
* snapshot, or an observer object whose `.next(snapshot)` method receives
* the latest snapshot
*/
subscribe(nextListenerOrObserver, errorListener, completeListener) {
const observer = toObserver(nextListenerOrObserver, errorListener, completeListener);
if (this._processingStatus !== raise_c17ec2bc_esm_ProcessingStatus.Stopped) {
this.observers.add(observer);
} else {
switch (this._snapshot.status) {
case 'done':
try {
observer.complete?.();
} catch (err) {
reportUnhandledError(err);
}
break;
case 'error':
{
const err = this._snapshot.error;
if (!observer.error) {
reportUnhandledError(err);
} else {
try {
observer.error(err);
} catch (err) {
reportUnhandledError(err);
}
}
break;
}
}
}
return {
unsubscribe: () => {
this.observers.delete(observer);
}
};
}
on(type, handler) {
let listeners = this.eventListeners.get(type);
if (!listeners) {
listeners = new Set();
this.eventListeners.set(type, listeners);
}
const wrappedHandler = handler.bind(undefined);
listeners.add(wrappedHandler);
return {
unsubscribe: () => {
listeners.delete(wrappedHandler);
}
};
}
/** Starts the Actor from the initial state */
start() {
if (this._processingStatus === raise_c17ec2bc_esm_ProcessingStatus.Running) {
// Do not restart the service if it is already started
return this;
}
if (this._syncSnapshot) {
this.subscribe({
next: snapshot => {
if (snapshot.status === 'active') {
this.system._relay(this, this._parent, {
type: `xstate.snapshot.${this.id}`,
snapshot
});
}
},
error: () => {}
});
}
this.system._register(this.sessionId, this);
if (this._systemId) {
this.system._set(this._systemId, this);
}
this._processingStatus = raise_c17ec2bc_esm_ProcessingStatus.Running;
// TODO: this isn't correct when rehydrating
const initEvent = createInitEvent(this.options.input);
this.system._sendInspectionEvent({
type: '@xstate.event',
sourceRef: this._parent,
actorRef: this,
event: initEvent
});
const status = this._snapshot.status;
switch (status) {
case 'done':
// a state machine can be "done" upon initialization (it could reach a final state using initial microsteps)
// we still need to complete observers, flush deferreds etc
this.update(this._snapshot, initEvent);
// TODO: rethink cleanup of observers, mailbox, etc
return this;
case 'error':
this._error(this._snapshot.error);
return this;
}
if (!this._parent) {
this.system.start();
}
if (this.logic.start) {
try {
this.logic.start(this._snapshot, this._actorScope);
} catch (err) {
this._snapshot = {
...this._snapshot,
status: 'error',
error: err
};
this._error(err);
return this;
}
}
// TODO: this notifies all subscribers but usually this is redundant
// there is no real change happening here
// we need to rethink if this needs to be refactored
this.update(this._snapshot, initEvent);
if (this.options.devTools) {
this.attachDevTools();
}
this.mailbox.start();
return this;
}
_process(event) {
let nextState;
let caughtError;
try {
nextState = this.logic.transition(this._snapshot, event, this._actorScope);
} catch (err) {
// we wrap it in a box so we can rethrow it later even if falsy value gets caught here
caughtError = {
err
};
}
if (caughtError) {
const {
err
} = caughtError;
this._snapshot = {
...this._snapshot,
status: 'error',
error: err
};
this._error(err);
return;
}
this.update(nextState, event);
if (event.type === XSTATE_STOP) {
this._stopProcedure();
this._complete();
}
}
_stop() {
if (this._processingStatus === raise_c17ec2bc_esm_ProcessingStatus.Stopped) {
return this;
}
this.mailbox.clear();
if (this._processingStatus === raise_c17ec2bc_esm_ProcessingStatus.NotStarted) {
this._processingStatus = raise_c17ec2bc_esm_ProcessingStatus.Stopped;
return this;
}
this.mailbox.enqueue({
type: XSTATE_STOP
});
return this;
}
/** Stops the Actor and unsubscribe all listeners. */
stop() {
if (this._parent) {
throw new Error('A non-root actor cannot be stopped directly.');
}
return this._stop();
}
_complete() {
for (const observer of this.observers) {
try {
observer.complete?.();
} catch (err) {
reportUnhandledError(err);
}
}
this.observers.clear();
}
_reportError(err) {
if (!this.observers.size) {
if (!this._parent) {
reportUnhandledError(err);
}
return;
}
let reportError = false;
for (const observer of this.observers) {
const errorListener = observer.error;
reportError ||= !errorListener;
try {
errorListener?.(err);
} catch (err2) {
reportUnhandledError(err2);
}
}
this.observers.clear();
if (reportError) {
reportUnhandledError(err);
}
}
_error(err) {
this._stopProcedure();
this._reportError(err);
if (this._parent) {
this.system._relay(this, this._parent, createErrorActorEvent(this.id, err));
}
}
// TODO: atm children don't belong entirely to the actor so
// in a way - it's not even super aware of them
// so we can't stop them from here but we really should!
// right now, they are being stopped within the machine's transition
// but that could throw and leave us with "orphaned" active actors
_stopProcedure() {
if (this._processingStatus !== raise_c17ec2bc_esm_ProcessingStatus.Running) {
// Actor already stopped; do nothing
return this;
}
// Cancel all delayed events
this.system.scheduler.cancelAll(this);
// TODO: mailbox.reset
this.mailbox.clear();
// TODO: after `stop` we must prepare ourselves for receiving events again
// events sent *after* stop signal must be queued
// it seems like this should be the common behavior for all of our consumers
// so perhaps this should be unified somehow for all of them
this.mailbox = new Mailbox(this._process.bind(this));
this._processingStatus = raise_c17ec2bc_esm_ProcessingStatus.Stopped;
this.system._unregister(this);
return this;
}
/** @internal */
_send(event) {
if (this._processingStatus === raise_c17ec2bc_esm_ProcessingStatus.Stopped) {
return;
}
this.mailbox.enqueue(event);
}
/**
* Sends an event to the running Actor to trigger a transition.
*
* @param event The event to send
*/
send(event) {
this.system._relay(undefined, this, event);
}
attachDevTools() {
const {
devTools
} = this.options;
if (devTools) {
const resolvedDevToolsAdapter = typeof devTools === 'function' ? devTools : devToolsAdapter;
resolvedDevToolsAdapter(this);
}
}
toJSON() {
return {
xstate$$type: $$ACTOR_TYPE,
id: this.id
};
}
/**
* Obtain the internal state of the actor, which can be persisted.
*
* @remarks
* The internal state can be persisted from any actor, not only machines.
*
* Note that the persisted state is not the same as the snapshot from
* {@link Actor.getSnapshot}. Persisted state represents the internal state of
* the actor, while snapshots represent the actor's last emitted value.
*
* Can be restored with {@link ActorOptions.state}
* @see https://stately.ai/docs/persistence
*/
getPersistedSnapshot(options) {
return this.logic.getPersistedSnapshot(this._snapshot, options);
}
[symbolObservable]() {
return this;
}
/**
* Read an actor’s snapshot synchronously.
*
* @remarks
* The snapshot represent an actor's last emitted value.
*
* When an actor receives an event, its internal state may change. An actor
* may emit a snapshot when a state transition occurs.
*
* Note that some actors, such as callback actors generated with
* `fromCallback`, will not emit snapshots.
* @see {@link Actor.subscribe} to subscribe to an actor’s snapshot values.
* @see {@link Actor.getPersistedSnapshot} to persist the internal state of an actor (which is more than just a snapshot).
*/
getSnapshot() {
return this._snapshot;
}
}
/**
* Creates a new actor instance for the given actor logic with the provided
* options, if any.
*
* @remarks
* When you create an actor from actor logic via `createActor(logic)`, you
* implicitly create an actor system where the created actor is the root actor.
* Any actors spawned from this root actor and its descendants are part of that
* actor system.
* @example
*
* ```ts
* import { createActor } from 'xstate';
* import { someActorLogic } from './someActorLogic.ts';
*
* // Creating the actor, which implicitly creates an actor system with itself as the root actor
* const actor = createActor(someActorLogic);
*
* actor.subscribe((snapshot) => {
* console.log(snapshot);
* });
*
* // Actors must be started by calling `actor.start()`, which will also start the actor system.
* actor.start();
*
* // Actors can receive events
* actor.send({ type: 'someEvent' });
*
* // You can stop root actors by calling `actor.stop()`, which will also stop the actor system and all actors in that system.
* actor.stop();
* ```
*
* @param logic - The actor logic to create an actor from. For a state machine
* actor logic creator, see {@link createMachine}. Other actor logic creators
* include {@link fromCallback}, {@link fromEventObservable},
* {@link fromObservable}, {@link fromPromise}, and {@link fromTransition}.
* @param options - Actor options
*/
function createActor(logic, ...[options]) {
return new Actor(logic, options);
}
/**
* Creates a new Interpreter instance for the given machine with the provided
* options, if any.
*
* @deprecated Use `createActor` instead
* @alias
*/
const interpret = (/* unused pure expression or super */ null && (createActor));
/**
* @deprecated Use `Actor` instead.
* @alias
*/
function resolveCancel(_, snapshot, actionArgs, actionParams, {
sendId
}) {
const resolvedSendId = typeof sendId === 'function' ? sendId(actionArgs, actionParams) : sendId;
return [snapshot, {
sendId: resolvedSendId
}, undefined];
}
function executeCancel(actorScope, params) {
actorScope.defer(() => {
actorScope.system.scheduler.cancel(actorScope.self, params.sendId);
});
}
/**
* Cancels a delayed `sendTo(...)` action that is waiting to be executed. The
* canceled `sendTo(...)` action will not send its event or execute, unless the
* `delay` has already elapsed before `cancel(...)` is called.
*
* @example
*
* ```ts
* import { createMachine, sendTo, cancel } from 'xstate';
*
* const machine = createMachine({
* // ...
* on: {
* sendEvent: {
* actions: sendTo(
* 'some-actor',
* { type: 'someEvent' },
* {
* id: 'some-id',
* delay: 1000
* }
* )
* },
* cancelEvent: {
* actions: cancel('some-id')
* }
* }
* });
* ```
*
* @param sendId The `id` of the `sendTo(...)` action to cancel.
*/
function raise_c17ec2bc_esm_cancel(sendId) {
function cancel(_args, _params) {
}
cancel.type = 'xstate.cancel';
cancel.sendId = sendId;
cancel.resolve = resolveCancel;
cancel.execute = executeCancel;
return cancel;
}
function resolveSpawn(actorScope, snapshot, actionArgs, _actionParams, {
id,
systemId,
src,
input,
syncSnapshot
}) {
const logic = typeof src === 'string' ? resolveReferencedActor(snapshot.machine, src) : src;
const resolvedId = typeof id === 'function' ? id(actionArgs) : id;
let actorRef;
let resolvedInput = undefined;
if (logic) {
resolvedInput = typeof input === 'function' ? input({
context: snapshot.context,
event: actionArgs.event,
self: actorScope.self
}) : input;
actorRef = createActor(logic, {
id: resolvedId,
src,
parent: actorScope.self,
syncSnapshot,
systemId,
input: resolvedInput
});
}
return [cloneMachineSnapshot(snapshot, {
children: {
...snapshot.children,
[resolvedId]: actorRef
}
}), {
id,
systemId,
actorRef,
src,
input: resolvedInput
}, undefined];
}
function executeSpawn(actorScope, {
actorRef
}) {
if (!actorRef) {
return;
}
actorScope.defer(() => {
if (actorRef._processingStatus === raise_c17ec2bc_esm_ProcessingStatus.Stopped) {
return;
}
actorRef.start();
});
}
function raise_c17ec2bc_esm_spawnChild(...[src, {
id,
systemId,
input,
syncSnapshot = false
} = {}]) {
function spawnChild(_args, _params) {
}
spawnChild.type = 'xstate.spawnChild';
spawnChild.id = id;
spawnChild.systemId = systemId;
spawnChild.src = src;
spawnChild.input = input;
spawnChild.syncSnapshot = syncSnapshot;
spawnChild.resolve = resolveSpawn;
spawnChild.execute = executeSpawn;
return spawnChild;
}
function resolveStop(_, snapshot, args, actionParams, {
actorRef
}) {
const actorRefOrString = typeof actorRef === 'function' ? actorRef(args, actionParams) : actorRef;
const resolvedActorRef = typeof actorRefOrString === 'string' ? snapshot.children[actorRefOrString] : actorRefOrString;
let children = snapshot.children;
if (resolvedActorRef) {
children = {
...children
};
delete children[resolvedActorRef.id];
}
return [cloneMachineSnapshot(snapshot, {
children
}), resolvedActorRef, undefined];
}
function executeStop(actorScope, actorRef) {
if (!actorRef) {
return;
}
// we need to eagerly unregister it here so a new actor with the same systemId can be registered immediately
// since we defer actual stopping of the actor but we don't defer actor creations (and we can't do that)
// this could throw on `systemId` collision, for example, when dealing with reentering transitions
actorScope.system._unregister(actorRef);
// this allows us to prevent an actor from being started if it gets stopped within the same macrostep
// this can happen, for example, when the invoking state is being exited immediately by an always transition
if (actorRef._processingStatus !== raise_c17ec2bc_esm_ProcessingStatus.Running) {
actorScope.stopChild(actorRef);
return;
}
// stopping a child enqueues a stop event in the child actor's mailbox
// we need for all of the already enqueued events to be processed before we stop the child
// the parent itself might want to send some events to a child (for example from exit actions on the invoking state)
// and we don't want to ignore those events
actorScope.defer(() => {
actorScope.stopChild(actorRef);
});
}
/**
* Stops a child actor.
*
* @param actorRef The actor to stop.
*/
function stopChild(actorRef) {
function stop(_args, _params) {
}
stop.type = 'xstate.stopChild';
stop.actorRef = actorRef;
stop.resolve = resolveStop;
stop.execute = executeStop;
return stop;
}
/**
* Stops a child actor.
*
* @deprecated Use `stopChild(...)` instead
* @alias
*/
const raise_c17ec2bc_esm_stop = (/* unused pure expression or super */ null && (stopChild));
function checkStateIn(snapshot, _, {
stateValue
}) {
if (typeof stateValue === 'string' && isStateId(stateValue)) {
const target = snapshot.machine.getStateNodeById(stateValue);
return snapshot._nodes.some(sn => sn === target);
}
return snapshot.matches(stateValue);
}
function raise_c17ec2bc_esm_stateIn(stateValue) {
function stateIn() {
return false;
}
stateIn.check = checkStateIn;
stateIn.stateValue = stateValue;
return stateIn;
}
function checkNot(snapshot, {
context,
event
}, {
guards
}) {
return !evaluateGuard(guards[0], context, event, snapshot);
}
/**
* Higher-order guard that evaluates to `true` if the `guard` passed to it
* evaluates to `false`.
*
* @category Guards
* @example
*
* ```ts
* import { setup, not } from 'xstate';
*
* const machine = setup({
* guards: {
* someNamedGuard: () => false
* }
* }).createMachine({
* on: {
* someEvent: {
* guard: not('someNamedGuard'),
* actions: () => {
* // will be executed if guard in `not(...)`
* // evaluates to `false`
* }
* }
* }
* });
* ```
*
* @returns A guard
*/
function raise_c17ec2bc_esm_not(guard) {
function not(_args, _params) {
return false;
}
not.check = checkNot;
not.guards = [guard];
return not;
}
function checkAnd(snapshot, {
context,
event
}, {
guards
}) {
return guards.every(guard => evaluateGuard(guard, context, event, snapshot));
}
/**
* Higher-order guard that evaluates to `true` if all `guards` passed to it
* evaluate to `true`.
*
* @category Guards
* @example
*
* ```ts
* import { setup, and } from 'xstate';
*
* const machine = setup({
* guards: {
* someNamedGuard: () => true
* }
* }).createMachine({
* on: {
* someEvent: {
* guard: and([({ context }) => context.value > 0, 'someNamedGuard']),
* actions: () => {
* // will be executed if all guards in `and(...)`
* // evaluate to true
* }
* }
* }
* });
* ```
*
* @returns A guard action object
*/
function raise_c17ec2bc_esm_and(guards) {
function and(_args, _params) {
return false;
}
and.check = checkAnd;
and.guards = guards;
return and;
}
function checkOr(snapshot, {
context,
event
}, {
guards
}) {
return guards.some(guard => evaluateGuard(guard, context, event, snapshot));
}
/**
* Higher-order guard that evaluates to `true` if any of the `guards` passed to
* it evaluate to `true`.
*
* @category Guards
* @example
*
* ```ts
* import { setup, or } from 'xstate';
*
* const machine = setup({
* guards: {
* someNamedGuard: () => true
* }
* }).createMachine({
* on: {
* someEvent: {
* guard: or([({ context }) => context.value > 0, 'someNamedGuard']),
* actions: () => {
* // will be executed if any of the guards in `or(...)`
* // evaluate to true
* }
* }
* }
* });
* ```
*
* @returns A guard action object
*/
function raise_c17ec2bc_esm_or(guards) {
function or(_args, _params) {
return false;
}
or.check = checkOr;
or.guards = guards;
return or;
}
// TODO: throw on cycles (depth check should be enough)
function evaluateGuard(guard, context, event, snapshot) {
const {
machine
} = snapshot;
const isInline = typeof guard === 'function';
const resolved = isInline ? guard : machine.implementations.guards[typeof guard === 'string' ? guard : guard.type];
if (!isInline && !resolved) {
throw new Error(`Guard '${typeof guard === 'string' ? guard : guard.type}' is not implemented.'.`);
}
if (typeof resolved !== 'function') {
return evaluateGuard(resolved, context, event, snapshot);
}
const guardArgs = {
context,
event
};
const guardParams = isInline || typeof guard === 'string' ? undefined : 'params' in guard ? typeof guard.params === 'function' ? guard.params({
context,
event
}) : guard.params : undefined;
if (!('check' in resolved)) {
// the existing type of `.guards` assumes non-nullable `TExpressionGuard`
// inline guards expect `TExpressionGuard` to be set to `undefined`
// it's fine to cast this here, our logic makes sure that we call those 2 "variants" correctly
return resolved(guardArgs, guardParams);
}
const builtinGuard = resolved;
return builtinGuard.check(snapshot, guardArgs, resolved // this holds all params
);
}
const isAtomicStateNode = stateNode => stateNode.type === 'atomic' || stateNode.type === 'final';
function getChildren(stateNode) {
return Object.values(stateNode.states).filter(sn => sn.type !== 'history');
}
function getProperAncestors(stateNode, toStateNode) {
const ancestors = [];
if (toStateNode === stateNode) {
return ancestors;
}
// add all ancestors
let m = stateNode.parent;
while (m && m !== toStateNode) {
ancestors.push(m);
m = m.parent;
}
return ancestors;
}
function getAllStateNodes(stateNodes) {
const nodeSet = new Set(stateNodes);
const adjList = getAdjList(nodeSet);
// add descendants
for (const s of nodeSet) {
// if previously active, add existing child nodes
if (s.type === 'compound' && (!adjList.get(s) || !adjList.get(s).length)) {
getInitialStateNodesWithTheirAncestors(s).forEach(sn => nodeSet.add(sn));
} else {
if (s.type === 'parallel') {
for (const child of getChildren(s)) {
if (child.type === 'history') {
continue;
}
if (!nodeSet.has(child)) {
const initialStates = getInitialStateNodesWithTheirAncestors(child);
for (const initialStateNode of initialStates) {
nodeSet.add(initialStateNode);
}
}
}
}
}
}
// add all ancestors
for (const s of nodeSet) {
let m = s.parent;
while (m) {
nodeSet.add(m);
m = m.parent;
}
}
return nodeSet;
}
function getValueFromAdj(baseNode, adjList) {
const childStateNodes = adjList.get(baseNode);
if (!childStateNodes) {
return {}; // todo: fix?
}
if (baseNode.type === 'compound') {
const childStateNode = childStateNodes[0];
if (childStateNode) {
if (isAtomicStateNode(childStateNode)) {
return childStateNode.key;
}
} else {
return {};
}
}
const stateValue = {};
for (const childStateNode of childStateNodes) {
stateValue[childStateNode.key] = getValueFromAdj(childStateNode, adjList);
}
return stateValue;
}
function getAdjList(stateNodes) {
const adjList = new Map();
for (const s of stateNodes) {
if (!adjList.has(s)) {
adjList.set(s, []);
}
if (s.parent) {
if (!adjList.has(s.parent)) {
adjList.set(s.parent, []);
}
adjList.get(s.parent).push(s);
}
}
return adjList;
}
function getStateValue(rootNode, stateNodes) {
const config = getAllStateNodes(stateNodes);
return getValueFromAdj(rootNode, getAdjList(config));
}
function isInFinalState(stateNodeSet, stateNode) {
if (stateNode.type === 'compound') {
return getChildren(stateNode).some(s => s.type === 'final' && stateNodeSet.has(s));
}
if (stateNode.type === 'parallel') {
return getChildren(stateNode).every(sn => isInFinalState(stateNodeSet, sn));
}
return stateNode.type === 'final';
}
const isStateId = str => str[0] === STATE_IDENTIFIER;
function getCandidates(stateNode, receivedEventType) {
const candidates = stateNode.transitions.get(receivedEventType) || [...stateNode.transitions.keys()].filter(eventDescriptor => {
// check if transition is a wildcard transition,
// which matches any non-transient events
if (eventDescriptor === WILDCARD) {
return true;
}
if (!eventDescriptor.endsWith('.*')) {
return false;
}
const partialEventTokens = eventDescriptor.split('.');
const eventTokens = receivedEventType.split('.');
for (let tokenIndex = 0; tokenIndex < partialEventTokens.length; tokenIndex++) {
const partialEventToken = partialEventTokens[tokenIndex];
const eventToken = eventTokens[tokenIndex];
if (partialEventToken === '*') {
const isLastToken = tokenIndex === partialEventTokens.length - 1;
return isLastToken;
}
if (partialEventToken !== eventToken) {
return false;
}
}
return true;
}).sort((a, b) => b.length - a.length).flatMap(key => stateNode.transitions.get(key));
return candidates;
}
/** All delayed transitions from the config. */
function getDelayedTransitions(stateNode) {
const afterConfig = stateNode.config.after;
if (!afterConfig) {
return [];
}
const mutateEntryExit = delay => {
const afterEvent = createAfterEvent(delay, stateNode.id);
const eventType = afterEvent.type;
stateNode.entry.push(raise_c17ec2bc_esm_raise(afterEvent, {
id: eventType,
delay
}));
stateNode.exit.push(raise_c17ec2bc_esm_cancel(eventType));
return eventType;
};
const delayedTransitions = Object.keys(afterConfig).flatMap(delay => {
const configTransition = afterConfig[delay];
const resolvedTransition = typeof configTransition === 'string' ? {
target: configTransition
} : configTransition;
const resolvedDelay = Number.isNaN(+delay) ? delay : +delay;
const eventType = mutateEntryExit(resolvedDelay);
return toArray(resolvedTransition).map(transition => ({
...transition,
event: eventType,
delay: resolvedDelay
}));
});
return delayedTransitions.map(delayedTransition => {
const {
delay
} = delayedTransition;
return {
...formatTransition(stateNode, delayedTransition.event, delayedTransition),
delay
};
});
}
function formatTransition(stateNode, descriptor, transitionConfig) {
const normalizedTarget = normalizeTarget(transitionConfig.target);
const reenter = transitionConfig.reenter ?? false;
const target = resolveTarget(stateNode, normalizedTarget);
const transition = {
...transitionConfig,
actions: toArray(transitionConfig.actions),
guard: transitionConfig.guard,
target,
source: stateNode,
reenter,
eventType: descriptor,
toJSON: () => ({
...transition,
source: `#${stateNode.id}`,
target: target ? target.map(t => `#${t.id}`) : undefined
})
};
return transition;
}
function formatTransitions(stateNode) {
const transitions = new Map();
if (stateNode.config.on) {
for (const descriptor of Object.keys(stateNode.config.on)) {
if (descriptor === NULL_EVENT) {
throw new Error('Null events ("") cannot be specified as a transition key. Use `always: { ... }` instead.');
}
const transitionsConfig = stateNode.config.on[descriptor];
transitions.set(descriptor, toTransitionConfigArray(transitionsConfig).map(t => formatTransition(stateNode, descriptor, t)));
}
}
if (stateNode.config.onDone) {
const descriptor = `xstate.done.state.${stateNode.id}`;
transitions.set(descriptor, toTransitionConfigArray(stateNode.config.onDone).map(t => formatTransition(stateNode, descriptor, t)));
}
for (const invokeDef of stateNode.invoke) {
if (invokeDef.onDone) {
const descriptor = `xstate.done.actor.${invokeDef.id}`;
transitions.set(descriptor, toTransitionConfigArray(invokeDef.onDone).map(t => formatTransition(stateNode, descriptor, t)));
}
if (invokeDef.onError) {
const descriptor = `xstate.error.actor.${invokeDef.id}`;
transitions.set(descriptor, toTransitionConfigArray(invokeDef.onError).map(t => formatTransition(stateNode, descriptor, t)));
}
if (invokeDef.onSnapshot) {
const descriptor = `xstate.snapshot.${invokeDef.id}`;
transitions.set(descriptor, toTransitionConfigArray(invokeDef.onSnapshot).map(t => formatTransition(stateNode, descriptor, t)));
}
}
for (const delayedTransition of stateNode.after) {
let existing = transitions.get(delayedTransition.eventType);
if (!existing) {
existing = [];
transitions.set(delayedTransition.eventType, existing);
}
existing.push(delayedTransition);
}
return transitions;
}
function formatInitialTransition(stateNode, _target) {
const resolvedTarget = typeof _target === 'string' ? stateNode.states[_target] : _target ? stateNode.states[_target.target] : undefined;
if (!resolvedTarget && _target) {
throw new Error(
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions, @typescript-eslint/no-base-to-string
`Initial state node "${_target}" not found on parent state node #${stateNode.id}`);
}
const transition = {
source: stateNode,
actions: !_target || typeof _target === 'string' ? [] : toArray(_target.actions),
eventType: null,
reenter: false,
target: resolvedTarget ? [resolvedTarget] : [],
toJSON: () => ({
...transition,
source: `#${stateNode.id}`,
target: resolvedTarget ? [`#${resolvedTarget.id}`] : []
})
};
return transition;
}
function resolveTarget(stateNode, targets) {
if (targets === undefined) {
// an undefined target signals that the state node should not transition from that state when receiving that event
return undefined;
}
return targets.map(target => {
if (typeof target !== 'string') {
return target;
}
if (isStateId(target)) {
return stateNode.machine.getStateNodeById(target);
}
const isInternalTarget = target[0] === STATE_DELIMITER;
// If internal target is defined on machine,
// do not include machine key on target
if (isInternalTarget && !stateNode.parent) {
return getStateNodeByPath(stateNode, target.slice(1));
}
const resolvedTarget = isInternalTarget ? stateNode.key + target : target;
if (stateNode.parent) {
try {
const targetStateNode = getStateNodeByPath(stateNode.parent, resolvedTarget);
return targetStateNode;
} catch (err) {
throw new Error(`Invalid transition definition for state node '${stateNode.id}':\n${err.message}`);
}
} else {
throw new Error(`Invalid target: "${target}" is not a valid target from the root node. Did you mean ".${target}"?`);
}
});
}
function resolveHistoryDefaultTransition(stateNode) {
const normalizedTarget = normalizeTarget(stateNode.config.target);
if (!normalizedTarget) {
return stateNode.parent.initial;
}
return {
target: normalizedTarget.map(t => typeof t === 'string' ? getStateNodeByPath(stateNode.parent, t) : t)
};
}
function isHistoryNode(stateNode) {
return stateNode.type === 'history';
}
function getInitialStateNodesWithTheirAncestors(stateNode) {
const states = getInitialStateNodes(stateNode);
for (const initialState of states) {
for (const ancestor of getProperAncestors(initialState, stateNode)) {
states.add(ancestor);
}
}
return states;
}
function getInitialStateNodes(stateNode) {
const set = new Set();
function iter(descStateNode) {
if (set.has(descStateNode)) {
return;
}
set.add(descStateNode);
if (descStateNode.type === 'compound') {
iter(descStateNode.initial.target[0]);
} else if (descStateNode.type === 'parallel') {
for (const child of getChildren(descStateNode)) {
iter(child);
}
}
}
iter(stateNode);
return set;
}
/** Returns the child state node from its relative `stateKey`, or throws. */
function getStateNode(stateNode, stateKey) {
if (isStateId(stateKey)) {
return stateNode.machine.getStateNodeById(stateKey);
}
if (!stateNode.states) {
throw new Error(`Unable to retrieve child state '${stateKey}' from '${stateNode.id}'; no child states exist.`);
}
const result = stateNode.states[stateKey];
if (!result) {
throw new Error(`Child state '${stateKey}' does not exist on '${stateNode.id}'`);
}
return result;
}
/**
* Returns the relative state node from the given `statePath`, or throws.
*
* @param statePath The string or string array relative path to the state node.
*/
function getStateNodeByPath(stateNode, statePath) {
if (typeof statePath === 'string' && isStateId(statePath)) {
try {
return stateNode.machine.getStateNodeById(statePath);
} catch {
// try individual paths
// throw e;
}
}
const arrayStatePath = toStatePath(statePath).slice();
let currentStateNode = stateNode;
while (arrayStatePath.length) {
const key = arrayStatePath.shift();
if (!key.length) {
break;
}
currentStateNode = getStateNode(currentStateNode, key);
}
return currentStateNode;
}
/**
* Returns the state nodes represented by the current state value.
*
* @param stateValue The state value or State instance
*/
function getStateNodes(stateNode, stateValue) {
if (typeof stateValue === 'string') {
const childStateNode = stateNode.states[stateValue];
if (!childStateNode) {
throw new Error(`State '${stateValue}' does not exist on '${stateNode.id}'`);
}
return [stateNode, childStateNode];
}
const childStateKeys = Object.keys(stateValue);
const childStateNodes = childStateKeys.map(subStateKey => getStateNode(stateNode, subStateKey)).filter(Boolean);
return [stateNode.machine.root, stateNode].concat(childStateNodes, childStateKeys.reduce((allSubStateNodes, subStateKey) => {
const subStateNode = getStateNode(stateNode, subStateKey);
if (!subStateNode) {
return allSubStateNodes;
}
const subStateNodes = getStateNodes(subStateNode, stateValue[subStateKey]);
return allSubStateNodes.concat(subStateNodes);
}, []));
}
function transitionAtomicNode(stateNode, stateValue, snapshot, event) {
const childStateNode = getStateNode(stateNode, stateValue);
const next = childStateNode.next(snapshot, event);
if (!next || !next.length) {
return stateNode.next(snapshot, event);
}
return next;
}
function transitionCompoundNode(stateNode, stateValue, snapshot, event) {
const subStateKeys = Object.keys(stateValue);
const childStateNode = getStateNode(stateNode, subStateKeys[0]);
const next = transitionNode(childStateNode, stateValue[subStateKeys[0]], snapshot, event);
if (!next || !next.length) {
return stateNode.next(snapshot, event);
}
return next;
}
function transitionParallelNode(stateNode, stateValue, snapshot, event) {
const allInnerTransitions = [];
for (const subStateKey of Object.keys(stateValue)) {
const subStateValue = stateValue[subStateKey];
if (!subStateValue) {
continue;
}
const subStateNode = getStateNode(stateNode, subStateKey);
const innerTransitions = transitionNode(subStateNode, subStateValue, snapshot, event);
if (innerTransitions) {
allInnerTransitions.push(...innerTransitions);
}
}
if (!allInnerTransitions.length) {
return stateNode.next(snapshot, event);
}
return allInnerTransitions;
}
function transitionNode(stateNode, stateValue, snapshot, event) {
// leaf node
if (typeof stateValue === 'string') {
return transitionAtomicNode(stateNode, stateValue, snapshot, event);
}
// compound node
if (Object.keys(stateValue).length === 1) {
return transitionCompoundNode(stateNode, stateValue, snapshot, event);
}
// parallel node
return transitionParallelNode(stateNode, stateValue, snapshot, event);
}
function getHistoryNodes(stateNode) {
return Object.keys(stateNode.states).map(key => stateNode.states[key]).filter(sn => sn.type === 'history');
}
function isDescendant(childStateNode, parentStateNode) {
let marker = childStateNode;
while (marker.parent && marker.parent !== parentStateNode) {
marker = marker.parent;
}
return marker.parent === parentStateNode;
}
function hasIntersection(s1, s2) {
const set1 = new Set(s1);
const set2 = new Set(s2);
for (const item of set1) {
if (set2.has(item)) {
return true;
}
}
for (const item of set2) {
if (set1.has(item)) {
return true;
}
}
return false;
}
function removeConflictingTransitions(enabledTransitions, stateNodeSet, historyValue) {
const filteredTransitions = new Set();
for (const t1 of enabledTransitions) {
let t1Preempted = false;
const transitionsToRemove = new Set();
for (const t2 of filteredTransitions) {
if (hasIntersection(computeExitSet([t1], stateNodeSet, historyValue), computeExitSet([t2], stateNodeSet, historyValue))) {
if (isDescendant(t1.source, t2.source)) {
transitionsToRemove.add(t2);
} else {
t1Preempted = true;
break;
}
}
}
if (!t1Preempted) {
for (const t3 of transitionsToRemove) {
filteredTransitions.delete(t3);
}
filteredTransitions.add(t1);
}
}
return Array.from(filteredTransitions);
}
function findLeastCommonAncestor(stateNodes) {
const [head, ...tail] = stateNodes;
for (const ancestor of getProperAncestors(head, undefined)) {
if (tail.every(sn => isDescendant(sn, ancestor))) {
return ancestor;
}
}
}
function getEffectiveTargetStates(transition, historyValue) {
if (!transition.target) {
return [];
}
const targets = new Set();
for (const targetNode of transition.target) {
if (isHistoryNode(targetNode)) {
if (historyValue[targetNode.id]) {
for (const node of historyValue[targetNode.id]) {
targets.add(node);
}
} else {
for (const node of getEffectiveTargetStates(resolveHistoryDefaultTransition(targetNode), historyValue)) {
targets.add(node);
}
}
} else {
targets.add(targetNode);
}
}
return [...targets];
}
function getTransitionDomain(transition, historyValue) {
const targetStates = getEffectiveTargetStates(transition, historyValue);
if (!targetStates) {
return;
}
if (!transition.reenter && targetStates.every(target => target === transition.source || isDescendant(target, transition.source))) {
return transition.source;
}
const lca = findLeastCommonAncestor(targetStates.concat(transition.source));
if (lca) {
return lca;
}
// at this point we know that it's a root transition since LCA couldn't be found
if (transition.reenter) {
return;
}
return transition.source.machine.root;
}
function computeExitSet(transitions, stateNodeSet, historyValue) {
const statesToExit = new Set();
for (const t of transitions) {
if (t.target?.length) {
const domain = getTransitionDomain(t, historyValue);
if (t.reenter && t.source === domain) {
statesToExit.add(domain);
}
for (const stateNode of stateNodeSet) {
if (isDescendant(stateNode, domain)) {
statesToExit.add(stateNode);
}
}
}
}
return [...statesToExit];
}
function areStateNodeCollectionsEqual(prevStateNodes, nextStateNodeSet) {
if (prevStateNodes.length !== nextStateNodeSet.size) {
return false;
}
for (const node of prevStateNodes) {
if (!nextStateNodeSet.has(node)) {
return false;
}
}
return true;
}
/** https://www.w3.org/TR/scxml/#microstepProcedure */
function microstep(transitions, currentSnapshot, actorScope, event, isInitial, internalQueue) {
if (!transitions.length) {
return currentSnapshot;
}
const mutStateNodeSet = new Set(currentSnapshot._nodes);
let historyValue = currentSnapshot.historyValue;
const filteredTransitions = removeConflictingTransitions(transitions, mutStateNodeSet, historyValue);
let nextState = currentSnapshot;
// Exit states
if (!isInitial) {
[nextState, historyValue] = exitStates(nextState, event, actorScope, filteredTransitions, mutStateNodeSet, historyValue, internalQueue, actorScope.actionExecutor);
}
// Execute transition content
nextState = resolveActionsAndContext(nextState, event, actorScope, filteredTransitions.flatMap(t => t.actions), internalQueue, undefined);
// Enter states
nextState = enterStates(nextState, event, actorScope, filteredTransitions, mutStateNodeSet, internalQueue, historyValue, isInitial);
const nextStateNodes = [...mutStateNodeSet];
if (nextState.status === 'done') {
nextState = resolveActionsAndContext(nextState, event, actorScope, nextStateNodes.sort((a, b) => b.order - a.order).flatMap(state => state.exit), internalQueue, undefined);
}
// eslint-disable-next-line no-useless-catch
try {
if (historyValue === currentSnapshot.historyValue && areStateNodeCollectionsEqual(currentSnapshot._nodes, mutStateNodeSet)) {
return nextState;
}
return cloneMachineSnapshot(nextState, {
_nodes: nextStateNodes,
historyValue
});
} catch (e) {
// TODO: Refactor this once proper error handling is implemented.
// See https://github.com/statelyai/rfcs/pull/4
throw e;
}
}
function getMachineOutput(snapshot, event, actorScope, rootNode, rootCompletionNode) {
if (rootNode.output === undefined) {
return;
}
const doneStateEvent = createDoneStateEvent(rootCompletionNode.id, rootCompletionNode.output !== undefined && rootCompletionNode.parent ? resolveOutput(rootCompletionNode.output, snapshot.context, event, actorScope.self) : undefined);
return resolveOutput(rootNode.output, snapshot.context, doneStateEvent, actorScope.self);
}
function enterStates(currentSnapshot, event, actorScope, filteredTransitions, mutStateNodeSet, internalQueue, historyValue, isInitial) {
let nextSnapshot = currentSnapshot;
const statesToEnter = new Set();
// those are states that were directly targeted or indirectly targeted by the explicit target
// in other words, those are states for which initial actions should be executed
// when we target `#deep_child` initial actions of its ancestors shouldn't be executed
const statesForDefaultEntry = new Set();
computeEntrySet(filteredTransitions, historyValue, statesForDefaultEntry, statesToEnter);
// In the initial state, the root state node is "entered".
if (isInitial) {
statesForDefaultEntry.add(currentSnapshot.machine.root);
}
const completedNodes = new Set();
for (const stateNodeToEnter of [...statesToEnter].sort((a, b) => a.order - b.order)) {
mutStateNodeSet.add(stateNodeToEnter);
const actions = [];
// Add entry actions
actions.push(...stateNodeToEnter.entry);
for (const invokeDef of stateNodeToEnter.invoke) {
actions.push(raise_c17ec2bc_esm_spawnChild(invokeDef.src, {
...invokeDef,
syncSnapshot: !!invokeDef.onSnapshot
}));
}
if (statesForDefaultEntry.has(stateNodeToEnter)) {
const initialActions = stateNodeToEnter.initial.actions;
actions.push(...initialActions);
}
nextSnapshot = resolveActionsAndContext(nextSnapshot, event, actorScope, actions, internalQueue, stateNodeToEnter.invoke.map(invokeDef => invokeDef.id));
if (stateNodeToEnter.type === 'final') {
const parent = stateNodeToEnter.parent;
let ancestorMarker = parent?.type === 'parallel' ? parent : parent?.parent;
let rootCompletionNode = ancestorMarker || stateNodeToEnter;
if (parent?.type === 'compound') {
internalQueue.push(createDoneStateEvent(parent.id, stateNodeToEnter.output !== undefined ? resolveOutput(stateNodeToEnter.output, nextSnapshot.context, event, actorScope.self) : undefined));
}
while (ancestorMarker?.type === 'parallel' && !completedNodes.has(ancestorMarker) && isInFinalState(mutStateNodeSet, ancestorMarker)) {
completedNodes.add(ancestorMarker);
internalQueue.push(createDoneStateEvent(ancestorMarker.id));
rootCompletionNode = ancestorMarker;
ancestorMarker = ancestorMarker.parent;
}
if (ancestorMarker) {
continue;
}
nextSnapshot = cloneMachineSnapshot(nextSnapshot, {
status: 'done',
output: getMachineOutput(nextSnapshot, event, actorScope, nextSnapshot.machine.root, rootCompletionNode)
});
}
}
return nextSnapshot;
}
function computeEntrySet(transitions, historyValue, statesForDefaultEntry, statesToEnter) {
for (const t of transitions) {
const domain = getTransitionDomain(t, historyValue);
for (const s of t.target || []) {
if (!isHistoryNode(s) && (
// if the target is different than the source then it will *definitely* be entered
t.source !== s ||
// we know that the domain can't lie within the source
// if it's different than the source then it's outside of it and it means that the target has to be entered as well
t.source !== domain ||
// reentering transitions always enter the target, even if it's the source itself
t.reenter)) {
statesToEnter.add(s);
statesForDefaultEntry.add(s);
}
addDescendantStatesToEnter(s, historyValue, statesForDefaultEntry, statesToEnter);
}
const targetStates = getEffectiveTargetStates(t, historyValue);
for (const s of targetStates) {
const ancestors = getProperAncestors(s, domain);
if (domain?.type === 'parallel') {
ancestors.push(domain);
}
addAncestorStatesToEnter(statesToEnter, historyValue, statesForDefaultEntry, ancestors, !t.source.parent && t.reenter ? undefined : domain);
}
}
}
function addDescendantStatesToEnter(stateNode, historyValue, statesForDefaultEntry, statesToEnter) {
if (isHistoryNode(stateNode)) {
if (historyValue[stateNode.id]) {
const historyStateNodes = historyValue[stateNode.id];
for (const s of historyStateNodes) {
statesToEnter.add(s);
addDescendantStatesToEnter(s, historyValue, statesForDefaultEntry, statesToEnter);
}
for (const s of historyStateNodes) {
addProperAncestorStatesToEnter(s, stateNode.parent, statesToEnter, historyValue, statesForDefaultEntry);
}
} else {
const historyDefaultTransition = resolveHistoryDefaultTransition(stateNode);
for (const s of historyDefaultTransition.target) {
statesToEnter.add(s);
if (historyDefaultTransition === stateNode.parent?.initial) {
statesForDefaultEntry.add(stateNode.parent);
}
addDescendantStatesToEnter(s, historyValue, statesForDefaultEntry, statesToEnter);
}
for (const s of historyDefaultTransition.target) {
addProperAncestorStatesToEnter(s, stateNode.parent, statesToEnter, historyValue, statesForDefaultEntry);
}
}
} else {
if (stateNode.type === 'compound') {
const [initialState] = stateNode.initial.target;
if (!isHistoryNode(initialState)) {
statesToEnter.add(initialState);
statesForDefaultEntry.add(initialState);
}
addDescendantStatesToEnter(initialState, historyValue, statesForDefaultEntry, statesToEnter);
addProperAncestorStatesToEnter(initialState, stateNode, statesToEnter, historyValue, statesForDefaultEntry);
} else {
if (stateNode.type === 'parallel') {
for (const child of getChildren(stateNode).filter(sn => !isHistoryNode(sn))) {
if (![...statesToEnter].some(s => isDescendant(s, child))) {
if (!isHistoryNode(child)) {
statesToEnter.add(child);
statesForDefaultEntry.add(child);
}
addDescendantStatesToEnter(child, historyValue, statesForDefaultEntry, statesToEnter);
}
}
}
}
}
}
function addAncestorStatesToEnter(statesToEnter, historyValue, statesForDefaultEntry, ancestors, reentrancyDomain) {
for (const anc of ancestors) {
if (!reentrancyDomain || isDescendant(anc, reentrancyDomain)) {
statesToEnter.add(anc);
}
if (anc.type === 'parallel') {
for (const child of getChildren(anc).filter(sn => !isHistoryNode(sn))) {
if (![...statesToEnter].some(s => isDescendant(s, child))) {
statesToEnter.add(child);
addDescendantStatesToEnter(child, historyValue, statesForDefaultEntry, statesToEnter);
}
}
}
}
}
function addProperAncestorStatesToEnter(stateNode, toStateNode, statesToEnter, historyValue, statesForDefaultEntry) {
addAncestorStatesToEnter(statesToEnter, historyValue, statesForDefaultEntry, getProperAncestors(stateNode, toStateNode));
}
function exitStates(currentSnapshot, event, actorScope, transitions, mutStateNodeSet, historyValue, internalQueue, _actionExecutor) {
let nextSnapshot = currentSnapshot;
const statesToExit = computeExitSet(transitions, mutStateNodeSet, historyValue);
statesToExit.sort((a, b) => b.order - a.order);
let changedHistory;
// From SCXML algorithm: https://www.w3.org/TR/scxml/#exitStates
for (const exitStateNode of statesToExit) {
for (const historyNode of getHistoryNodes(exitStateNode)) {
let predicate;
if (historyNode.history === 'deep') {
predicate = sn => isAtomicStateNode(sn) && isDescendant(sn, exitStateNode);
} else {
predicate = sn => {
return sn.parent === exitStateNode;
};
}
changedHistory ??= {
...historyValue
};
changedHistory[historyNode.id] = Array.from(mutStateNodeSet).filter(predicate);
}
}
for (const s of statesToExit) {
nextSnapshot = resolveActionsAndContext(nextSnapshot, event, actorScope, [...s.exit, ...s.invoke.map(def => stopChild(def.id))], internalQueue, undefined);
mutStateNodeSet.delete(s);
}
return [nextSnapshot, changedHistory || historyValue];
}
function getAction(machine, actionType) {
return machine.implementations.actions[actionType];
}
function resolveAndExecuteActionsWithContext(currentSnapshot, event, actorScope, actions, extra, retries) {
const {
machine
} = currentSnapshot;
let intermediateSnapshot = currentSnapshot;
for (const action of actions) {
const isInline = typeof action === 'function';
const resolvedAction = isInline ? action :
// the existing type of `.actions` assumes non-nullable `TExpressionAction`
// it's fine to cast this here to get a common type and lack of errors in the rest of the code
// our logic below makes sure that we call those 2 "variants" correctly
getAction(machine, typeof action === 'string' ? action : action.type);
const actionArgs = {
context: intermediateSnapshot.context,
event,
self: actorScope.self,
system: actorScope.system
};
const actionParams = isInline || typeof action === 'string' ? undefined : 'params' in action ? typeof action.params === 'function' ? action.params({
context: intermediateSnapshot.context,
event
}) : action.params : undefined;
if (!resolvedAction || !('resolve' in resolvedAction)) {
actorScope.actionExecutor({
type: typeof action === 'string' ? action : typeof action === 'object' ? action.type : action.name || '(anonymous)',
info: actionArgs,
params: actionParams,
exec: resolvedAction
});
continue;
}
const builtinAction = resolvedAction;
const [nextState, params, actions] = builtinAction.resolve(actorScope, intermediateSnapshot, actionArgs, actionParams, resolvedAction,
// this holds all params
extra);
intermediateSnapshot = nextState;
if ('retryResolve' in builtinAction) {
retries?.push([builtinAction, params]);
}
if ('execute' in builtinAction) {
actorScope.actionExecutor({
type: builtinAction.type,
info: actionArgs,
params,
exec: builtinAction.execute.bind(null, actorScope, params)
});
}
if (actions) {
intermediateSnapshot = resolveAndExecuteActionsWithContext(intermediateSnapshot, event, actorScope, actions, extra, retries);
}
}
return intermediateSnapshot;
}
function resolveActionsAndContext(currentSnapshot, event, actorScope, actions, internalQueue, deferredActorIds) {
const retries = deferredActorIds ? [] : undefined;
const nextState = resolveAndExecuteActionsWithContext(currentSnapshot, event, actorScope, actions, {
internalQueue,
deferredActorIds
}, retries);
retries?.forEach(([builtinAction, params]) => {
builtinAction.retryResolve(actorScope, nextState, params);
});
return nextState;
}
function macrostep(snapshot, event, actorScope, internalQueue) {
let nextSnapshot = snapshot;
const microstates = [];
function addMicrostate(microstate, event, transitions) {
actorScope.system._sendInspectionEvent({
type: '@xstate.microstep',
actorRef: actorScope.self,
event,
snapshot: microstate,
_transitions: transitions
});
microstates.push(microstate);
}
// Handle stop event
if (event.type === XSTATE_STOP) {
nextSnapshot = cloneMachineSnapshot(stopChildren(nextSnapshot, event, actorScope), {
status: 'stopped'
});
addMicrostate(nextSnapshot, event, []);
return {
snapshot: nextSnapshot,
microstates
};
}
let nextEvent = event;
// Assume the state is at rest (no raised events)
// Determine the next state based on the next microstep
if (nextEvent.type !== XSTATE_INIT) {
const currentEvent = nextEvent;
const isErr = isErrorActorEvent(currentEvent);
const transitions = selectTransitions(currentEvent, nextSnapshot);
if (isErr && !transitions.length) {
// TODO: we should likely only allow transitions selected by very explicit descriptors
// `*` shouldn't be matched, likely `xstate.error.*` shouldnt be either
// similarly `xstate.error.actor.*` and `xstate.error.actor.todo.*` have to be considered too
nextSnapshot = cloneMachineSnapshot(snapshot, {
status: 'error',
error: currentEvent.error
});
addMicrostate(nextSnapshot, currentEvent, []);
return {
snapshot: nextSnapshot,
microstates
};
}
nextSnapshot = microstep(transitions, snapshot, actorScope, nextEvent, false,
// isInitial
internalQueue);
addMicrostate(nextSnapshot, currentEvent, transitions);
}
let shouldSelectEventlessTransitions = true;
while (nextSnapshot.status === 'active') {
let enabledTransitions = shouldSelectEventlessTransitions ? selectEventlessTransitions(nextSnapshot, nextEvent) : [];
// eventless transitions should always be selected after selecting *regular* transitions
// by assigning `undefined` to `previousState` we ensure that `shouldSelectEventlessTransitions` gets always computed to true in such a case
const previousState = enabledTransitions.length ? nextSnapshot : undefined;
if (!enabledTransitions.length) {
if (!internalQueue.length) {
break;
}
nextEvent = internalQueue.shift();
enabledTransitions = selectTransitions(nextEvent, nextSnapshot);
}
nextSnapshot = microstep(enabledTransitions, nextSnapshot, actorScope, nextEvent, false, internalQueue);
shouldSelectEventlessTransitions = nextSnapshot !== previousState;
addMicrostate(nextSnapshot, nextEvent, enabledTransitions);
}
if (nextSnapshot.status !== 'active') {
stopChildren(nextSnapshot, nextEvent, actorScope);
}
return {
snapshot: nextSnapshot,
microstates
};
}
function stopChildren(nextState, event, actorScope) {
return resolveActionsAndContext(nextState, event, actorScope, Object.values(nextState.children).map(child => stopChild(child)), [], undefined);
}
function selectTransitions(event, nextState) {
return nextState.machine.getTransitionData(nextState, event);
}
function selectEventlessTransitions(nextState, event) {
const enabledTransitionSet = new Set();
const atomicStates = nextState._nodes.filter(isAtomicStateNode);
for (const stateNode of atomicStates) {
loop: for (const s of [stateNode].concat(getProperAncestors(stateNode, undefined))) {
if (!s.always) {
continue;
}
for (const transition of s.always) {
if (transition.guard === undefined || evaluateGuard(transition.guard, nextState.context, event, nextState)) {
enabledTransitionSet.add(transition);
break loop;
}
}
}
}
return removeConflictingTransitions(Array.from(enabledTransitionSet), new Set(nextState._nodes), nextState.historyValue);
}
/**
* Resolves a partial state value with its full representation in the state
* node's machine.
*
* @param stateValue The partial state value to resolve.
*/
function resolveStateValue(rootNode, stateValue) {
const allStateNodes = getAllStateNodes(getStateNodes(rootNode, stateValue));
return getStateValue(rootNode, [...allStateNodes]);
}
function isMachineSnapshot(value) {
return !!value && typeof value === 'object' && 'machine' in value && 'value' in value;
}
const machineSnapshotMatches = function matches(testValue) {
return matchesState(testValue, this.value);
};
const machineSnapshotHasTag = function hasTag(tag) {
return this.tags.has(tag);
};
const machineSnapshotCan = function can(event) {
const transitionData = this.machine.getTransitionData(this, event);
return !!transitionData?.length &&
// Check that at least one transition is not forbidden
transitionData.some(t => t.target !== undefined || t.actions.length);
};
const machineSnapshotToJSON = function toJSON() {
const {
_nodes: nodes,
tags,
machine,
getMeta,
toJSON,
can,
hasTag,
matches,
...jsonValues
} = this;
return {
...jsonValues,
tags: Array.from(tags)
};
};
const machineSnapshotGetMeta = function getMeta() {
return this._nodes.reduce((acc, stateNode) => {
if (stateNode.meta !== undefined) {
acc[stateNode.id] = stateNode.meta;
}
return acc;
}, {});
};
function createMachineSnapshot(config, machine) {
return {
status: config.status,
output: config.output,
error: config.error,
machine,
context: config.context,
_nodes: config._nodes,
value: getStateValue(machine.root, config._nodes),
tags: new Set(config._nodes.flatMap(sn => sn.tags)),
children: config.children,
historyValue: config.historyValue || {},
matches: machineSnapshotMatches,
hasTag: machineSnapshotHasTag,
can: machineSnapshotCan,
getMeta: machineSnapshotGetMeta,
toJSON: machineSnapshotToJSON
};
}
function cloneMachineSnapshot(snapshot, config = {}) {
return createMachineSnapshot({
...snapshot,
...config
}, snapshot.machine);
}
function getPersistedSnapshot(snapshot, options) {
const {
_nodes: nodes,
tags,
machine,
children,
context,
can,
hasTag,
matches,
getMeta,
toJSON,
...jsonValues
} = snapshot;
const childrenJson = {};
for (const id in children) {
const child = children[id];
childrenJson[id] = {
snapshot: child.getPersistedSnapshot(options),
src: child.src,
systemId: child._systemId,
syncSnapshot: child._syncSnapshot
};
}
const persisted = {
...jsonValues,
context: persistContext(context),
children: childrenJson
};
return persisted;
}
function persistContext(contextPart) {
let copy;
for (const key in contextPart) {
const value = contextPart[key];
if (value && typeof value === 'object') {
if ('sessionId' in value && 'send' in value && 'ref' in value) {
copy ??= Array.isArray(contextPart) ? contextPart.slice() : {
...contextPart
};
copy[key] = {
xstate$$type: $$ACTOR_TYPE,
id: value.id
};
} else {
const result = persistContext(value);
if (result !== value) {
copy ??= Array.isArray(contextPart) ? contextPart.slice() : {
...contextPart
};
copy[key] = result;
}
}
}
}
return copy ?? contextPart;
}
function resolveRaise(_, snapshot, args, actionParams, {
event: eventOrExpr,
id,
delay
}, {
internalQueue
}) {
const delaysMap = snapshot.machine.implementations.delays;
if (typeof eventOrExpr === 'string') {
throw new Error(
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
`Only event objects may be used with raise; use raise({ type: "${eventOrExpr}" }) instead`);
}
const resolvedEvent = typeof eventOrExpr === 'function' ? eventOrExpr(args, actionParams) : eventOrExpr;
let resolvedDelay;
if (typeof delay === 'string') {
const configDelay = delaysMap && delaysMap[delay];
resolvedDelay = typeof configDelay === 'function' ? configDelay(args, actionParams) : configDelay;
} else {
resolvedDelay = typeof delay === 'function' ? delay(args, actionParams) : delay;
}
if (typeof resolvedDelay !== 'number') {
internalQueue.push(resolvedEvent);
}
return [snapshot, {
event: resolvedEvent,
id,
delay: resolvedDelay
}, undefined];
}
function executeRaise(actorScope, params) {
const {
event,
delay,
id
} = params;
if (typeof delay === 'number') {
actorScope.defer(() => {
const self = actorScope.self;
actorScope.system.scheduler.schedule(self, self, event, delay, id);
});
return;
}
}
/**
* Raises an event. This places the event in the internal event queue, so that
* the event is immediately consumed by the machine in the current step.
*
* @param eventType The event to raise.
*/
function raise_c17ec2bc_esm_raise(eventOrExpr, options) {
function raise(_args, _params) {
}
raise.type = 'xstate.raise';
raise.event = eventOrExpr;
raise.id = options?.id;
raise.delay = options?.delay;
raise.resolve = resolveRaise;
raise.execute = executeRaise;
return raise;
}
},
19131(__unused_webpack_module, __webpack_exports__, __webpack_require__) {
__webpack_require__.d(__webpack_exports__, {
mj: () => (setup)
});
/* import */ var _actors_dist_xstate_actors_esm_js__rspack_import_0 = __webpack_require__(50649);
/* import */ var _raise_c17ec2bc_esm_js__rspack_import_1 = __webpack_require__(92287);
/* import */ var _log_2a773d37_esm_js__rspack_import_2 = __webpack_require__(45801);
/**
* Asserts that the given event object is of the specified type or types. Throws
* an error if the event object is not of the specified types.
*
* @example
*
* ```ts
* // ...
* entry: ({ event }) => {
* assertEvent(event, 'doNothing');
* // event is { type: 'doNothing' }
* },
* // ...
* exit: ({ event }) => {
* assertEvent(event, 'greet');
* // event is { type: 'greet'; message: string }
*
* assertEvent(event, ['greet', 'notify']);
* // event is { type: 'greet'; message: string }
* // or { type: 'notify'; message: string; level: 'info' | 'error' }
* },
* ```
*/
function assertEvent(event, type) {
const types = toArray(type);
if (!types.includes(event.type)) {
const typesText = types.length === 1 ? `type "${types[0]}"` : `one of types "${types.join('", "')}"`;
throw new Error(`Expected event ${JSON.stringify(event)} to have ${typesText}`);
}
}
const cache = new WeakMap();
function memo(object, key, fn) {
let memoizedData = cache.get(object);
if (!memoizedData) {
memoizedData = {
[key]: fn()
};
cache.set(object, memoizedData);
} else if (!(key in memoizedData)) {
memoizedData[key] = fn();
}
return memoizedData[key];
}
const EMPTY_OBJECT = {};
const toSerializableAction = action => {
if (typeof action === 'string') {
return {
type: action
};
}
if (typeof action === 'function') {
if ('resolve' in action) {
return {
type: action.type
};
}
return {
type: action.name
};
}
return action;
};
class StateNode {
constructor(/** The raw config used to create the machine. */
config, options) {
this.config = config;
/**
* The relative key of the state node, which represents its location in the
* overall state value.
*/
this.key = void 0;
/** The unique ID of the state node. */
this.id = void 0;
/**
* The type of this state node:
*
* - `'atomic'` - no child state nodes
* - `'compound'` - nested child state nodes (XOR)
* - `'parallel'` - orthogonal nested child state nodes (AND)
* - `'history'` - history state node
* - `'final'` - final state node
*/
this.type = void 0;
/** The string path from the root machine node to this node. */
this.path = void 0;
/** The child state nodes. */
this.states = void 0;
/**
* The type of history on this state node. Can be:
*
* - `'shallow'` - recalls only top-level historical state value
* - `'deep'` - recalls historical state value at all levels
*/
this.history = void 0;
/** The action(s) to be executed upon entering the state node. */
this.entry = void 0;
/** The action(s) to be executed upon exiting the state node. */
this.exit = void 0;
/** The parent state node. */
this.parent = void 0;
/** The root machine node. */
this.machine = void 0;
/**
* The meta data associated with this state node, which will be returned in
* State instances.
*/
this.meta = void 0;
/**
* The output data sent with the "xstate.done.state._id_" event if this is a
* final state node.
*/
this.output = void 0;
/**
* The order this state node appears. Corresponds to the implicit document
* order.
*/
this.order = -1;
this.description = void 0;
this.tags = [];
this.transitions = void 0;
this.always = void 0;
this.parent = options._parent;
this.key = options._key;
this.machine = options._machine;
this.path = this.parent ? this.parent.path.concat(this.key) : [];
this.id = this.config.id || [this.machine.id, ...this.path].join(_raise_c17ec2bc_esm_js__rspack_import_1.S);
this.type = this.config.type || (this.config.states && Object.keys(this.config.states).length ? 'compound' : this.config.history ? 'history' : 'atomic');
this.description = this.config.description;
this.order = this.machine.idMap.size;
this.machine.idMap.set(this.id, this);
this.states = this.config.states ? (0,_raise_c17ec2bc_esm_js__rspack_import_1.m)(this.config.states, (stateConfig, key) => {
const stateNode = new StateNode(stateConfig, {
_parent: this,
_key: key,
_machine: this.machine
});
return stateNode;
}) : EMPTY_OBJECT;
if (this.type === 'compound' && !this.config.initial) {
throw new Error(`No initial state specified for compound state node "#${this.id}". Try adding { initial: "${Object.keys(this.states)[0]}" } to the state config.`);
}
// History config
this.history = this.config.history === true ? 'shallow' : this.config.history || false;
this.entry = (0,_raise_c17ec2bc_esm_js__rspack_import_1.t)(this.config.entry).slice();
this.exit = (0,_raise_c17ec2bc_esm_js__rspack_import_1.t)(this.config.exit).slice();
this.meta = this.config.meta;
this.output = this.type === 'final' || !this.parent ? this.config.output : undefined;
this.tags = (0,_raise_c17ec2bc_esm_js__rspack_import_1.t)(config.tags).slice();
}
/** @internal */
_initialize() {
this.transitions = (0,_raise_c17ec2bc_esm_js__rspack_import_1.f)(this);
if (this.config.always) {
this.always = (0,_raise_c17ec2bc_esm_js__rspack_import_1.a)(this.config.always).map(t => (0,_raise_c17ec2bc_esm_js__rspack_import_1.b)(this, _raise_c17ec2bc_esm_js__rspack_import_1.N, t));
}
Object.keys(this.states).forEach(key => {
this.states[key]._initialize();
});
}
/** The well-structured state node definition. */
get definition() {
return {
id: this.id,
key: this.key,
version: this.machine.version,
type: this.type,
initial: this.initial ? {
target: this.initial.target,
source: this,
actions: this.initial.actions.map(toSerializableAction),
eventType: null,
reenter: false,
toJSON: () => ({
target: this.initial.target.map(t => `#${t.id}`),
source: `#${this.id}`,
actions: this.initial.actions.map(toSerializableAction),
eventType: null
})
} : undefined,
history: this.history,
states: (0,_raise_c17ec2bc_esm_js__rspack_import_1.m)(this.states, state => {
return state.definition;
}),
on: this.on,
transitions: [...this.transitions.values()].flat().map(t => ({
...t,
actions: t.actions.map(toSerializableAction)
})),
entry: this.entry.map(toSerializableAction),
exit: this.exit.map(toSerializableAction),
meta: this.meta,
order: this.order || -1,
output: this.output,
invoke: this.invoke,
description: this.description,
tags: this.tags
};
}
/** @internal */
toJSON() {
return this.definition;
}
/** The logic invoked as actors by this state node. */
get invoke() {
return memo(this, 'invoke', () => (0,_raise_c17ec2bc_esm_js__rspack_import_1.t)(this.config.invoke).map((invokeConfig, i) => {
const {
src,
systemId
} = invokeConfig;
const resolvedId = invokeConfig.id ?? (0,_raise_c17ec2bc_esm_js__rspack_import_1.c)(this.id, i);
const sourceName = typeof src === 'string' ? src : `xstate.invoke.${(0,_raise_c17ec2bc_esm_js__rspack_import_1.c)(this.id, i)}`;
return {
...invokeConfig,
src: sourceName,
id: resolvedId,
systemId: systemId,
toJSON() {
const {
onDone,
onError,
...invokeDefValues
} = invokeConfig;
return {
...invokeDefValues,
type: 'xstate.invoke',
src: sourceName,
id: resolvedId
};
}
};
}));
}
/** The mapping of events to transitions. */
get on() {
return memo(this, 'on', () => {
const transitions = this.transitions;
return [...transitions].flatMap(([descriptor, t]) => t.map(t => [descriptor, t])).reduce((map, [descriptor, transition]) => {
map[descriptor] = map[descriptor] || [];
map[descriptor].push(transition);
return map;
}, {});
});
}
get after() {
return memo(this, 'delayedTransitions', () => (0,_raise_c17ec2bc_esm_js__rspack_import_1.g)(this));
}
get initial() {
return memo(this, 'initial', () => (0,_raise_c17ec2bc_esm_js__rspack_import_1.d)(this, this.config.initial));
}
/** @internal */
next(snapshot, event) {
const eventType = event.type;
const actions = [];
let selectedTransition;
const candidates = memo(this, `candidates-${eventType}`, () => (0,_raise_c17ec2bc_esm_js__rspack_import_1.h)(this, eventType));
for (const candidate of candidates) {
const {
guard
} = candidate;
const resolvedContext = snapshot.context;
let guardPassed = false;
try {
guardPassed = !guard || (0,_raise_c17ec2bc_esm_js__rspack_import_1.e)(guard, resolvedContext, event, snapshot);
} catch (err) {
const guardType = typeof guard === 'string' ? guard : typeof guard === 'object' ? guard.type : undefined;
throw new Error(`Unable to evaluate guard ${guardType ? `'${guardType}' ` : ''}in transition for event '${eventType}' in state node '${this.id}':\n${err.message}`);
}
if (guardPassed) {
actions.push(...candidate.actions);
selectedTransition = candidate;
break;
}
}
return selectedTransition ? [selectedTransition] : undefined;
}
/** All the event types accepted by this state node and its descendants. */
get events() {
return memo(this, 'events', () => {
const {
states
} = this;
const events = new Set(this.ownEvents);
if (states) {
for (const stateId of Object.keys(states)) {
const state = states[stateId];
if (state.states) {
for (const event of state.events) {
events.add(`${event}`);
}
}
}
}
return Array.from(events);
});
}
/**
* All the events that have transitions directly from this state node.
*
* Excludes any inert events.
*/
get ownEvents() {
const events = new Set([...this.transitions.keys()].filter(descriptor => {
return this.transitions.get(descriptor).some(transition => !(!transition.target && !transition.actions.length && !transition.reenter));
}));
return Array.from(events);
}
}
const STATE_IDENTIFIER = '#';
class StateMachine {
constructor(/** The raw config used to create the machine. */
config, implementations) {
this.config = config;
/** The machine's own version. */
this.version = void 0;
this.schemas = void 0;
this.implementations = void 0;
/** @internal */
this.__xstatenode = true;
/** @internal */
this.idMap = new Map();
this.root = void 0;
this.id = void 0;
this.states = void 0;
this.events = void 0;
this.id = config.id || '(machine)';
this.implementations = {
actors: implementations?.actors ?? {},
actions: implementations?.actions ?? {},
delays: implementations?.delays ?? {},
guards: implementations?.guards ?? {}
};
this.version = this.config.version;
this.schemas = this.config.schemas;
this.transition = this.transition.bind(this);
this.getInitialSnapshot = this.getInitialSnapshot.bind(this);
this.getPersistedSnapshot = this.getPersistedSnapshot.bind(this);
this.restoreSnapshot = this.restoreSnapshot.bind(this);
this.start = this.start.bind(this);
this.root = new StateNode(config, {
_key: this.id,
_machine: this
});
this.root._initialize();
this.states = this.root.states; // TODO: remove!
this.events = this.root.events;
}
/**
* Clones this state machine with the provided implementations and merges the
* `context` (if provided).
*
* @param implementations Options (`actions`, `guards`, `actors`, `delays`,
* `context`) to recursively merge with the existing options.
* @returns A new `StateMachine` instance with the provided implementations.
*/
provide(implementations) {
const {
actions,
guards,
actors,
delays
} = this.implementations;
return new StateMachine(this.config, {
actions: {
...actions,
...implementations.actions
},
guards: {
...guards,
...implementations.guards
},
actors: {
...actors,
...implementations.actors
},
delays: {
...delays,
...implementations.delays
}
});
}
resolveState(config) {
const resolvedStateValue = (0,_raise_c17ec2bc_esm_js__rspack_import_1.r)(this.root, config.value);
const nodeSet = (0,_raise_c17ec2bc_esm_js__rspack_import_1.i)((0,_raise_c17ec2bc_esm_js__rspack_import_1.j)(this.root, resolvedStateValue));
return (0,_raise_c17ec2bc_esm_js__rspack_import_1.k)({
_nodes: [...nodeSet],
context: config.context || {},
children: {},
status: (0,_raise_c17ec2bc_esm_js__rspack_import_1.l)(nodeSet, this.root) ? 'done' : config.status || 'active',
output: config.output,
error: config.error,
historyValue: config.historyValue
}, this);
}
/**
* Determines the next snapshot given the current `snapshot` and received
* `event`. Calculates a full macrostep from all microsteps.
*
* @param snapshot The current snapshot
* @param event The received event
*/
transition(snapshot, event, actorScope) {
return (0,_raise_c17ec2bc_esm_js__rspack_import_1.n)(snapshot, event, actorScope, []).snapshot;
}
/**
* Determines the next state given the current `state` and `event`. Calculates
* a microstep.
*
* @param state The current state
* @param event The received event
*/
microstep(snapshot, event, actorScope) {
return (0,_raise_c17ec2bc_esm_js__rspack_import_1.n)(snapshot, event, actorScope, []).microstates;
}
getTransitionData(snapshot, event) {
return (0,_raise_c17ec2bc_esm_js__rspack_import_1.o)(this.root, snapshot.value, snapshot, event) || [];
}
/**
* The initial state _before_ evaluating any microsteps. This "pre-initial"
* state is provided to initial actions executed in the initial state.
*/
getPreInitialState(actorScope, initEvent, internalQueue) {
const {
context
} = this.config;
const preInitial = (0,_raise_c17ec2bc_esm_js__rspack_import_1.k)({
context: typeof context !== 'function' && context ? context : {},
_nodes: [this.root],
children: {},
status: 'active'
}, this);
if (typeof context === 'function') {
const assignment = ({
spawn,
event,
self
}) => context({
spawn,
input: event.input,
self
});
return (0,_raise_c17ec2bc_esm_js__rspack_import_1.p)(preInitial, initEvent, actorScope, [(0,_log_2a773d37_esm_js__rspack_import_2.a)(assignment)], internalQueue, undefined);
}
return preInitial;
}
/**
* Returns the initial `State` instance, with reference to `self` as an
* `ActorRef`.
*/
getInitialSnapshot(actorScope, input) {
const initEvent = (0,_raise_c17ec2bc_esm_js__rspack_import_1.q)(input); // TODO: fix;
const internalQueue = [];
const preInitialState = this.getPreInitialState(actorScope, initEvent, internalQueue);
const nextState = (0,_raise_c17ec2bc_esm_js__rspack_import_1.s)([{
target: [...(0,_raise_c17ec2bc_esm_js__rspack_import_1.u)(this.root)],
source: this.root,
reenter: true,
actions: [],
eventType: null,
toJSON: null // TODO: fix
}], preInitialState, actorScope, initEvent, true, internalQueue);
const {
snapshot: macroState
} = (0,_raise_c17ec2bc_esm_js__rspack_import_1.n)(nextState, initEvent, actorScope, internalQueue);
return macroState;
}
start(snapshot) {
Object.values(snapshot.children).forEach(child => {
if (child.getSnapshot().status === 'active') {
child.start();
}
});
}
getStateNodeById(stateId) {
const fullPath = (0,_raise_c17ec2bc_esm_js__rspack_import_1.v)(stateId);
const relativePath = fullPath.slice(1);
const resolvedStateId = (0,_raise_c17ec2bc_esm_js__rspack_import_1.w)(fullPath[0]) ? fullPath[0].slice(STATE_IDENTIFIER.length) : fullPath[0];
const stateNode = this.idMap.get(resolvedStateId);
if (!stateNode) {
throw new Error(`Child state node '#${resolvedStateId}' does not exist on machine '${this.id}'`);
}
return (0,_raise_c17ec2bc_esm_js__rspack_import_1.x)(stateNode, relativePath);
}
get definition() {
return this.root.definition;
}
toJSON() {
return this.definition;
}
getPersistedSnapshot(snapshot, options) {
return (0,_raise_c17ec2bc_esm_js__rspack_import_1.y)(snapshot, options);
}
restoreSnapshot(snapshot, _actorScope) {
const children = {};
const snapshotChildren = snapshot.children;
Object.keys(snapshotChildren).forEach(actorId => {
const actorData = snapshotChildren[actorId];
const childState = actorData.snapshot;
const src = actorData.src;
const logic = typeof src === 'string' ? (0,_raise_c17ec2bc_esm_js__rspack_import_1.z)(this, src) : src;
if (!logic) {
return;
}
const actorRef = (0,_raise_c17ec2bc_esm_js__rspack_import_1.A)(logic, {
id: actorId,
parent: _actorScope.self,
syncSnapshot: actorData.syncSnapshot,
snapshot: childState,
src,
systemId: actorData.systemId
});
children[actorId] = actorRef;
});
const restoredSnapshot = (0,_raise_c17ec2bc_esm_js__rspack_import_1.k)({
...snapshot,
children,
_nodes: Array.from((0,_raise_c17ec2bc_esm_js__rspack_import_1.i)((0,_raise_c17ec2bc_esm_js__rspack_import_1.j)(this.root, snapshot.value)))
}, this);
const seen = new Set();
function reviveContext(contextPart, children) {
if (seen.has(contextPart)) {
return;
}
seen.add(contextPart);
for (const key in contextPart) {
const value = contextPart[key];
if (value && typeof value === 'object') {
if ('xstate$$type' in value && value.xstate$$type === _raise_c17ec2bc_esm_js__rspack_import_1.$) {
contextPart[key] = children[value.id];
continue;
}
reviveContext(value, children);
}
}
}
reviveContext(restoredSnapshot.context, children);
return restoredSnapshot;
}
}
/**
* Creates a state machine (statechart) with the given configuration.
*
* The state machine represents the pure logic of a state machine actor.
*
* @example
*
* ```ts
* import { createMachine } from 'xstate';
*
* const lightMachine = createMachine({
* id: 'light',
* initial: 'green',
* states: {
* green: {
* on: {
* TIMER: { target: 'yellow' }
* }
* },
* yellow: {
* on: {
* TIMER: { target: 'red' }
* }
* },
* red: {
* on: {
* TIMER: { target: 'green' }
* }
* }
* }
* });
*
* const lightActor = createActor(lightMachine);
* lightActor.start();
*
* lightActor.send({ type: 'TIMER' });
* ```
*
* @param config The state machine configuration.
* @param options DEPRECATED: use `setup({ ... })` or `machine.provide({ ... })`
* to provide machine implementations instead.
*/
function createMachine(config, implementations) {
return new StateMachine(config, implementations);
}
/** @internal */
function createInertActorScope(actorLogic) {
const self = createActor(actorLogic);
const inertActorScope = {
self,
defer: () => {},
id: '',
logger: () => {},
sessionId: '',
stopChild: () => {},
system: self.system,
emit: () => {},
actionExecutor: () => {}
};
return inertActorScope;
}
/** @deprecated Use `initialTransition(…)` instead. */
function getInitialSnapshot(actorLogic, ...[input]) {
const actorScope = createInertActorScope(actorLogic);
return actorLogic.getInitialSnapshot(actorScope, input);
}
/**
* Determines the next snapshot for the given `actorLogic` based on the given
* `snapshot` and `event`.
*
* If the `snapshot` is `undefined`, the initial snapshot of the `actorLogic` is
* used.
*
* @deprecated Use `transition(…)` instead.
* @example
*
* ```ts
* import { getNextSnapshot } from 'xstate';
* import { trafficLightMachine } from './trafficLightMachine.ts';
*
* const nextSnapshot = getNextSnapshot(
* trafficLightMachine, // actor logic
* undefined, // snapshot (or initial state if undefined)
* { type: 'TIMER' }
* ); // event object
*
* console.log(nextSnapshot.value);
* // => 'yellow'
*
* const nextSnapshot2 = getNextSnapshot(
* trafficLightMachine, // actor logic
* nextSnapshot, // snapshot
* { type: 'TIMER' }
* ); // event object
*
* console.log(nextSnapshot2.value);
* // =>'red'
* ```
*/
function getNextSnapshot(actorLogic, snapshot, event) {
const inertActorScope = createInertActorScope(actorLogic);
inertActorScope.self._snapshot = snapshot;
return actorLogic.transition(snapshot, event, inertActorScope);
}
// at the moment we allow extra actors - ones that are not specified by `children`
// this could be reconsidered in the future
function setup({
schemas,
actors,
actions,
guards,
delays
}) {
return {
createMachine: config => createMachine({
...config,
schemas
}, {
actors,
actions,
guards,
delays
})
};
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
class SimulatedClock {
constructor() {
this.timeouts = new Map();
this._now = 0;
this._id = 0;
this._flushing = false;
this._flushingInvalidated = false;
}
now() {
return this._now;
}
getId() {
return this._id++;
}
setTimeout(fn, timeout) {
this._flushingInvalidated = this._flushing;
const id = this.getId();
this.timeouts.set(id, {
start: this.now(),
timeout,
fn
});
return id;
}
clearTimeout(id) {
this._flushingInvalidated = this._flushing;
this.timeouts.delete(id);
}
set(time) {
if (this._now > time) {
throw new Error('Unable to travel back in time');
}
this._now = time;
this.flushTimeouts();
}
flushTimeouts() {
if (this._flushing) {
this._flushingInvalidated = true;
return;
}
this._flushing = true;
const sorted = [...this.timeouts].sort(([_idA, timeoutA], [_idB, timeoutB]) => {
const endA = timeoutA.start + timeoutA.timeout;
const endB = timeoutB.start + timeoutB.timeout;
return endB > endA ? -1 : 1;
});
for (const [id, timeout] of sorted) {
if (this._flushingInvalidated) {
this._flushingInvalidated = false;
this._flushing = false;
this.flushTimeouts();
return;
}
if (this.now() - timeout.start >= timeout.timeout) {
this.timeouts.delete(id);
timeout.fn.call(null);
}
}
this._flushing = false;
}
increment(ms) {
this._now += ms;
this.flushTimeouts();
}
}
/**
* Returns a promise that resolves to the `output` of the actor when it is done.
*
* @example
*
* ```ts
* const machine = createMachine({
* // ...
* output: {
* count: 42
* }
* });
*
* const actor = createActor(machine);
*
* actor.start();
*
* const output = await toPromise(actor);
*
* console.log(output);
* // logs { count: 42 }
* ```
*/
function toPromise(actor) {
return new Promise((resolve, reject) => {
actor.subscribe({
complete: () => {
resolve(actor.getSnapshot().output);
},
error: reject
});
});
}
/**
* Given actor `logic`, a `snapshot`, and an `event`, returns a tuple of the
* `nextSnapshot` and `actions` to execute.
*
* This is a pure function that does not execute `actions`.
*/
function transition(logic, snapshot, event) {
const executableActions = [];
const actorScope = createInertActorScope(logic);
actorScope.actionExecutor = action => {
executableActions.push(action);
};
const nextSnapshot = logic.transition(snapshot, event, actorScope);
return [nextSnapshot, executableActions];
}
/**
* Given actor `logic` and optional `input`, returns a tuple of the
* `nextSnapshot` and `actions` to execute from the initial transition (no
* previous state).
*
* This is a pure function that does not execute `actions`.
*/
function initialTransition(logic, ...[input]) {
const executableActions = [];
const actorScope = createInertActorScope(logic);
actorScope.actionExecutor = action => {
executableActions.push(action);
};
const nextSnapshot = logic.getInitialSnapshot(actorScope, input);
return [nextSnapshot, executableActions];
}
const defaultWaitForOptions = (/* unused pure expression or super */ null && ({
timeout: Infinity // much more than 10 seconds
}));
/**
* Subscribes to an actor ref and waits for its emitted value to satisfy a
* predicate, and then resolves with that value. Will throw if the desired state
* is not reached after an optional timeout. (defaults to Infinity).
*
* @example
*
* ```js
* const state = await waitFor(someService, (state) => {
* return state.hasTag('loaded');
* });
*
* state.hasTag('loaded'); // true
* ```
*
* @param actorRef The actor ref to subscribe to
* @param predicate Determines if a value matches the condition to wait for
* @param options
* @returns A promise that eventually resolves to the emitted value that matches
* the condition
*/
function waitFor(actorRef, predicate, options) {
const resolvedOptions = {
...defaultWaitForOptions,
...options
};
return new Promise((res, rej) => {
const {
signal
} = resolvedOptions;
if (signal?.aborted) {
// eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
rej(signal.reason);
return;
}
let done = false;
const handle = resolvedOptions.timeout === Infinity ? undefined : setTimeout(() => {
dispose();
rej(new Error(`Timeout of ${resolvedOptions.timeout} ms exceeded`));
}, resolvedOptions.timeout);
const dispose = () => {
clearTimeout(handle);
done = true;
sub?.unsubscribe();
if (abortListener) {
signal.removeEventListener('abort', abortListener);
}
};
function checkEmitted(emitted) {
if (predicate(emitted)) {
dispose();
res(emitted);
}
}
/**
* If the `signal` option is provided, this will be the listener for its
* `abort` event
*/
let abortListener;
// eslint-disable-next-line prefer-const
let sub; // avoid TDZ when disposing synchronously
// See if the current snapshot already matches the predicate
checkEmitted(actorRef.getSnapshot());
if (done) {
return;
}
// only define the `abortListener` if the `signal` option is provided
if (signal) {
abortListener = () => {
dispose();
// XState does not "own" the signal, so we should reject with its reason (if any)
// eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
rej(signal.reason);
};
signal.addEventListener('abort', abortListener);
}
sub = actorRef.subscribe({
next: checkEmitted,
error: err => {
dispose();
// eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
rej(err);
},
complete: () => {
dispose();
rej(new Error(`Actor terminated without satisfying predicate`));
}
});
if (done) {
sub.unsubscribe();
}
});
}
},
},function(__webpack_require__) {
var __webpack_exec__ = function(moduleId) { return __webpack_require__(__webpack_require__.s = moduleId) }
var __webpack_exports__ = (__webpack_exec__(19131));
}
]);
|