Jan 6, 2018 • Arnout Roemers
Version 2.0.0 of mount-lite was a breaking version. Most notably, as written in this previous blog post, version 2 is vastly smaller and simpler than version 1, and some features were removed.
As some of you may know, version 1 had the ability to have mount-lite infer the state dependency graph, instead of just a dependency sequence based on the load order. The tools.namespace library was used for this feature. More specifically, version 0.3.0-alpha-3 was used, expecting it to be released soon. That was two years ago, and it is still in alpha. This is not to nag on the developers of tools.namespace by the way.
Many other projects used and still use the stable version 0.2.11 of tools.namespace, yielding conflicts with mount-lite. Because of this and not seeing the feature being used a lot in the projects where I introduced mount-lite, the dependency graph feature was removed in the version 2 rewrite. The tiny core of version 2 went back to using the simpler load-order sequence, just as the original mount library does.
And then this issue was posted on the GitHub repository.
Dmitrii Balakhonskii wanted to use mount-lite, but required this inferred dependency graph feature. I could sympathize with him; it was a nice feature! And although the core of mount-lite has become very small, it offers an easy extension point. It also ships with a few extensions, but those were not sufficient for Dmitrii’s cyrus project template.
He gave a good pointer however on how to calculate the dependency graph with the stable version of the tools.namespace library. I thought, why not add this dependency graph feature again, but this time as an optional extension? So we did.
Say you have the following namespaces with defstates:
When Clojure loads these namespaces (assuming namespace
d is the entrypoint), mount-lite will by default use the following states sequence:
If you would call the standard
(mount/start #'c/c), it would start all states up to
c in the sequence.
This includes state
b, which is technically not required, as the
c state does not really depend on it.
This is where the new extension can help.
Using this extension, a dependency graph is used instead.
Schematically, the dependencies can be represented as below.
Note that the dependencies are deduced based on namespace level; while state
d does not technically require
c2, it is deemed as such.
Using above dependency graph, mount-lite deduces that state
b is not required by
c, and therefore won’t start it.
The same goes for when you stop your states up to a certain state; if all states are started, and you call
(stop #'b/b), it would not stop state
c2 (and neither state
a of course).
For the extension to work, you need to include
[org.clojure/tools.namespace "0.2.11"] in your project’s dependencies.
Since the feature is an optional extension, this dependency is not included by default.
And of course, you need
The extension resides in the
mount.extensions.namespace-deps namespace, and has a
stop function, just as you are used to.
So in most cases you can just replace
mount.lite with the new namespace in your source code, and you’re done:
More documentation on the new extension can be found here.
As always, have fun!