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