Angular2 - 取消流程:可观察 - > debounceTime - > mergeMap - >订阅

时间:2017-03-02 10:53:23

标签: angular

如何取消

  

可观察 - > debounceTime - > mergeMap - >订阅

在mergeMap方法中流动?

我想实现类似的目标:

    var request = this.listPartners.filterChange.asObservable()
        .debounceTime(300)
        .mergeMap(value =>
        {
            if (value != "")
                return this.partnersService.list({ filter: value }) //call http get
            return null;
        });
    if (request)
        request.subscribe(partners => this.partners = partners);

但我得到错误:

  

例外:您提供了“未定义”的预期流。您   可以提供Observable,Promise,Array或Iterable。

我知道mergeMap必须返回Observable,Array ...,但是如果value == ""我不想向服务器发出请求。怎么样?

已编辑:

我的第一个代码是(不返回null):

    var request = this.listPartners.filterChange.asObservable()
        .debounceTime(300)
        .mergeMap(value =>
        {
            if (value != "")
                return this.partnersService.list({ filter: value })
        });
    if (request)
        request.subscribe(partners => this.partners = partners);

与调用return;相同,但如果我拨打return nullreturn;或没有任何内容,则例外情况相同。

为Jai asnwer编辑:

    var request = this.listPartners.filterChange.asObservable()
        .debounceTime(300)
        .mergeMap(value =>
        {
            if (value != "")
                return this.partnersService.list({ filter: value })
            return;
        });
    console.log("request", request);        //this is not called if (value == "")
    if (request)
        request.subscribe(partners => this.partners = partners);

3 个答案:

答案 0 :(得分:1)

只是return;而不是返回null:

// return null;
return;

您可以返回目前正在进行的具有某些属性的Observable,并检查该属性是否为空白:

var request = this.listPartners.filterChange.asObservable()
  .debounceTime(300)
  .mergeMap(value => this.partnersService.list({filter: value }));

if (request.filter !== "")
  request.subscribe(partners => this.partners = partners);

答案 1 :(得分:1)

你可以过滤它

var request = this.listPartners.filterChange.asObservable()
        .debounceTime(300)
        .filter((value)=> value!='') /// will stop the rest if the value is ''
        .mergeMap(value =>
        {
            if (value != "")
                return this.partnersService.list({ filter: value }) //call http get
            return null;
        });
    if (request)
        request.subscribe(partners => this.partners = partners);

答案 2 :(得分:0)

你不能在mergeMap函数中返回null。

回归;

public class Foo<T> {

    private final Set<Set<T>> setsOfSets = new HashSet<>();

    public static void main(String[] args) {
        final Foo<String> tester = new Foo<>();
        Set<String> set1 = new HashSet<>(Arrays.asList("A", "B"));
        Set<String> set2 = new HashSet<>(Arrays.asList("C", "D"));
        Set<String> set3 = new HashSet<>(Arrays.asList("E", "F"));
        tester.addRecord(set1);
        tester.addRecord(set2);
        tester.addRecord(set3);

        System.out.println(tester.getFirstSet());
        System.out.println(tester.getFirstSet());
        System.out.println(tester.getFirstSet());
        System.out.println(tester.getFirstSet());
    }

    public void addRecord(Set<T> record) {
        setsOfSets.add(record);
    }

    public List<T> getFirstSet() {
        Iterator<Set<T>> iterator = setsOfSets.iterator();
        while (iterator.hasNext()) {
            final Set<T> set = iterator.next();
            iterator.remove();
            return new ArrayList<>(set);
        }
        //if there are no sets present
        return null;
    }
}