cujojs / wire

A light, fast, flexible Javascript IOC container
Other
861 stars 68 forks source link

Container Compilation? #166

Open ev45ive opened 10 years ago

ev45ive commented 10 years ago

I love what you have done. I would very much like to use something like wire.js in my projects. There is DI for backbone, there is one for Angular but there are not even close to wire.js. There is a concern that library is still in beta, and there are speed and debugging issues I am afraid of in production intended projects.

AFAIK for bigger applications wire.js (with lots of promises and big call stacks) gets slower and not easiest thing to debug. I like to think about scopes as Dependency Injectors. All major DIs i know do container compilation to give back the spped and inline some of the wiring as plain function factories.(especially when values are given, and synchronous - no need for promises and long call stacks then).

There exist now a lot of work on javascript compilation / transpilation (using Node.js and alike) out there. Situation is even simpler here as all we need to do is compile JSONlike wire files to flat js code, no JS parsing needed.

Have you thought about it? Any plans to implement it?

briancavalier commented 10 years ago

Hey @ev45ive, thanks! @unscriptable and I had talked about the idea a while back, and about possibly doing this via wire's cram build plugin. It would be a really cool feature.

It's possible to structure applications into multiple wire specs that are wired as the user navigates through the system, rather than wiring the entire application at startup time. In fact, that was one of the original design goals when we first started working on wire. We've worked on large projects, and wiring speed hasn't yet proven to be a significant issue. Wire also got significantly faster recently with the update to use when.js 3.x.

All that said, it's always nice to make wiring faster :)

I think it would be difficult in the current architecture to get to a fully-compiled version, where a wire spec is completely transformed into imperative code (especially synchronous, imperative code since wire is highly async and handles non-determinism in component ordering!).

However, I think it would be extremely interesting to try to see what is possible. For example, maybe there are a few simple things we could start with, such as statically determining the optimal component wiring order based on a dependency graph built from $refs (wire already builds such a graph internally to detect circular dependencies). With that graph, it may be possible to output some imperative code that minimizes promise usage and still wires everything in the right order. It'd probably take some refactoring to expose the graph, but it's probably doable.

Would you be interested in doing some experiments or helping with such a thing?