nexushoratio / userscripts

Random userscripts for browsers.
GNU General Public License v3.0
2 stars 0 forks source link

Generic way to capture bugs #205

Closed nexushoratio closed 11 months ago

nexushoratio commented 11 months ago

Like what we have going with SPA.addError(), but way down in NH.base.

I do not think it is possible to just catch all Errors. Obviously LI itself does, because it keeps eating LIT's. But from a userscript, may be tricky.

nexushoratio commented 11 months ago

No generic way to catch errors that works inside of userscripts.

The recommended way seems to be self.addEventListener('error', handler);, where using self rather than window works on both apps and workers.

One approach could be to listen for a CustomEvent like we do for urlchange, but if we do that, we really want to have a function in base that generate the event. And if we do that, we might as well just directly call something to save it.

So, now we are kind of looking at a pubsub service with a memory, not just a dispatcher. Though one can be built on top of Dispatcher.

nexushoratio commented 11 months ago

The built in MessageChannel looks like it will work.

  const channel = new MessageChannel();

  channel.port2.addEventListener('message', (event) => {
    logger.log('port2:', event.data);
  });

  for (let i = 0; i < 100; i += 1) {
    channel.port1.postMessage(i);
  }
  channel.port2.start();

So, just needs a little wrapping.

Technically, this is another WEB API, like console. Though at least not DOM related.

nexushoratio commented 11 months ago

Actually, just write our own to avoid any more use of the WEB API in base.