Skip to content
This repository was archived by the owner on Oct 19, 2018. It is now read-only.

add auto-load feature #6

Open
catmando opened this issue Sep 28, 2017 · 2 comments
Open

add auto-load feature #6

catmando opened this issue Sep 28, 2017 · 2 comments

Comments

@catmando
Copy link
Contributor

Consider this example to understand the theory:

class Bar < Foo
  ...
  do_something_with Baz
  ...
  def my_fat_method
    Hello.there()
  end
end

Hyperloop automatically requires all files in the hyperloop directory in alphabetical order. This means the above case will not work (assuming Foo is a file named foo.rb, and Baz is in baz.rb.)

Rails does all this by intercepting const_missing, and using file naming convention finds the file, requires it, and returns the newly defined const.

In the case of Hyperloop we have to do this by running a preprocessor whenever we notice a file has changed or been added.

I think the preprocessor can actually leverage the rails autoload feature like this:

  1. Patch the auto-loader so it logs someplace when it has to require a file (so we can record the load order)
  2. create dummy classes and modules for all the client side hyperloop constants (i.e. Hyper::Component, Hyperloop::Store etc etc) (otherwise we aint going to find these)
  3. Then start loading the files in alphabetical order and adding each file to the same log as the auto-loader patch is using.

The log will now be the ordered list of requires!

If this doesn't work, then another approach would be to do something similar in the server side JS environment.

Finally there have been other ideas about dynamically requiring and loading the code from the server in JIT fashion. This would be great as the actual page download would now be dependent only on the files used! By architecting systems (and hyperloop itself) you could automatically minimize the download size. Pretty complicated but cool.

And now really finally: I don't quite understand why Opal can't do this anyway??? Opal has all the code compiled ahead of time, and the requires (which must be static) point to the object to "load" (meaning execute top level context). So why can't we just keep track of the file names, and implement our own auto-loader in Opal itself.

@catmando
Copy link
Contributor Author

catmando commented May 5, 2018

@janbiedermann is this closed now? It seems like ordering is taken care of somehow

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants