@dimensional-innovations/electron-background 2.2.0 → 2.2.2

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/README.md CHANGED
@@ -73,7 +73,7 @@ init({
73
73
  ```
74
74
 
75
75
  ### AutoStart
76
- Registers the application as a login item so it launches automatically at system startup. On Windows, boot-initiated launches are detected via a `--autostart` argument, allowing an optional delay before window creation. On macOS/Linux the delay is skipped since boot detection is not reliable.
76
+ Registers the application as a login item so it launches automatically at system startup. On Windows, boot-initiated launches are detected via a `--autostart` argument, allowing an optional delay before initialization. On macOS/Linux the delay is skipped since boot detection is not reliable.
77
77
 
78
78
  ```typescript
79
79
  // Default: 30-second delay on boot launches
@@ -1,12 +1,14 @@
1
- import { InitPlugin, NonBrowserWindowInitContext } from './init';
1
+ import { BrowserWindowInitContext, InitPlugin } from './init';
2
2
  export interface AutoStartOptions {
3
- /** Delay in seconds before window creation on boot launches (0 = no delay). Default: 30 */
3
+ /** Delay in seconds before initialization on boot launches (0 = no delay). Default: 30 */
4
4
  startupDelay?: number;
5
5
  }
6
6
  /**
7
7
  * Registers the application as a login item for automatic startup and optionally
8
- * delays window creation. The delay only applies when the app was auto-started at
9
- * system boot (detected via the `--autostart` process argument on Windows).
8
+ * delays content loading on boot. The delay runs in `beforeLoad` the window is
9
+ * already created and visible (black background) while waiting, so it only applies
10
+ * when the app was auto-started at system boot (detected via the `--autostart`
11
+ * process argument on Windows).
10
12
  *
11
13
  * On macOS/Linux, boot detection is not reliably possible, so the delay is always
12
14
  * skipped — the app launches instantly regardless of how it was started.
@@ -21,5 +23,6 @@ export declare class AutoStart implements InitPlugin {
21
23
  constructor(enabled?: boolean, options?: AutoStartOptions);
22
24
  /** Returns true when the app was launched by the OS login item (Windows only). */
23
25
  private isAutoStartLaunch;
24
- afterReady(context: NonBrowserWindowInitContext): Promise<void>;
26
+ beforeLoad(context: BrowserWindowInitContext): Promise<void>;
27
+ afterLoad(context: BrowserWindowInitContext): Promise<void>;
25
28
  }
package/dist/AutoStart.js CHANGED
@@ -15,8 +15,10 @@ const electron_1 = require("electron");
15
15
  const AUTOSTART_ARG = '--autostart';
16
16
  /**
17
17
  * Registers the application as a login item for automatic startup and optionally
18
- * delays window creation. The delay only applies when the app was auto-started at
19
- * system boot (detected via the `--autostart` process argument on Windows).
18
+ * delays content loading on boot. The delay runs in `beforeLoad` the window is
19
+ * already created and visible (black background) while waiting, so it only applies
20
+ * when the app was auto-started at system boot (detected via the `--autostart`
21
+ * process argument on Windows).
20
22
  *
21
23
  * On macOS/Linux, boot detection is not reliably possible, so the delay is always
22
24
  * skipped — the app launches instantly regardless of how it was started.
@@ -35,14 +37,25 @@ class AutoStart {
35
37
  isAutoStartLaunch() {
36
38
  return process.platform === 'win32' && process.argv.includes(AUTOSTART_ARG);
37
39
  }
38
- afterReady(context) {
40
+ beforeLoad(context) {
41
+ return __awaiter(this, void 0, void 0, function* () {
42
+ if (!this.enabled)
43
+ return;
44
+ if (this.startupDelay > 0 && this.isAutoStartLaunch()) {
45
+ context.log.info(`[AutoStart] Boot launch detected — delaying ${this.startupDelay}s before loading content`);
46
+ yield new Promise(resolve => setTimeout(resolve, this.startupDelay * 1000));
47
+ }
48
+ else if (this.startupDelay > 0) {
49
+ context.log.info('[AutoStart] Manual launch — skipping startup delay');
50
+ }
51
+ });
52
+ }
53
+ afterLoad(context) {
39
54
  return __awaiter(this, void 0, void 0, function* () {
40
55
  if (!this.enabled) {
41
56
  context.log.info('[AutoStart] Disabled (not packaged)');
42
57
  return;
43
58
  }
44
- // On Windows, include the autostart arg so boot launches can be distinguished
45
- // from manual launches. On other platforms, no args needed.
46
59
  try {
47
60
  if (process.platform === 'win32') {
48
61
  electron_1.app.setLoginItemSettings({ openAtLogin: true, args: [AUTOSTART_ARG] });
@@ -55,15 +68,6 @@ class AutoStart {
55
68
  catch (err) {
56
69
  context.log.error('[AutoStart] Failed to register login item:', err);
57
70
  }
58
- if (this.startupDelay > 0 && this.isAutoStartLaunch()) {
59
- context.log.info(`[AutoStart] Boot launch detected — delaying ${this.startupDelay}s before window creation`);
60
- yield new Promise(resolve => {
61
- setTimeout(resolve, this.startupDelay * 1000);
62
- });
63
- }
64
- else if (this.startupDelay > 0) {
65
- context.log.info('[AutoStart] Manual launch — skipping startup delay');
66
- }
67
71
  });
68
72
  }
69
73
  }
package/dist/init.js CHANGED
@@ -39,6 +39,25 @@ class InitContext {
39
39
  }
40
40
  }
41
41
  exports.InitContext = InitContext;
42
+ function getPluginName(plugin) {
43
+ var _a;
44
+ return ((_a = plugin.constructor) === null || _a === void 0 ? void 0 : _a.name) || 'UnknownPlugin';
45
+ }
46
+ function runPluginPhase(plugins, phase, context, logger) {
47
+ return __awaiter(this, void 0, void 0, function* () {
48
+ for (const plugin of plugins) {
49
+ const method = plugin[phase];
50
+ if (method) {
51
+ try {
52
+ yield method.call(plugin, context);
53
+ }
54
+ catch (err) {
55
+ logger.error(`[init] Plugin "${getPluginName(plugin)}" threw during ${phase}:`, err);
56
+ }
57
+ }
58
+ }
59
+ });
60
+ }
42
61
  /**
43
62
  * Initializes the application, creating a browser window, and loads the provided app url.
44
63
  *
@@ -55,29 +74,21 @@ function init({ appUrl, browserWindowOptions = { height: 1920, width: 1080, back
55
74
  });
56
75
  process.on('SIGTERM', electron_1.app.quit);
57
76
  const context = new InitContext(appUrl, browserWindowOptions, electron_log_1.default);
58
- for (const plugin of plugins) {
59
- if (plugin.beforeReady) {
60
- yield plugin.beforeReady(context);
61
- }
62
- }
77
+ yield runPluginPhase(plugins, 'beforeReady', context, electron_log_1.default);
63
78
  yield electron_1.app.whenReady();
64
- for (const plugin of plugins) {
65
- if (plugin.afterReady) {
66
- yield plugin.afterReady(context);
67
- }
68
- }
79
+ yield runPluginPhase(plugins, 'afterReady', context, electron_log_1.default);
69
80
  context.browserWindow = new electron_1.BrowserWindow(context.browserWindowOptions);
70
- for (const plugin of plugins) {
71
- if (plugin.beforeLoad) {
72
- yield plugin.beforeLoad(context);
73
- }
81
+ yield runPluginPhase(plugins, 'beforeLoad', context, electron_log_1.default);
82
+ try {
83
+ yield context.browserWindow.loadURL(context.appUrl);
74
84
  }
75
- yield context.browserWindow.loadURL(context.appUrl);
76
- for (const plugin of plugins) {
77
- if (plugin.afterLoad) {
78
- yield plugin.afterLoad(context);
79
- }
85
+ catch (err) {
86
+ electron_log_1.default.error('[init] Failed to load app URL:', err);
87
+ }
88
+ if (context.browserWindow && !context.browserWindow.isDestroyed() && !context.browserWindow.isVisible()) {
89
+ context.browserWindow.show();
80
90
  }
91
+ yield runPluginPhase(plugins, 'afterLoad', context, electron_log_1.default);
81
92
  context.browserWindow.on('closed', () => context.browserWindow = undefined);
82
93
  return context;
83
94
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dimensional-innovations/electron-background",
3
- "version": "2.2.0",
3
+ "version": "2.2.2",
4
4
  "description": "the background script for electron apps",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",