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

Web Workers Neu.storage not appearing in Folder #27

Open
danidre14 opened this issue Sep 19, 2021 · 1 comment
Open

Web Workers Neu.storage not appearing in Folder #27

danidre14 opened this issue Sep 19, 2021 · 1 comment

Comments

@danidre14
Copy link

Expected Behavior

When you import Neu in the web worker and try to use Neutralino.storage.putData or Neutralino.storage.getData in the worker, it should appear in the .storage folder, just as when you call the functions in the main Window scope.

Actual Behavior

When you import Neu in the web worker and try to use Neutralino.storage.putData or Neutralino.storage.getData in the worker, it doesn't fail or error out, however, the .storage folder does not populate with the new bucket file.

Steps to Reproduce the Problem

1. Create a worker thread.
var myWorker = new Worker(URL);
2. Within the worker, import Neu and call the functions in the onmessage:

importScripts("../../resources/js/neutralino.js");
self.onmessage = async function () {
 for(var i = 10; i < 18; i++) {
   console.log("uid"+i);
   await Neu.storage.putData({ bucket: "uid"+i, data: "uid"+i });
 }
}

3. Post a message to the worker from the main file:

// within button click
myWorker.postMessage("Hello world");

When run and the button pressed, the console logs uid10 all the way up to uid17 from the console.log, however, the .storage file does not have the newly created files.

Specifications

  • NeutralinoJs Version: binary: [2.8.0]; client: [1.5.0];
  • NeutralinoJs CLI version: 6.2.0
  • Platform: Windows 10 64-bit
@danidre14
Copy link
Author

After extensive reviewing of the neutralinojs code (after running it in a beautifier), I realized that the functions send a fetch request to the server through the port. However, you use window.NL_PORT. It occurred to me that window is undefined in a worker.

The current hack way I did for it to work was to do:

var window = self;
importScripts("../../resources/js/neutralino.js");
self.onmessage = async function () {
 for(var i = 10; i < 18; i++) {
   console.log("uid"+i);
   await Neu.storage.putData({ bucket: "uid"+i, data: "uid"+i });
 }
}

I had to put the window as the webworker process before importing the neu script to the web worker. Thus, it will populate the properties into the window object, and then access them when making the request.

I think this is a flaw on neu's end, it should check if it is in a webworker or not, and provide some functions or not depending on whether it is or not.

I will create another issue with that feature as a request.

When you are ready, you can close this issue, since it is solved.

@shalithasuranga shalithasuranga transferred this issue from neutralinojs/neutralinojs Nov 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant