ruby-hyperloop / hyperloop-config

The project has moved to Hyperstack!! - internal gem used by other hyperloop gems to define config settings
https://hyperstack.org/
MIT License
3 stars 4 forks source link

add auto-load feature #6

Open catmando opened 6 years ago

catmando commented 6 years ago

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 commented 6 years ago

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

janbiedermann commented 6 years ago

closed by https://github.com/ruby-hyperloop/hyperloop-config/blob/edge/lib/hyperloop/autoloader.rb and https://github.com/ruby-hyperloop/hyperloop-config/blob/edge/lib/hyperloop/autoloader_starter.rb