本文介紹了更新反應(yīng)堆中對(duì)象的狀態(tài)的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
給出以下方法:
private Mono<UserProfileUpdate> upsertUserIdentifier(UserProfileUpdate profileUpdate, String id){
return userIdentifierRepository.findUserIdentifier(id)
.switchIfEmpty(Mono.defer(() -> {
profileUpdate.setNewUser(true);
return createProfileIdentifier(profileUpdate.getType(), id);
}))
.map(userIdentifier -> {
profileUpdate.setProfileId(userIdentifier.getProfileId());
return profileUpdate;
});
}
switchIfEmpty
和map
運(yùn)算符使profileUpdate
對(duì)象發(fā)生突變。在switchIfEmpty
運(yùn)算符中進(jìn)行變異是否安全?關(guān)于map
,如果我理解正確的話,這是不安全的,對(duì)象profileUpdate
必須是不可變的,對(duì)嗎?例如:
private Mono<UserProfileUpdate> upsertUserIdentifier(UserProfileUpdate profileUpdate, String id){
return userIdentifierRepository.findUserIdentifier(id)
.switchIfEmpty(Mono.defer(() -> {
profileUpdate.setNewUser(true);
return createProfileIdentifier(profileUpdate.getType(), id);
}))
.map(userIdentifier -> profileUpdate.withProfileId(userIdentifier.getProfileId()));
}
在鏈的后面,另一個(gè)方法改變對(duì)象:
public Mono<UserProfileUpdate> transform(UserProfileUpdate profUpdate) {
if (profUpdate.isNewUser()) {
profUpdate.getAttributesToSet().putAll(profUpdate.getAttributesToSim());
} else if (!profUpdate.getAttributesToSim().isEmpty()) {
return userProfileRepository.findUserProfileById(profUpdate.getProfileId())
.map(profile -> {
profUpdate.getAttributesToSet().putAll(
collectMissingAttributes(profUpdate.getAttributesToSim(), profile.getAttributes().keySet()));
return profUpdate;
});
}
return Mono.just(profUpdate);
}
上述方法調(diào)用方式如下:
Mono.just(update)
.flatMap(update -> upsertUserIdentifier(update, id))
.flatMap(this::transform)
推薦答案
回答模糊,但…視情況而定!
在返回的Mono
或Flux
中突變輸入?yún)?shù)的危險(xiǎn)來自于所述Mono
或Flux
可以被多次訂閱。在這種情況下,您手中突然有一個(gè)共享資源,這可能會(huì)導(dǎo)致令人費(fèi)解的問題。
但如果從受控良好的上下文中調(diào)用有問題的方法,則它可以是安全的。
在您的例子中,flatMap
確保內(nèi)部發(fā)布者只訂閱一次。因此,只要您僅在此類PlatMap中使用這些方法,它們就可以安全地更改其輸入?yún)?shù)(它保留在Platmap函數(shù)的作用域中)。
這篇關(guān)于更新反應(yīng)堆中對(duì)象的狀態(tài)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,