Comments (5)
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.
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.
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.
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.
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)
- How to publish data between Node.js modules? HOT 2
- Bower Release v2.0.5
- Competing consumers HOT 1
- Help improve the quality of the DefinitelyTyped (TypeScript) definitions
- postal.preserve: Still taking a ConduitJS dependency HOT 2
- Default Subscriber
- Stack overflow in IE 11 when publish called on channel with many pending unsubscribes
- Shouldn't postal.channel() implement the Singleton pattern? HOT 2
- Subscriber callback method can brake calling other subscribers HOT 1
- Devtools extension for Chrome
- Messages published inside a subscriber won't be passed to uncached subscribers
- why cache only for result === true
- postal.lodash.js Error: Module name "lodash/after" has not been loaded
- Is this project still active? HOT 6
- Wait for messages to be delivered and processed in unit tests
- timeStamp in envelope should honor timezone
- Timestamp missing on wireTap envelopes
- Thank you
- Regarding publish and subscribe, Important question
- How does postal.js support replay?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from postal.js.