@nlozgachev/pipelined 0.20.0 → 0.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -28,6 +28,7 @@ __export(src_exports, {
28
28
  Logged: () => Logged,
29
29
  Maybe: () => Maybe,
30
30
  Num: () => Num,
31
+ Op: () => Op,
31
32
  Optional: () => Optional,
32
33
  Predicate: () => Predicate,
33
34
  Reader: () => Reader,
@@ -288,15 +289,13 @@ var uncurry3 = (f) => (a, b, c) => f(a)(b)(c);
288
289
  var uncurry4 = (f) => (a, b, c, d) => f(a)(b)(c)(d);
289
290
 
290
291
  // src/Core/Deferred.ts
291
- var _store = /* @__PURE__ */ new WeakMap();
292
292
  var Deferred;
293
293
  ((Deferred2) => {
294
- Deferred2.fromPromise = (p) => {
295
- const d = { then: ((f) => p.then(f)) };
296
- _store.set(d, p);
297
- return d;
298
- };
299
- Deferred2.toPromise = (d) => _store.get(d) ?? new Promise((resolve) => d.then(resolve));
294
+ Deferred2.fromPromise = (p) => (
295
+ // eslint-disable-next-line unicorn/no-thenable -- Deferred is intentionally thenable; it is the mechanism that makes Task awaitable
296
+ { then: ((f) => p.then(f)) }
297
+ );
298
+ Deferred2.toPromise = (d) => new Promise((resolve) => d.then(resolve));
300
299
  })(Deferred || (Deferred = {}));
301
300
 
302
301
  // src/Core/Lens.ts
@@ -406,6 +405,880 @@ var Maybe;
406
405
  Maybe2.ap = (arg) => (data) => (0, Maybe2.isSome)(data) && (0, Maybe2.isSome)(arg) ? (0, Maybe2.some)(data.value(arg.value)) : (0, Maybe2.none)();
407
406
  })(Maybe || (Maybe = {}));
408
407
 
