I am using flux with authobahn and I have concerns about my architecture. I have some components that gets their state by subscribing to pubsub topic.
Currently I am using flux for my data fetching and my actions looks like this:
// I have a global session
window.AutobahnSession = ..;
// RoomsActions
var RoomsActions = {
subscribeToRoom: function(roomName) {
var onData = function(data) {
Dispatcher.dispatch({
type: "ROOM_MESSAGE_ARRIVED",
message: data,
roomName: roomName
});
};
var subscription = window.AutobahnSession.subscribe('rooms/' + roomName, onData);
// Sending the subscription to be persisted on the store,
// So I can close & remove it later.
Dispatcher.dispatch({
type: "ROOM_SUBSCRIPTION_CREATED",
subscription: subscription,
roomName: roomName
});
},
// Called on componentWillUnmount
unsubscribeToRoom: function(roomName) {
Dispatcher.dispatch({
type: "UNSUBSCRIBE_TO_ROOM",
roomName: roomName
});
}
};
// RoomsStore
RoomsStore.dispatchToken = ChatAppDispatcher.register(function(action) {
switch(action.type) {
case "ROOM_MESSAGE_ARRIVED":
// I have the message to a messages array and emitChange
break;
case "ROOM_SUBSCRIPTION_CREATED":
// I add the subscription to mapping from roomName to subscription
break;
case "UNSUBSCRIBE_TO_ROOM":
// Get the subscription by it's room name, and close it and
// remove from the mapping.
break;
}
});
Is this the right approach for handling the subscriptions is good? I don't really like the idea of subscribeToRoom action which always listen for events, and sending the subscription in the dispatcher.