聆听流中的多个可观察对象

时间:2020-10-20 23:20:41

标签: angular typescript rxjs

我真的无法理解。 我有一个正在监听对话框的容器。该对话框可以发出不同的动作。基于发出的动作,我想调用一些其他逻辑。 我真的很想在没有嵌套订阅或if语句的情况下进行存档。我怎样才能做到这一点? 这是我尝试过的:

    this.commonProducts$
      .pipe(
        take(1),
        map(commonProducts => (this.dialogRef = this.dialog.open(AddProductDialogComponent, { data: commonProducts }))),
        switchMap(() => {
          return this.dialogRef.actions.pull(DialogProductAddActions.frequency).pipe(
            map(newProduct => {
              // do some actions
            })
          );
        }),
        switchMap(() => {
          return this.dialogRef.actions.pull(DialogProductAddActions.schedule).pipe(
            map(newProduct => {
              // do other action
            })
          );
        })
      )
      .subscribe(() => this.dialogRef.close());

但是只有第一个切换图有效。

1 个答案:

答案 0 :(得分:0)

您可以尝试在rxjs中使用iif运算符,并按如下所示修改代码

import org.springframework.boot.env.PropertiesPropertySourceLoader;
import org.springframework.boot.env.PropertySourceLoader;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.PropertySourceFactory;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

public class MyPropertySourceFactory implements PropertySourceFactory {

    private static PropertySourceLoader loader = new PropertiesPropertySourceLoader();

    @Override
    public PropertySource<?> createPropertySource(String name, EncodedResource resource)
            throws IOException {
        PropertySource<?> propSource = null;
        String propName = name;
        if (StringUtils.isEmpty(propName)) {
            propName = getNameForResource(resource.getResource());
        }
        String fileName = resource.getResource().getFilename();
        List<Resource> resList = getPackageInsideResourcesByPattern("/**/" + fileName);

        for (Resource res : resList) {
             List<PropertySource<?>> propertySources = loader.load(propName, res);
            if (!propertySources.isEmpty()) {
                propSource = propertySources.get(0);
                break;
            }

        }
        return propSource;
    }


    private List<Resource> getPackageInsideResourcesByPattern(String resourceName) throws IOException {
        String resourcePathPattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + resourceName;
        ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
        return Arrays.asList(resourcePatternResolver.getResources(resourcePathPattern));
    }

    private String getNameForResource(Resource resource) {
        String name = resource.getDescription();
        if (!StringUtils.hasText(name)) {
            name = resource.getClass().getSimpleName() + "@" + System.identityHashCode(resource);
        }
        return name;
    }


}