408
+ // src/internal/Op.util.ts
409
+ var _abortedNil = { kind: "Nil", reason: "aborted" };
410
+ var _droppedNil = { kind: "Nil", reason: "dropped" };
411
+ var _replacedNil = { kind: "Nil", reason: "replaced" };
412
+ var _evictedNil = { kind: "Nil", reason: "evicted" };
413
+ var _idle = { kind: "Idle" };
414
+ var _pending = { kind: "Pending" };
415
+ var ok = (value) => ({ kind: "Ok", value });
416
+ var err = (error) => ({ kind: "Err", error });
417
+ var cancellableWait = (ms, signal) => {
418
+ if (ms <= 0) return Promise.resolve();
419
+ return new Promise((resolve) => {
420
+ const id = setTimeout(resolve, ms);
421
+ signal.addEventListener("abort", () => {
422
+ clearTimeout(id);
423
+ resolve();
424
+ }, { once: true });
425
+ });
426
+ };
427
+ var runWithRetry = (op, input, signal, options, onRetrying) => {
428
+ const { attempts, backoff, when: shouldRetry } = options;
429
+ const getDelay = (n) => backoff === void 0 ? 0 : typeof backoff === "function" ? backoff(n) : backoff;
430
+ const attempt = async (left) => {
431
+ const result = await Deferred.toPromise(op._factory(input, signal));
432
+ if (result === null || signal.aborted) return null;
433
+ if (result.kind === "Ok") return result;
434
+ if (left <= 1) return result;
435
+ if (shouldRetry !== void 0 && !shouldRetry(result.error)) return result;
436
+ const attemptNumber = attempts - left + 1;
437
+ const ms = getDelay(attemptNumber);
438
+ onRetrying({
439
+ kind: "Retrying",
440
+ attempt: attemptNumber,
441
+ lastError: result.error,
442
+ ...ms > 0 ? { nextRetryIn: ms } : {}
443
+ });
444
+ await cancellableWait(ms, signal);
445
+ if (signal.aborted) return null;
446
+ return attempt(left - 1);
447
+ };
448
+ return attempt(attempts);
449
+ };
450
+ var execute = (op, input, controller, retryOptions, timeoutOptions, onRetrying) => {
451
+ const { signal } = controller;
452
+ const toOutcome = (r) => r === null ? _abortedNil : r.kind === "Ok" ? ok(r.value) : err(r.error);
453
+ const runPromise = retryOptions !== void 0 && onRetrying !== void 0 ? runWithRetry(op, input, signal, retryOptions, onRetrying).then(toOutcome) : Deferred.toPromise(op._factory(input, signal)).then(toOutcome);
454
+ if (timeoutOptions === void 0) return Deferred.fromPromise(runPromise);
455
+ let timerId;
456
+ return Deferred.fromPromise(Promise.race([
457
+ runPromise.then((outcome) => {
458
+ clearTimeout(timerId);
459
+ return outcome;
460
+ }),
461
+ new Promise((resolve) => {
462
+ timerId = setTimeout(() => {
463
+ controller.abort();
464
+ resolve(err(timeoutOptions.onTimeout()));
465
+ }, timeoutOptions.ms);
466
+ })
467
+ ]));
468
+ };
469
+ var makeRestartable = (op, minInterval, retryOptions, timeoutOptions) => {
470
+ let currentState = _idle;
471
+ let currentController;
472
+ let currentResolve;
473
+ let waitController;
474
+ let lastStartTime = 0;
475
+ const subscribers = /* @__PURE__ */ new Set();
476
+ const emit = (state) => {
477
+ currentState = state;
478
+ subscribers.forEach((cb) => cb(state));
479
+ };
480
+ const run = (input) => Deferred.fromPromise(
481
+ new Promise((resolve) => {
482
+ waitController?.abort();
483
+ waitController = void 0;
484
+ currentController?.abort();
485
+ const prev = currentResolve;
486
+ currentResolve = resolve;
487
+ currentController = new AbortController();
488
+ const controller = currentController;
489
+ prev?.(_replacedNil);
490
+ const startExecution = () => {
491
+ if (currentController !== controller) return;
492
+ lastStartTime = Date.now();
493
+ emit(_pending);
494
+ const onRetrying = retryOptions ? (r) => {
495
+ if (currentController === controller) emit(r);
496
+ } : void 0;
497
+ execute(op, input, controller, retryOptions, timeoutOptions, onRetrying).then((outcome) => {
498
+ if (currentController !== controller) return;
499
+ const r = currentResolve;
500
+ currentResolve = void 0;
501
+ currentController = void 0;
502
+ emit(outcome);
503
+ r?.(outcome);
504
+ });
505
+ };
506
+ const gap = minInterval !== void 0 ? Math.max(0, minInterval - (Date.now() - lastStartTime)) : 0;
507
+ if (gap > 0) {
508
+ waitController = new AbortController();
509
+ const wc = waitController;
510
+ cancellableWait(gap, wc.signal).then(() => {
511
+ if (waitController === wc) waitController = void 0;
512
+ startExecution();
513
+ });
514
+ } else {
515
+ startExecution();
516
+ }
517
+ })
518
+ );
519
+ const abort = () => {
520
+ waitController?.abort();
521
+ waitController = void 0;
522
+ currentController?.abort();
523
+ currentController = void 0;
524
+ const r = currentResolve;
525
+ currentResolve = void 0;
526
+ if (currentState.kind !== "Idle") emit(_abortedNil);
527
+ r?.(_abortedNil);
528
+ };
529
+ return {
530
+ get state() {
531
+ return currentState;
532
+ },
533
+ run,
534
+ abort,
535
+ subscribe: (cb) => {
536
+ subscribers.add(cb);
537
+ if (currentState.kind !== "Idle") cb(currentState);
538
+ return () => subscribers.delete(cb);
539
+ }
540
+ };
541
+ };
542
+ var makeExclusive = (op, cooldown, retryOptions, timeoutOptions) => {
543
+ let currentState = _idle;
544
+ let currentController;
545
+ let currentResolve;
546
+ let cooldownTimer;
547
+ const subscribers = /* @__PURE__ */ new Set();
548
+ const emit = (state) => {
549
+ currentState = state;
550
+ subscribers.forEach((cb) => cb(state));
551
+ };
552
+ const run = (input) => {
553
+ if (currentController !== void 0 || cooldownTimer !== void 0) {
554
+ return Deferred.fromPromise(Promise.resolve(_droppedNil));
555
+ }
556
+ return Deferred.fromPromise(
557
+ new Promise((resolve) => {
558
+ currentResolve = resolve;
559
+ currentController = new AbortController();
560
+ const controller = currentController;
561
+ emit(_pending);
562
+ const onRetrying = retryOptions ? (r) => {
563
+ if (currentController === controller) emit(r);
564
+ } : void 0;
565
+ execute(op, input, controller, retryOptions, timeoutOptions, onRetrying).then((outcome) => {
566
+ if (currentController !== controller) return;
567
+ const r = currentResolve;
568
+ currentResolve = void 0;
569
+ currentController = void 0;
570
+ emit(outcome);
571
+ r?.(outcome);
572
+ if (cooldown !== void 0 && cooldown > 0) {
573
+ cooldownTimer = setTimeout(() => {
574
+ cooldownTimer = void 0;
575
+ }, cooldown);
576
+ }
577
+ });
578
+ })
579
+ );
580
+ };
581
+ const abort = () => {
582
+ if (cooldownTimer !== void 0) {
583
+ clearTimeout(cooldownTimer);
584
+ cooldownTimer = void 0;
585
+ }
586
+ currentController?.abort();
587
+ currentController = void 0;
588
+ const r = currentResolve;
589
+ currentResolve = void 0;
590
+ if (currentState.kind !== "Idle") emit(_abortedNil);
591
+ r?.(_abortedNil);
592
+ };
593
+ return {
594
+ get state() {
595
+ return currentState;
596
+ },
597
+ run,
598
+ abort,
599
+ subscribe: (cb) => {
600
+ subscribers.add(cb);
601
+ if (currentState.kind !== "Idle") cb(currentState);
602
+ return () => subscribers.delete(cb);
603
+ }
604
+ };
605
+ };
606
+ var makeQueue = (op, maxSize, overflow, concurrency, dedupe, retryOptions, timeoutOptions) => {
607
+ const maxConcurrency = concurrency ?? 1;
608
+ let currentState = _idle;
609
+ let generation = 0;
610
+ let inFlight = 0;
611
+ const queue = [];
612
+ const inflightControllers = /* @__PURE__ */ new Set();
613
+ const inflightResolvers = [];
614
+ const subscribers = /* @__PURE__ */ new Set();
615
+ const emit = (state) => {
616
+ currentState = state;
617
+ subscribers.forEach((cb) => cb(state));
618
+ };
619
+ const startOne = (input, resolve, myGeneration) => {
620
+ inFlight++;
621
+ const controller = new AbortController();
622
+ inflightControllers.add(controller);
623
+ inflightResolvers.push(resolve);
624
+ emit(_pending);
625
+ const onRetrying = retryOptions ? (r) => {
626
+ if (generation === myGeneration && inflightControllers.has(controller)) emit(r);
627
+ } : void 0;
628
+ execute(op, input, controller, retryOptions, timeoutOptions, onRetrying).then((outcome) => {
629
+ inflightControllers.delete(controller);
630
+ const idx = inflightResolvers.indexOf(resolve);
631
+ if (idx !== -1) inflightResolvers.splice(idx, 1);
632
+ if (generation !== myGeneration) {
633
+ resolve(_abortedNil);
634
+ return;
635
+ }
636
+ inFlight--;
637
+ emit(outcome);
638
+ resolve(outcome);
639
+ if (queue.length > 0) {
640
+ const next = queue.shift();
641
+ startOne(next.input, next.resolve, generation);
642
+ }
643
+ });
644
+ };
645
+ const run = (input) => {
646
+ const myGeneration = generation;
647
+ if (dedupe !== void 0) {
648
+ const idx = queue.findIndex((item) => dedupe(input, item.input));
649
+ if (idx !== -1) {
650
+ const dup = queue.splice(idx, 1)[0];
651
+ dup.resolve(_droppedNil);
652
+ }
653
+ }
654
+ if (inFlight < maxConcurrency) {
655
+ return Deferred.fromPromise(
656
+ new Promise((resolve) => {
657
+ startOne(input, resolve, myGeneration);
658
+ })
659
+ );
660
+ }
661
+ if (maxSize === void 0 || queue.length < maxSize) {
662
+ return Deferred.fromPromise(
663
+ new Promise((resolve) => {
664
+ queue.push({ input, resolve });
665
+ emit({ kind: "Queued", position: queue.length - 1 });
666
+ })
667
+ );
668
+ }
669
+ if (overflow === "replace-last") {
670
+ return Deferred.fromPromise(
671
+ new Promise((resolve) => {
672
+ const tail = queue.pop();
673
+ tail.resolve(_evictedNil);
674
+ queue.push({ input, resolve });
675
+ emit({ kind: "Queued", position: queue.length - 1 });
676
+ })
677
+ );
678
+ }
679
+ return Deferred.fromPromise(Promise.resolve(_droppedNil));
680
+ };
681
+ const abort = () => {
682
+ generation++;
683
+ inflightControllers.forEach((c) => c.abort());
684
+ inflightControllers.clear();
685
+ const toResolve = inflightResolvers.splice(0);
686
+ const queuedResolvers = queue.splice(0).map((item) => item.resolve);
687
+ inFlight = 0;
688
+ if (currentState.kind !== "Idle") emit(_abortedNil);
689
+ toResolve.forEach((r) => r(_abortedNil));
690
+ queuedResolvers.forEach((r) => r(_abortedNil));
691
+ };
692
+ return {
693
+ get state() {
694
+ return currentState;
695
+ },
696
+ run,
697
+ abort,
698
+ subscribe: (cb) => {
699
+ subscribers.add(cb);
700
+ if (currentState.kind !== "Idle") cb(currentState);
701
+ return () => subscribers.delete(cb);
702
+ }
703
+ };
704
+ };
705
+ var makeBuffered = (op, size, retryOptions, timeoutOptions) => {
706
+ const bufferSize = size ?? 1;
707
+ let currentState = _idle;
708
+ let currentController;
709
+ let currentResolve;
710
+ const buffer = [];
711
+ const subscribers = /* @__PURE__ */ new Set();
712
+ const emit = (state) => {
713
+ currentState = state;
714
+ subscribers.forEach((cb) => cb(state));
715
+ };
716
+ const startRun = (input, resolve) => {
717
+ currentResolve = resolve;
718
+ currentController = new AbortController();
719
+ const controller = currentController;
720
+ emit(_pending);
721
+ const onRetrying = retryOptions ? (r) => {
722
+ if (currentController === controller) emit(r);
723
+ } : void 0;
724
+ execute(op, input, controller, retryOptions, timeoutOptions, onRetrying).then((outcome) => {
725
+ if (currentController !== controller) return;
726
+ const r = currentResolve;
727
+ currentResolve = void 0;
728
+ currentController = void 0;
729
+ emit(outcome);
730
+ r?.(outcome);
731
+ if (buffer.length > 0) {
732
+ const next = buffer.shift();
733
+ startRun(next.input, next.resolve);
734
+ }
735
+ });
736
+ };
737
+ const run = (input) => Deferred.fromPromise(
738
+ new Promise((resolve) => {
739
+ if (currentController === void 0) {
740
+ startRun(input, resolve);
741
+ } else if (buffer.length < bufferSize) {
742
+ buffer.push({ input, resolve });
743
+ emit({ kind: "Queued", position: buffer.length - 1 });
744
+ } else {
745
+ const evicted = buffer.shift();
746
+ evicted.resolve(_evictedNil);
747
+ buffer.push({ input, resolve });
748
+ emit({ kind: "Queued", position: buffer.length - 1 });
749
+ }
750
+ })
751
+ );
752
+ const abort = () => {
753
+ currentController?.abort();
754
+ currentController = void 0;
755
+ const cr = currentResolve;
756
+ currentResolve = void 0;
757
+ const bufferedResolvers = buffer.splice(0).map((item) => item.resolve);
758
+ if (currentState.kind !== "Idle") emit(_abortedNil);
759
+ cr?.(_abortedNil);
760
+ bufferedResolvers.forEach((r) => r(_abortedNil));
761
+ };
762
+ return {
763
+ get state() {
764
+ return currentState;
765
+ },
766
+ run,
767
+ abort,
768
+ subscribe: (cb) => {
769
+ subscribers.add(cb);
770
+ if (currentState.kind !== "Idle") cb(currentState);
771
+ return () => subscribers.delete(cb);
772
+ }
773
+ };
774
+ };
775
+ var makeDebounced = (op, ms, leading, maxWait, retryOptions, timeoutOptions) => {
776
+ let currentState = _idle;
777
+ let currentController;
778
+ let currentResolve;
779
+ let leadingController;
780
+ let leadingResolve;
781
+ let pendingResolve;
782
+ let timerId;
783
+ let pendingInput;
784
+ let firstCallAt = 0;
785
+ const subscribers = /* @__PURE__ */ new Set();
786
+ const emit = (state) => {
787
+ currentState = state;
788
+ subscribers.forEach((cb) => cb(state));
789
+ };
790
+ const fireLeading = (input, resolve) => {
791
+ leadingController = new AbortController();
792
+ const controller = leadingController;
793
+ leadingResolve = resolve;
794
+ emit(_pending);
795
+ const onRetrying = retryOptions ? (r) => {
796
+ if (leadingController === controller) emit(r);
797
+ } : void 0;
798
+ execute(op, input, controller, retryOptions, timeoutOptions, onRetrying).then((outcome) => {
799
+ if (leadingController !== controller) return;
800
+ const r = leadingResolve;
801
+ leadingResolve = void 0;
802
+ leadingController = void 0;
803
+ emit(outcome);
804
+ r?.(outcome);
805
+ });
806
+ };
807
+ const fireTrailing = () => {
808
+ timerId = void 0;
809
+ firstCallAt = 0;
810
+ const capturedResolve = pendingResolve;
811
+ pendingResolve = void 0;
812
+ if (capturedResolve === void 0) return;
813
+ currentResolve = capturedResolve;
814
+ const toRun = pendingInput;
815
+ pendingInput = void 0;
816
+ currentController = new AbortController();
817
+ const controller = currentController;
818
+ emit(_pending);
819
+ const onRetrying = retryOptions ? (r) => {
820
+ if (currentController === controller) emit(r);
821
+ } : void 0;
822
+ execute(op, toRun, controller, retryOptions, timeoutOptions, onRetrying).then((outcome) => {
823
+ if (currentController !== controller) return;
824
+ const r = currentResolve;
825
+ currentResolve = void 0;
826
+ currentController = void 0;
827
+ emit(outcome);
828
+ r?.(outcome);
829
+ });
830
+ };
831
+ const scheduleTrailing = () => {
832
+ if (timerId !== void 0) clearTimeout(timerId);
833
+ let delay = ms;
834
+ if (maxWait !== void 0 && firstCallAt > 0) {
835
+ const maxDelay = firstCallAt + maxWait - Date.now();
836
+ delay = Math.min(ms, Math.max(0, maxDelay));
837
+ }
838
+ timerId = setTimeout(fireTrailing, delay);
839
+ };
840
+ const inDebounceWindow = () => timerId !== void 0 || leadingController !== void 0 || currentController !== void 0;
841
+ const run = (input) => Deferred.fromPromise(
842
+ new Promise((resolve) => {
843
+ if (!inDebounceWindow()) {
844
+ firstCallAt = Date.now();
845
+ if (leading) {
846
+ fireLeading(input, resolve);
847
+ scheduleTrailing();
848
+ } else {
849
+ pendingInput = input;
850
+ pendingResolve = resolve;
851
+ scheduleTrailing();
852
+ }
853
+ } else {
854
+ const prev = pendingResolve;
855
+ pendingInput = input;
856
+ pendingResolve = resolve;
857
+ prev?.(_evictedNil);
858
+ scheduleTrailing();
859
+ }
860
+ })
861
+ );
862
+ const abort = () => {
863
+ if (timerId !== void 0) {
864
+ clearTimeout(timerId);
865
+ timerId = void 0;
866
+ pendingInput = void 0;
867
+ firstCallAt = 0;
868
+ }
869
+ const pr = pendingResolve;
870
+ pendingResolve = void 0;
871
+ const cr = currentResolve;
872
+ currentResolve = void 0;
873
+ currentController?.abort();
874
+ currentController = void 0;
875
+ const lr = leadingResolve;
876
+ leadingResolve = void 0;
877
+ leadingController?.abort();
878
+ leadingController = void 0;
879
+ if (currentState.kind !== "Idle") emit(_abortedNil);
880
+ pr?.(_abortedNil);
881
+ cr?.(_abortedNil);
882
+ lr?.(_abortedNil);
883
+ };
884
+ return {
885
+ get state() {
886
+ return currentState;
887
+ },
888
+ run,
889
+ abort,
890
+ subscribe: (cb) => {
891
+ subscribers.add(cb);
892
+ if (currentState.kind !== "Idle") cb(currentState);
893
+ return () => subscribers.delete(cb);
894
+ }
895
+ };
896
+ };
897
+ var makeThrottled = (op, ms, trailing, retryOptions, timeoutOptions) => {
898
+ let currentState = _idle;
899
+ let currentController;
900
+ let currentResolve;
901
+ let cooldownTimer;
902
+ let pendingInput;
903
+ let pendingResolve;
904
+ const subscribers = /* @__PURE__ */ new Set();
905
+ const emit = (state) => {
906
+ currentState = state;
907
+ subscribers.forEach((cb) => cb(state));
908
+ };
909
+ const fireOp = (input, resolve) => {
910
+ currentResolve = resolve;
911
+ currentController = new AbortController();
912
+ const controller = currentController;
913
+ emit(_pending);
914
+ const onRetrying = retryOptions ? (r) => {
915
+ if (currentController === controller) emit(r);
916
+ } : void 0;
917
+ execute(op, input, controller, retryOptions, timeoutOptions, onRetrying).then((outcome) => {
918
+ if (currentController !== controller) return;
919
+ const r = currentResolve;
920
+ currentResolve = void 0;
921
+ currentController = void 0;
922
+ emit(outcome);
923
+ r?.(outcome);
924
+ });
925
+ };
926
+ const startCooldown = () => {
927
+ cooldownTimer = setTimeout(() => {
928
+ cooldownTimer = void 0;
929
+ if (trailing && pendingInput !== void 0) {
930
+ const input = pendingInput;
931
+ const resolve = pendingResolve;
932
+ pendingInput = void 0;
933
+ pendingResolve = void 0;
934
+ fireOp(input, resolve);
935
+ startCooldown();
936
+ }
937
+ }, ms);
938
+ };
939
+ const run = (input) => {
940
+ if (cooldownTimer !== void 0) {
941
+ if (!trailing) {
942
+ return Deferred.fromPromise(Promise.resolve(_droppedNil));
943
+ }
944
+ return Deferred.fromPromise(
945
+ new Promise((resolve) => {
946
+ const prev = pendingResolve;
947
+ pendingInput = input;
948
+ pendingResolve = resolve;
949
+ prev?.(_evictedNil);
950
+ })
951
+ );
952
+ }
953
+ return Deferred.fromPromise(
954
+ new Promise((resolve) => {
955
+ fireOp(input, resolve);
956
+ startCooldown();
957
+ })
958
+ );
959
+ };
960
+ const abort = () => {
961
+ if (cooldownTimer !== void 0) {
962
+ clearTimeout(cooldownTimer);
963
+ cooldownTimer = void 0;
964
+ }
965
+ currentController?.abort();
966
+ currentController = void 0;
967
+ const cr = currentResolve;
968
+ currentResolve = void 0;
969
+ const pr = pendingResolve;
970
+ pendingResolve = void 0;
971
+ pendingInput = void 0;
972
+ if (currentState.kind !== "Idle") emit(_abortedNil);
973
+ cr?.(_abortedNil);
974
+ pr?.(_abortedNil);
975
+ };
976
+ return {
977
+ get state() {
978
+ return currentState;
979
+ },
980
+ run,
981
+ abort,
982
+ subscribe: (cb) => {
983
+ subscribers.add(cb);
984
+ if (currentState.kind !== "Idle") cb(currentState);
985
+ return () => subscribers.delete(cb);
986
+ }
987
+ };
988
+ };
989
+ var makeConcurrent = (op, n, overflow, retryOptions, timeoutOptions) => {
990
+ let currentState = _idle;
991
+ let inflight = 0;
992
+ let generation = 0;
993
+ const controllers = /* @__PURE__ */ new Set();
994
+ const inflightResolvers = [];
995
+ const overflowQueue = [];
996
+ const subscribers = /* @__PURE__ */ new Set();
997
+ const emit = (state) => {
998
+ currentState = state;
999
+ subscribers.forEach((cb) => cb(state));
1000
+ };
1001
+ const startOne = (input, resolve, myGeneration) => {
1002
+ inflight++;
1003
+ const controller = new AbortController();
1004
+ controllers.add(controller);
1005
+ inflightResolvers.push(resolve);
1006
+ emit(_pending);
1007
+ const onRetrying = retryOptions ? (r) => {
1008
+ if (generation === myGeneration && controllers.has(controller)) emit(r);
1009
+ } : void 0;
1010
+ execute(op, input, controller, retryOptions, timeoutOptions, onRetrying).then((outcome) => {
1011
+ controllers.delete(controller);
1012
+ const idx = inflightResolvers.indexOf(resolve);
1013
+ if (idx !== -1) inflightResolvers.splice(idx, 1);
1014
+ if (generation !== myGeneration) {
1015
+ resolve(_abortedNil);
1016
+ return;
1017
+ }
1018
+ inflight--;
1019
+ emit(outcome);
1020
+ resolve(outcome);
1021
+ if (overflowQueue.length > 0) {
1022
+ const next = overflowQueue.shift();
1023
+ startOne(next.input, next.resolve, generation);
1024
+ }
1025
+ });
1026
+ };
1027
+ const run = (input) => {
1028
+ const myGeneration = generation;
1029
+ if (inflight < n) {
1030
+ return Deferred.fromPromise(
1031
+ new Promise((resolve) => {
1032
+ startOne(input, resolve, myGeneration);
1033
+ })
1034
+ );
1035
+ }
1036
+ if (overflow === "drop") {
1037
+ return Deferred.fromPromise(Promise.resolve(_droppedNil));
1038
+ }
1039
+ return Deferred.fromPromise(
1040
+ new Promise((resolve) => {
1041
+ overflowQueue.push({ input, resolve });
1042
+ emit({ kind: "Queued", position: overflowQueue.length - 1 });
1043
+ })
1044
+ );
1045
+ };
1046
+ const abort = () => {
1047
+ generation++;
1048
+ controllers.forEach((c) => c.abort());
1049
+ controllers.clear();
1050
+ const toResolve = inflightResolvers.splice(0);
1051
+ const queuedResolvers = overflowQueue.splice(0).map((item) => item.resolve);
1052
+ inflight = 0;
1053
+ if (currentState.kind !== "Idle") emit(_abortedNil);
1054
+ toResolve.forEach((r) => r(_abortedNil));
1055
+ queuedResolvers.forEach((r) => r(_abortedNil));
1056
+ };
1057
+ return {
1058
+ get state() {
1059
+ return currentState;
1060
+ },
1061
+ run,
1062
+ abort,
1063
+ subscribe: (cb) => {
1064
+ subscribers.add(cb);
1065
+ if (currentState.kind !== "Idle") cb(currentState);
1066
+ return () => subscribers.delete(cb);
1067
+ }
1068
+ };
1069
+ };
1070
+ var makeKeyed = (op, keyFn, perKey, timeoutOptions) => {
1071
+ const stateMap = /* @__PURE__ */ new Map();
1072
+ const slots = /* @__PURE__ */ new Map();
1073
+ const subscribers = /* @__PURE__ */ new Set();
1074
+ const emitSnapshot = () => {
1075
+ const snapshot = new Map(stateMap);
1076
+ subscribers.forEach((cb) => cb(snapshot));
1077
+ };
1078
+ const run = (input) => {
1079
+ const k = keyFn(input);
1080
+ if (slots.has(k)) {
1081
+ if (perKey === "exclusive") {
1082
+ return Deferred.fromPromise(Promise.resolve(_droppedNil));
1083
+ }
1084
+ const existing = slots.get(k);
1085
+ existing.controller.abort();
1086
+ const prev = existing.resolve;
1087
+ slots.delete(k);
1088
+ prev(_replacedNil);
1089
+ }
1090
+ return Deferred.fromPromise(
1091
+ new Promise((resolve) => {
1092
+ const controller = new AbortController();
1093
+ slots.set(k, { controller, resolve });
1094
+ stateMap.set(k, _pending);
1095
+ emitSnapshot();
1096
+ execute(op, input, controller, void 0, timeoutOptions, void 0).then((outcome) => {
1097
+ const slot = slots.get(k);
1098
+ if (!slot || slot.controller !== controller) {
1099
+ resolve(_abortedNil);
1100
+ return;
1101
+ }
1102
+ slots.delete(k);
1103
+ stateMap.set(k, outcome);
1104
+ emitSnapshot();
1105
+ resolve(outcome);
1106
+ });
1107
+ })
1108
+ );
1109
+ };
1110
+ const abort = (key) => {
1111
+ if (key !== void 0) {
1112
+ const slot = slots.get(key);
1113
+ if (slot) {
1114
+ slot.controller.abort();
1115
+ const r = slot.resolve;
1116
+ slots.delete(key);
1117
+ stateMap.set(key, _abortedNil);
1118
+ emitSnapshot();
1119
+ r(_abortedNil);
1120
+ }
1121
+ } else {
1122
+ const toResolve = [];
1123
+ for (const [k, slot] of slots) {
1124
+ slot.controller.abort();
1125
+ toResolve.push(slot.resolve);
1126
+ stateMap.set(k, _abortedNil);
1127
+ }
1128
+ slots.clear();
1129
+ if (toResolve.length > 0) emitSnapshot();
1130
+ toResolve.forEach((r) => r(_abortedNil));
1131
+ }
1132
+ };
1133
+ return {
1134
+ get state() {
1135
+ return new Map(stateMap);
1136
+ },
1137
+ run,
1138
+ abort,
1139
+ subscribe: (cb) => {
1140
+ subscribers.add(cb);
1141
+ if (stateMap.size > 0) cb(new Map(stateMap));
1142
+ return () => subscribers.delete(cb);
1143
+ }
1144
+ };
1145
+ };
1146
+ var makeOnce = (op, retryOptions, timeoutOptions) => {
1147
+ let currentState = _idle;
1148
+ let currentController;
1149
+ let currentResolve;
1150
+ const subscribers = /* @__PURE__ */ new Set();
1151
+ const emit = (state) => {
1152
+ currentState = state;
1153
+ subscribers.forEach((cb) => cb(state));
1154
+ };
1155
+ const run = (input) => {
1156
+ if (currentState.kind !== "Idle") {
1157
+ return Deferred.fromPromise(Promise.resolve(_droppedNil));
1158
+ }
1159
+ return Deferred.fromPromise(
1160
+ new Promise((resolve) => {
1161
+ currentResolve = resolve;
1162
+ currentController = new AbortController();
1163
+ const controller = currentController;
1164
+ emit(_pending);
1165
+ const onRetrying = retryOptions ? (r) => {
1166
+ if (currentController === controller) emit(r);
1167
+ } : void 0;
1168
+ execute(op, input, controller, retryOptions, timeoutOptions, onRetrying).then((outcome) => {
1169
+ if (currentController !== controller) return;
1170
+ const r = currentResolve;
1171
+ currentResolve = void 0;
1172
+ currentController = void 0;
1173
+ emit(outcome);
1174
+ r?.(outcome);
1175
+ });
1176
+ })
1177
+ );
1178
+ };
1179
+ const abort = () => {
1180
+ currentController?.abort();
1181
+ currentController = void 0;
1182
+ const r = currentResolve;
1183
+ currentResolve = void 0;
1184
+ if (currentState.kind !== "Idle") emit(_abortedNil);
1185
+ r?.(_abortedNil);
1186
+ };
1187
+ return {
1188
+ get state() {
1189
+ return currentState;
1190
+ },
1191
+ run,
1192
+ abort,
1193
+ subscribe: (cb) => {
1194
+ subscribers.add(cb);
1195
+ if (currentState.kind !== "Idle") cb(currentState);
1196
+ return () => subscribers.delete(cb);
1197
+ }
1198
+ };
1199
+ };
1200
+
1201
+ // src/Core/Op.ts
1202
+ var Op;
1203
+ ((Op2) => {
1204
+ Op2.nil = (reason) => ({ kind: "Nil", reason });
1205
+ Op2.create = (factory, onError) => ({
1206
+ _factory: (input, signal) => Deferred.fromPromise(
1207
+ factory(input, signal).then((value) => Result.ok(value)).catch((e) => signal.aborted ? null : Result.err(onError(e)))
1208
+ )
1209
+ });
1210
+ Op2.ok = (value) => ({ kind: "Ok", value });
1211
+ Op2.err = (error) => ({ kind: "Err", error });
1212
+ Op2.isOk = (outcome) => outcome.kind === "Ok";
1213
+ Op2.isErr = (outcome) => outcome.kind === "Err";
1214
+ Op2.isNil = (outcome) => outcome.kind === "Nil";
1215
+ Op2.match = (cases) => (outcome) => {
1216
+ if (outcome.kind === "Ok") return cases.ok(outcome.value);
1217
+ if (outcome.kind === "Err") return cases.err(outcome.error);
1218
+ return cases.nil();
1219
+ };
1220
+ Op2.fold = (onErr, onOk, onNil) => (outcome) => {
1221
+ if (outcome.kind === "Ok") return onOk(outcome.value);
1222
+ if (outcome.kind === "Err") return onErr(outcome.error);
1223
+ return onNil();
1224
+ };
1225
+ Op2.getOrElse = (defaultValue) => (outcome) => outcome.kind === "Ok" ? outcome.value : defaultValue();
1226
+ Op2.map = (f) => (outcome) => outcome.kind === "Ok" ? (0, Op2.ok)(f(outcome.value)) : outcome;
1227
+ Op2.mapError = (f) => (outcome) => outcome.kind === "Err" ? (0, Op2.err)(f(outcome.error)) : outcome;
1228
+ Op2.chain = (f) => (outcome) => outcome.kind === "Ok" ? f(outcome.value) : outcome;
1229
+ Op2.tap = (f) => (outcome) => {
1230
+ if (outcome.kind === "Ok") f(outcome.value);
1231
+ return outcome;
1232
+ };
1233
+ Op2.recover = (f) => (outcome) => outcome.kind === "Err" ? f(outcome.error) : outcome;
1234
+ Op2.toResult = (onNil) => (outcome) => {
1235
+ if (outcome.kind === "Ok") return Result.ok(outcome.value);
1236
+ if (outcome.kind === "Err") return Result.err(outcome.error);
1237
+ return Result.err(onNil());
1238
+ };
1239
+ Op2.toMaybe = (outcome) => outcome.kind === "Ok" ? Maybe.some(outcome.value) : Maybe.none();
1240
+ Op2.all = (invocations) => Deferred.fromPromise(Promise.all(invocations.map(Deferred.toPromise)));
1241
+ Op2.race = (invocations) => Deferred.fromPromise(Promise.race(invocations.map(Deferred.toPromise)));
1242
+ function interpret(op, options) {
1243
+ const { strategy, retry: retryOptions, timeout: timeoutOptions } = options;
1244
+ switch (strategy) {
1245
+ case "once":
1246
+ return makeOnce(op, retryOptions, timeoutOptions);
1247
+ case "restartable":
1248
+ return makeRestartable(op, options.minInterval, retryOptions, timeoutOptions);
1249
+ case "exclusive":
1250
+ return makeExclusive(op, options.cooldown, retryOptions, timeoutOptions);
1251
+ case "queue":
1252
+ return makeQueue(
1253
+ op,
1254
+ options.maxSize,
1255
+ options.overflow,
1256
+ options.concurrency,
1257
+ options.dedupe,
1258
+ retryOptions,
1259
+ timeoutOptions
1260
+ );
1261
+ case "buffered":
1262
+ return makeBuffered(op, options.size, retryOptions, timeoutOptions);
1263
+ case "debounced":
1264
+ return makeDebounced(op, options.ms ?? 0, options.leading ?? false, options.maxWait, retryOptions, timeoutOptions);
1265
+ case "throttled":
1266
+ return makeThrottled(op, options.ms ?? 0, options.trailing ?? false, retryOptions, timeoutOptions);
1267
+ case "concurrent":
1268
+ return makeConcurrent(
1269
+ op,
1270
+ options.n ?? 1,
1271
+ options.overflow ?? "drop",
1272
+ retryOptions,
1273
+ timeoutOptions
1274
+ );
1275
+ case "keyed":
1276
+ return makeKeyed(op, options.key ?? ((i) => i), options.perKey ?? "exclusive", timeoutOptions);
1277
+ }
1278
+ }
1279
+ Op2.interpret = interpret;
1280
+ })(Op || (Op = {}));
1281
+
409
1282
  // src/Core/Optional.ts
