Giter Site home page Giter Site logo

Comments (5)

dertseha avatar dertseha commented on July 19, 2024

In which business cases would this be required? Which problem should it solve?
I'm curious since I don't see a reason to have such a functionality. (Which doesn't mean there is no use for it, I don't see a problem that could be solved differently, which is why I'm asking for other aspects)

from postal.js.

clescot avatar clescot commented on July 19, 2024

Hi,
thanks for the quick reply.
i need to execute a callback after 2 events are fired : the first one is fired when modernizr load a sessionStorage polyfill if not supported by the browser.
the second event contains a variable.
the final callback which needs to be executed use the sessionStorage and the a variable.
my first idea was to set a priority on handlers.
i've resolved my issue by positionning some booleans when handlers are called. when all booleans are true, i execute the desired function.
so, maybe we need a postal.subscribe function which take as argument an array of channel/topic, which fire a callback when all events are emitted.

Charles.

from postal.js.

dertseha avatar dertseha commented on July 19, 2024

Ah, ok - this is a different topic and something like withPriority() wouldn't help you here.
For clarification, withPriority() would only be applicable if the bus is somehow federated with another transport which could provide more than one event for distribution within one transport package - and the local bus could use the priority indicator to decide the order of the notifications. But since typical event distribution - even as in your case - is done one-by-one, with possible long time between them, it wouldn't help you here; For each event there wouldn't be any other at the same time to compete with.

What you are facing is a state-machine related problem. You have four states: Your starting state and your 'final' state that you wait for, as well as two intermediate states, one for each of the two events to come first ("A ready, B pending" and "B ready, A pending"). This state-machine could be implemented full-blown with dedicated objects per state, or, in a simple form, using booleans like you did.

The event bus can't help you here - it wouldn't be its responsibility. While your case is a simple one with just two events close together, a full blown solution needs to consider N events with various delays between them - not to mention error conditions.
No, the user has to handle this separately, but there are libraries to help you here. As it so happens, @ifandelse also provides https://github.com/ifandelse/machina.js . I didn't look into it, but I believe I also saw some postal.js adapter for that somewhere.

from postal.js.

clescot avatar clescot commented on July 19, 2024

thanks for your very concise response!
you're right, i need either to keep my boolean solution, or use a FSM solution for a more important problem.
i will look towards https://github.com/ifandelse/machina.js .

best regards,

Charles.

from postal.js.

webpro avatar webpro commented on July 19, 2024

You could also see this as a matter of "async control flow". One solution to go down that road is using (two) promises, and continue a flow when both operations are resolved (the fact that the variable is already, synchronously, available does not really matter here, just resolve a promise with that value).

from postal.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.