Fork me on GitHub
#biff
<
2022-12-27
>
Epidiah Ravachol19:12:42

Hello there, I've got a question about changing one of the bb tasks, specifically dev, I think. I'm a little new to this and a bit unsure. I'm writing my code in vim and using https://liquidz.github.io/vim-iced/ to handle all the repl interaction. But to connect to a repl, vim-iced needs me to launch that repl with its own iced repl command--a step that bb dev bypasses. I suspect I can fix it by changing line 90 of com.biffweb.tasks to (apply shell {:extra-env {"BIFF_ENV" "dev"}} "iced repl" but I'm not sure of the best way to go about do that. Any advice would be welcomed!

Jacob O'Bryant19:12:07

Hey! You can override tasks by defining a custom dev task in your project (https://github.com/jacobobryant/biff/blob/master/example/tasks/src/com/example/tasks.clj) and then edit bb.edn (https://github.com/jacobobryant/biff/blob/master/example/bb.edn#L2) to point to that custom task. See the hello task for an example. So you can copy-and-paste the dev task into that file and make whatever changes you need. Switching that command to launch iced repl should do the trick. Then from vim, you can start the app by calling the start function from your main namespace.

1
Jacob O'Bryant19:12:14

That being said, it looks like you can also just run the normal bb dev command and then do :IcedConnect

Jacob O'Bryant19:12:38

A potential downside is that iced repl might be injecting additional libs/nrepl middleware that Biff does not, so you might want to edit the dev task as you've mentioned anyway. On the other hand--if you ever want to connect to your app in production via bb prod-dev or bb prod-repl, you'll need to use :IcedConnect anyway. You could try :IcedConnect and then see if everything works normally, and then switch to a custom dev task only if you run into any problems.

Epidiah Ravachol20:12:21

Yeah, I thought I would be able to just run bb dev and then feed the port to the :IcedConnect command, but it was throwing an error telling me to start the repl with iced repl instead. At some point, I think I inadvertently got two repls going, one from bb dev and one from vim-iced, which was the cause of some confusion.

Epidiah Ravachol20:12:42

Creating a custom bb dev has things working now, though!

Jacob O'Bryant20:12:27

sounds good! If you ever want to get :IcedConnect working e.g. for use with prod, you should be able to have biff emulate iced repl's behavior by adding libraries to deps.edn as needed and modifying the -main function (https://github.com/jacobobryant/biff/blob/master/example/src/com/example.clj#L72) to start nrepl with the same middleware that iced repl does--might need to poke around in Iced's source code to see exactly what it's including though.

Epidiah Ravachol20:12:01

Thanks! That seems pretty simple. Presumably, once I do that, I can remove the custom bb dev.

👍 1
Epidiah Ravachol20:12:25

Got to say, I'm digging working with Biff.

Jacob O'Bryant21:12:08

Yep. And glad to hear it!

Epidiah Ravachol14:12:24

Weirdly, after two days of the overriding tasks solution working, bb is telling me it can't find the namespace I put the override in. Near as I can tell, I haven't touched anything that would affect that. So I suspect it is haunted. Might have to investigate modifying the -main function sooner than I thought.

Jacob O'Bryant16:12:33

this is a bug in bb I've been meaning to investigate/mention in the docs/report. sometimes the class path gets messed up/stale for some reason. you can fix it by passing --force to bb, e.g. bb --force dev or bb --force -e nil

Epidiah Ravachol16:12:32

Wonderful! That did the trick. I can put my investigations off for another day.

🙂 1
pavlosmelissinos19:12:46

Is there a straightforward way to do a com.biffweb/submit-tx and then a com.biffweb/q on the updated state within the same HTTP request? (get-sys) works but that's not meant for production. I thought xtdb.api/await-tx would work but it doesn't.

Jacob O'Bryant20:12:29

Yep:

(fn some-handler [{:keys [biff.xtdb/node biff/db] :as req}]
  (let [;; biff/submit-tx includes a call to `xt/await-tx`, so after it returns
        ;; you know the transaction has been indexed.
        _ (biff/submit-tx req [...])
        db (xt/db node)]
    (q db ...)))

;; alternatively
(fn some-handler [{:keys [biff/db] :as req}]
  (let [_ (biff/submit-tx req [...])
        ;; useful if you want to pass req to more functions and have it include
        ;; the new db value
        {:keys [biff/db] :as req} (biff/merge-context req)]
    (q db ...)))

🙏 2
pavlosmelissinos20:12:39

Oooh, I didn't realize node was mutable! 😅 Thanks, that makes it all much easier

👌 1
🎅 1