fxos-components / bridge

Exposes services between JavaScript contexts
https://fxos-components.github.io/bridge
MIT License
20 stars 8 forks source link

Explore a way to load .js files into 'window' thread type #20

Closed wilsonpage closed 9 years ago

wilsonpage commented 9 years ago

Currently users have to define .html sources for 'window' type threads. This is not ideal as it means that scripts can't be moved between thread types easily.

Open questions:

  1. If scripts have dependencies these have to be loaded differently depending on the env (eg. importScripts vs async script loading). How can users author once and run anywhere? Requirejs can run in both window and worker context, so this might be handy.
  2. For window at least we'd have to have a 'container' page to load user's scripts into. What does this look like? Is it a real file or a memory based Blob?
  3. Should single threads be able to load multiple sources, or should users' scripts always run in dedicated thread?
wilsonpage commented 9 years ago
var src = 'my-service.js';
var base = document.location.origin;
var html = '<script src="' + base + '/threads.js"><\/script><script src="' + base + '/' + src + '"><\/script>';
var blob = new Blob([html], {type: "text/html"});
var iframe = document.createElement('iframe');
iframe.src = URL.createObjectURL(blob);
document.body.appendChild(iframe);
gmarty commented 9 years ago

What about the following?

iframe.src = 'data:text/html;utf-8,<!DOCTYPE html><script defer src="' + base + '/threads.js"><script defer src="' + base + '/' + src + '"></script>';

Also, note the defer attribute.

wilsonpage commented 9 years ago

Nice! Even simpler! I had no idea you could write data-uris like that!

wilsonpage commented 9 years ago

After some discussion with @arcturus we've decided that the for reasons of limiting 'magic' theads.js should not be auto-loaded into the window. It seems like it would be a slippery slope to adding a lot of script loading cruft to the library.

I wrote a gist to illustrate how it's possible to run the same service in a Worker or a Window.

wilsonpage commented 9 years ago

Closing this. Willing to reopen if this proves to be valuable.