Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event 'open' on app is triggered once #7860

Closed
panther7 opened this issue Apr 4, 2022 · 8 comments
Closed

Event 'open' on app is triggered once #7860

panther7 opened this issue Apr 4, 2022 · 8 comments

Comments

@panther7
Copy link

panther7 commented Apr 4, 2022

Win 10 (x64)

  • NWjs 0.62.2 - ok
  • NWjs 0.63.0 - fail

manifest.json:

{
  "name": "test",
  "version": "0.1",

  "app": {
    "background": {
      "scripts": ["init.js"]
    }
  },

  "permissions": [
      "webview",
      "<all_urls>",
      "node",
      "proxy"
  ]
}

init.js:

(function () {
    // open dev tools
    chrome.developerPrivate.openDevTools({ renderViewId: -1, renderProcessId: -1, extensionId: chrome.runtime.id });
    // listener
    nw.App.on('open', (params) => console.log('onOpen', params));
})();

test case:

nw.exe "https://nwjs.io" // open event is triggered
nw.exe "https://nwjs.io" // nothing
nw.exe "https://nwjs.io" // nothing

@rogerwang

@panther7 panther7 changed the title Event open on app is triggered once Event 'open' on app is triggered once Apr 5, 2022
@sysrage
Copy link

sysrage commented Apr 5, 2022

I am also seeing this issue. It happens with 0.63.0 in both Linux and Windows.

An odd error message is also appearing, every time the app is launched:

[0405/104534.014465:ERROR:zip_reader.cc(140)] Cannot open ZIP from file handle 3

@sysrage
Copy link

sysrage commented Apr 5, 2022

I created a test to find this regression and it helped narrow-down the behavior. It seems that any event handlers attached to the open event are removed once they fire...

This shows the bug in action. After the first open event, the remaining 5 events do not trigger the event handler:

    const util = require('util');
    const exec = util.promisify(require('child_process').exec);

    let eventFired;
    nw.App.on('open', (params) => {
      console.log('params', params);
      eventFired = true;
    });

    const testOpenEvent = async () => {
      eventFired = false;
      const result = await exec(`${process.argv} ${nw.App.startPath} --testing`);
      if (!eventFired) return false;
      return true;
    };

    (async () => {
      for (let i = 0; i <= 5; i++) {
        console.log(`Test attempt: ${i}`);

        if (! await testOpenEvent()) {
          console.log('FAIL: The `open` event did not fire!');
        }
      }
    })();

image

However, if the event handler is re-attached to the open event every time, it works as expected:

    const util = require('util');
    const exec = util.promisify(require('child_process').exec);

    const testOpenEvent = async () => {
      let eventFired = false
      nw.App.on('open', (params) => {
        console.log('params', params);
        eventFired = true;
      });

      const result = await exec(`${process.argv} ${nw.App.startPath} --testing`);
      if (!eventFired) return false;
      return true;
    };

    (async () => {
      for (let i = 0; i <= 5; i++) {
        console.log(`Test attempt: ${i}`);

        if (! await testOpenEvent()) {
          console.log('FAIL: The `open` event did not fire!');
        }
      }
    })();

image

@rogerwang rogerwang self-assigned this Apr 5, 2022
@panther7
Copy link
Author

panther7 commented Apr 5, 2022

Maybe same issue with other events like nw.App.on("reopen", callback).

@arudnev
Copy link

arudnev commented Apr 5, 2022

On macOS the reopen event is fired only the first time when you click on the app icon in the dock after initial app start, in versions prior to 0.63.0 it was firing every time

@panther7
Copy link
Author

Any news?

@rogerwang
Copy link
Member

This is caused by https://chromium-review.googlesource.com/c/chromium/src/+/3233175
Will fix it soon.

@rogerwang
Copy link
Member

This is fixed in git and will be available in the next nightly build.

@anurag-2911
Copy link

I have fixed this issue by adding my function in nw.App.onOpen.addListener and in this case it doesn't remove the listener.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants