@lynker-desktop/electron-window-manager 0.0.9-alpha.49 → 0.0.9-alpha.50

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/main/index.js CHANGED
@@ -455,7 +455,7 @@ class WindowsManager {
455
455
  win.loadURL = async (url, useNativeLoadURL = false) => {
456
456
  return new Promise(async (resolve, reject) => {
457
457
  if (useNativeLoadURL) {
458
- console.error('useNativeLoadURL win.loadURL');
458
+ log('log', 'useNativeLoadURL win.loadURL');
459
459
  try {
460
460
  await originLoadURL.call(win, url);
461
461
  resolve(undefined);
@@ -466,7 +466,7 @@ class WindowsManager {
466
466
  return;
467
467
  }
468
468
  try {
469
- console.error('customLoadURL win.loadURL');
469
+ log('log', 'customLoadURL win.loadURL');
470
470
  // @ts-ignore
471
471
  await this.preloadWebContentsConfig.customLoadURL(url || 'about:blank', (url) => originLoadURL.call(win, url), win.webContents);
472
472
  try {
@@ -488,7 +488,7 @@ class WindowsManager {
488
488
  win.webContents.loadURL = async (url, useNativeLoadURL = false) => {
489
489
  return new Promise(async (resolve, reject) => {
490
490
  if (useNativeLoadURL) {
491
- console.error('useNativeLoadURL win.webContents.loadURL');
491
+ log('log', 'useNativeLoadURL win.webContents.loadURL');
492
492
  try {
493
493
  await originWebContentsLoadURL.call(win.webContents, url);
494
494
  resolve(undefined);
@@ -499,7 +499,7 @@ class WindowsManager {
499
499
  return;
500
500
  }
501
501
  try {
502
- console.error('customLoadURL win.webContents.loadURL');
502
+ log('log', 'customLoadURL win.webContents.loadURL');
503
503
  // @ts-ignore
504
504
  await this.preloadWebContentsConfig.customLoadURL(url || 'about:blank', (url) => originWebContentsLoadURL.call(win.webContents, url), win.webContents);
505
505
  try {
@@ -517,7 +517,7 @@ class WindowsManager {
517
517
  };
518
518
  }
519
519
  catch (error) {
520
- console.error('customLoadURL error', error);
520
+ log('error', 'customLoadURL error', error);
521
521
  }
522
522
  }
523
523
  window = win;
@@ -768,11 +768,11 @@ class WindowsManager {
768
768
  log('log', 'create', this.windows.keys());
769
769
  // 初始化值
770
770
  window.webContents.on('did-finish-load', () => {
771
- console.error('did-finish-load', window.webContents.id);
771
+ log('log', 'did-finish-load', window.webContents.id);
772
772
  initWebContentsVal(window, `${preload || ''}`);
773
773
  });
774
774
  window.webContents.on('did-start-loading', () => {
775
- console.error('did-start-loading', window.webContents.id);
775
+ log('log', 'did-start-loading', window.webContents.id);
776
776
  initWebContentsVal(window, `${preload || ''}`);
777
777
  });
778
778
  if (type === 'BW') {
@@ -783,7 +783,7 @@ class WindowsManager {
783
783
  try {
784
784
  window.dispatchEvent(new Event('focus'));
785
785
  } catch (error) {
786
- console.error('focus', error);
786
+ // 忽略错误,避免影响主流程
787
787
  }
788
788
  `);
789
789
  }
@@ -797,7 +797,7 @@ class WindowsManager {
797
797
  try {
798
798
  window.dispatchEvent(new Event('blur'));
799
799
  } catch (error) {
800
- console.error('blur', error);
800
+ // 忽略错误,避免影响主流程
801
801
  }
802
802
  `);
803
803
  }
@@ -1039,7 +1039,9 @@ class WindowsManager {
1039
1039
  // 窗口已销毁,触发清理
1040
1040
  if (win) {
1041
1041
  const winId = win.id || win._id;
1042
- this.windows.delete(winId);
1042
+ if (winId !== undefined) {
1043
+ this.windows.delete(winId);
1044
+ }
1043
1045
  if (win._name) {
1044
1046
  this.windowsByName.delete(win._name);
1045
1047
  }
@@ -1318,25 +1320,46 @@ class WindowsManager {
1318
1320
  }
1319
1321
  // 生成一个bv 做为预加载资源窗口,加载完成后销毁
1320
1322
  async createPreloadWebContents(url) {
1321
- return new Promise(async (resolve, reject) => {
1322
- let bv = await this.create({
1323
+ let bv = null;
1324
+ try {
1325
+ bv = await this.create({
1323
1326
  type: 'BV',
1324
1327
  url,
1325
1328
  name: `preload-web-contents-${md5(url)}`,
1326
1329
  extraData: `${url}`
1327
1330
  });
1328
- bv.webContents.on('did-finish-load', () => {
1329
- this.close(bv.id || bv._id);
1330
- resolve(true);
1331
- bv = null;
1332
- });
1333
- bv.webContents.on('did-fail-load', () => {
1334
- this.close(bv.id || bv._id);
1335
- reject(false);
1336
- bv = null;
1331
+ return new Promise((resolve, reject) => {
1332
+ const winId = bv.id || bv._id;
1333
+ if (!winId) {
1334
+ reject(new Error('Failed to get window ID'));
1335
+ return;
1336
+ }
1337
+ bv.webContents.on('did-finish-load', () => {
1338
+ if (winId) {
1339
+ this.close(winId);
1340
+ }
1341
+ resolve(true);
1342
+ bv = null;
1343
+ });
1344
+ bv.webContents.on('did-fail-load', () => {
1345
+ if (winId) {
1346
+ this.close(winId);
1347
+ }
1348
+ reject(new Error('Failed to load web contents'));
1349
+ bv = null;
1350
+ });
1351
+ bv.webContents.loadURL(url);
1337
1352
  });
1338
- bv.webContents.loadURL(url);
1339
- });
1353
+ }
1354
+ catch (error) {
1355
+ if (bv) {
1356
+ const winId = bv.id || bv._id;
1357
+ if (winId) {
1358
+ this.close(winId);
1359
+ }
1360
+ }
1361
+ throw error;
1362
+ }
1340
1363
  }
1341
1364
  async getWindowForWebContentsId(wcId) {
1342
1365
  const wc = electron.webContents.fromId(wcId);
@@ -1518,15 +1541,17 @@ const initialize = (preload, loadingViewUrl, errorViewUrl, preloadWebContentsCon
1518
1541
  const res = wm.getAll();
1519
1542
  const obj = {};
1520
1543
  res.forEach(i => {
1521
- // @ts-ignore
1522
- obj[i.id || i._id] = {
1523
- winId: Number(`${i?.id || i?._id || -1}`),
1524
- winName: `${i?._name || ''}`,
1525
- winType: `${i?._type || ''}`,
1526
- winExtraData: `${i?._extraData || ''}`,
1527
- winInitUrl: `${i?._initUrl || ''}`,
1528
- winZIndex: `${i?._zIndex || 0}`,
1529
- };
1544
+ const winId = i.id || i._id;
1545
+ if (winId !== undefined) {
1546
+ obj[winId] = {
1547
+ winId: Number(`${winId}`),
1548
+ winName: `${i?._name || ''}`,
1549
+ winType: `${i?._type || ''}`,
1550
+ winExtraData: `${i?._extraData || ''}`,
1551
+ winInitUrl: `${i?._initUrl || ''}`,
1552
+ winZIndex: `${i?._zIndex || 0}`,
1553
+ };
1554
+ }
1530
1555
  });
1531
1556
  return obj;
1532
1557
  }