【发布时间】:2020-02-05 14:02:47
【问题描述】:
我对响应式编程和 observables 比较陌生,所以可能有一个明显的解决方案,但我还是不知道如何在 javascript 中使用 observables 制作这个简单的缓存服务
private legalIds: number[]
private personsCache: Person[] // Person has an id as a property
getPersons(ids: number[]) {
const fromCache = personsCache.filter(p => ids.includes(p.id)) // Get the cached persons
const newPersons = getPersons(ids.filter(id => legalIds.includes(id) && !fromCache.includes(id))) // Get the ones not already cached, if the ids are allowed
return fromCache.concat(newPserons)
}
您将如何使用 observables 以脚本方式实现上述 chaching 功能?没有存储数组等的命令式编程。
【问题讨论】:
-
提供的答案适合您吗?或者你有什么问题吗?写详细的答案是一个相当大的努力,如果理解的话,我希望至少得到反馈
标签: javascript rxjs observable reactive-programming