aadsm / jsmediatags

Media Tags Reader (ID3, MP4, FLAC)
Other
752 stars 128 forks source link

add support for webworker #98

Closed ctf0 closed 4 years ago

ctf0 commented 6 years ago

is it even possible ?

aadsm commented 5 years ago

Yeah, I don’t see why it wouldn’t be possible. XHR is possible inside webworkers. It’s also a great idea! Is this something you’d like to implement?

ctf0 commented 5 years ago

am very new to webworkers, so any guidance is much appreciated 🏆

aadsm commented 5 years ago

Hey @ctf0, sorry it took me ages to reply but I've been super busy at work lately. I'd say the two main gotchas to add webworker support is:

I think the best method is for you to put the code you have that uses jsmediatags in a webworker (I'm expecting this lib to work out of the box in a webworker, if it doesn't feel free to add details here). You then need to provide a way for to get the data out of the webworker. Here's an idea how to do that:

Put your code that uses jsmediatags into its own file (e.g.: tagReader.js). Here's an example for the read method:

var jsmediatags = require("jsmediatags");
self.onmessage = function(message) {
  read(message.data);
};
function read(location) {
  jsmediatags.read(location, {
    onSuccess: function(tag) {
      self.postMessage({location: location, type: 'success', data: tag});
    },
    onError: function(error) {
      self.postMessage({location: location, type: 'error', error: error});
    }
  });
}

Then use the file in a webworker like:

var tagReaderWorker = new Worker('tagReader.js');
tagReaderWorker.onmessage = function(message) {
  if (message.data.type === 'success') {
    console.log(message.data.location, message.data.data);
  } else {
    console.log('error', message.data.error);
  }
};
tagReaderWorker.postMessage("http://www.example.com/music-file.mp3");

I hope this helps!

ctf0 commented 4 years ago

@aadsm hi again, sorry for the very late reply, i just got the chance to test the above.

atm because the lib is not compatible with browser, so using var jsmediatags = require("jsmediatags"); is not possible and we get

Uncaught ReferenceError: jsmediatags is not defined

is there any hope to use jsmediatags for browser through npm ?