我在 authobahn 使用通量,我对我的架构感到担忧。我有一些组件通过订阅 pubsub 主题来获取它们的状态。
目前我正在使用通量来获取数据,我的操作如下所示:
// 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;
}
});
这是处理订阅的正确方法吗?我不太喜欢 subscribeToRoom 动作的想法,它总是监听事件,并在调度程序中发送订阅。