@heycar/heycars-map 2.0.0-switchMap1 → 2.0.0-switchMap10

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.
Files changed (122) hide show
  1. package/README.md +43 -29
  2. package/dist/index.d.ts +1 -1
  3. package/dist/index.js +1 -1
  4. package/dist/v2/App.js +5 -3
  5. package/dist/v2/Demo/DemoBusinessQuoting.js +3 -2
  6. package/dist/v2/Demo/DemoBusinessRecomendPlace.js +1 -1
  7. package/dist/v2/api/cdn.d.ts +1 -0
  8. package/dist/v2/api/cdn.js +2 -0
  9. package/dist/v2/api/googleSnapRoad.js +7 -2
  10. package/dist/v2/api/pingConnection.d.ts +9 -0
  11. package/dist/v2/api/pingConnection.js +111 -0
  12. package/dist/v2/business-components/BusinessQuotingMap/BusinessQuotingMap.js +5 -2
  13. package/dist/v2/business-components/BusinessRecomendPlaceMap/BusinessRecomendPlaceMap.d.ts +2 -2
  14. package/dist/v2/business-components/BusinessRecomendPlaceMap/BusinessRecomendPlaceMap.js +4 -3
  15. package/dist/v2/business-components/BusinessRecomendPlaceMap/index.js +3 -3
  16. package/dist/v2/business-components/BusinessReselectPlaceMap/BusinessReselectPlaceMap.d.ts +2 -2
  17. package/dist/v2/business-components/BusinessReselectPlaceMap/BusinessReselectPlaceMap.js +22 -13
  18. package/dist/v2/business-components/BusinessTaxiServiceMap/BusinessTaxiServiceMap.js +12 -5
  19. package/dist/v2/chunks/{BusinessRecomendPlaceMap.5be4fdcf.js → BusinessRecomendPlaceMap.b4d45108.js} +30 -18
  20. package/dist/v2/chunks/{Loading.fc6e75d0.js → Loading.492acbc2.js} +30 -2
  21. package/dist/v2/components/Loading/Loading.css.d.ts +5 -0
  22. package/dist/v2/components/Loading/Loading.d.ts +6 -0
  23. package/dist/v2/components/Loading/Loading.js +4 -2
  24. package/dist/v2/components/Loading/index.js +3 -2
  25. package/dist/v2/components/MapProvider/MapProvider.d.ts +2 -2
  26. package/dist/v2/components/MapProvider/MapProvider.js +23 -13
  27. package/dist/v2/css/Loading-49e58ced.css +46 -0
  28. package/dist/v2/hooks/useDrivingRoute.js +4 -0
  29. package/dist/v2/hooks/useMapGCJ02.js +4 -37
  30. package/dist/v2/hooks/useMapGeometry.d.ts +1 -3
  31. package/dist/v2/hooks/useMapGeometry.js +4 -4
  32. package/dist/v2/hooks/useMapGraspRoad.js +1 -2
  33. package/dist/v2/hooks/useMapInChina.d.ts +3 -15
  34. package/dist/v2/hooks/useMapInChina.js +11 -16
  35. package/dist/v2/hooks/useMapLoader.d.ts +13 -7
  36. package/dist/v2/hooks/useMapLoader.js +144 -82
  37. package/dist/v2/hooks/useMapPlace.d.ts +1 -0
  38. package/dist/v2/hooks/useMapPlace.js +52 -44
  39. package/dist/v2/hooks/useMapRecomendPlace.d.ts +1 -0
  40. package/dist/v2/hooks/useMapRecomendPlace.js +75 -67
  41. package/dist/v2/hooks/useMapSupplier.d.ts +8 -0
  42. package/dist/v2/hooks/useMapSupplier.js +17 -0
  43. package/dist/v2/hooks/usePingConnection.d.ts +10 -0
  44. package/dist/v2/hooks/usePingConnection.js +26 -0
  45. package/dist/v2/hooks-business/useBusinessQuotingMap.d.ts +2 -0
  46. package/dist/v2/hooks-business/useBusinessQuotingMap.js +7 -2
  47. package/dist/v2/hooks-business/useBusinessRecomendPlaceMap.js +1 -1
  48. package/dist/v2/index.js +1 -1
  49. package/dist/v2/types/global.d.ts +3 -0
  50. package/dist/v2/types/interface.d.ts +1 -1
  51. package/dist/v2/utils/ManualAbortError.d.ts +3 -0
  52. package/dist/v2/utils/ManualAbortError.js +14 -0
  53. package/dist/v2/utils/helper.d.ts +2 -0
  54. package/dist/v2/utils/helper.js +29 -0
  55. package/dist/v2/utils/log.js +1 -1
  56. package/dist/v2/utils/patchGoogleMapLoader.d.ts +2 -0
  57. package/dist/v2/utils/patchGoogleMapLoader.js +64 -0
  58. package/dist/v3/App.js +5 -3
  59. package/dist/v3/Demo/DemoBusinessQuoting.js +4 -3
  60. package/dist/v3/Demo/DemoBusinessRecomendPlace.js +1 -1
  61. package/dist/v3/api/cdn.d.ts +1 -0
  62. package/dist/v3/api/cdn.js +2 -0
  63. package/dist/v3/api/googleSnapRoad.js +7 -2
  64. package/dist/v3/api/pingConnection.d.ts +9 -0
  65. package/dist/v3/api/pingConnection.js +111 -0
  66. package/dist/v3/business-components/BusinessQuotingMap/BusinessQuotingMap.js +5 -2
  67. package/dist/v3/business-components/BusinessRecomendPlaceMap/BusinessRecomendPlaceMap.d.ts +2 -2
  68. package/dist/v3/business-components/BusinessRecomendPlaceMap/BusinessRecomendPlaceMap.js +4 -3
  69. package/dist/v3/business-components/BusinessRecomendPlaceMap/index.js +3 -3
  70. package/dist/v3/business-components/BusinessReselectPlaceMap/BusinessReselectPlaceMap.d.ts +2 -2
  71. package/dist/v3/business-components/BusinessReselectPlaceMap/BusinessReselectPlaceMap.js +22 -13
  72. package/dist/v3/business-components/BusinessTaxiServiceMap/BusinessTaxiServiceMap.js +12 -5
  73. package/dist/v3/chunks/{BusinessRecomendPlaceMap.39888296.js → BusinessRecomendPlaceMap.6ba5dbb1.js} +30 -18
  74. package/dist/v3/chunks/{Loading.a8e2f26a.js → Loading.21d8e759.js} +26 -2
  75. package/dist/v3/components/Loading/Loading.css.d.ts +5 -0
  76. package/dist/v3/components/Loading/Loading.d.ts +8 -0
  77. package/dist/v3/components/Loading/Loading.js +4 -2
  78. package/dist/v3/components/Loading/index.js +3 -2
  79. package/dist/v3/components/MapProvider/MapProvider.d.ts +6 -3
  80. package/dist/v3/components/MapProvider/MapProvider.js +21 -13
  81. package/dist/v3/css/Loading-49e58ced.css +46 -0
  82. package/dist/v3/hooks/useDrivingRoute.js +4 -0
  83. package/dist/v3/hooks/useMapGCJ02.js +4 -37
  84. package/dist/v3/hooks/useMapGeometry.d.ts +1 -3
  85. package/dist/v3/hooks/useMapGeometry.js +4 -4
  86. package/dist/v3/hooks/useMapGraspRoad.js +1 -2
  87. package/dist/v3/hooks/useMapInChina.d.ts +3 -15
  88. package/dist/v3/hooks/useMapInChina.js +11 -16
  89. package/dist/v3/hooks/useMapLoader.d.ts +13 -7
  90. package/dist/v3/hooks/useMapLoader.js +144 -82
  91. package/dist/v3/hooks/useMapPlace.d.ts +1 -0
  92. package/dist/v3/hooks/useMapPlace.js +52 -44
  93. package/dist/v3/hooks/useMapRecomendPlace.d.ts +1 -0
  94. package/dist/v3/hooks/useMapRecomendPlace.js +75 -67
  95. package/dist/v3/hooks/useMapSupplier.d.ts +8 -0
  96. package/dist/v3/hooks/useMapSupplier.js +17 -0
  97. package/dist/v3/hooks/usePingConnection.d.ts +10 -0
  98. package/dist/v3/hooks/usePingConnection.js +26 -0
  99. package/dist/v3/hooks-business/useBusinessQuotingMap.d.ts +2 -0
  100. package/dist/v3/hooks-business/useBusinessQuotingMap.js +7 -2
  101. package/dist/v3/hooks-business/useBusinessRecomendPlaceMap.js +1 -1
  102. package/dist/v3/index.js +1 -1
  103. package/dist/v3/types/global.d.ts +3 -0
  104. package/dist/v3/types/interface.d.ts +1 -1
  105. package/dist/v3/utils/ManualAbortError.d.ts +3 -0
  106. package/dist/v3/utils/ManualAbortError.js +14 -0
  107. package/dist/v3/utils/helper.d.ts +2 -0
  108. package/dist/v3/utils/helper.js +29 -0
  109. package/dist/v3/utils/log.js +1 -1
  110. package/dist/v3/utils/patchGoogleMapLoader.d.ts +2 -0
  111. package/dist/v3/utils/patchGoogleMapLoader.js +64 -0
  112. package/package.json +2 -2
  113. package/dist/v2/api/googleConnection.d.ts +0 -5
  114. package/dist/v2/api/googleConnection.js +0 -51
  115. package/dist/v2/css/Loading-523061b9.css +0 -9
  116. package/dist/v2/hooks/useGoogleConnection.d.ts +0 -7
  117. package/dist/v2/hooks/useGoogleConnection.js +0 -15
  118. package/dist/v3/api/googleConnection.d.ts +0 -5
  119. package/dist/v3/api/googleConnection.js +0 -51
  120. package/dist/v3/css/Loading-523061b9.css +0 -9
  121. package/dist/v3/hooks/useGoogleConnection.d.ts +0 -7
  122. package/dist/v3/hooks/useGoogleConnection.js +0 -15
