File size: 55,325 Bytes
31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 e391660 31e8457 | 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 | /**
* Reactive state primitives for values managed by an `AtomRegistry`.
*
* An `Atom` describes how to produce or update one piece of reactive state. The
* registry runs atom reads, remembers current values, tracks dependencies
* between atoms, starts effects and streams, and cleans up atoms that are no
* longer used. This module includes the atom constructors and update helpers
* used for cached values, effect-backed values, streams, browser state, stored
* values, and server-rendered values.
*
* @since 4.0.0
*/
import * as Arr from "../../Array.ts";
import * as Cause from "../../Cause.ts";
import * as Context from "../../Context.ts";
import * as Duration from "../../Duration.ts";
import * as Effect from "../../Effect.ts";
import type { LazyArg } from "../../Function.ts";
import type * as Inspectable from "../../Inspectable.ts";
import * as Layer from "../../Layer.ts";
import * as Option from "../../Option.ts";
import type { Pipeable } from "../../Pipeable.ts";
import type { ReadonlyRecord } from "../../Record.ts";
import * as Schema from "../../Schema.ts";
import * as Scope from "../../Scope.ts";
import * as Stream from "../../Stream.ts";
import * as SubscriptionRef from "../../SubscriptionRef.ts";
import type { NoInfer } from "../../Types.ts";
import * as KeyValueStore from "../persistence/KeyValueStore.ts";
import * as AsyncResult from "./AsyncResult.ts";
import { AtomRegistry } from "./AtomRegistry.ts";
import * as Registry from "./AtomRegistry.ts";
import * as Reactivity from "./Reactivity.ts";
/**
* Type-level identifier used to recognize `Atom` values.
*
* @category type IDs
* @since 4.0.0
*/
export type TypeId = "~effect/reactivity/Atom";
/**
* Runtime identifier attached to `Atom` values and used by `isAtom`.
*
* @category type IDs
* @since 4.0.0
*/
export declare const TypeId: TypeId;
/**
* Reactive value read by an `AtomRegistry`, with metadata controlling caching, laziness, refresh behavior, and initial value targeting.
*
* @category models
* @since 4.0.0
*/
export interface Atom<A> extends Pipeable, Inspectable.Inspectable {
readonly [TypeId]: TypeId;
readonly keepAlive: boolean;
readonly lazy: boolean;
readonly read: (get: AtomContext) => A;
readonly refresh?: (f: <A>(atom: Atom<A>) => void) => void;
readonly label?: readonly [name: string, stack: string];
readonly idleTTL?: number;
readonly initialValueTarget?: Atom<A>;
}
/**
* Returns `true` when a value is an `Atom`.
*
* @category guards
* @since 4.0.0
*/
export declare const isAtom: (u: unknown) => u is Atom<any>;
/**
* Extracts the value type produced by an `Atom`.
*
* @category utility types
* @since 4.0.0
*/
export type Type<T extends Atom<any>> = T extends Atom<infer A> ? A : never;
/**
* Extracts the success value type from an atom whose value is an `AsyncResult`.
*
* @category utility types
* @since 4.0.0
*/
export type Success<T extends Atom<any>> = T extends Atom<AsyncResult.AsyncResult<infer A, infer _>> ? A : never;
/**
* Extracts the item type from an atom whose value is a `PullResult`.
*
* @category utility types
* @since 4.0.0
*/
export type PullSuccess<T extends Atom<any>> = T extends Atom<PullResult<infer A, infer _>> ? A : never;
/**
* Extracts the failure error type from an atom whose value is an `AsyncResult`.
*
* @category utility types
* @since 4.0.0
*/
export type Failure<T extends Atom<any>> = T extends Atom<AsyncResult.AsyncResult<infer _, infer E>> ? E : never;
/**
* Returns an atom type without serializable metadata, preserving `Writable` read and write types when the input atom is writable.
*
* @category utility types
* @since 4.0.0
*/
export type WithoutSerializable<T extends Atom<any>> = T extends Writable<infer R, infer W> ? Writable<R, W> : Atom<Type<T>>;
/**
* Runtime identifier attached to writable atoms and used by `isWritable`.
*
* @category type IDs
* @since 4.0.0
*/
export declare const WritableTypeId: WritableTypeId;
/**
* Type-level identifier used to recognize writable atoms.
*
* @category type IDs
* @since 4.0.0
*/
export type WritableTypeId = "~effect/reactivity/Atom/Writable";
/**
* Atom that can also be written to, using a `WriteContext` and an input value to update reactive state.
*
* @category models
* @since 4.0.0
*/
export interface Writable<R, W = R> extends Atom<R> {
readonly [WritableTypeId]: WritableTypeId;
readonly write: (ctx: WriteContext<R>, value: W) => void;
}
/**
* Context passed to atom read functions for reading dependencies, awaiting `AsyncResult` or `Option` values, managing subscriptions and finalizers, refreshing atoms, and updating writable atoms.
*
* @category context
* @since 4.0.0
*/
export interface AtomContext {
<A>(atom: Atom<A>): A;
get<A>(this: AtomContext, atom: Atom<A>): A;
result<A, E>(this: AtomContext, atom: Atom<AsyncResult.AsyncResult<A, E>>, options?: {
readonly suspendOnWaiting?: boolean | undefined;
}): Effect.Effect<A, E>;
resultOnce<A, E>(this: AtomContext, atom: Atom<AsyncResult.AsyncResult<A, E>>, options?: {
readonly suspendOnWaiting?: boolean | undefined;
}): Effect.Effect<A, E>;
once<A>(this: AtomContext, atom: Atom<A>): A;
addFinalizer(this: AtomContext, f: () => void): void;
mount<A>(this: AtomContext, atom: Atom<A>): void;
refresh<A>(this: AtomContext, atom: Atom<A>): void;
refreshSelf(this: AtomContext): void;
self<A>(this: AtomContext): Option.Option<A>;
setSelf<A>(this: AtomContext, a: A): void;
set<R, W>(this: AtomContext, atom: Writable<R, W>, value: W): void;
setResult<A, E, W>(this: AtomContext, atom: Writable<AsyncResult.AsyncResult<A, E>, W>, value: W): Effect.Effect<A, E>;
some<A>(this: AtomContext, atom: Atom<Option.Option<A>>): Effect.Effect<A>;
someOnce<A>(this: AtomContext, atom: Atom<Option.Option<A>>): Effect.Effect<A>;
stream<A>(this: AtomContext, atom: Atom<A>, options?: {
readonly withoutInitialValue?: boolean;
readonly bufferSize?: number;
}): Stream.Stream<A>;
streamResult<A, E>(this: AtomContext, atom: Atom<AsyncResult.AsyncResult<A, E>>, options?: {
readonly withoutInitialValue?: boolean;
readonly bufferSize?: number;
}): Stream.Stream<A, E>;
subscribe<A>(this: AtomContext, atom: Atom<A>, f: (_: A) => void, options?: {
readonly immediate?: boolean;
}): void;
readonly registry: Registry.AtomRegistry;
}
/**
* Context passed to writable atom write functions for reading atoms, refreshing or setting the current atom, and writing to other writable atoms.
*
* @category context
* @since 4.0.0
*/
export interface WriteContext<A> {
get<T>(this: WriteContext<A>, atom: Atom<T>): T;
refreshSelf(this: WriteContext<A>): void;
setSelf(this: WriteContext<A>, a: A): void;
set<R, W>(this: WriteContext<A>, atom: Writable<R, W>, value: W): void;
}
/**
* Returns a copy of an atom with an idle time-to-live: finite durations dispose it after inactivity, while an infinite duration keeps it alive.
*
* @category combinators
* @since 4.0.0
*/
export declare const setIdleTTL: {
/**
* Returns a copy of an atom with an idle time-to-live: finite durations dispose it after inactivity, while an infinite duration keeps it alive.
*
* @category combinators
* @since 4.0.0
*/
(duration: Duration.Input): <A extends Atom<any>>(self: A) => A;
/**
* Returns a copy of an atom with an idle time-to-live: finite durations dispose it after inactivity, while an infinite duration keeps it alive.
*
* @category combinators
* @since 4.0.0
*/
<A extends Atom<any>>(self: A, duration: Duration.Input): A;
};
/**
* Returns `true` when an atom is writable.
*
* @category refinements
* @since 4.0.0
*/
export declare const isWritable: <R, W>(atom: Atom<R>) => atom is Writable<R, W>;
/**
* Creates a read-only atom from a read function and an optional custom refresh registration callback.
*
* @category constructors
* @since 4.0.0
*/
export declare const readable: <A>(read: (get: AtomContext) => A, refresh?: (f: <A_1>(atom: Atom<A_1>) => void) => void) => Atom<A>;
/**
* Creates a writable atom from read and write functions, with an optional custom refresh registration callback.
*
* @category constructors
* @since 4.0.0
*/
export declare const writable: <R, W>(read: (get: AtomContext) => R, write: (ctx: WriteContext<R>, value: W) => void, refresh?: (f: <A>(atom: Atom<A>) => void) => void) => Writable<R, W>;
/**
* Creates an atom from a synchronous value or read function, or from an `Effect` or `Stream` whose state is exposed as an `AsyncResult`; plain values create writable state atoms.
*
* @category constructors
* @since 4.0.0
*/
export declare const make: {
/**
* Creates an atom from a synchronous value or read function, or from an `Effect` or `Stream` whose state is exposed as an `AsyncResult`; plain values create writable state atoms.
*
* @category constructors
* @since 4.0.0
*/
<A, E>(create: (get: AtomContext) => Effect.Effect<A, E, Scope.Scope | AtomRegistry>, options?: {
readonly initialValue?: A | undefined;
readonly uninterruptible?: boolean | undefined;
}): Atom<AsyncResult.AsyncResult<A, E>>;
/**
* Creates an atom from a synchronous value or read function, or from an `Effect` or `Stream` whose state is exposed as an `AsyncResult`; plain values create writable state atoms.
*
* @category constructors
* @since 4.0.0
*/
<A, E>(effect: Effect.Effect<A, E, Scope.Scope | AtomRegistry>, options?: {
readonly initialValue?: A;
readonly uninterruptible?: boolean | undefined;
}): Atom<AsyncResult.AsyncResult<A, E>>;
/**
* Creates an atom from a synchronous value or read function, or from an `Effect` or `Stream` whose state is exposed as an `AsyncResult`; plain values create writable state atoms.
*
* @category constructors
* @since 4.0.0
*/
<A, E>(create: (get: AtomContext) => Stream.Stream<A, E, AtomRegistry>, options?: {
readonly initialValue?: A;
}): Atom<AsyncResult.AsyncResult<A, E | Cause.NoSuchElementError>>;
/**
* Creates an atom from a synchronous value or read function, or from an `Effect` or `Stream` whose state is exposed as an `AsyncResult`; plain values create writable state atoms.
*
* @category constructors
* @since 4.0.0
*/
<A, E>(stream: Stream.Stream<A, E, AtomRegistry>, options?: {
readonly initialValue?: A;
}): Atom<AsyncResult.AsyncResult<A, E | Cause.NoSuchElementError>>;
/**
* Creates an atom from a synchronous value or read function, or from an `Effect` or `Stream` whose state is exposed as an `AsyncResult`; plain values create writable state atoms.
*
* @category constructors
* @since 4.0.0
*/
<A>(create: (get: AtomContext) => A): Atom<A>;
/**
* Creates an atom from a synchronous value or read function, or from an `Effect` or `Stream` whose state is exposed as an `AsyncResult`; plain values create writable state atoms.
*
* @category constructors
* @since 4.0.0
*/
<A>(initialValue: A): Writable<A>;
};
/**
* Atom that builds a `Context` from a `Layer` and exposes constructors for atoms, functions, pulls, and subscription refs that run with that context.
*
* @category models
* @since 4.0.0
*/
export interface AtomRuntime<R, ER = never> extends Atom<AsyncResult.AsyncResult<Context.Context<R>, ER>> {
readonly factory: RuntimeFactory;
readonly layer: Atom<Layer.Layer<R, ER>>;
readonly atom: {
<A, E>(create: (get: AtomContext) => Effect.Effect<A, E, Scope.Scope | R | AtomRegistry | Reactivity.Reactivity>, options?: {
readonly initialValue?: A;
readonly uninterruptible?: boolean | undefined;
}): Atom<AsyncResult.AsyncResult<A, E | ER>>;
<A, E>(effect: Effect.Effect<A, E, Scope.Scope | R | AtomRegistry | Reactivity.Reactivity>, options?: {
readonly initialValue?: A;
readonly uninterruptible?: boolean | undefined;
}): Atom<AsyncResult.AsyncResult<A, E | ER>>;
<A, E>(create: (get: AtomContext) => Stream.Stream<A, E, AtomRegistry | Reactivity.Reactivity | R>, options?: {
readonly initialValue?: A;
}): Atom<AsyncResult.AsyncResult<A, E | ER | Cause.NoSuchElementError>>;
<A, E>(stream: Stream.Stream<A, E, AtomRegistry | Reactivity.Reactivity | R>, options?: {
readonly initialValue?: A;
}): Atom<AsyncResult.AsyncResult<A, E | ER | Cause.NoSuchElementError>>;
};
readonly fn: {
<Arg>(): {
<E, A>(fn: (arg: Arg, get: FnContext) => Effect.Effect<A, E, Scope.Scope | AtomRegistry | Reactivity.Reactivity | R>, options?: {
readonly initialValue?: A | undefined;
readonly reactivityKeys?: ReadonlyArray<unknown> | ReadonlyRecord<string, ReadonlyArray<unknown>> | undefined;
readonly concurrent?: boolean | undefined;
}): AtomResultFn<Arg, A, E | ER>;
<E, A>(fn: (arg: Arg, get: FnContext) => Stream.Stream<A, E, AtomRegistry | Reactivity.Reactivity | R>, options?: {
readonly initialValue?: A | undefined;
readonly reactivityKeys?: ReadonlyArray<unknown> | ReadonlyRecord<string, ReadonlyArray<unknown>> | undefined;
readonly concurrent?: boolean | undefined;
}): AtomResultFn<Arg, A, E | ER | Cause.NoSuchElementError>;
};
<E, A, Arg = void>(fn: (arg: Arg, get: FnContext) => Effect.Effect<A, E, Scope.Scope | AtomRegistry | Reactivity.Reactivity | R>, options?: {
readonly initialValue?: A | undefined;
readonly reactivityKeys?: ReadonlyArray<unknown> | ReadonlyRecord<string, ReadonlyArray<unknown>> | undefined;
readonly concurrent?: boolean | undefined;
}): AtomResultFn<Arg, A, E | ER>;
<E, A, Arg = void>(fn: (arg: Arg, get: FnContext) => Stream.Stream<A, E, AtomRegistry | Reactivity.Reactivity | R>, options?: {
readonly initialValue?: A | undefined;
readonly reactivityKeys?: ReadonlyArray<unknown> | ReadonlyRecord<string, ReadonlyArray<unknown>> | undefined;
readonly concurrent?: boolean | undefined;
}): AtomResultFn<Arg, A, E | ER | Cause.NoSuchElementError>;
};
readonly pull: <A, E>(create: ((get: AtomContext) => Stream.Stream<A, E, R | AtomRegistry | Reactivity.Reactivity>) | Stream.Stream<A, E, R | AtomRegistry | Reactivity.Reactivity>, options?: {
readonly disableAccumulation?: boolean;
readonly initialValue?: ReadonlyArray<A>;
}) => Writable<PullResult<A, E | ER>, void>;
readonly subscriptionRef: <A, E>(create: Effect.Effect<SubscriptionRef.SubscriptionRef<A>, E, Scope.Scope | R | AtomRegistry | Reactivity.Reactivity> | ((get: AtomContext) => Effect.Effect<SubscriptionRef.SubscriptionRef<A>, E, Scope.Scope | R | AtomRegistry | Reactivity.Reactivity>)) => Writable<AsyncResult.AsyncResult<A, E>, A>;
}
/**
* Factory for `AtomRuntime` values that share a `Layer.MemoMap` and a set of global layers.
*
* @category models
* @since 4.0.0
*/
export interface RuntimeFactory {
<R, E>(create: Layer.Layer<R, E, AtomRegistry | Reactivity.Reactivity> | ((get: AtomContext) => Layer.Layer<R, E, AtomRegistry | Reactivity.Reactivity>)): AtomRuntime<R, E>;
readonly memoMap: Layer.MemoMap;
readonly addGlobalLayer: <A, E>(layer: Layer.Layer<A, E, AtomRegistry | Reactivity.Reactivity>) => void;
/**
* Uses the `Reactivity` service from the runtime to refresh the atom whenever
* the keys change.
*/
readonly withReactivity: (keys: ReadonlyArray<unknown> | ReadonlyRecord<string, ReadonlyArray<unknown>>) => <A extends Atom<any>>(atom: A) => A;
}
/**
* Creates a `RuntimeFactory` backed by the supplied `Layer.MemoMap`.
*
* @category constructors
* @since 4.0.0
*/
export declare const context: (options: {
readonly memoMap: Layer.MemoMap;
}) => RuntimeFactory;
/**
* Default `Layer.MemoMap` used by the module-level `runtime` factory.
*
* @category context
* @since 4.0.0
*/
export declare const defaultMemoMap: Layer.MemoMap;
/**
* Default `RuntimeFactory` created with `defaultMemoMap`.
*
* @category context
* @since 4.0.0
*/
export declare const runtime: RuntimeFactory;
/**
* Returns `Rx.runtime.withReactivity` for refreshing an atom whenever the
* keys change in the `Reactivity` service.
*
* **When to use**
*
* Use to refresh an atom whenever one or more invalidation keys change in the
* default reactivity runtime.
*
* @category reactivity
* @since 4.0.0
*/
export declare const withReactivity: (keys: ReadonlyArray<unknown> | ReadonlyRecord<string, ReadonlyArray<unknown>>) => <A extends Atom<any>>(atom: A) => A;
/**
* Creates a writable atom backed by a `SubscriptionRef`, or by an effect that produces one, updating from ref changes and writing atom updates back to the ref.
*
* @category constructors
* @since 4.0.0
*/
export declare const subscriptionRef: {
/**
* Creates a writable atom backed by a `SubscriptionRef`, or by an effect that produces one, updating from ref changes and writing atom updates back to the ref.
*
* @category constructors
* @since 4.0.0
*/
<A>(ref: SubscriptionRef.SubscriptionRef<A> | ((get: AtomContext) => SubscriptionRef.SubscriptionRef<A>)): Writable<A>;
/**
* Creates a writable atom backed by a `SubscriptionRef`, or by an effect that produces one, updating from ref changes and writing atom updates back to the ref.
*
* @category constructors
* @since 4.0.0
*/
<A, E>(effect: Effect.Effect<SubscriptionRef.SubscriptionRef<A>, E, Scope.Scope | AtomRegistry> | ((get: AtomContext) => Effect.Effect<SubscriptionRef.SubscriptionRef<A>, E, Scope.Scope | AtomRegistry>)): Writable<AsyncResult.AsyncResult<A, E>, A>;
};
/**
* Context passed to `fn` and `fnSync` computations for reading atoms, awaiting results, registering finalizers, refreshing atoms, subscribing to changes, and writing updates.
*
* @category models
* @since 4.0.0
*/
export interface FnContext {
<A>(atom: Atom<A>): A;
result<A, E>(this: FnContext, atom: Atom<AsyncResult.AsyncResult<A, E>>, options?: {
readonly suspendOnWaiting?: boolean | undefined;
}): Effect.Effect<A, E>;
addFinalizer(this: FnContext, f: () => void): void;
mount<A>(this: FnContext, atom: Atom<A>): void;
refresh<A>(this: FnContext, atom: Atom<A>): void;
self<A>(this: FnContext): Option.Option<A>;
setSelf<A>(this: FnContext, a: A): void;
set<R, W>(this: FnContext, atom: Writable<R, W>, value: W): void;
setResult<A, E, W>(this: FnContext, atom: Writable<AsyncResult.AsyncResult<A, E>, W>, value: W): Effect.Effect<A, E>;
some<A>(this: FnContext, atom: Atom<Option.Option<A>>): Effect.Effect<A>;
stream<A>(this: FnContext, atom: Atom<A>, options?: {
readonly withoutInitialValue?: boolean;
readonly bufferSize?: number;
}): Stream.Stream<A>;
streamResult<A, E>(this: FnContext, atom: Atom<AsyncResult.AsyncResult<A, E>>, options?: {
readonly withoutInitialValue?: boolean;
readonly bufferSize?: number;
}): Stream.Stream<A, E>;
subscribe<A>(this: FnContext, atom: Atom<A>, f: (_: A) => void, options?: {
readonly immediate?: boolean;
}): void;
readonly registry: Registry.AtomRegistry;
}
/**
* Creates a writable atom for a synchronous function; writing an argument re-runs the function, returning `Option.none` before the first call unless an initial value is supplied.
*
* @category constructors
* @since 4.0.0
*/
export declare const fnSync: {
/**
* Creates a writable atom for a synchronous function; writing an argument re-runs the function, returning `Option.none` before the first call unless an initial value is supplied.
*
* @category constructors
* @since 4.0.0
*/
<Arg>(): {
/**
* Creates a writable atom for a synchronous function; writing an argument re-runs the function, returning `Option.none` before the first call unless an initial value is supplied.
*
* @category constructors
* @since 4.0.0
*/
<A>(f: (arg: Arg, get: FnContext) => A): Writable<Option.Option<A>, Arg>;
/**
* Creates a writable atom for a synchronous function; writing an argument re-runs the function, returning `Option.none` before the first call unless an initial value is supplied.
*
* @category constructors
* @since 4.0.0
*/
<A>(f: (arg: Arg, get: FnContext) => A, options: {
readonly initialValue: A;
}): Writable<A, Arg>;
};
/**
* Creates a writable atom for a synchronous function; writing an argument re-runs the function, returning `Option.none` before the first call unless an initial value is supplied.
*
* @category constructors
* @since 4.0.0
*/
<A, Arg = void>(f: (arg: Arg, get: FnContext) => A): Writable<Option.Option<A>, Arg>;
/**
* Creates a writable atom for a synchronous function; writing an argument re-runs the function, returning `Option.none` before the first call unless an initial value is supplied.
*
* @category constructors
* @since 4.0.0
*/
<A, Arg = void>(f: (arg: Arg, get: FnContext) => A, options: {
readonly initialValue: A;
}): Writable<A, Arg>;
};
/**
* Writable async function atom whose value is an `AsyncResult` and whose writes accept function arguments plus `Reset` and `Interrupt` controls.
*
* @category models
* @since 4.0.0
*/
export interface AtomResultFn<Arg, A, E = never> extends Writable<AsyncResult.AsyncResult<A, E>, Arg | Reset | Interrupt> {
}
/**
* Defines the control symbol that can be written to an `AtomResultFn` to reset it to its initial state.
*
* **When to use**
*
* Use when you need an `AtomResultFn` write value that clears the current async
* result and returns it to the initial state.
*
* @category symbols
* @since 4.0.0
*/
export declare const Reset: unique symbol;
/**
* Type of the `Reset` control symbol accepted by `AtomResultFn` writes.
*
* @category symbols
* @since 4.0.0
*/
export type Reset = typeof Reset;
/**
* Defines the control symbol that can be written to an `AtomResultFn` to interrupt the current asynchronous computation.
*
* **When to use**
*
* Use when you need an `AtomResultFn` write value that interrupts the currently
* running async computation.
*
* @category symbols
* @since 4.0.0
*/
export declare const Interrupt: unique symbol;
/**
* Type of the `Interrupt` control symbol accepted by `AtomResultFn` writes.
*
* @category symbols
* @since 4.0.0
*/
export type Interrupt = typeof Interrupt;
/**
* Creates a writable atom for an `Effect` or `Stream` function; writing an argument starts the computation and exposes its state as an `AsyncResult`.
*
* @category constructors
* @since 4.0.0
*/
export declare const fn: {
/**
* Creates a writable atom for an `Effect` or `Stream` function; writing an argument starts the computation and exposes its state as an `AsyncResult`.
*
* @category constructors
* @since 4.0.0
*/
<Arg>(): <E, A>(fn: (arg: Arg, get: FnContext) => Effect.Effect<A, E, Scope.Scope | AtomRegistry>, options?: {
readonly initialValue?: A | undefined;
readonly concurrent?: boolean | undefined;
}) => AtomResultFn<Arg, A, E>;
/**
* Creates a writable atom for an `Effect` or `Stream` function; writing an argument starts the computation and exposes its state as an `AsyncResult`.
*
* @category constructors
* @since 4.0.0
*/
<E, A, Arg = void>(fn: (arg: Arg, get: FnContext) => Effect.Effect<A, E, Scope.Scope | AtomRegistry>, options?: {
readonly initialValue?: A | undefined;
readonly concurrent?: boolean | undefined;
}): AtomResultFn<Arg, A, E>;
/**
* Creates a writable atom for an `Effect` or `Stream` function; writing an argument starts the computation and exposes its state as an `AsyncResult`.
*
* @category constructors
* @since 4.0.0
*/
<Arg>(): <E, A>(fn: (arg: Arg, get: FnContext) => Stream.Stream<A, E, AtomRegistry>, options?: {
readonly initialValue?: A | undefined;
readonly concurrent?: boolean | undefined;
}) => AtomResultFn<Arg, A, E | Cause.NoSuchElementError>;
/**
* Creates a writable atom for an `Effect` or `Stream` function; writing an argument starts the computation and exposes its state as an `AsyncResult`.
*
* @category constructors
* @since 4.0.0
*/
<E, A, Arg = void>(fn: (arg: Arg, get: FnContext) => Stream.Stream<A, E, AtomRegistry>, options?: {
readonly initialValue?: A | undefined;
readonly concurrent?: boolean | undefined;
}): AtomResultFn<Arg, A, E | Cause.NoSuchElementError>;
};
/**
* `AsyncResult` produced by `pull`, containing a non-empty batch of pulled items and a `done` flag, or `NoSuchElementError` when the stream completes without items.
*
* @category models
* @since 4.0.0
*/
export type PullResult<A, E = never> = AsyncResult.AsyncResult<{
readonly done: boolean;
readonly items: Arr.NonEmptyArray<A>;
}, E | Cause.NoSuchElementError>;
/**
* Creates a writable atom that pulls an initial chunk from a stream and then pulls the next chunk whenever it is written to, accumulating items unless `disableAccumulation` is enabled.
*
* @category constructors
* @since 4.0.0
*/
export declare const pull: <A, E>(create: ((get: AtomContext) => Stream.Stream<A, E, AtomRegistry>) | Stream.Stream<A, E, AtomRegistry>, options?: {
readonly disableAccumulation?: boolean | undefined;
}) => Writable<PullResult<A, E>, void>;
/**
* Creates a memoized atom factory that returns the same object for the same argument, using weak references for cached values when the platform supports them.
*
* @category constructors
* @since 4.0.0
*/
export declare const family: <Arg, T extends object>(f: (arg: Arg) => T) => (arg: Arg) => T;
/**
* Uses a fallback `AsyncResult` atom while the primary atom is `Initial`, marking the fallback result as waiting until the primary atom produces a non-initial result.
*
* @category combinators
* @since 4.0.0
*/
export declare const withFallback: {
/**
* Uses a fallback `AsyncResult` atom while the primary atom is `Initial`, marking the fallback result as waiting until the primary atom produces a non-initial result.
*
* @category combinators
* @since 4.0.0
*/
<E2, A2>(fallback: Atom<AsyncResult.AsyncResult<A2, E2>>): <R extends Atom<AsyncResult.AsyncResult<any, any>>>(self: R) => [R] extends [Writable<infer _, infer RW>] ? Writable<AsyncResult.AsyncResult<AsyncResult.AsyncResult.Success<Type<R>> | A2, AsyncResult.AsyncResult.Failure<Type<R>> | E2>, RW> : Atom<AsyncResult.AsyncResult<AsyncResult.AsyncResult.Success<Type<R>> | A2, AsyncResult.AsyncResult.Failure<Type<R>> | E2>>;
/**
* Uses a fallback `AsyncResult` atom while the primary atom is `Initial`, marking the fallback result as waiting until the primary atom produces a non-initial result.
*
* @category combinators
* @since 4.0.0
*/
<R extends Atom<AsyncResult.AsyncResult<any, any>>, A2, E2>(self: R, fallback: Atom<AsyncResult.AsyncResult<A2, E2>>): [R] extends [Writable<infer _, infer RW>] ? Writable<AsyncResult.AsyncResult<AsyncResult.AsyncResult.Success<Type<R>> | A2, AsyncResult.AsyncResult.Failure<Type<R>> | E2>, RW> : Atom<AsyncResult.AsyncResult<AsyncResult.AsyncResult.Success<Type<R>> | A2, AsyncResult.AsyncResult.Failure<Type<R>> | E2>>;
};
/**
* Returns a copy of an atom that remains cached and mounted even when no subscribers are using it.
*
* @category combinators
* @since 4.0.0
*/
export declare const keepAlive: <A extends Atom<any>>(self: A) => A;
/**
* Allows a reactive value to be disposed of when it is not in use.
*
* **Details**
*
* Atoms have this behavior by default, so use this to undo `keepAlive` on a copied atom.
*
* @category combinators
* @since 4.0.0
*/
export declare const autoDispose: <A extends Atom<any>>(self: A) => A;
/**
* Sets whether an atom should be lazy.
*
* **Details**
*
* Lazy atoms defer recomputation while they have no active listeners or active
* non-lazy dependents, rebuilding the next time their value is observed.
*
* @category combinators
* @since 4.0.0
*/
export declare const setLazy: {
/**
* Sets whether an atom should be lazy.
*
* **Details**
*
* Lazy atoms defer recomputation while they have no active listeners or active
* non-lazy dependents, rebuilding the next time their value is observed.
*
* @category combinators
* @since 4.0.0
*/
(lazy: boolean): <A extends Atom<any>>(self: A) => A;
/**
* Sets whether an atom should be lazy.
*
* **Details**
*
* Lazy atoms defer recomputation while they have no active listeners or active
* non-lazy dependents, rebuilding the next time their value is observed.
*
* @category combinators
* @since 4.0.0
*/
<A extends Atom<any>>(self: A, lazy: boolean): A;
};
/**
* Attaches a diagnostic label to an atom.
*
* **Details**
*
* The label is used for inspection and debugging metadata and does not change the
* atom's read or write behavior.
*
* @category combinators
* @since 4.0.0
*/
export declare const withLabel: {
/**
* Attaches a diagnostic label to an atom.
*
* **Details**
*
* The label is used for inspection and debugging metadata and does not change the
* atom's read or write behavior.
*
* @category combinators
* @since 4.0.0
*/
(name: string): <A extends Atom<any>>(self: A) => A;
/**
* Attaches a diagnostic label to an atom.
*
* **Details**
*
* The label is used for inspection and debugging metadata and does not change the
* atom's read or write behavior.
*
* @category combinators
* @since 4.0.0
*/
<A extends Atom<any>>(self: A, name: string): A;
};
/**
* Pairs an atom with an initial value for registry initialization.
*
* **When to use**
*
* Use to preload an atom value when constructing or seeding a registry.
*
* **Details**
*
* The returned tuple can be supplied to `AtomRegistry` initial values so the atom
* starts with the provided value before it is first rebuilt.
*
* @category combinators
* @since 4.0.0
*/
export declare const initialValue: {
/**
* Pairs an atom with an initial value for registry initialization.
*
* **When to use**
*
* Use to preload an atom value when constructing or seeding a registry.
*
* **Details**
*
* The returned tuple can be supplied to `AtomRegistry` initial values so the atom
* starts with the provided value before it is first rebuilt.
*
* @category combinators
* @since 4.0.0
*/
<A>(initialValue: A): (self: Atom<A>) => readonly [Atom<A>, A];
/**
* Pairs an atom with an initial value for registry initialization.
*
* **When to use**
*
* Use to preload an atom value when constructing or seeding a registry.
*
* **Details**
*
* The returned tuple can be supplied to `AtomRegistry` initial values so the atom
* starts with the provided value before it is first rebuilt.
*
* @category combinators
* @since 4.0.0
*/
<A>(self: Atom<A>, initialValue: A): readonly [Atom<A>, A];
};
/**
* Creates a derived atom by reading another atom with a custom `AtomContext`
* function.
*
* **Details**
*
* If the source is writable, the derived atom keeps the source write input and
* forwards writes to the source. `initialValueTarget` controls which atom receives
* preloaded initial values for the derived atom.
*
* @category combinators
* @since 4.0.0
*/
export declare const transform: {
/**
* Creates a derived atom by reading another atom with a custom `AtomContext`
* function.
*
* **Details**
*
* If the source is writable, the derived atom keeps the source write input and
* forwards writes to the source. `initialValueTarget` controls which atom receives
* preloaded initial values for the derived atom.
*
* @category combinators
* @since 4.0.0
*/
<R extends Atom<any>, B>(f: (get: AtomContext, atom: R) => B, options?: {
readonly initialValueTarget?: Atom<B> | undefined;
}): (self: R) => [R] extends [Writable<infer _, infer RW>] ? Writable<B, RW> : Atom<B>;
/**
* Creates a derived atom by reading another atom with a custom `AtomContext`
* function.
*
* **Details**
*
* If the source is writable, the derived atom keeps the source write input and
* forwards writes to the source. `initialValueTarget` controls which atom receives
* preloaded initial values for the derived atom.
*
* @category combinators
* @since 4.0.0
*/
<R extends Atom<any>, B>(self: R, f: (get: AtomContext, atom: R) => B, options?: {
readonly initialValueTarget?: Atom<B> | undefined;
}): [R] extends [Writable<infer _, infer RW>] ? Writable<B, RW> : Atom<B>;
};
/**
* Maps the current value of an atom with a pure function.
*
* **Details**
*
* When the source atom is writable, the returned atom remains writable and keeps
* the source atom's write input type.
*
* @category combinators
* @since 4.0.0
*/
export declare const map: {
/**
* Maps the current value of an atom with a pure function.
*
* **Details**
*
* When the source atom is writable, the returned atom remains writable and keeps
* the source atom's write input type.
*
* @category combinators
* @since 4.0.0
*/
<R extends Atom<any>, B>(f: (_: Type<R>) => B): (self: R) => [R] extends [Writable<infer _, infer RW>] ? Writable<B, RW> : Atom<B>;
/**
* Maps the current value of an atom with a pure function.
*
* **Details**
*
* When the source atom is writable, the returned atom remains writable and keeps
* the source atom's write input type.
*
* @category combinators
* @since 4.0.0
*/
<R extends Atom<any>, B>(self: R, f: (_: Type<R>) => B): [R] extends [Writable<infer _, infer RW>] ? Writable<B, RW> : Atom<B>;
};
/**
* Maps the successful value inside an `AsyncResult` atom.
*
* **Details**
*
* Initial and failure states are preserved, and writable source atoms keep their
* original write input type.
*
* @category combinators
* @since 4.0.0
*/
export declare const mapResult: {
/**
* Maps the successful value inside an `AsyncResult` atom.
*
* **Details**
*
* Initial and failure states are preserved, and writable source atoms keep their
* original write input type.
*
* @category combinators
* @since 4.0.0
*/
<R extends Atom<AsyncResult.AsyncResult<any, any>>, B>(f: (_: AsyncResult.AsyncResult.Success<Type<R>>) => B): (self: R) => [R] extends [Writable<infer _, infer RW>] ? Writable<AsyncResult.AsyncResult<B, AsyncResult.AsyncResult.Failure<Type<R>>>, RW> : Atom<AsyncResult.AsyncResult<B, AsyncResult.AsyncResult.Failure<Type<R>>>>;
/**
* Maps the successful value inside an `AsyncResult` atom.
*
* **Details**
*
* Initial and failure states are preserved, and writable source atoms keep their
* original write input type.
*
* @category combinators
* @since 4.0.0
*/
<R extends Atom<AsyncResult.AsyncResult<any, any>>, B>(self: R, f: (_: AsyncResult.AsyncResult.Success<Type<R>>) => B): [R] extends [Writable<infer _, infer RW>] ? Writable<AsyncResult.AsyncResult<B, AsyncResult.AsyncResult.Failure<Type<R>>>, RW> : Atom<AsyncResult.AsyncResult<B, AsyncResult.AsyncResult.Failure<Type<R>>>>;
};
/**
* Creates an atom that publishes source changes only after the source has stopped
* changing for the specified duration.
*
* **Details**
*
* The current source value is used immediately, and any pending debounce timer is
* cleared when the derived atom is disposed.
*
* @category combinators
* @since 4.0.0
*/
export declare const debounce: {
/**
* Creates an atom that publishes source changes only after the source has stopped
* changing for the specified duration.
*
* **Details**
*
* The current source value is used immediately, and any pending debounce timer is
* cleared when the derived atom is disposed.
*
* @category combinators
* @since 4.0.0
*/
(duration: Duration.Input): <A extends Atom<any>>(self: A) => WithoutSerializable<A>;
/**
* Creates an atom that publishes source changes only after the source has stopped
* changing for the specified duration.
*
* **Details**
*
* The current source value is used immediately, and any pending debounce timer is
* cleared when the derived atom is disposed.
*
* @category combinators
* @since 4.0.0
*/
<A extends Atom<any>>(self: A, duration: Duration.Input): WithoutSerializable<A>;
};
/**
* Creates a derived atom that reads the source and schedules a refresh after the
* specified duration.
*
* **Details**
*
* The scheduled refresh is canceled when the derived atom's lifetime is disposed.
*
* @category combinators
* @since 4.0.0
*/
export declare const withRefresh: {
/**
* Creates a derived atom that reads the source and schedules a refresh after the
* specified duration.
*
* **Details**
*
* The scheduled refresh is canceled when the derived atom's lifetime is disposed.
*
* @category combinators
* @since 4.0.0
*/
(duration: Duration.Input): <A extends Atom<any>>(self: A) => WithoutSerializable<A>;
/**
* Creates a derived atom that reads the source and schedules a refresh after the
* specified duration.
*
* **Details**
*
* The scheduled refresh is canceled when the derived atom's lifetime is disposed.
*
* @category combinators
* @since 4.0.0
*/
<A extends Atom<any>>(self: A, duration: Duration.Input): WithoutSerializable<A>;
};
/**
* Adds stale-while-revalidate refresh behavior to an async result atom.
*
* **Details**
*
* Automatic revalidation during reads is skipped while the current value is
* fresh within `staleTime`. Manual `refresh` calls remain forceful and always
* forward to the wrapped atom. Use `revalidateOnMount` to control whether stale data should trigger a
* background refresh on first mount. Use `revalidateOnFocus` to control
* focus behavior. `true` respects `staleTime` and `"always"` forces refetch.
*
* @category combinators
* @since 4.0.0
*/
export declare const swr: {
/**
* Adds stale-while-revalidate refresh behavior to an async result atom.
*
* **Details**
*
* Automatic revalidation during reads is skipped while the current value is
* fresh within `staleTime`. Manual `refresh` calls remain forceful and always
* forward to the wrapped atom. Use `revalidateOnMount` to control whether stale data should trigger a
* background refresh on first mount. Use `revalidateOnFocus` to control
* focus behavior. `true` respects `staleTime` and `"always"` forces refetch.
*
* @category combinators
* @since 4.0.0
*/
(options: {
readonly staleTime: Duration.Input;
readonly revalidateOnMount?: boolean | undefined;
readonly revalidateOnFocus?: boolean | "always" | undefined;
readonly focusSignal?: Atom<any> | undefined;
}): <R extends Atom<AsyncResult.AsyncResult<any, any>>>(self: R) => WithoutSerializable<R>;
/**
* Adds stale-while-revalidate refresh behavior to an async result atom.
*
* **Details**
*
* Automatic revalidation during reads is skipped while the current value is
* fresh within `staleTime`. Manual `refresh` calls remain forceful and always
* forward to the wrapped atom. Use `revalidateOnMount` to control whether stale data should trigger a
* background refresh on first mount. Use `revalidateOnFocus` to control
* focus behavior. `true` respects `staleTime` and `"always"` forces refetch.
*
* @category combinators
* @since 4.0.0
*/
<R extends Atom<AsyncResult.AsyncResult<any, any>>>(self: R, options: {
readonly staleTime: Duration.Input;
readonly revalidateOnMount?: boolean | undefined;
readonly revalidateOnFocus?: boolean | "always" | undefined;
readonly focusSignal?: Atom<any> | undefined;
}): WithoutSerializable<R>;
};
/**
* Wraps an atom in a writable optimistic atom.
*
* **Details**
*
* Writes accept transition atoms containing `AsyncResult` values. Waiting
* successes are shown optimistically while transitions run; when successful
* transitions finish, the source atom is refreshed, and failures roll the value
* back to the latest source value.
*
* @category Optimistic
* @since 4.0.0
*/
export declare const optimistic: <A>(self: Atom<A>) => Writable<A, Atom<AsyncResult.AsyncResult<A, unknown>>>;
/**
* Creates an `AtomResultFn` that applies an optimistic update before running the
* underlying mutation.
*
* **Details**
*
* The reducer computes the provisional value from the current value and mutation
* input. The wrapped function result then completes the transition or updates the
* optimistic value through the provided setter callback.
*
* @category Optimistic
* @since 4.0.0
*/
export declare const optimisticFn: {
/**
* Creates an `AtomResultFn` that applies an optimistic update before running the
* underlying mutation.
*
* **Details**
*
* The reducer computes the provisional value from the current value and mutation
* input. The wrapped function result then completes the transition or updates the
* optimistic value through the provided setter callback.
*
* @category Optimistic
* @since 4.0.0
*/
<A, W, XA, XE, OW = void>(options: {
readonly reducer: (current: NoInfer<A>, update: OW) => NoInfer<W>;
readonly fn: AtomResultFn<OW, XA, XE> | ((set: (result: NoInfer<W>) => void) => AtomResultFn<OW, XA, XE>);
}): (self: Writable<A, Atom<AsyncResult.AsyncResult<W, unknown>>>) => AtomResultFn<OW, XA, XE>;
/**
* Creates an `AtomResultFn` that applies an optimistic update before running the
* underlying mutation.
*
* **Details**
*
* The reducer computes the provisional value from the current value and mutation
* input. The wrapped function result then completes the transition or updates the
* optimistic value through the provided setter callback.
*
* @category Optimistic
* @since 4.0.0
*/
<A, W, XA, XE, OW = void>(self: Writable<A, Atom<AsyncResult.AsyncResult<W, unknown>>>, options: {
readonly reducer: (current: NoInfer<A>, update: OW) => NoInfer<W>;
readonly fn: AtomResultFn<OW, XA, XE> | ((set: (result: NoInfer<W>) => void) => AtomResultFn<OW, XA, XE>);
}): AtomResultFn<OW, XA, XE>;
};
/**
* Runs synchronous atom updates as a batch.
*
* **Details**
*
* Stale nodes are rebuilt and listeners are notified after the callback completes,
* so dependent updates observe the final batched state.
*
* @category batching
* @since 4.0.0
*/
export declare const batch: (f: () => void) => void;
/**
* Creates a browser-only signal atom that increments when the document becomes visible.
*
* **Details**
*
* It listens for `visibilitychange` events on `window` and removes the listener
* when the atom is disposed.
*
* @category Focus
* @since 4.0.0
*/
export declare const windowFocusSignal: Atom<number>;
/**
* Creates a combinator that refreshes an atom whenever the supplied signal atom
* changes.
*
* **Details**
*
* The derived atom also subscribes to the source atom so normal source updates are
* forwarded to its own value.
*
* @category Focus
* @since 4.0.0
*/
export declare const makeRefreshOnSignal: <_>(signal: Atom<_>) => <A extends Atom<any>>(self: A) => WithoutSerializable<A>;
/**
* Refreshes an atom whenever `windowFocusSignal` changes.
*
* **Details**
*
* This helper is browser-only because `windowFocusSignal` depends on `window` and
* `document.visibilityState`.
*
* @category Focus
* @since 4.0.0
*/
export declare const refreshOnWindowFocus: <A extends Atom<any>>(self: A) => WithoutSerializable<A>;
/**
* Creates a writable atom backed by a `KeyValueStore` entry.
*
* **Details**
*
* Values are encoded and decoded with the supplied schema. In sync mode the atom
* exposes the decoded value and writes the default value when the key is missing;
* in async mode it exposes an `AsyncResult` of the decoded value.
*
* @category KeyValueStore
* @since 4.0.0
*/
export declare const kvs: <S extends Schema.Codec<any, any>, const Mode extends "sync" | "async" = never>(options: {
readonly runtime: AtomRuntime<KeyValueStore.KeyValueStore, any>;
readonly key: string;
readonly schema: S;
readonly defaultValue: LazyArg<S["Type"]>;
readonly mode?: Mode | undefined;
}) => Writable<"async" extends Mode ? AsyncResult.AsyncResult<S["Type"]> : S["Type"], S["Type"]>;
/**
* Creates an atom that reads and writes a URL search parameter.
*
* **Gotchas**
*
* If you pass a schema, it has to be synchronous and have no context.
*
* @category search params
* @since 4.0.0
*/
export declare const searchParam: <S extends Schema.Codec<any, string> = never>(name: string, options?: {
readonly schema?: S | undefined;
}) => Writable<[S] extends [never] ? string : Option.Option<S["Type"]>>;
/**
* Converts an atom into a stream using the `AtomRegistry` service.
*
* **Details**
*
* The stream emits the atom's current value immediately and then emits subsequent
* changes until the stream scope is closed.
*
* @category converting
* @since 4.0.0
*/
export declare const toStream: <A>(self: Atom<A>) => Stream.Stream<A, never, AtomRegistry>;
/**
* Converts an `AsyncResult` atom into a stream using the `AtomRegistry` service.
*
* **Details**
*
* Initial results are skipped, successes are emitted as stream values, and
* failures fail the stream with the result cause.
*
* @category converting
* @since 4.0.0
*/
export declare const toStreamResult: <A, E>(self: Atom<AsyncResult.AsyncResult<A, E>>) => Stream.Stream<A, E, AtomRegistry>;
/**
* Reads an atom's current value from the `AtomRegistry` service.
*
* @category converting
* @since 4.0.0
*/
export declare const get: <A>(self: Atom<A>) => Effect.Effect<A, never, AtomRegistry>;
/**
* Reads a writable atom, computes a return value and next write value, writes the
* next value, and returns the computed result.
*
* @category converting
* @since 4.0.0
*/
export declare const modify: {
/**
* Reads a writable atom, computes a return value and next write value, writes the
* next value, and returns the computed result.
*
* @category converting
* @since 4.0.0
*/
<R, W, A>(f: (_: R) => [returnValue: A, nextValue: W]): (self: Writable<R, W>) => Effect.Effect<A, never, AtomRegistry>;
/**
* Reads a writable atom, computes a return value and next write value, writes the
* next value, and returns the computed result.
*
* @category converting
* @since 4.0.0
*/
<R, W, A>(self: Writable<R, W>, f: (_: R) => [returnValue: A, nextValue: W]): Effect.Effect<A, never, AtomRegistry>;
};
/**
* Writes a value to a writable atom through the `AtomRegistry` service.
*
* @category converting
* @since 4.0.0
*/
export declare const set: {
/**
* Writes a value to a writable atom through the `AtomRegistry` service.
*
* @category converting
* @since 4.0.0
*/
<W>(value: W): <R>(self: Writable<R, W>) => Effect.Effect<void, never, AtomRegistry>;
/**
* Writes a value to a writable atom through the `AtomRegistry` service.
*
* @category converting
* @since 4.0.0
*/
<R, W>(self: Writable<R, W>, value: W): Effect.Effect<void, never, AtomRegistry>;
};
/**
* Updates a writable atom by reading its current value from the registry and
* writing the value returned by the update function.
*
* @category converting
* @since 4.0.0
*/
export declare const update: {
/**
* Updates a writable atom by reading its current value from the registry and
* writing the value returned by the update function.
*
* @category converting
* @since 4.0.0
*/
<R, W>(f: (_: R) => W): (self: Writable<R, W>) => Effect.Effect<void, never, AtomRegistry>;
/**
* Updates a writable atom by reading its current value from the registry and
* writing the value returned by the update function.
*
* @category converting
* @since 4.0.0
*/
<R, W>(self: Writable<R, W>, f: (_: R) => W): Effect.Effect<void, never, AtomRegistry>;
};
/**
* Reads an `AsyncResult` atom as an effect through the `AtomRegistry` service.
*
* **Details**
*
* The effect waits while the result is `Initial`, and also while it is waiting
* when `suspendOnWaiting` is enabled. Successes succeed with the value and
* failures fail with the result cause.
*
* @category converting
* @since 4.0.0
*/
export declare const getResult: <A, E>(self: Atom<AsyncResult.AsyncResult<A, E>>, options?: {
readonly suspendOnWaiting?: boolean | undefined;
}) => Effect.Effect<A, E, AtomRegistry>;
/**
* Runs a refresh request for an atom through the `AtomRegistry` service.
*
* **When to use**
*
* Use to invalidate and recompute an atom from an Effect that has access to the
* active registry.
*
* @category converting
* @since 4.0.0
*/
export declare const refresh: <A>(self: Atom<A>) => Effect.Effect<void, never, AtomRegistry>;
/**
* Mounts an atom in the `AtomRegistry` for the lifetime of the current scope.
*
* **Details**
*
* Mounting keeps the atom subscribed with a no-op listener until the scope
* finalizer releases it.
*
* @category converting
* @since 4.0.0
*/
export declare const mount: <A>(self: Atom<A>) => Effect.Effect<void, never, AtomRegistry | Scope.Scope>;
/**
* The type id used to mark atoms that carry serialization metadata.
*
* @category type IDs
* @since 4.0.0
*/
export declare const SerializableTypeId: SerializableTypeId;
/**
* The literal type of the serializable atom marker.
*
* @category type IDs
* @since 4.0.0
*/
export type SerializableTypeId = "~effect-atom/atom/Atom/Serializable";
/**
* Serialization metadata attached to an atom.
*
* **Details**
*
* The key identifies the atom in dehydrated state, and the encode/decode
* functions convert between the atom value and the schema encoded value.
*
* @category Serializable
* @since 4.0.0
*/
export interface Serializable<S extends Schema.Top> {
readonly [SerializableTypeId]: {
readonly key: string;
readonly encode: (value: S["Type"]) => S["Encoded"];
readonly decode: (value: S["Encoded"]) => S["Type"];
};
}
/**
* Returns `true` when an atom carries `Serializable` metadata.
*
* @category Serializable
* @since 4.0.0
*/
export declare const isSerializable: (self: Atom<any>) => self is Atom<any> & Serializable<any>;
/**
* Attaches serialization metadata to an atom using a schema and stable key.
*
* **Details**
*
* The schema is converted to a JSON codec for synchronous encode/decode, and the
* key is also used as the atom label when the atom does not already have one.
*
* @category combinators
* @since 4.0.0
*/
export declare const serializable: {
/**
* Attaches serialization metadata to an atom using a schema and stable key.
*
* **Details**
*
* The schema is converted to a JSON codec for synchronous encode/decode, and the
* key is also used as the atom label when the atom does not already have one.
*
* @category combinators
* @since 4.0.0
*/
<R extends Atom<any>, S extends Schema.Codec<Type<R>, any>>(options: {
readonly key: string;
readonly schema: S;
}): (self: R) => R & Serializable<S>;
/**
* Attaches serialization metadata to an atom using a schema and stable key.
*
* **Details**
*
* The schema is converted to a JSON codec for synchronous encode/decode, and the
* key is also used as the atom label when the atom does not already have one.
*
* @category combinators
* @since 4.0.0
*/
<R extends Atom<any>, S extends Schema.Codec<Type<R>, any>>(self: R, options: {
readonly key: string;
readonly schema: S;
}): R & Serializable<S>;
};
/**
* The type id used to mark atoms with a server-side read override.
*
* @category type IDs
* @since 4.0.0
*/
export declare const ServerValueTypeId: "~effect-atom/atom/Atom/ServerValue";
/**
* Sets the value of an Atom when read on the server.
*
* @category ServerValue
* @since 4.0.0
*/
export declare const withServerValue: {
/**
* Sets the value of an Atom when read on the server.
*
* @category ServerValue
* @since 4.0.0
*/
<A extends Atom<any>>(read: (get: <A>(atom: Atom<A>) => A) => Type<A>): (self: A) => A;
/**
* Sets the value of an Atom when read on the server.
*
* @category ServerValue
* @since 4.0.0
*/
<A extends Atom<any>>(self: A, read: (get: <A>(atom: Atom<A>) => A) => Type<A>): A;
};
/**
* Sets an `AsyncResult` atom's server-side value to
* `AsyncResult.initial(true)`.
*
* @category ServerValue
* @since 4.0.0
*/
export declare const withServerValueInitial: <A extends Atom<AsyncResult.AsyncResult<any, any>>>(self: A) => A;
/**
* Reads an atom from a registry, using its server-side read override when one is
* present.
*
* **Details**
*
* Nested reads performed by the override are resolved against the same registry.
*
* @category ServerValue
* @since 4.0.0
*/
export declare const getServerValue: {
/**
* Reads an atom from a registry, using its server-side read override when one is
* present.
*
* **Details**
*
* Nested reads performed by the override are resolved against the same registry.
*
* @category ServerValue
* @since 4.0.0
*/
(registry: Registry.AtomRegistry): <A>(self: Atom<A>) => A;
/**
* Reads an atom from a registry, using its server-side read override when one is
* present.
*
* **Details**
*
* Nested reads performed by the override are resolved against the same registry.
*
* @category ServerValue
* @since 4.0.0
*/
<A>(self: Atom<A>, registry: Registry.AtomRegistry): A;
};
//# sourceMappingURL=Atom.d.ts.map |