nponiros / sync_client

Module using IndexedDB to save data and can later use a server to synchronize the data in the database with other devices
MIT License
36 stars 13 forks source link

Use self iso window so the library can be used in a worker. #8

Open koenvd opened 6 years ago

koenvd commented 6 years ago

Fix for using the library in a worker where window is not defined causing an error to be thrown when the SyncClient is loaded.

nponiros commented 6 years ago

Hi there, thanks for the pull request.

Have you tested this in a worker? More specifically have you tried creating a CUID? As far as I understand this will not work because of navigator.mimeTypes used in cuid.js which I got from https://github.com/ericelliott/cuid.

Cuid was in the mean time updated to work with Node.js and if we also update we should be able to install it via npm and load via webpack. Is this something that you would be willing to try and doing? I don't have much time unfortunately. If not I can just merge the pull request and maybe add a note that getID() will not work in a worker.

koenvd commented 6 years ago

Good point.

navigator.mimeTypes is not defined in a worker context. I'm not sure if the alternative implementation will work as well from within a worker in a browser (access to os.hostname): https://github.com/ericelliott/cuid/blob/master/lib/fingerprint.js

But I also don't understand immediately why the getID() method is needed ...

Noticed as well that the event handlers of 'online' & 'offline' must be added to the initial evaluation of the worker script (Have a warning like Event handler of 'online' event must be added on the initial evaluation of worker script.). So there is some extra work todo as well ...

Will look into it a bit more.

nponiros commented 6 years ago

For some reason I didn't want to use dexie to create the IDs so I added cuid and getID(). I don't even remember why I didn't want to create IDs using dexie. Probably because I already had used cuid in other projects. I don't think that it is a major issue if getID() doesn't work in a worker. I guess most people will use dexie to create IDs.

Thanks for the info regarding fingerprint.js. I guess fingerprint.browser.js can be used but this also doesn't work in a worker. But as I said: I find it okay if getID() doesn't work in a worker.