【问题标题】:Can I observe additions to an array with rx.js?我可以使用 rx.js 观察对数组的添加吗?
【发布时间】:2013-01-06 03:21:01
【问题描述】:

fromArray Rx wiki on github

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>          

看起来subscribe 函数在创建时只会触发一次。这似乎有点用词不当,因为我实际上只是在为数组而不是观察它的变化。不过,该代码几乎与 wiki 上的代码完全相同。所以要么我做错了,要么subscribe 没有按我的预期工作。

【问题讨论】:

    标签: javascript coffeescript observable reactive-extensions-js rxjs


    【解决方案1】:

    在 RxJS 中,您要查找的内容称为 Subject。 您可以将数据推送到其中并从那里流式传输。

    示例:

    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));
    

    【讨论】:

    • 在您的示例中,array 是什么? @弗雷德里克
    • 那是包含元素的实际数组。当您将一个元素推入数组时,您还会在 arraySubject 上调用 next(),以便通知任何订阅者。当然,您也可以将其直接构建到您自己的类中。
    • 在本例中,数组与arraySubject 完全分离。您所做的只是向订阅者推送新的价值
    • 如果数组不是通过调用 pushToArray 填充的,而是通过例如 lib 提供的组件填充?
    • 如果我错了,请原谅我,但这个例子基本上不会忽略可观察的数组吗?我的意思是 arraySubject 永远不会与数组交互......每次你推送到数组时,你只是在 observable 上强制执行下一个。这真的是正确/唯一的方法吗?
    【解决方案2】:

    我发现Rx.Observable.ofObjectChanges(obj) 可以正常工作。

    来自文档页面:

    使用 Object.observe 根据对对象的更改创建一个 Observable 序列。

    希望对你有帮助。

    【讨论】:

    【解决方案3】:

    这个怎么样:

    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']);
    

    【讨论】:

      【解决方案4】:

      Observable.fromArray 创建一个 Observable,当您添加订阅者时,它会立即为每个数组项触发事件。因此,它不会“监视”该数组的更改。

      如果您需要“可推送集合”,Bacon.js 中的 Bus 类可能就是您想要的。对于 RxJs,我的 MessageQueue 小类具有类似的功能。

      【讨论】:

      • 所以它是一个观察者......没有。谁想出了这些东西? (修辞)。感谢您提供有关 MessageQueue 的提示。
      • 哦,然后是 FRB (documentup.com/montagejs/frb),它实际上可以在任何 JS 对象上为您提供可观察的绑定。或者至少是对象的任何属性。不确定直接观察数组。
      • 对 RxJS 提供的“主题”开箱即用的东西使用另一个库在我看来不是一个好方法。
      猜你喜欢
      • 2011-06-17
      • 2021-09-25
      • 2019-09-09
      • 1970-01-01
      • 1970-01-01
      • 2012-03-27
      • 2012-04-20
      • 1970-01-01
      • 2016-01-21
      相关资源
      最近更新 更多