Fork me on GitHub

Hi there! How should I handle async coeffects? I’m using this fx for loading data from async-storage, but i realised that it should be cfx instead, is there way to handle it properly?

(reg-fx :localstorage  (fn [{:keys [on-success on-fail]}]
                         (-> (async-storage/load)
                             (.then on-success)
                             (.catch on-fail))))


@glebkaf If you are loading something async then it should be handled with an effects handler like re-frame-http-fx (i don't mean you should use re-frame-http-fx, just that the process will be the same)


@mikethompson Yeah, thanks 🙂 I'm already using effect handler like this aswell as async-flow, just thought that it should be cofx since my event handler "requires localstorage from the world". I'm wished something like this:

    [(inject-cofx :asyncstore)]  
    (fn [{:keys {lasyncstore}} _]
    ; proceed with asyncstore


If this is one step in a boot sequence, which involves coordinating multiple async actions, you might need


Perf question: who’s using dispatches for literally everything in their re-frame apps? My code is getting a bit cumbersome building up and reducing big mappings of functions to process data in an event handler. I’ve started to see that I could simplify it into lots of dispatch calls - with the new 0.8 pure dispatch syntax, any cool optimisations going on? I.e. if I dispatch in an event handler, is it processed right away or might it be some fraction of a second later in a different requestAnimationFrame?


@heeton From what I understand any dispatch will be ran in the next animation frame.


@musheddev Even though we can start to inspect the return from a pure event handler and immediately execute?


No, not really true. The dispatch will be run pretty much immediately.


Just to be clear: Next Annimation frame is potentially 16ms away. But a dispatch is handled virtually immediately.


@mikethompson virtually meaning ? My scenario would involve doing maybe a dozen minor calculations and assignments to the DB via dispatching instead of reducing a set of operations over the db myself in one handler. Should I be concerned potentially skipping into new frames, or is it a non-issue?


Classic relational transaction sort of stuff. If I handle an event and decrease one user’s bank account balance, then dispatch an event to increase another’s, am I ever likely to get an in-between state that isn’t consistent? Or do they happen at effectively the same time no matter if I’m doing hundreds of things like that.


@mikethompson So events dispatched through an event handler would executed immediately in the order listed like a function call, while events dispatched through an ui event handler would be queued to the next animation frame?


@musheddev no, every dispatch is put into a queue the same way. Then handled FIFO


@heeton See goog.async.nextTick for definition of immedaitely


Sweet, thanks


is there a good article anwhere on vs reframe ?