private static MyUser1 ReduceUser(MyUser1 user, object action) { var changed = false; if (action is ActionChangeUserName a) { if (a.targetUserId == user.id) { user.name = a.newName; user.MarkMutated(); changed = true; } } user.contacts = user.MutateField(user.contacts, action, ContactsReducer, ref changed); if (changed) { user.MarkMutated(); } return(user); }
private static MyUser1 ReduceUser(MyUser1 user, object action) { if (action is ActionLogoutUser) { return(null); } if (action is ActionLoginUser a1) { return(a1.newLoggedInUser); } if (action is ActionOnUser a) { bool userChanged = false; var isTargetUser = a.IsTargetUser(user); var name = user.name.Mutate(isTargetUser, a, ReduceUserName, ref userChanged); var age = user.age.Mutate(isTargetUser, a, ReduceUserAge, ref userChanged); var contacts = user.MutateField(user.contacts, a, ReduceContacts, ref userChanged); if (userChanged) { return(new MyUser1(name, age, contacts)); } } return(user); // None of the fields changed, old user can be returned }