javascript 我可以用 rx.js 观察数组的添加吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14466285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I observe additions to an array with rx.js?
提问by jcollum
coffee> rext = require 'rx'
coffee> arr = [1..5]
[ 1, 2, 3, 4, 5 ]
coffee> obs = rext.Observable.fromArray(arr)
{ _subscribe: [Function] }
coffee> obs.subscribe( (x) -> console.log("added value: " + x))
added value: 1
added value: 2
added value: 3
added value: 4
added value: 5
{ isStopped: true,
observer:
{ isStopped: true,
_onNext: [Function],
_onError: [Function: defaultError],
_onCompleted: [Function: noop] },
m: { isDisposed: true, current: null } }
coffee> arr.push(12) # expecting "added value: 12"
6 # instead got new length of array
coffee>
It really looks like the subscribe
function will only fire one time, when it's created. It seems like it's a bit of a misnomer, since I'm really just for-eaching the array instead of observing changes on it. That code is almost exactly the same as what's on the wiki though. So either I'm doing it wrong or the subscribe
doesn't work how I expect.
看起来该subscribe
函数只会在创建时触发一次。这似乎有点用词不当,因为我实际上只是在对数组进行 for-each,而不是观察它的变化。该代码几乎与维基上的代码完全相同。所以要么我做错了,要么subscribe
不按我的预期工作。
采纳答案by raimohanska
Observable.fromArray creates an Observable that immediately fires events for each array items, when you add a Subscriber. So, it won't be "watching" the changes to that array.
Observable.fromArray 创建一个 Observable,当您添加订阅者时,它会立即为每个数组项触发事件。因此,它不会“监视”对该数组的更改。
If you need a "pushable collection", the Bus class in Bacon.jsmight be what you're looking for. For RxJs there's my little MessageQueueclass that has a similar functionality.
如果您需要一个“可推送集合”,那么Bacon.js 中的 Bus 类可能就是您要找的。对于 RxJs,我的小MessageQueue类具有类似的功能。
回答by Frederic Leitenberger
In RxJS what you are looking for is called a Subject
.
You can push data into it and stream it from there.
在 RxJS 中,您要查找的内容称为Subject
. 您可以将数据推入其中并从那里流式传输。
Example:
例子:
var array = [];
var arraySubject = new Rx.Subject();
var pushToArray = function (item) {
array.push(item);
arraySubject.next(item);
}
// Subscribe to the subject to react to changes
arraySubject.subscribe((item) => console.log(item));
回答by Pablo
I found Rx.Observable.ofObjectChanges(obj)to work just as I expected.
我发现Rx.Observable.ofObjectChanges(obj)可以按照我的预期工作。
From the documentation page:
从文档页面:
Creates an Observable sequence from changes to an object using Object.observe.
使用 Object.observe 从对对象的更改创建一个 Observable 序列。
Hope it helps.
希望能帮助到你。
回答by Charles Robertson
How about this:
这个怎么样:
var subject = new Rx.Subject();
//scan example building an array over time
var example = subject.scan((acc, curr) => {
return acc.concat(curr);
}
,[]);
//log accumulated values
var subscribe = example.subscribe(val =>
console.log('Accumulated array:', val)
);
//next values into subject
subject.next(['Joe']);
subject.next(['Jim']);