Map我想向一些消费者展示一个。该地图包含 s 列表File及其上传状态:
export interface FileProgress {
file: File;
sent: boolean;
}
Run Code Online (Sandbox Code Playgroud)
因此,它Map存储在BehaviorSubject我将使用以下方法公开的a 中.asObservable():
private readonly fileProgressSubject: BehaviorSubject<Map<string, FileProgress>>;
我的问题是,对底层数据结构执行更新的最佳方法是什么?
如果BehaviorSubject包含一个简单类型,例如mySubject: BehaviorSubject<boolean>,我只需调用即可mySubject.next(true),瞧我已经更新了该值。
但更新Map我的内部fileProgressSubject就比较麻烦了。我需要访问地图,然后对其进行更改,所以这可以工作,但感觉不对:
const temp = fileProgressSubject.getValue();
temp.delete(someFileKey); // EXAMPLE OPERATION - DELETION
fileProgressSubject.next(temp);
Run Code Online (Sandbox Code Playgroud)
这是更新我的底层的“正确”方法吗Map?我读过,调用.getValue()aSubject是强制使用反应式构造,并且可能意味着您没有做正确的事情。
我的另一个想法是,由于这BehaviorSubject将作为可观察对象公开,因此即使我可以看到 Typescript 编译器不允许这样做:
const fileObservable = fileProgressSubject.asObservable();
fileObservable.pipe(
map(fileProgressMap => fileProgressMap.delete(someFileName)), // DELETE A FILE FROM THE MAP …Run Code Online (Sandbox Code Playgroud) 我在 Spring Boot 项目中使用 Resilience4J 来调用 REST 客户端,如下所示:
@Retry(name = "customerService")
public Customer getCustomer(String customerNumber) {
restTemplate.exchange("https://customer-service.net", HttpMethod.GET, ..., customerNumber);
}
Run Code Online (Sandbox Code Playgroud)
使用此配置:
resilience4j.retry:
instances:
customerService:
maxAttempts: 3
waitDuration: 10s
retryExceptions:
- org.springframework.web.client.HttpServerErrorException
Run Code Online (Sandbox Code Playgroud)
我的期望是,如果restTemplate.exchange()调用 且客户服务以 a 响应HttpServerErrorException,则该getCustomer()方法将在等待 10 秒后再调用 3 次。
然而,这种情况并非如此。
该方法不会再被调用,并且会立即抛出异常。
看到示例中包含后备方法,我决定添加它,即使我真的不想调用不同的方法,我只想再次调用我的原始方法。
无论如何,我指定了一个回退:
@Retry(name = "customerService", fallback = "customerFb")
public Customer getCustomer(String customerNumber) {
return getCustomer();
}
private Customer customerFb(String customerNumber, HttpServerErrorException ex) {
log.error("Could not retrieve customer... retrying...");
return getCustomer();
}
private …Run Code Online (Sandbox Code Playgroud)