如何使用 Immutable.js 提供 Flux 商店之间的依赖关系?

How to feed dependencies between Flux stores using Immutable.js?

我有一个简单的聊天应用程序和以下商店:

我正在使用 immutable.js 来存储数据。问题是,MessageStore 需要使用来自 ChatGroupStoreUserStore 的数据,每条消息都是这样构造的:

{
    id: 10,
    body: 'message body',
    peer: {...} // UserStore or ChatGroupStore item - destination
    author: {...} // UserStore or ChatGroupStore item - creator of the message
}

我应该如何根据 ChatGroupStoreUserStore 更新来更新 MessageStore 项目?

我是这样使用 AppDispatcher.waitFor() 的:

MessageStore.dispatchToken = AppDispatcher.register(function(action) {
    switch(action.actionType) {
    case UserConstants.USER_UPDATE:
        AppDispatcher.waitFor([
            UserStore.dispatchToken
        ]);

        // update message logic
        break;
    }
});

从我的角度来看,我将不得不等待 UserStore 更新,然后找到更新用户的所有消息并更新它们。但是我如何找到更新的对等点?我认为通过引用在 UserStore 中搜索是不够的,因为不可变数据在数据更改时不会保留引用,那么我将不得不在查询中应用更多。但随后我将不得不在 MessageStore 处理程序中应用其他商店的查询逻辑。

目前我正在将同行存储为每条消息中的参考,也许我应该改为:

{
    id: 10,
    peer: {
        peerType: 'user', // chatGroup
        peerId: 20
    }
}

如果有人能对此有所了解,那就太好了。我真的很困惑。

在所有情况下,我认为最好的解决方案是不要将相关数据嵌套并避免对来自服务器的数据进行转换,这将减少我需要做的工作量来保存数据始终保持最新。那么在你看来,你所要做的就是订阅变化,把必要的数据放在一起。

助焊剂的替代品

还有一个良好且维护良好的状态容器解决方案,称为 Redux which I suggest everyone to at least try. It has only one store and combines the whole state into a single deep object, although you can create each reducer separately. It also has a good way to integrate it with React, see Usage with React