我正在寻找一种使用RxJS的管道运算符来改进现有代码的方法。
代替此:
export const initialState = {}
export const createReducerActions = (
actionType => ({
[actionType]: (
(prevState, { data }) => (
data
)
),
})
)
const createDataStorageReducer = (
actionType => (
createReducer(
(
createReducerActions(
actionType,
)
),
(
initialState
),
)
)
)
我正在寻找RxJS中是否存在某种executeImmediately
函数,如下所示:
export const initialState = {}
export const createReducerActions = (
actionType => ({
[actionType]: (
(prevState, { data }) => (
data
)
),
})
)
const createDataStorageReducer = (
actionType => (
of(actionType)
.pipe(
map(createReducerActions),
map(reducerActions => (
createReducer(
reducerActions,
initialState,
)
))
)
.executeImmediately()
)
)
答案 0 :(得分:1)
您不会同时获取Observable
的值。您获得的最接近的结果是创建一个BehaviorSubject
,并将其订阅到Observable
。然后,您可以同步获取BehaviorSubject
的值。
console.log(typeof(rxjs.BehaviorSubject));
const o = rxjs.interval(1000);
const bs = new rxjs.BehaviorSubject("hello");
bs.subscribe(v => console.log(v));
o.subscribe(bs);
bs.next("world");
// You can get the current value of a BS with getValue()
console.log(`bs value right now is: ${bs.getValue()}`);
setTimeout(() => {
console.log(`bs value after 3 seconds is: ${bs.getValue()}`);
}, 3000);
<script src="https://unpkg.com/rxjs@6.2.2/bundles/rxjs.umd.min.js"></script>