Flux和AutobahnJs

时间:2015-10-05 15:01:18

标签: reactjs-flux flux autobahn wamp-protocol

我正在使用flux与authobahn,我担心我的架构。 我有一些组件通过订阅pubsub主题获得他们的状态。

目前我正在使用flux来获取数据,我的操作如下所示:

// 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操作的想法,它始终监听事件,并在调度程序中发送订阅。

0 个答案:

没有答案