nbb

Openefit 2023-09-05T05:27:31.464179Z

New to Clojure. I wrote a little countdown alarm script based on the ink-demo, but when I tested it with 1000 hours, I encountered a JavaScript heap out of memory error. I found out that the code below was causing significant memory and runtime overhead.

(doseq [n (range 1 @left-time)]
        (js/setTimeout #(swap! left-time dec) (* n 1000)))
Therefore, I modified it to use setInterval instead.
(def count
      (js/setInterval
       #(if (> @left-time 0)
          (swap! left-time dec)
          (js/clearInterval count))
       1000))
Should I submit a pull request in README for this?

borkdude 2023-09-05T07:42:04.223779Z

@openefit please do!

Openefit 2023-09-05T10:01:28.886409Z

Submitted, thanks for merging. By the way, nbb is really awesome!

borkdude 2023-09-05T10:02:19.565669Z

Thank you :)

👍 1
2023-09-05T20:22:50.195139Z

Working on a web framework for personal projects big and small. One big enhancement would be to auto-refresh the browser after sending data to the nrepl server. Are there any systems in place that would at least let me observe the eval request? Thinking if I could at least react to eval, I could broadcast a SSE to the browser to refresh in development mode.

2023-09-06T07:38:36.596239Z

Thanks! That’s exactly the kind of alternative I had in mind

borkdude 2023-09-05T20:27:08.565949Z

You mean some kind of middleware thingie?

2023-09-05T20:27:42.692869Z

Oh yeah! I forgot nrepl supports that 😅 That would probably work!

borkdude 2023-09-05T20:27:56.345519Z

well, nbb nrepl doesn't support it (yet)

borkdude 2023-09-05T20:28:14.172519Z

also bb nrepl doesn't support it (yet)

2023-09-05T20:30:28.908279Z

I see. I'll start by reading through https://github.com/babashka/nbb/blob/main/src/nbb/impl/nrepl_server.cljs and see where that leads

borkdude 2023-09-05T20:32:02.257629Z

I guess you could re-define your eval keybindings to something which evaluates + also adds a (broadcast ..) call to the mix in your editor

2023-09-05T20:34:12.226939Z

Oh right, something like that crossed my mind too. It's a viable alternative for personal use, but that would be a strong choice with the goal of publishing a framework

2023-09-05T20:34:54.436019Z

A quick solution I suppose would be to use something like browser-sync to watch cljs files, and refresh browser on save

2023-09-05T20:35:56.598079Z

Would it be possible to start a nrepl server via the api, then proxy it to intercept?

borkdude 2023-09-05T20:36:12.242429Z

there is an nrepl api yes

2023-09-05T20:39:04.963029Z

On second thought, maybe auto-refreshing on eval is not a great DX. It's not uncommon to check state like:

(comment
  @routes)
Which might create problems if it refreshes on every eval. Analyzing the eval to guess when it's a good idea to reload sounds messy so perhaps browser-sync or something more minimal inspired by it is a better solution

2023-09-05T20:40:52.486949Z

Yeah that might work better: • Watch cljs files for changes • On save, connect to repl and eval the file, if successful send SSE to refresh • Server adds a script on dev mode to listen for those SSEs and triggers a refresh

2023-09-05T20:42:35.634839Z

Argh maybe not, (sorry thinking out loud), not every file save should be eval'd. So maybe proxy request, but try and distinguish between a form eval (don't refresh), and a buffer or file eval (refresh). I am pretty sure every time I eval'd a buffer or file, I immediately refreshed the page to look for changes.

2023-09-05T20:46:39.078019Z

For the middleware approach, is it critical it matches the existing nrepl middleware interface\implementation? In theory, it wouldn't be much work to create a promise-based ring-like middleware system for the nbb implementation

2023-09-05T20:47:12.032309Z

I already have the promise-based ring-like middleware for my request layer so copying that over would be fairly trivial.

borkdude 2023-09-05T20:54:48.197859Z

I don't think nbb nrepl should be compatible with existing middleware since that's usually all based on JVM anyway, right

borkdude 2023-09-05T20:54:55.530819Z

feel free to draft something

2023-09-05T21:11:46.576889Z

To get started thinking... 1. Clone latest nbb (or update my fork) 2. Add in middleware args and support it in the server API 3. Use npm link 4. Link this client project to it, start with some logging middleware to see what happens Does that make sense? One more question that comes to mind: Should we support middleware out of nbb.edn or is it ok to start without that for now?

borkdude 2023-09-05T21:26:13.001229Z

probably makes sense

Chris McCormick 2023-09-06T04:56:36.135829Z

@jayzawrotny you might find this a useful reference too: https://github.com/chr15m/sitefox/blob/32d33aa723de9afa578efc4f35bcc8e2ddfbb7ae/src/sitefox/reloader.cljs#L10

Chris McCormick 2023-09-06T04:57:01.652649Z

It is not tested very well but last time I checked it was working.