sobey-monitor-sdk 1.0.9 → 1.1.1

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.umd.js CHANGED
@@ -1649,41 +1649,56 @@
1649
1649
  class MonitorSDK {
1650
1650
  constructor() {
1651
1651
  this.initialized = false;
1652
+ this.initializing = false;
1652
1653
  }
1653
1654
  /**
1654
1655
  * 初始化 SDK
1655
1656
  * 如果配置了 configUrl,将先从远程获取配置(仅获取一次)
1657
+ * 无需 await,SDK 内部会自动处理异步逻辑
1656
1658
  */
1657
- async init(userConfig) {
1658
- if (this.initialized) {
1659
- console.warn('[Monitor] SDK already initialized');
1659
+ init(userConfig) {
1660
+ if (this.initialized || this.initializing) {
1661
+ if (this.initialized) {
1662
+ console.warn('[Monitor] SDK already initialized');
1663
+ }
1660
1664
  return;
1661
1665
  }
1662
- let finalConfig = { ...userConfig };
1663
- // 如果配置了 configUrl,从远程获取配置
1666
+ // 如果配置了 configUrl,异步获取远程配置后再初始化
1664
1667
  if (userConfig.configUrl) {
1665
- try {
1666
- const remoteConfig = await this.fetchRemoteConfig(userConfig.configUrl);
1668
+ this.initializing = true;
1669
+ this.fetchRemoteConfig(userConfig.configUrl)
1670
+ .then((remoteConfig) => {
1667
1671
  // 远程配置作为基础,用户本地配置覆盖远程配置
1668
- finalConfig = this.mergeUserConfig(remoteConfig, userConfig);
1672
+ const finalConfig = this.mergeUserConfig(remoteConfig, userConfig);
1669
1673
  if (userConfig.debug) {
1670
1674
  console.log('[Monitor] Remote config loaded:', remoteConfig);
1671
1675
  }
1672
- }
1673
- catch (error) {
1676
+ this.doInit(finalConfig, true);
1677
+ })
1678
+ .catch((error) => {
1679
+ this.initializing = false;
1674
1680
  console.error('[Monitor] Failed to fetch remote config:', error);
1675
- throw new Error('[Monitor] Failed to fetch remote config. Please check configUrl.');
1676
- }
1681
+ });
1677
1682
  }
1678
- // 初始化配置(远程配置模式下跳过初始校验,因为已经合并了远程配置)
1683
+ else {
1684
+ // 没有 configUrl,直接同步初始化
1685
+ this.doInit(userConfig, false);
1686
+ }
1687
+ }
1688
+ /**
1689
+ * 执行实际的初始化逻辑
1690
+ */
1691
+ doInit(finalConfig, isRemoteConfig) {
1692
+ // 初始化配置
1679
1693
  config.init({
1680
1694
  ...finalConfig,
1681
1695
  version: finalConfig.version || SDK_VERSION,
1682
- }, !!userConfig.configUrl);
1696
+ }, isRemoteConfig);
1683
1697
  // 初始化上下文
1684
1698
  const maxBreadcrumbs = config.get().behavior?.maxBreadcrumbs || 20;
1685
1699
  context.init(maxBreadcrumbs);
1686
1700
  this.initialized = true;
1701
+ this.initializing = false;
1687
1702
  if (config.get().debug) {
1688
1703
  console.log('[Monitor] SDK initialized', config.get());
1689
1704
  }
@@ -1746,12 +1761,32 @@
1746
1761
  /**
1747
1762
  * 动态更新配置
1748
1763
  * @description 可以在运行时更新 SDK 配置,如 debug、sampling、report 等
1764
+ * 如果传入 configUrl,会从远程获取配置并合并
1749
1765
  */
1750
1766
  updateConfig(partialConfig) {
1751
1767
  this.checkInit();
1752
- config.update(partialConfig);
1753
- if (config.get().debug) {
1754
- console.log('[Monitor] Config updated', partialConfig);
1768
+ // 如果传入了 configUrl,从远程获取配置并更新
1769
+ if (partialConfig.configUrl) {
1770
+ this.fetchRemoteConfig(partialConfig.configUrl)
1771
+ .then((remoteConfig) => {
1772
+ // 远程配置作为基础,传入的其他本地配置覆盖远程配置
1773
+ const mergedConfig = this.mergeUserConfig(remoteConfig, partialConfig);
1774
+ // 移除 configUrl,不需要存到配置里
1775
+ delete mergedConfig.configUrl;
1776
+ config.update(mergedConfig);
1777
+ if (config.get().debug) {
1778
+ console.log('[Monitor] Config updated from remote:', mergedConfig);
1779
+ }
1780
+ })
1781
+ .catch((error) => {
1782
+ console.error('[Monitor] Failed to fetch remote config:', error);
1783
+ });
1784
+ }
1785
+ else {
1786
+ config.update(partialConfig);
1787
+ if (config.get().debug) {
1788
+ console.log('[Monitor] Config updated', partialConfig);
1789
+ }
1755
1790
  }
1756
1791
  }
1757
1792
  /**