@hlw-uni/mp-vue 2.0.7 → 2.0.9

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
@@ -639,6 +639,7 @@ var __publicField = (obj, key, value) => {
639
639
  popup_unit_id: ""
640
640
  };
641
641
  let adapter$1 = null;
642
+ let pending$1 = null;
642
643
  function setConfigAd(a) {
643
644
  adapter$1 = a;
644
645
  }
@@ -657,24 +658,35 @@ var __publicField = (obj, key, value) => {
657
658
  async function loadConfig2(force = false) {
658
659
  if (loaded.value && !force)
659
660
  return;
661
+ if (pending$1 && !force)
662
+ return pending$1;
660
663
  if (!(adapter$1 == null ? void 0 : adapter$1.getConfig)) {
661
664
  console.warn("[useAd] adapter.getConfig 未注入;先调用 setConfigAd()");
662
665
  return;
663
666
  }
664
- try {
665
- const cfg = unwrapPayload(await adapter$1.getConfig());
666
- if (cfg) {
667
- store.config = cfg;
668
- store.loaded = true;
667
+ const fn = adapter$1.getConfig;
668
+ const flight = (async () => {
669
+ try {
670
+ const cfg = unwrapPayload(await fn());
671
+ if (cfg) {
672
+ store.config = cfg;
673
+ store.loaded = true;
674
+ }
675
+ } catch (e) {
676
+ console.warn("[useAd] load config failed", e);
677
+ } finally {
678
+ if (pending$1 === flight)
679
+ pending$1 = null;
669
680
  }
670
- } catch (e) {
671
- console.warn("[useAd] load config failed", e);
672
- }
681
+ })();
682
+ pending$1 = flight;
683
+ return flight;
673
684
  }
674
685
  function getUnitId(type) {
675
686
  return store.config[`${type}_unit_id`] || "";
676
687
  }
677
688
  async function showReward(onClose) {
689
+ await loadConfig2();
678
690
  const unitId = getUnitId("reward");
679
691
  if (!unitId) {
680
692
  msg().toast("激励广告未配置");
@@ -689,6 +701,7 @@ var __publicField = (obj, key, value) => {
689
701
  await onClose(closeRes);
690
702
  }
691
703
  async function showPopup() {
704
+ await loadConfig2();
692
705
  const unitId = getUnitId("popup");
693
706
  if (!unitId)
694
707
  return false;
package/dist/index.mjs CHANGED
@@ -638,6 +638,7 @@ const EMPTY = {
638
638
  popup_unit_id: ""
639
639
  };
640
640
  let adapter$1 = null;
641
+ let pending$1 = null;
641
642
  function setConfigAd(a) {
642
643
  adapter$1 = a;
643
644
  }
@@ -656,24 +657,35 @@ function useAd() {
656
657
  async function loadConfig2(force = false) {
657
658
  if (loaded.value && !force)
658
659
  return;
660
+ if (pending$1 && !force)
661
+ return pending$1;
659
662
  if (!(adapter$1 == null ? void 0 : adapter$1.getConfig)) {
660
663
  console.warn("[useAd] adapter.getConfig 未注入;先调用 setConfigAd()");
661
664
  return;
662
665
  }
663
- try {
664
- const cfg = unwrapPayload(await adapter$1.getConfig());
665
- if (cfg) {
666
- store.config = cfg;
667
- store.loaded = true;
666
+ const fn = adapter$1.getConfig;
667
+ const flight = (async () => {
668
+ try {
669
+ const cfg = unwrapPayload(await fn());
670
+ if (cfg) {
671
+ store.config = cfg;
672
+ store.loaded = true;
673
+ }
674
+ } catch (e) {
675
+ console.warn("[useAd] load config failed", e);
676
+ } finally {
677
+ if (pending$1 === flight)
678
+ pending$1 = null;
668
679
  }
669
- } catch (e) {
670
- console.warn("[useAd] load config failed", e);
671
- }
680
+ })();
681
+ pending$1 = flight;
682
+ return flight;
672
683
  }
673
684
  function getUnitId(type) {
674
685
  return store.config[`${type}_unit_id`] || "";
675
686
  }
676
687
  async function showReward(onClose) {
688
+ await loadConfig2();
677
689
  const unitId = getUnitId("reward");
678
690
  if (!unitId) {
679
691
  msg().toast("激励广告未配置");
@@ -688,6 +700,7 @@ function useAd() {
688
700
  await onClose(closeRes);
689
701
  }
690
702
  async function showPopup() {
703
+ await loadConfig2();
691
704
  const unitId = getUnitId("popup");
692
705
  if (!unitId)
693
706
  return false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hlw-uni/mp-vue",
3
- "version": "2.0.7",
3
+ "version": "2.0.9",
4
4
  "description": "hlw-uni 小程序运行时 — Vue 组件 + composables + theme + http + 工具集(合并自原 mp-core)",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -99,6 +99,9 @@ const EMPTY: AdConfig = {
99
99
 
100
100
  let adapter: AdAdapter | null = null;
101
101
 
102
+ /** 进行中的 loadConfig promise;并发调用时复用,避免冷启动多发请求 */
103
+ let pending: Promise<void> | null = null;
104
+
102
105
  /**
103
106
  * 注入业务回调,应用启动时调用一次。
104
107
  * 不调用也不会崩,但 loadConfig / showReward 会无效。
@@ -136,22 +139,33 @@ export function useAd() {
136
139
  const store = useAdStore();
137
140
  const { config, loaded } = storeToRefs(store);
138
141
 
139
- /** 拉取广告配置(小程序冷启动后调一次即可) */
142
+ /**
143
+ * 拉取广告配置(小程序冷启动后调一次即可)。
144
+ * 并发调用复用同一次请求;`force=true` 跳过 loaded/pending 短路重新拉。
145
+ */
140
146
  async function loadConfig(force = false): Promise<void> {
141
147
  if (loaded.value && !force) return;
148
+ if (pending && !force) return pending;
142
149
  if (!adapter?.getConfig) {
143
150
  console.warn("[useAd] adapter.getConfig 未注入;先调用 setConfigAd()");
144
151
  return;
145
152
  }
146
- try {
147
- const cfg = unwrapPayload(await adapter.getConfig());
148
- if (cfg) {
149
- store.config = cfg;
150
- store.loaded = true;
153
+ const fn = adapter.getConfig;
154
+ const flight = (async () => {
155
+ try {
156
+ const cfg = unwrapPayload(await fn());
157
+ if (cfg) {
158
+ store.config = cfg;
159
+ store.loaded = true;
160
+ }
161
+ } catch (e) {
162
+ console.warn("[useAd] load config failed", e);
163
+ } finally {
164
+ if (pending === flight) pending = null;
151
165
  }
152
- } catch (e) {
153
- console.warn("[useAd] load config failed", e);
154
- }
166
+ })();
167
+ pending = flight;
168
+ return flight;
155
169
  }
156
170
 
157
171
  /** 取指定类型的 unit_id(hlw-ad 组件 / 业务直接调时用) */
@@ -170,6 +184,7 @@ export function useAd() {
170
184
  * @param onClose 关闭回调;不传则只播。retry / toast / 发奖全在业务侧自己写。
171
185
  */
172
186
  async function showReward(onClose?: (res: AdCloseResult) => void | Promise<void>): Promise<void> {
187
+ await loadConfig();
173
188
  const unitId = getUnitId("reward");
174
189
  if (!unitId) {
175
190
  msg().toast("激励广告未配置");
@@ -188,6 +203,7 @@ export function useAd() {
188
203
  * @returns true=展示成功;false=配置缺失 / show 失败(如近期已展示过)
189
204
  */
190
205
  async function showPopup(): Promise<boolean> {
206
+ await loadConfig();
191
207
  const unitId = getUnitId("popup");
192
208
  if (!unitId) return false;
193
209
  return await showInterstitialAd(unitId);