@@ -2,7 +2,7 @@ import { load as loadAmap } from "@amap/amap-jsapi-loader";
2
2
  import { type LoaderOptions } from "@googlemaps/js-api-loader";
3
3
  import { type Ref } from "vue-demi";
4
4
  import type { VNodeChild } from "../demi-polyfill";
5
- import type { Language } from "../types/interface";
5
+ import type { ConnectionStatus, Language } from "../types/interface";
6
6
  export declare const DEFAULT_AMAP_PLUGINS: AmapLoaderProps["plugins"];
7
7
  export declare const DEFAULT_GMAP_LIBRARIES: LoaderOptions["libraries"];
8
8
  export declare enum Status {
@@ -19,7 +19,7 @@ declare global {
19
19
  };
20
20
  }
21
21
  }
22
- export interface AmapLoaderProps extends Pick<GmapLoaderProps, "fallback" | "loading" | "onChange">, Omit<AMapLoaderOptions, "key" | "version" | "plugins"> {
22
+ export interface AmapLoaderProps extends Pick<GmapLoaderProps, "fallback" | "loading" | "onSuccess" | "onFail">, Omit<AMapLoaderOptions, "key" | "version" | "plugins"> {
23
23
  apiKey: string;
24
24
  apiSecret?: string;
25
25
  apiServiceHost?: string;
@@ -29,6 +29,7 @@ export interface AmapLoaderProps extends Pick<GmapLoaderProps, "fallback" | "loa
29
29
  }
30
30
  export declare const useAmapLoader: (props: AmapLoaderProps) => {
31
31
  statusRef: import("vue-demi").ShallowRef<Status>;
32
+ pingStatusRef: import("vue-demi").ShallowRef<ConnectionStatus>;
32
33
  readyPromise: Promise<undefined>;
33
34
  };
34
35
  export interface GmapLoaderProps extends LoaderOptions {
@@ -42,19 +43,23 @@ export interface GmapLoaderProps extends LoaderOptions {
42
43
  */
43
44
  fallback?: () => VNodeChild;
44
45
  /**
45
- * Callback prop used to access `@googlemaps/js-api-loader` and `Status`.
46
- *
47
- * Note: The callback be executed multiple times in the lifecycle of the component.
46
+ * 成功回调只会触发一次
48
47
  */
49
- onChange?: (status: Status) => void;
48
+ onSuccess?: () => void;
49
+ /**
50
+ * 失败回调可能会触发多次
51
+ */
52
+ onFail?: () => void;
50
53
  }
51
54
  export declare const useGmapLoader: (props: GmapLoaderProps) => {
52
55
  statusRef: import("vue-demi").ShallowRef<Status>;
56
+ pingStatusRef: import("vue-demi").ShallowRef<ConnectionStatus>;
53
57
  readyPromise: Promise<undefined>;
54
58
  };
55
59
  export type MapSupplier = "gmap" | "amap";
56
60
  export interface UseMapLoaderOutput {
57
61
  statusRef: Ref<Status>;
62
+ pingStatusRef: Ref<ConnectionStatus>;
58
63
  readyPromise: Promise<undefined>;
59
64
  }
60
65
  export interface UseMapLoaderProps {
@@ -64,7 +69,8 @@ export interface UseMapLoaderProps {
64
69
  amapServiceHost?: string;
65
70
  supplier: MapSupplier;
66
71
  language: Language;
67
- onChange?: (status: Status) => any;
72
+ onSuccess?: () => void;
73
+ onFail?: () => void;
68
74
  }
69
75
  export declare const useMapLoader: (props: UseMapLoaderProps) => UseMapLoaderOutput;
70
76
  export {};
@@ -1,6 +1,8 @@
1
1
  import { shallowRef, computed } from "vue-demi";
2
+ import { apiPingAmap, apiPingGmap } from "../api/pingConnection.js";
2
3
  import { watchPostEffectForDeepOption } from "../utils/compare.js";
3
- import { createRunOnce } from "../utils/helper.js";
4
+ import { createOneConcurrent, sleep, createRunOnce } from "../utils/helper.js";
5
+ import { patchGoogleMapLoader } from "../utils/patchGoogleMapLoader.js";
4
6
  var commonjsGlobal = typeof globalThis !== "undefined" ? globalThis : typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : {};
5
7
  var distExports = {};
6
8
  var dist = {
@@ -537,6 +539,7 @@ class Loader {
537
539
  }
538
540
  }
539
541
  }
542
+ const MIN_MAP_RELOAD_INTERVAL = 5e3;
540
543
  const DEFAULT_AMAP_PLUGINS = [
541
544
  "AMap.Geocoder",
542
545
  "AMap.Driving",
@@ -551,118 +554,173 @@ var Status = /* @__PURE__ */ ((Status2) => {
551
554
  return Status2;
552
555
  })(Status || {});
553
556
  const useAmapLoader = (props) => {
554
- const { disableRef, onChange } = props;
557
+ const { disableRef, onSuccess, onFail } = props;
555
558
  let resolveLoader;
556
- let rejectLoader;
557
- const readyPromise = new Promise((resolve, reject) => {
559
+ const readyPromise = new Promise((resolve) => {
558
560
  resolveLoader = resolve;
559
- rejectLoader = reject;
560
561
  });
561
562
  const statusRef = shallowRef(
562
563
  "LOADING"
563
564
  /* LOADING */
564
565
  );
566
+ const pingStatusRef = shallowRef("connected");
565
567
  const optionsRef = computed(() => {
566
- const { onChange: onChange2, loading, fallback, apiKey, apiSecret, apiServiceHost, version, ...options } = props;
568
+ const {
569
+ onSuccess: onSuccess2,
570
+ onFail: onFail2,
571
+ loading,
572
+ fallback,
573
+ apiKey,
574
+ apiSecret,
575
+ apiServiceHost,
576
+ version,
577
+ ...options
578
+ } = props;
567
579
  return { ...options, key: apiKey, version: version != null ? version : "2.0" };
568
580
  });
569
- watchPostEffectForDeepOption(
570
- () => [optionsRef.value, disableRef == null ? void 0 : disableRef.value],
571
- ([options, disable]) => {
572
- if (disable)
573
- return;
574
- const setStatusAndExecuteCallback = (status) => {
575
- statusRef.value = status;
576
- if (status === "SUCCESS")
577
- resolveLoader();
578
- if (status === "FAILURE")
579
- rejectLoader();
580
- onChange == null ? void 0 : onChange(statusRef.value);
581
- };
582
- setStatusAndExecuteCallback(
583
- "LOADING"
584
- /* LOADING */
585
- );
586
- window._AMapSecurityConfig = props.apiServiceHost ? {
587
- serviceHost: props.apiServiceHost
588
- } : { securityJsCode: props.apiSecret };
589
- distExports.load(options).then(
590
- () => setStatusAndExecuteCallback(
591
- "SUCCESS"
592
- /* SUCCESS */
593
- ),
594
- () => setStatusAndExecuteCallback(
595
- "FAILURE"
596
- /* FAILURE */
597
- )
598
- );
581
+ const defineSecurityOnce = createRunOnce(() => {
582
+ window._AMapSecurityConfig = props.apiServiceHost ? {
583
+ serviceHost: props.apiServiceHost
584
+ } : { securityJsCode: props.apiSecret };
585
+ });
586
+ const reload = createOneConcurrent((options) => {
587
+ if (statusRef.value === "SUCCESS")
588
+ return Promise.resolve();
589
+ defineSecurityOnce();
590
+ distExports.reset();
591
+ statusRef.value = "LOADING";
592
+ return distExports.load(options).then(
593
+ () => {
594
+ statusRef.value = "SUCCESS";
595
+ resolveLoader();
596
+ onSuccess == null ? void 0 : onSuccess();
597
+ },
598
+ () => {
599
+ statusRef.value = "FAILURE";
600
+ onFail == null ? void 0 : onFail();
601
+ }
602
+ );
603
+ });
604
+ const keepRetry = createOneConcurrent(async () => {
605
+ if (!(disableRef == null ? void 0 : disableRef.value) && statusRef.value === "SUCCESS") {
606
+ pingStatusRef.value = "pending";
607
+ while (!(disableRef == null ? void 0 : disableRef.value) && pingStatusRef.value !== "connected") {
608
+ if (pingStatusRef.value === "unconnected")
609
+ await sleep(MIN_MAP_RELOAD_INTERVAL);
610
+ pingStatusRef.value = await apiPingAmap();
611
+ }
599
612
  }
600
- );
601
- return { statusRef, readyPromise };
613
+ const options = optionsRef.value;
614
+ while (!(disableRef == null ? void 0 : disableRef.value) && statusRef.value !== "SUCCESS") {
615
+ if (statusRef.value === "LOADING") {
616
+ await reload(options);
617
+ continue;
618
+ }
619
+ await sleep(MIN_MAP_RELOAD_INTERVAL);
620
+ const pingStatus = await apiPingAmap();
621
+ if (pingStatus === "unconnected")
622
+ continue;
623
+ await reload(options);
624
+ }
625
+ });
626
+ watchPostEffectForDeepOption(() => [optionsRef.value, disableRef == null ? void 0 : disableRef.value], keepRetry);
627
+ return { statusRef, pingStatusRef, readyPromise };
602
628
  };
603
- const loadGmapOnce = createRunOnce((opts) => {
604
- const loader = new Loader(opts);
605
- return loader.load();
606
- });
607
629
  const useGmapLoader = (props) => {
608
- const { disableRef, onChange } = props;
630
+ const { disableRef, onSuccess, onFail } = props;
609
631
  let resolveLoader;
610
- let rejectLoader;
611
- const readyPromise = new Promise((resolve, reject) => {
632
+ const readyPromise = new Promise((resolve) => {
612
633
  resolveLoader = resolve;
613
- rejectLoader = reject;
614
634
  });
615
635
  const statusRef = shallowRef(
616
636
  "LOADING"
617
637
  /* LOADING */
618
638
  );
639
+ const pingStatusRef = shallowRef("pending");
619
640
  const optionsRef = computed(() => {
620
- const { onChange: onChange2, fallback, loading, language, ...options } = props;
641
+ const { onSuccess: onSuccess2, onFail: onFail2, fallback, loading, language, ...options } = props;
621
642
  return options;
622
643
  });
623
- watchPostEffectForDeepOption(
624
- () => [optionsRef.value, disableRef == null ? void 0 : disableRef.value],
625
- ([options, disable]) => {
626
- if (disable)
627
- return;
628
- const language = navigator.language;
629
- const setStatusAndExecuteCallback = (status) => {
630
- statusRef.value = status;
631
- if (status === "SUCCESS")
632
- resolveLoader();
633
- if (status === "FAILURE")
634
- rejectLoader();
635
- onChange == null ? void 0 : onChange(statusRef.value);
636
- };
637
- setStatusAndExecuteCallback(
638
- "LOADING"
639
- /* LOADING */
640
- );
641
- loadGmapOnce({ ...options, language }).then(
642
- () => setStatusAndExecuteCallback(
643
- "SUCCESS"
644
- /* SUCCESS */
645
- ),
646
- () => setStatusAndExecuteCallback(
647
- "FAILURE"
648
- /* FAILURE */
649
- )
650
- );
644
+ const reload = createOneConcurrent((options, signal) => {
645
+ if (statusRef.value === "SUCCESS")
646
+ return Promise.resolve();
647
+ const loader = new Loader({ ...options, retries: 0 });
648
+ patchGoogleMapLoader(loader);
649
+ if (loader.status !== LoaderStatus.INITIALIZED) {
650
+ window.google = void 0;
651
+ loader.reset();
651
652
  }
652
- );
653
- return { statusRef, readyPromise };
653
+ const abortPromise = new Promise((resolve) => signal == null ? void 0 : signal.addEventListener("abort", resolve));
654
+ statusRef.value = "LOADING";
655
+ const loadPromise = loader.load().then(
656
+ () => {
657
+ if (signal == null ? void 0 : signal.aborted)
658
+ return;
659
+ statusRef.value = "SUCCESS";
660
+ resolveLoader();
661
+ onSuccess == null ? void 0 : onSuccess();
662
+ },
663
+ () => {
664
+ if (signal == null ? void 0 : signal.aborted)
665
+ return;
666
+ statusRef.value = "FAILURE";
667
+ onFail == null ? void 0 : onFail();
668
+ }
669
+ );
670
+ return Promise.race([abortPromise, loadPromise]);
671
+ });
672
+ const keepRetry = createOneConcurrent(async () => {
673
+ if (!(disableRef == null ? void 0 : disableRef.value) && statusRef.value === "SUCCESS") {
674
+ pingStatusRef.value = "pending";
675
+ while (!(disableRef == null ? void 0 : disableRef.value) && pingStatusRef.value !== "connected") {
676
+ if (pingStatusRef.value === "unconnected")
677
+ await sleep(MIN_MAP_RELOAD_INTERVAL);
678
+ pingStatusRef.value = await apiPingGmap();
679
+ }
680
+ }
681
+ const language = navigator.language;
682
+ const options = { ...optionsRef.value, language };
683
+ while (!(disableRef == null ? void 0 : disableRef.value) && statusRef.value !== "SUCCESS") {
684
+ if (statusRef.value === "FAILURE" || pingStatusRef.value === "unconnected") {
685
+ await sleep(MIN_MAP_RELOAD_INTERVAL);
686
+ pingStatusRef.value = await apiPingGmap();
687
+ if (pingStatusRef.value === "connected")
688
+ statusRef.value = "LOADING";
689
+ continue;
690
+ }
691
+ const controller = new AbortController();
692
+ const reloadPromise = reload(options, controller.signal);
693
+ pingStatusRef.value = await apiPingGmap();
694
+ if (pingStatusRef.value === "unconnected") {
695
+ controller.abort();
696
+ continue;
697
+ }
698
+ await reloadPromise;
699
+ }
700
+ });
701
+ watchPostEffectForDeepOption(() => [optionsRef.value, disableRef == null ? void 0 : disableRef.value], keepRetry);
702
+ return { statusRef, pingStatusRef, readyPromise };
654
703
  };
655
704
  const useMapLoader = (props) => {
656
- const { gmapKey, amapKey, amapSecret, amapServiceHost, language, onChange } = props;
657
- const { statusRef: amapStatusRef, readyPromise: amapReadyPromise } = useAmapLoader({
705
+ const { gmapKey, amapKey, amapSecret, amapServiceHost, language, onSuccess, onFail } = props;
706
+ const {
707
+ statusRef: amapStatusRef,
708
+ pingStatusRef: amapPingStatusRef,
709
+ readyPromise: amapReadyPromise
710
+ } = useAmapLoader({
658
711
  apiKey: amapKey,
659
712
  apiSecret: amapSecret,
660
713
  apiServiceHost: amapServiceHost,
661
714
  plugins: DEFAULT_AMAP_PLUGINS,
662
715
  disableRef: computed(() => props.supplier !== "amap"),
663
- onChange
716
+ onSuccess,
717
+ onFail
664
718
  });
665
- const { statusRef: gmapStatusRef, readyPromise: gmapReadyPromise } = useGmapLoader({
719
+ const {
720
+ statusRef: gmapStatusRef,
721
+ pingStatusRef: gmapPingStatusRef,
722
+ readyPromise: gmapReadyPromise
723
+ } = useGmapLoader({
666
724
  apiKey: gmapKey,
667
725
  language,
668
726
  version: "weekly",
@@ -670,12 +728,16 @@ const useMapLoader = (props) => {
670
728
  // region: "CN",
671
729
  libraries: DEFAULT_GMAP_LIBRARIES,
672
730
  disableRef: computed(() => props.supplier !== "gmap"),
673
- onChange
731
+ onSuccess,
732
+ onFail
674
733
  });
675
734
  return {
676
735
  get statusRef() {
677
736
  return props.supplier === "gmap" ? gmapStatusRef : amapStatusRef;
678
737
  },
738
+ get pingStatusRef() {
739
+ return props.supplier === "gmap" ? gmapPingStatusRef : amapPingStatusRef;
740
+ },
679
741
  get readyPromise() {
680
742
  return props.supplier === "gmap" ? gmapReadyPromise : amapReadyPromise;
681
743
  }
@@ -3,6 +3,7 @@ import type { Place, Point } from "../types/interface";
3
3
  export interface UseMapPlaceProps {
4
4
  pointRef: Ref<Point>;
5
5
  emptyPlaceName: string;
6
+ signal?: AbortSignal;
6
7
  onChange?: (value: Place) => any;
7
8
  }
8
9
  export declare const useAmapPlace: (props: UseMapPlaceProps) => {
@@ -1,23 +1,23 @@
1
1
  import { ref, reactive, watch } from "vue-demi";
2
2
  import { REGEO_TIMEOUT } from "../api/contants.js";
3
3
  import { createPipeTw } from "../utils/cn2tw.js";
4
- import { equalAssign } from "../utils/helper.js";
4
+ import { equalAssign, createAbortable } from "../utils/helper.js";
5
5
  import { amapPlaceName2DisplayName, gmapPlaceName2DisplayName } from "../utils/transform.js";
6
- import { useAmapInChina } from "./useMapInChina.js";
6
+ import { inChina } from "./useMapInChina.js";
7
7
  import { useMapSupplier } from "./useMapSupplier.js";
8
8
  import { useUpdate } from "./useUdate.js";
9
9
  const useAmapPlace = (props) => {
10
- const { onChange, emptyPlaceName } = props;
10
+ const { signal, onChange, emptyPlaceName } = props;
11
11
  const defaultPoint = [...props.pointRef.value];
12
12
  const pointRef = ref(defaultPoint);
13
13
  const { readyPromise, language } = useMapSupplier();
14
- const { inChina } = useAmapInChina();
15
14
  const place = reactive({
16
15
  lng: pointRef.value[0],
17
16
  lat: pointRef.value[1],
18
17
  name: "",
19
18
  displayName: ""
20
19
  });
20
+ const abortable = createAbortable(signal);
21
21
  const { idx: placeKey, update } = useUpdate();
22
22
  const updatePlace = (value) => {
23
23
  pointRef.value = value;
@@ -29,37 +29,42 @@ const useAmapPlace = (props) => {
29
29
  place.name = value.name;
30
30
  place.displayName = value.displayName;
31
31
  };
32
- const getPlaceByRegeo = ([lng, lat]) => {
32
+ const getPlaceByRegeo = abortable(([lng, lat]) => {
33
33
  return new Promise((resolve) => {
34
- const geocoder = new AMap.Geocoder({ lang: language === "en" ? "en" : "zh_cn" });
35
- const pipeTw = createPipeTw(language === "zh-TW");
36
- const isChina = inChina([lng, lat]);
37
- geocoder.getAddress(new AMap.LngLat(lng, lat), (status, result) => {
38
- switch (status) {
39
- case "complete": {
40
- const { formattedAddress, addressComponent } = result.regeocode;
41
- const name = formattedAddress ? formattedAddress : emptyPlaceName;
42
- const displayName = amapPlaceName2DisplayName(name, { isChina, ...addressComponent });
43
- resolve({ lng, lat, name: pipeTw(name), displayName: pipeTw(displayName) });
44
- return;
45
- }
46
- case "no_data":
47
- case "error": {
48
- const name = emptyPlaceName;
49
- const displayName = emptyPlaceName;
50
- resolve({ lng, lat, name, displayName });
51
- return;
52
- }
53
- }
54
- });
55
- setTimeout(() => {
56
- console.warn("高德逆地理请求超时!");
34
+ const handleFailed = () => {
57
35
  const name = emptyPlaceName;
58
36
  const displayName = emptyPlaceName;
59
37
  resolve({ lng, lat, name, displayName });
38
+ };
39
+ readyPromise.then(() => {
40
+ const geocoder = new AMap.Geocoder({ lang: language === "en" ? "en" : "zh_cn" });
41
+ const pipeTw = createPipeTw(language === "zh-TW");
42
+ const isChina = inChina([lng, lat]);
43
+ geocoder.getAddress(new AMap.LngLat(lng, lat), (status, result) => {
44
+ switch (status) {
45
+ case "complete": {
46
+ const { formattedAddress, addressComponent } = result.regeocode;
47
+ const name = formattedAddress ? formattedAddress : emptyPlaceName;
48
+ const displayName = amapPlaceName2DisplayName(name, {
49
+ isChina,
50
+ ...addressComponent
51
+ });
52
+ resolve({ lng, lat, name: pipeTw(name), displayName: pipeTw(displayName) });
53
+ return;
54
+ }
55
+ case "no_data":
56
+ case "error": {
57
+ handleFailed();
58
+ }
59
+ }
60
+ });
61
+ }, handleFailed);
62
+ setTimeout(() => {
63
+ console.warn("高德逆地理请求超时!");
64
+ handleFailed();
60
65
  }, REGEO_TIMEOUT);
61
66
  });
62
- };
67
+ });
63
68
  watch(
64
69
  () => [placeKey.value],
65
70
  async () => {
@@ -72,7 +77,7 @@ const useAmapPlace = (props) => {
72
77
  return { place, updatePlace, setPlace, getPlaceByRegeo };
73
78
  };
74
79
  const useGmapPlace = (props) => {
75
- const { onChange, emptyPlaceName } = props;
80
+ const { signal, onChange, emptyPlaceName } = props;
76
81
  const defaultPoint = [...props.pointRef.value];
77
82
  const pointRef = ref(defaultPoint);
78
83
  const { readyPromise, language } = useMapSupplier();
@@ -83,6 +88,7 @@ const useGmapPlace = (props) => {
83
88
  name: "",
84
89
  displayName: ""
85
90
  });
91
+ const abortable = createAbortable(signal);
86
92
  const updatePlace = (value) => {
87
93
  pointRef.value = value;
88
94
  update();
@@ -93,28 +99,30 @@ const useGmapPlace = (props) => {
93
99
  place.name = value.name;
94
100
  place.displayName = value.displayName;
95
101
  };
96
- const getPlaceByRegeo = ([lng, lat]) => {
102
+ const getPlaceByRegeo = abortable(([lng, lat]) => {
97
103
  return new Promise((resolve) => {
98
- const geocoder = new google.maps.Geocoder();
99
- const pipeTw = createPipeTw(language === "zh-TW");
100
- geocoder.geocode({ language, location: { lng, lat } }).then(({ results }) => {
101
- const name = results[0].formatted_address ? results[0].formatted_address : emptyPlaceName;
102
- const displayName = gmapPlaceName2DisplayName(name, results[0].address_components);
103
- resolve({ lng, lat, name: pipeTw(name), displayName: pipeTw(displayName) });
104
- }).catch((err) => {
105
- console.error(err);
104
+ const handleFailed = (err) => {
105
+ if (err)
106
+ console.error(err);
106
107
  const name = emptyPlaceName;
107
108
  const displayName = emptyPlaceName;
108
109
  resolve({ lng, lat, name, displayName });
109
- });
110
+ };
111
+ readyPromise.then(() => {
112
+ const geocoder = new google.maps.Geocoder();
113
+ const pipeTw = createPipeTw(language === "zh-TW");
114
+ geocoder.geocode({ language, location: { lng, lat } }).then(({ results }) => {
115
+ const name = results[0].formatted_address ? results[0].formatted_address : emptyPlaceName;
116
+ const displayName = gmapPlaceName2DisplayName(name, results[0].address_components);
117
+ resolve({ lng, lat, name: pipeTw(name), displayName: pipeTw(displayName) });
118
+ }).catch(handleFailed);
119
+ }, handleFailed);
110
120
  setTimeout(() => {
111
121
  console.warn("谷歌逆地理请求超时!");
112
- const name = emptyPlaceName;
113
- const displayName = emptyPlaceName;
114
- resolve({ lng, lat, name, displayName });
122
+ handleFailed();
115
123
  }, REGEO_TIMEOUT);
116
124
  });
117
- };
125
+ });
118
126
  watch(
119
127
  () => placeKey.value,
120
128
  async () => {
@@ -5,6 +5,7 @@ export interface UseMapRecomendPlaceProps<C = Record<string, any>> {
5
5
  context?: C;
6
6
  emptyPlaceName: string;
7
7
  queryingPlaceName: string;
8
+ signal?: AbortSignal;
8
9
  getLimit: (context?: C) => number;
9
10
  getAvailable?: (place: Place) => Promise<boolean>;
10
11
  getRecomendPlace: (place: Place, context?: C) => Promise<RecommendZonePlaces | undefined>;