410
1283
  var Optional;
411
1284
  ((Optional2) => {
@@ -760,17 +1633,6 @@ var TaskMaybe;
760
1633
  })(TaskMaybe || (TaskMaybe = {}));
761
1634
 
762
1635
  // src/Core/TaskResult.ts
763
- var cancellableWait = (ms, signal) => {
764
- if (ms <= 0) return Promise.resolve();
765
- if (!signal) return new Promise((r) => setTimeout(r, ms));
766
- return new Promise((resolve) => {
767
- const id = setTimeout(resolve, ms);
768
- signal.addEventListener("abort", () => {
769
- clearTimeout(id);
770
- resolve();
771
- }, { once: true });
772
- });
773
- };
774
1636
  var TaskResult;
775
1637
  ((TaskResult2) => {
776
1638
  TaskResult2.ok = (value) => Task.resolve(Result.ok(value));
@@ -790,75 +1652,6 @@ var TaskResult;
790
1652
  )(data);
791
1653
  TaskResult2.getOrElse = (defaultValue) => (data) => Task.map(Result.getOrElse(defaultValue))(data);
792
1654
  TaskResult2.tap = (f) => (data) => Task.map(Result.tap(f))(data);
793
- TaskResult2.retry = (options) => (data) => Task.from((signal) => {
794
- const { attempts, backoff, when: shouldRetry } = options;
795
- const getDelay = (n) => backoff === void 0 ? 0 : typeof backoff === "function" ? backoff(n) : backoff;
796
- const run = (left) => Deferred.toPromise(data(signal)).then((result) => {
797
- if (Result.isOk(result)) return result;
798
- if (left <= 1) return result;
799
- if (shouldRetry !== void 0 && !shouldRetry(result.error)) {
800
- return result;
801
- }
802
- if (signal?.aborted) return result;
803
- const ms = getDelay(attempts - left + 1);
804
- return cancellableWait(ms, signal).then(() => {
805
- if (signal?.aborted) return result;
806
- return run(left - 1);
807
- });
808
- });
809
- return run(attempts);
810
- });
811
- TaskResult2.pollUntil = (options) => (task) => Task.from((signal) => {
812
- const { when: predicate, delay } = options;
813
- const getDelay = (attempt) => delay === void 0 ? 0 : typeof delay === "function" ? delay(attempt) : delay;
814
- const run = (attempt) => Deferred.toPromise(task(signal)).then((result) => {
815
- if (Result.isErr(result)) return result;
816
- if (predicate(result.value)) return result;
817
- if (signal?.aborted) return result;
818
- const ms = getDelay(attempt);
819
- return cancellableWait(ms, signal).then(() => {
820
- if (signal?.aborted) return result;
821
- return run(attempt + 1);
822
- });
823
- });
824
- return run(1);
825
- });
826
- TaskResult2.timeout = (ms, onTimeout) => (data) => Task.from((outerSignal) => {
827
- const controller = new AbortController();
828
- const onOuterAbort = () => controller.abort();
829
- outerSignal?.addEventListener("abort", onOuterAbort, { once: true });
830
- let timerId;
831
- return Promise.race([
832
- Deferred.toPromise(data(controller.signal)).then((result) => {
833
- clearTimeout(timerId);
834
- outerSignal?.removeEventListener("abort", onOuterAbort);
835
- return result;
836
- }),
837
- new Promise((resolve) => {
838
- timerId = setTimeout(() => {
839
- controller.abort();
840
- outerSignal?.removeEventListener("abort", onOuterAbort);
841
- resolve(Result.err(onTimeout()));
842
- }, ms);
843
- })
844
- ]);
845
- });
846
- TaskResult2.abortable = (factory, onError) => {
847
- const controller = new AbortController();
848
- const task = (outerSignal) => {
849
- if (outerSignal) {
850
- if (outerSignal.aborted) {
851
- controller.abort(outerSignal.reason);
852
- } else {
853
- outerSignal.addEventListener("abort", () => controller.abort(outerSignal.reason), { once: true });
854
- }
855
- }
856
- return Deferred.fromPromise(
857
- factory(controller.signal).then(Result.ok).catch((e) => Result.err(onError(e)))
858
- );
859
- };
860
- return { task, abort: () => controller.abort() };
861
- };
862
1655
  })(TaskResult || (TaskResult = {}));
863
1656
 
864
1657
  // src/Core/Validation.ts
@@ -895,7 +1688,7 @@ var Validation;
895
1688
  return data;
896
1689
  };
897
1690
  Validation2.recover = (fallback) => (data) => (0, Validation2.isValid)(data) ? data : fallback(data.errors);
898
- Validation2.recoverUnless = (blockedErrors, fallback) => (data) => (0, Validation2.isInvalid)(data) && !data.errors.some((err) => blockedErrors.includes(err)) ? fallback() : data;
1691
+ Validation2.recoverUnless = (blockedErrors, fallback) => (data) => (0, Validation2.isInvalid)(data) && !data.errors.some((err2) => blockedErrors.includes(err2)) ? fallback() : data;
899
1692
  Validation2.product = (first, second) => {
900
1693
  if ((0, Validation2.isValid)(first) && (0, Validation2.isValid)(second)) return (0, Validation2.valid)([first.value, second.value]);
901
1694
  const errors = [
@@ -1398,7 +2191,7 @@ var Rec;
1398
2191
  const vals = Object.values(data);
1399
2192
  const result = {};
1400
2193
  for (let i = 0; i < keys2.length; i++) {
1401
- result[keys2[i]] = f(vals[i]);
2194
+ Object.defineProperty(result, keys2[i], { value: f(vals[i]), writable: true, enumerable: true, configurable: true });
1402
2195
  }
1403
2196
  return result;
1404
2197
  };
@@ -1407,21 +2200,30 @@ var Rec;
1407
2200
  const vals = Object.values(data);
1408
2201
  const result = {};
1409
2202
  for (let i = 0; i < keys2.length; i++) {
1410
- result[keys2[i]] = f(keys2[i], vals[i]);
2203
+ Object.defineProperty(result, keys2[i], {
2204
+ value: f(keys2[i], vals[i]),
2205
+ writable: true,
2206
+ enumerable: true,
2207
+ configurable: true
2208
+ });
1411
2209
  }
1412
2210
  return result;
1413
2211
  };
