Fork me on GitHub
#re-frame
<
2020-09-14
>
Eugen14:09:48

hi, I'm quite new with re-frame and I trying to figure out how to deal with global objects from window.xxxx ? I would put them in an atom as not to have external references all over the code and set the atom during application initialization. (don't know yet how to detect changes to these global objects, I assume the reference will be stable througout the lifetime of the app / browser window). What is the pettern for dealing with this situations (links to docs appreciated) ?

isak14:09:50

I don't see any need for a pattern there - just access the object normally (.-thing js/window)

Eugen14:09:27

how do I handle tests ?

Eugen14:09:57

if they don't run in the browser and I would like to pass a shape of that object.

Eugen14:09:01

my use case is for an integration MetaMask browser extension that adds window.ethereum. I believe I can create the ethereum object via a librar if need be

Eugen15:09:02

my concern is that if I depend on (.-ethereum js/window) I get all these references to a global, external object scattered across the app

Eugen15:09:29

but I am new so I don't know any better, just asking some questions to understand why

p-himik15:09:12

It depends on where you use the values. If you use them in event handlers, you can create a cofx that injects their values. If you use them in subs, then I think the only approach would be to store the value in the app-db, make the subs use that value, and update the value using events from JS. At are events at least for some of the properties of window.

p-himik15:09:14

If you care only about testing and don't care about keeping your subs pure, then you can just wrap all access to js/window in a function that can be mocked or reconfigured in tests.

✔️ 3
Eugen15:09:19

thanks, it's a bit more clear now. I would like to have them pure. I like a stable floor that doesn't dissappear from underneath me when I'm coding 🙂

superstructor22:09:08

Custom coeffects (for sync reads) and effects (for async reads and any writes) are the pure solution to this problem. @U011NGC5FFY And @U2FRKM4TW is correct that you you'd need to put values in app-db to use in subs. In fact, sub functions should be a function of their inputs (i.e. query-v for signal fns, and app-db or other subs for computation fns) otherwise it breaks the subscription cache and sub deduplication. As equality of two subs is if the query-v is = .

👍 3
mikethompson23:09:52

I'd hook the windows events and cause a dispatch

(.onresize js/window #(dispatch [:window-resize (.innerHeight js/windows) ...]))

mikethompson23:09:44

In the event handler I'd update the value in app-db. Then the subscription will fire.