【问题标题】:Resetting/Restart Rxjs Stream on form submit在表单提交时重置/重新启动 Rxjs 流
【发布时间】:2020-04-13 22:41:20
【问题描述】:

我有一个输入字段,在提交时会进行 http 调用,然后绘制图表。当我单击图形的任何节点时,会进行相同的 http 调用,并将结果附加到先前的结果并更新图形。到这里为止一切正常。我正在使用扫描运算符来更新我的结果集。现在,我想要的是在我提交输入表单时重置结果集(即返回新的原始响应)并在单击图形节点时附加到结果集。关于如何实现这一点的任何想法?主要是如何在表单提交时重置此流?或者如何在表单提交上显示新数据并在节点点击上显示更新数据

Here linkingDetailsByAccount$ makes the http call and gets the data from the server.
this.linkingDetailsByAccountSubject.next(account);

在节点点击和表单提交时调用相同的代码,然后激活我的流。

graph$ = this.linkingDetailsByAccount$.pipe(
pluck('graph'),
scan((linkedDetails, adjacency) => {
  const { nodes: linkedNodes = [], edges: linkedEdges = [] } = linkedDetails;
  const { nodes: newNodes = [], edges: newEdges = [] } = adjacency;


  const updatedNodes = differenceBy(newNodes, linkedNodes, 'id');
  const updatedEdges = differenceWith(
    newEdges,
    linkedEdges,
    (newEdge: VisEdge, existingEdge: VisEdge) => newEdge.from === existingEdge.to
  );
  const allNodes = [...linkedNodes, ...updatedNodes];
  const allEdges = [...linkedEdges, ...updatedEdges];

  return {
    nodes: allNodes,
    edges: allEdges
  };
}, {} as NodesEdges)
);

感谢您对此的任何意见。

谢谢,

瓦特萨尔

【问题讨论】:

    标签: rxjs observable angular-observable


    【解决方案1】:

    编辑:当我从 OP 收到更多详细信息时更新了答案。

    我会怎么做,就是把它变成一个类似于状态管理器的迷你 Redux。

    所以scan 运算符应该接受函数或事件对象。

    首先,您要存储您进行的初始 HTTP 调用的第一个初始状态。您将使用此对象在表单提交时重置您的状态。

    然后创建一个graphEvents主题。

    interface UpdateGraphEvent {
      type: 'Update';
      account: any;
    }
    
    interface ResetGraphEvent {
      type: 'Reset';
      account: any;
    }
    
    type GraphEvent = UpdateGraphEvent | ResetGraphEvent;
    
    this.graphEvents$ = new Subject<GraphEvent>();
    

    然后您可以使用新的 graphEvents$ 主题来替换 linkingDetailsByAccountSubject 的使用。

    // When you want to update with new data.
    this.graphEvent$.next({type: 'Update', account: account});
    
    // when you want to reset with initial data.
    this.graphEvent$.next({type: 'Reset', account: this.initialAccount});
    

    然后在您的信息流中使用它。

    graph$ = this.graphEvent$.pipe(
    pluck('graph'),
    scan((linkedDetails, event: GraphEvent) => {
      if (event.type === 'Reset') {
        return {
          nodes: event.account.nodes,
          edges: event.account.edges,
        }
      }
    
      const { nodes: linkedNodes = [], edges: linkedEdges = [] } = linkedDetails;
      const { nodes: newNodes = [], edges: newEdges = [] } = event.account;
    
    
      const updatedNodes = differenceBy(newNodes, linkedNodes, 'id');
      const updatedEdges = differenceWith(
        newEdges,
        linkedEdges,
        (newEdge: VisEdge, existingEdge: VisEdge) => newEdge.from === existingEdge.to
      );
      const allNodes = [...linkedNodes, ...updatedNodes];
      const allEdges = [...linkedEdges, ...updatedEdges];
    
      return {
        nodes: allNodes,
        edges: allEdges
      };
    }, {} as NodesEdges)
    );
    

    graphEvent$ 将是一个发出这些事件 (GraphEvent) 的主题。

    【讨论】:

    • 我想我可能不太清楚这个问题。当我说重置时,我不想只返回空数组。我只是不希望它附加到较旧的结果中。所以它应该可以放弃旧的结果集并从新的结果集重新开始
    • @Vatsal 想要重置流的原因是什么?它会给你带来什么好处?
    • 我有一个输入字段,提交时会进行 http 调用,然后绘制图表。当我单击图形的任何节点时,会进行相同的 http 调用,并将结果附加到先前的结果并更新图形。到这里为止一切正常。我正在使用扫描运算符来更新我的结果集。现在,我想要的是在提交输入表单时重置结果集,并在单击图形节点时附加到结果集。关于如何实现这一点的任何想法?主要是如何在表单提交时重置此流?
    • @Vatsal 这与您的原始问题相同,没有新信息。抱歉,我正在努力解释您所需要的东西。当您说“重置结果集”时,听起来您只是想将流重置为其开始时的原始值,即一个空对象?
    • 好的,首先,当用户使用帐户提交表单时 - 我从服务器获取一些数据并将其绘制在图表中。现在,每当我单击任何图形节点时,相同的流都会再次获取数据并将其添加到旧的结果集中。这很好,因为这些所有帐户都是相关的。现在,如果我想通过在输入字段中再次输入数据来进行全新搜索 - 我该如何进行全新搜索?使用当前设置,它只是不断附加到以前的结果集。我希望它在单击图形节点时附加并在提交表单时重新开始
    猜你喜欢
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-20
    • 2021-10-08
    • 2021-07-01
    • 2015-05-12
    • 2015-09-29
    相关资源
    最近更新 更多