简体   繁体   English

如何使用 RxJS 运算符创建可观察的自定义对象流

[英]How to create an observable stream of custom objects using RxJS operators

I currently have input which looks as follows我目前有如下输入

const config = {
  'mainA': { sub1: { name: 'test1'}, sub2: { name: 'test2'}},
  'mainB': { sub1: { name: 'test3'}, sub2: { name: 'test4'}}
};

I'm trying to write a function (createCustomObservable) which would create an observable using standard RsJS operators as follows我正在尝试编写一个函数 (createCustomObservable),它将使用标准 RsJS 运算符创建一个可观察对象,如下所示

var observable = createCustomObservable(config);
observable.subscribe((x) => console.log(x));

The console output should read as follows控制台输出应如下所示

{'mainA': 'test1'} -> {'mainA': 'test2'} -> {'mainB': 'test3'} ->  {'mainB': 'test4'} 

A series of objects with a single propery具有单一属性的一系列对象

Does anyone have any idea how to realise this using RxJS operators?有谁知道如何使用 RxJS 运算符来实现这一点? Any help would be appreciated.任何帮助将不胜感激。

The main problem for what you trying to solve is traverse the object to get all the objects that contains the "name" field, and get their value.您尝试解决的主要问题是遍历对象以获取包含“名称”字段的所有对象,并获取它们的值。

There's no Rx operator to automatically do that, so to achieve this task you can simply use Rx.Observable.create - https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/create.md没有 Rx 运算符可以自动执行此操作,因此要完成此任务,您只需使用Rx.Observable.create - https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/创建.md

const config = {
'mainA': {
    sub1: {
        name: 'test1'
    },
    sub2: {
        name: 'test2'
    }
},
'mainB': {
    sub1: {
        name: 'test3'
    },
    sub2: {
        name: 'test4'
    }
}
};


function traverse(o, func) {
    for (var i in o) {
        func.apply(this, [i, o[i]]);
        if (o[i] !== null && typeof(o[i]) == "object") {
            //going on step down in the object tree!!
            traverse(o[i], func);
        }
    }
}

var source = Rx.Observable.create(function(observer) {

    traverse(config, function(key, value) {
        if (key == "name")
            observer.onNext(value);
    });

    observer.onCompleted();
    return function() {
        console.log('disposed');
    };
});

source.subscribe(function(next) {
    console.log(next);
})

Sample: https://jsbin.com/yolufovubi/edit?js,console示例: https : //jsbin.com/yolufovubi/edit?js,console

we can create a new stream by Observable constructor, You have to manually call next(), error() and complete() functions.我们可以通过 Observable 构造函数创建一个新的流,你必须手动调用 next()、error() 和 complete() 函数。

function createCustomObservable(config) {
   return new Observable(
      observer => {
         try {
           observer.next(config)
        } catch(err) {
          observer.error(err);
       } finally {
         observer.complete();
      }
    })
 }

and use it this way并以这种方式使用它

var observable = createCustomObservable(config);
observable.subscribe((x) => console.log(x));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM