virtua 0.43.3 → 0.43.5

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.
@@ -704,7 +704,7 @@ const createScroller = (store, isHorizontal) => {
704
704
  let viewportElement;
705
705
  let scrollObserver;
706
706
  let cancelScroll;
707
- const [initialized, resolveInitialized, rejectInitialized] = createPromise();
707
+ const [initialized, resolveInitialized] = createPromise();
708
708
  const scrollOffsetKey = isHorizontal ? "scrollLeft" : "scrollTop";
709
709
  const overflowKey = isHorizontal ? "overflowX" : "overflowY";
710
710
  // The given offset will be clamped by browser
@@ -712,7 +712,11 @@ const createScroller = (store, isHorizontal) => {
712
712
  const scheduleImperativeScroll = async (getTargetOffset, smooth) => {
713
713
  // Wait for element assign. The element may be undefined if scrollRef prop is used and scroll is scheduled on mount.
714
714
  // https://github.com/inokawa/virtua/pull/733
715
- await initialized;
715
+ // https://github.com/inokawa/virtua/pull/750
716
+ const ok = await initialized;
717
+ if (!ok) {
718
+ return;
719
+ }
716
720
  if (cancelScroll) {
717
721
  // Cancel waiting scrollTo
718
722
  cancelScroll();
@@ -720,19 +724,21 @@ const createScroller = (store, isHorizontal) => {
720
724
  const waitForMeasurement = () => {
721
725
  // Wait for the scroll destination items to be measured.
722
726
  // The measurement will be done asynchronously and the timing is not predictable so we use promise.
723
- const [promise, resolve, reject] = createPromise();
724
- cancelScroll = reject;
727
+ const [promise, resolve] = createPromise();
728
+ cancelScroll = () => {
729
+ resolve(false);
730
+ };
725
731
  // Resize event may not happen when the window/tab is not visible, or during browser back in Safari.
726
732
  // We have to wait for the initial measurement to avoid failing imperative scroll on mount.
727
733
  // https://github.com/inokawa/virtua/issues/450
728
734
  if (isInitialMeasurementDone(store)) {
729
- // Reject when items around scroll destination completely measured
730
- timeout(reject, 150);
735
+ // Cancel when items around scroll destination completely measured
736
+ timeout(cancelScroll, 150);
731
737
  }
732
738
  return [
733
739
  promise,
734
740
  store.$subscribe(UPDATE_SIZE_EVENT, () => {
735
- resolve();
741
+ resolve(true);
736
742
  }),
737
743
  ];
738
744
  };
@@ -744,11 +750,10 @@ const createScroller = (store, isHorizontal) => {
744
750
  }
745
751
  const [promise, unsubscribe] = waitForMeasurement();
746
752
  try {
747
- await promise;
748
- }
749
- catch (e) {
750
- // canceled
751
- return;
753
+ if (!(await promise)) {
754
+ // canceled
755
+ return;
756
+ }
752
757
  }
753
758
  finally {
754
759
  unsubscribe();
@@ -765,11 +770,10 @@ const createScroller = (store, isHorizontal) => {
765
770
  try {
766
771
  viewportElement[scrollOffsetKey] = normalizeOffset(getTargetOffset(), isHorizontal);
767
772
  store.$update(ACTION_MANUAL_SCROLL);
768
- await promise;
769
- }
770
- catch (e) {
771
- // canceled or finished
772
- return;
773
+ if (!(await promise)) {
774
+ // canceled or finished
775
+ return;
776
+ }
773
777
  }
774
778
  finally {
775
779
  unsubscribe();
@@ -802,11 +806,11 @@ const createScroller = (store, isHorizontal) => {
802
806
  viewport[scrollOffsetKey] += jump;
803
807
  }
804
808
  });
805
- resolveInitialized();
809
+ resolveInitialized(true);
806
810
  },
807
811
  $dispose() {
808
812
  scrollObserver && scrollObserver._dispose();
809
- rejectInitialized();
813
+ resolveInitialized(false);
810
814
  },
811
815
  $scrollTo(offset) {
812
816
  scheduleImperativeScroll(() => offset);
@@ -855,7 +859,7 @@ const createWindowScroller = (store, isHorizontal) => {
855
859
  let containerElement;
856
860
  let scrollObserver;
857
861
  let cancelScroll;
858
- const [initialized, resolveInitialized, rejectInitialized] = createPromise();
862
+ const [initialized, resolveInitialized] = createPromise();
859
863
  const calcOffsetToViewport = (node, viewport, window, isHorizontal, offset = 0) => {
860
864
  // TODO calc offset only when it changes (maybe impossible)
861
865
  const offsetKey = isHorizontal ? "offsetLeft" : "offsetTop";
@@ -872,26 +876,32 @@ const createWindowScroller = (store, isHorizontal) => {
872
876
  const scheduleImperativeScroll = async (getTargetOffset, smooth) => {
873
877
  // Wait for element assign. The element may be undefined if scrollRef prop is used and scroll is scheduled on mount.
874
878
  // https://github.com/inokawa/virtua/pull/733
875
- await initialized;
879
+ // https://github.com/inokawa/virtua/pull/750
880
+ const ok = await initialized;
881
+ if (!ok) {
882
+ return;
883
+ }
876
884
  if (cancelScroll) {
877
885
  cancelScroll();
878
886
  }
879
887
  const waitForMeasurement = () => {
880
888
  // Wait for the scroll destination items to be measured.
881
889
  // The measurement will be done asynchronously and the timing is not predictable so we use promise.
882
- const [promise, resolve, reject] = createPromise();
883
- cancelScroll = reject;
890
+ const [promise, resolve] = createPromise();
891
+ cancelScroll = () => {
892
+ resolve(false);
893
+ };
884
894
  // Resize event may not happen when the window/tab is not visible, or during browser back in Safari.
885
895
  // We have to wait for the initial measurement to avoid failing imperative scroll on mount.
886
896
  // https://github.com/inokawa/virtua/issues/450
887
897
  if (isInitialMeasurementDone(store)) {
888
- // Reject when items around scroll destination completely measured
889
- timeout(reject, 150);
898
+ // Cancel when items around scroll destination completely measured
899
+ timeout(cancelScroll, 150);
890
900
  }
891
901
  return [
892
902
  promise,
893
903
  store.$subscribe(UPDATE_SIZE_EVENT, () => {
894
- resolve();
904
+ resolve(true);
895
905
  }),
896
906
  ];
897
907
  };
@@ -904,10 +914,9 @@ const createWindowScroller = (store, isHorizontal) => {
904
914
  }
905
915
  const [promise, unsubscribe] = waitForMeasurement();
906
916
  try {
907
- await promise;
908
- }
909
- catch (e) {
910
- return;
917
+ if (!(await promise)) {
918
+ return;
919
+ }
911
920
  }
912
921
  finally {
913
922
  unsubscribe();
@@ -926,10 +935,9 @@ const createWindowScroller = (store, isHorizontal) => {
926
935
  [isHorizontal ? "left" : "top"]: normalizeOffset(getTargetOffset(), isHorizontal),
927
936
  });
928
937
  store.$update(ACTION_MANUAL_SCROLL);
929
- await promise;
930
- }
931
- catch (e) {
932
- return;
938
+ if (!(await promise)) {
939
+ return;
940
+ }
933
941
  }
934
942
  finally {
935
943
  unsubscribe();
@@ -955,12 +963,12 @@ const createWindowScroller = (store, isHorizontal) => {
955
963
  window.scrollBy(isHorizontal ? jump : 0, isHorizontal ? 0 : jump);
956
964
  }
957
965
  }, () => calcOffsetToViewport(container, documentBody, window, isHorizontal));
958
- resolveInitialized();
966
+ resolveInitialized(true);
959
967
  },
960
968
  $dispose() {
961
969
  scrollObserver && scrollObserver._dispose();
962
970
  containerElement = undefined;
963
- rejectInitialized();
971
+ resolveInitialized(false);
964
972
  },
965
973
  $fixScrollJump: () => {
966
974
  scrollObserver && scrollObserver._fixScrollJump();