1414
2212
  Rec2.filter = (predicate) => (data) => {
1415
2213
  const result = {};
1416
2214
  for (const [k, v] of Object.entries(data)) {
1417
- if (predicate(v)) result[k] = v;
2215
+ if (predicate(v)) {
2216
+ Object.defineProperty(result, k, { value: v, writable: true, enumerable: true, configurable: true });
2217
+ }
1418
2218
  }
1419
2219
  return result;
1420
2220
  };
1421
2221
  Rec2.filterWithKey = (predicate) => (data) => {
1422
2222
  const result = {};
1423
2223
  for (const [k, v] of Object.entries(data)) {
1424
- if (predicate(k, v)) result[k] = v;
2224
+ if (predicate(k, v)) {
2225
+ Object.defineProperty(result, k, { value: v, writable: true, enumerable: true, configurable: true });
2226
+ }
1425
2227
  }
1426
2228
  return result;
1427
2229
  };
@@ -1434,8 +2236,11 @@ var Rec;
1434
2236
  const result = {};
1435
2237
  for (const item of items) {
1436
2238
  const key = keyFn(item);
1437
- if (key in result) result[key].push(item);
1438
- else result[key] = [item];
2239
+ if (Object.hasOwn(result, key)) {
2240
+ result[key].push(item);
2241
+ } else {
2242
+ Object.defineProperty(result, key, { value: [item], writable: true, enumerable: true, configurable: true });
2243
+ }
1439
2244
  }
