helix

https://github.com/Lokeh/helix
Aron 2020-05-21T10:27:48.410600Z

I did a quick search and nothing came up, is there some guide maybe on how to use async stuff with hooks? That is, I am somewhat familiar with csp, but haven't used it in a long time. I know that I want to run a .focus() call in a use-layout-effect hook, every time some specific event happens. I could use an event listener in a useEffect that runs once and then trigger it every time, but that requires me to add additional references to the components, complicating things. Instead, I would like to export a channel and when a specific message appears on the channel, run the layout effect. Does this make sense?

orestis 2020-05-21T11:56:28.414900Z

Hrm I think that you can’t control to run or not run a layout effect, right? Rules of hooks say no conditionals. I haven’t used core async so any guidance might be wrong there, but I think the general approach is to start a “process” on mount and clean it up on unmount. Process could be an event listener or a go-loop?

Aron 2020-05-21T12:00:47.415700Z

You can run it with dependencies and internal conditionals

Aron 2020-05-21T12:01:10.416200Z

so you trigger when changes something and then check that the something isn't null

Eliraz 2020-05-21T13:05:20.417100Z

@ashnur it does make sense.

Eliraz 2020-05-21T13:06:49.417400Z

I used hook that returns a channel

Eliraz 2020-05-21T13:07:57.418100Z

and once the channel got something, it takes it and put it on a state / atom.

Eliraz 2020-05-21T13:08:52.418700Z

when I'm thinking about it, you can actually build a state management system with core.async

Eliraz 2020-05-21T13:09:13.419Z

maybe there is one already?

Aron 2020-05-21T14:00:13.419900Z

Maybe, but I want to keep this simple dependency and instrumentation wise, at this point the whole thing is just some hooks, not even a use-reducer

Aron 2020-05-21T14:01:18.421200Z

Good idea to make a custom hook return a channel, I was trying to imagine it from the other side where the message is coming from, now it's clear why that is a bad idea : )

dominicm 2020-05-21T16:27:30.421500Z

Should fast-refresh work for components where I'm not using helix at all? e.g.

(defn c [^js props]
  (let [[s s!] (react/useState nil)]
    ...))
I'm not saying it doesn't (haven't tested first hand), but I have reports it doesn't & I wanted to check whether I'm supposed to dig in or not.

lilactown 2020-05-21T16:29:16.421700Z

it will not work, no

lilactown 2020-05-21T16:30:13.422500Z

for each component, certain function calls need to be emitted to register the component with the fast-refresh runtime

lilactown 2020-05-21T16:35:36.426200Z

the moving pieces are: • a “signature” function is created specifically for the component: https://github.com/Lokeh/helix/blob/master/src/helix/core.clj#L151-L153 • after the component has been defined, the signature function is called with a hash of the current hooks used inside the component: https://github.com/Lokeh/helix/blob/master/src/helix/core.clj#L179-L182 • next, the component is then registered with a unique identifier for the component that will remain stable across reloads: https://github.com/Lokeh/helix/blob/master/src/helix/core.clj#L183 • finally, the component calls the signature function on render to handle some edge case with custom hooks I can’t remember: https://github.com/Lokeh/helix/blob/master/src/helix/core.clj#L165-L166

1👍
lilactown 2020-05-21T16:38:07.426400Z

helix handles generating all that code for you

Aron 2020-05-21T16:56:12.426800Z

@lilactown do you use forwardRef ?

lilactown 2020-05-21T16:56:35.427200Z

I haven’t used forwardRef but helix components should support it

lilactown 2020-05-21T16:56:41.427400Z

what’s up?

Aron 2020-05-21T16:57:27.427800Z

eh, I am just struggling between a bunch of bad decisions : )

Aron 2020-05-21T16:59:18.429900Z

have to figure where to add an additional element just so I can scroll it into the view and then use a reference from it and share it with the rest of the app

orestis 2020-05-21T17:14:29.431100Z

Is there anything else to be done to get fast-refresh working? Shadow, repl support, etc?

lilactown 2020-05-21T17:15:41.431300Z

AFAIK shadow supports it well enough when using the :reload-strategy :full dev config

lilactown 2020-05-21T17:17:36.431500Z

I think there’s still some work to be done to ensure that we are reloading hooks correctly. the heuristic helix currently uses to generate the hook signature per component is pretty naive

lilactown 2020-05-21T17:17:50.431700Z

and currently, custom hooks are not handled at all

lilactown 2020-05-21T17:18:00.431900Z

but it should work for the 80% case

lilactown 2020-05-21T17:21:16.432100Z

by “custom hooks are not handled at all” I mean that you can generate signatures for custom hooks so that changing a custom hook will also reload components that use it correctly

dominicm 2020-05-21T17:25:00.432300Z

We've got it working with Figwheel

lilactown 2020-05-21T18:00:02.432500Z

glad to hear!