let currentConversation = props.conversation;
if (!currentConversation && conversations) {
currentConversation = conversations
.filter(item => {
if (item && item.members) {
return fromJS(item.members).includes(
props.remoteUser.user.id.toString()
);
} else {
return false;
}
})
.first();
}
filter
过滤集合中需要的数据, 详解: https://github.com/facebook/immutable-js/blob/master/type-definitions/Immutable.d.ts#L1977fromJS
将 JSON 对象转换为 Immutable 对象, 源码:: https://github.com/facebook/immutable-js/blob/master/type-definitions/Immutable.d.ts#L86includes
(同 contains) 判断是否包含某个值, 源码: https://github.com/facebook/immutable-js/blob/master/type-definitions/Immutable.d.ts#L1763first
返回集合中的第一个值, 源码: https://github.com/facebook/immutable-js/blob/master/type-definitions/Immutable.d.ts#L1769
shouldComponentUpdate(nextProps, nextState) {
let sameState = is(fromJS(this.state), fromJS(nextState));
let sourceSame = is(fromJS(nextProps.source), fromJS(this.props.source));
let metaSame = is(fromJS(nextProps.meta), fromJS(this.props.meta));
let loadingSame = is(fromJS(nextProps.isLoading), fromJS(this.props.isLoading));
return !sameState || !sourceSame || !metaSame || !loadingSame
}
fromJS
将 JSON 对象转换为 Immutable 对象, 源码:: https://github.com/facebook/immutable-js/blob/master/type-definitions/Immutable.d.ts#L86is
判断两个对象是否相等,源码::https://github.com/facebook/immutable-js/blob/master/type-definitions/Immutable.d.ts#L109
state => state.getIn(["messages", "isDone"]);
state => state.get("auth");
getIn
取出树形结构中 messages -> isDone 的值,源码::https://github.com/facebook/immutable-js/blob/master/type-definitions/Immutable.d.ts#L1783get
用 key 取值,源码::https://github.com/facebook/immutable-js/blob/master/type-definitions/Immutable.d.ts#L1752
let messageList = state
.updateIn(["messages", action.message.id], oldValue => {
if (oldValue) {
return [action.message, ...oldValue];
}
return oldValue;
})
.set("isLoading", false);
return messageList;
updateIn
更新树形结构中 messages -> id 的赋值,源码::https://github.com/facebook/immutable-js/blob/master/type-definitions/Immutable.d.ts#L605set
给指定的 key 赋值,源码::https://github.com/facebook/immutable-js/blob/master/type-definitions/Immutable.d.ts#L166
export default function reducer(state = initialState, action) {
switch (action.type) {
case types.GET_CONV:
return state.withMutations(s =>
s.set("conv", fromJS(action.data)).set("isLoading", false)
);
case types.UPDATE_CONV:
return state.update("conv", conv => {
let inConv = false;
let newConv = conv.map(item => {
if (item.get("id") === action.data.id) {
item = item.mergeDeep(fromJS(action.data));
inConv = true;
}
return item;
});
if (!inConv) {
newConv.unshift(fromJS(action.data));
}
return newConv;
});
default:
return state;
}
}
withMutations
操作多次,只会在最后一个操作 clone 出新的 Immutable 对象来,源码::https://github.com/facebook/immutable-js/blob/master/type-definitions/Immutable.d.ts#L706mergeDeep
深度合并,当有冲突时,用action.data
值覆盖item
值,源码::https://github.com/facebook/immutable-js/blob/master/type-definitions/Immutable.d.ts#L550