conjure icon indicating copy to clipboard operation
conjure copied to clipboard

Wait until a nREPL connection is created?

Open Invertisment opened this issue 1 year ago • 2 comments

I have developed a plugin that adds some middleware. But if I try to call server.with-conn-and-ops-or-warn then it doesn't crash when the REPL isn't connected. So the call either results in a NOOP or it succeeds. It's random.

So I would like to have some kind of a way to know that it succeeded.

The callback would also be useful when Conjure decides to reconnect to a different REPL. It could notify the child plugins about it.

The alternative could be that I would try to brute-force that function until it "connects" but it's not a good thing to do.

Invertisment avatar Aug 15 '22 07:08 Invertisment

The function takes a opts table which can contain an :else function which will be called if there is no current connection. You could use that to schedule a retry until the REPL is connected. This is pretty internal and undocumented though so YMMV over time if I end up refactoring this code one day. Should be stable for a long time though.

I think you'll just have to check the state of the client and inspect the REPL it's connected to if you want to know if you're still connected to the same one. Not planning on adding plugin-plugin support (plugins for Conjure as a first class thing), I'd rather they piggieback on what is already there or are added to the core through pull requests etc. Some core changes can be made to accommodate plugins but I don't plan on adding notification systems, hooks, extra APIs and deep internal documentation for every internal function (there's a LOT of code I've written over years and it changes, it'd be really hard to maintain that right now).

You can also use this to see if there's a connection https://github.com/Olical/conjure/blob/08236a13164a948fe403f34fe1f593ead0a3c6fe/fnl/conjure/client/clojure/nrepl/server.fnl#L25-L28

Or inspect the state and get the current connection like so: https://github.com/Olical/conjure/blob/08236a13164a948fe403f34fe1f593ead0a3c6fe/fnl/conjure/client/clojure/nrepl/server.fnl#L16

If you're going to be reaching into the internals of Conjure and poking things, might as well reach deeper and access the data you're interested in. It's all there, you have access to it, just have to dig through the code a little to see how I'm doing it. Shouldn't need anything extra added to accommodate further plugins (although I'd probably refer to them as mods since they're piggiebacking and modifying Conjure while it's running?).

Olical avatar Aug 17 '22 12:08 Olical

I already did what you described (unfortunately the connected? function was private so I copied the code):

https://github.com/Invertisment/conjure-clj-additions/blob/master/fnl/conjure-clj-additions/additional-fns.fnl#L47-L52

https://github.com/Invertisment/conjure-clj-additions/blob/master/fnl/conjure-clj-additions/additional-fns.fnl#L55-L56

Even the timer:

https://github.com/Invertisment/conjure-clj-additions/blob/master/fnl/conjure-clj-additions/load-util.fnl#L8-L24

The only problematic thing is that sometimes the test middleware fails to load and then I must restart the REPL as even the editor restarts don't help. That I can't understand yet.

Also this timer loading mechanism sometimes produces multiple error messages when it fails to connect and I think it's somehow related to vim.schedule_wrap. When I jump through buffers several times rapidly then it produces some errors as the timer still goes on.

I'll try the :else option. It will also be handy (if related) to probably detect when test namespace is not found (there is no way to do it if I use nREPL to run tests because I use test namespace lookup matcher and I output "ok/not ok" in the command field instead of the log).

Invertisment avatar Aug 17 '22 12:08 Invertisment