1440
2245
  return result;
1441
2246
  };
@@ -1443,7 +2248,7 @@ var Rec;
1443
2248
  const result = {};
1444
2249
  for (const key of pickedKeys) {
1445
2250
  if (Object.hasOwn(data, key)) {
1446
- result[key] = data[key];
2251
+ Object.defineProperty(result, key, { value: data[key], writable: true, enumerable: true, configurable: true });
1447
2252
  }
1448
2253
  }
1449
2254
  return result;
@@ -1453,7 +2258,12 @@ var Rec;
1453
2258
  const result = {};
1454
2259
  for (const key of Object.keys(data)) {
1455
2260
  if (!omitSet.has(key)) {
1456
- result[key] = data[key];
2261
+ Object.defineProperty(result, key, {
2262
+ value: data[key],
2263
+ writable: true,
2264
+ enumerable: true,
2265
+ configurable: true
2266
+ });
1457
2267
  }
1458
2268
  }
1459
2269
  return result;
@@ -1467,14 +2277,16 @@ var Rec;
1467
2277
  Rec2.mapKeys = (f) => (data) => {
1468
2278
  const result = {};
1469
2279
  for (const [k, v] of Object.entries(data)) {
1470
- result[f(k)] = v;
2280
+ Object.defineProperty(result, f(k), { value: v, writable: true, enumerable: true, configurable: true });
1471
2281
  }
1472
2282
  return result;
1473
2283
  };
1474
2284
  Rec2.compact = (data) => {
1475
2285
  const result = {};
1476
2286
  for (const [k, v] of Object.entries(data)) {
1477
- if (v.kind === "Some") result[k] = v.value;
2287
+ if (v.kind === "Some") {
2288
+ Object.defineProperty(result, k, { value: v.value, writable: true, enumerable: true, configurable: true });
2289
+ }
1478
2290
  }
1479
2291
  return result;
1480
2292
  };
@@ -1607,6 +2419,7 @@ var Uniq;
1607
2419
  Logged,
1608
2420
  Maybe,
1609
2421
  Num,
2422
+ Op,
1610
2423
  Optional,
1611
2424
  Predicate,
1612
2425
  Reader,