Architecting Angular Applications with Redux,RxJS,and NgRx
上QQ阅读APP看书,第一时间看更新

Listening to the dispatcher

We have established that the dispatcher dispatches a message to anyone who will listen. Now it is time to be that listener. The dispatcher needs a register or subscribe method so that you, who listens, have the ability to listen for incoming messages. The setup for that usually looks something like this:

dispatcher.register(function(message){});

Now, when you set up a listener this way, it will have the capability to listen to any message type being sent. You want to narrow this down; usually, a listener is specified to only handle a few message types around a certain theme. Your listener most likely looks something like this:

dispatcher.register((message) => {
switch(message.type) {
case 'SELECT_ITEM':
// do something
}
});

OK, so we are able to filter out only the message types we care about, but before actually filling in some code we need to think about who this listener is. The answer is simple: it is the store.