在RxJs 5.4.3中如何实现类似于.ofArrayChanges()
可以实现的模式?
(这不是5.4.3中的功能)
http://jsfiddle.net/ablochs/6jr6syhz/1/
var arr = [1,2,3];
var source = Rx.Observable.ofArrayChanges(arr);
var subscription = source.subscribe(
function (x) { console.log('Next: ' + x); },
function (e) { console.log('Error: ' + e); },
function ( ) { console.log('Completed'); });
arr.push(4)
答案 0 :(得分:1)
由于Array.observe
已过时,我建议您在RxJS中建模整个州。以下是文档中的教程 - Creating applications with RxJS
为您的案例实施:
const onPush$ = new Rx.Subject();
const onChange$ = new Rx.Subject();
const initialValue = [1, 2, 3];
const series$ = Rx.Observable
.merge(
onPush$.map(value =>
series => {
series.push(value);
return series;
}
),
onChange$.map(({ index, value }) =>
series => {
series[index] = value;
return series;
}
)
)
.scan(
(series, makeNew) => makeNew(series),
initialValue
);
series$.subscribe(v => console.log(v));
console.log('push 10');
onPush$.next(10);
console.log('change 0th to be 9');
onChange$.next({ index: 0, value: 9 });
console.log('push 1000');
onPush$.next(1000);

<script src="https://unpkg.com/rxjs/bundles/Rx.min.js"></script>
&#13;
另一个hacky但仍然可能的解决方案是使用ES6 Proxy。类似的东西 - Getter/setter on javascript array?