當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript EntityAdapter.upsertMany方法代碼示例

本文整理匯總了TypeScript中@ngrx/entity.EntityAdapter.upsertMany方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript EntityAdapter.upsertMany方法的具體用法?TypeScript EntityAdapter.upsertMany怎麽用?TypeScript EntityAdapter.upsertMany使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在@ngrx/entity.EntityAdapter的用法示例。


在下文中一共展示了EntityAdapter.upsertMany方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: mergeServerUpserts

  // #endregion merge save results

  // #region query & save helpers
  /**
   *
   * @param entities Entities to merge
   * @param collection Collection into which entities are merged
   * @param defaultMergeStrategy How to merge when action's MergeStrategy is unspecified
   * @param [mergeStrategy] The action's MergeStrategy
   */
  private mergeServerUpserts(
    entities: T[],
    collection: EntityCollection<T>,
    defaultMergeStrategy: MergeStrategy,
    mergeStrategy?: MergeStrategy
  ): EntityCollection<T> {
    if (entities == null || entities.length === 0) {
      return collection; // nothing to merge.
    }

    let didMutate = false;
    let changeState = collection.changeState;
    mergeStrategy = mergeStrategy == null ? defaultMergeStrategy : mergeStrategy;

    switch (mergeStrategy) {
      case MergeStrategy.IgnoreChanges:
        return this.adapter.upsertMany(entities, collection);

      case MergeStrategy.OverwriteChanges:
        collection = this.adapter.upsertMany(entities, collection);

        changeState = entities.reduce((chgState, entity) => {
          const id = this.selectId(entity);
          const change = chgState[id];
          if (change) {
            if (!didMutate) {
              chgState = { ...chgState };
              didMutate = true;
            }
            delete chgState[id];
          }
          return chgState;
        }, collection.changeState);

        return didMutate ? { ...collection, changeState } : collection;

      case MergeStrategy.PreserveChanges: {
        const upsertEntities = [] as T[];
        changeState = entities.reduce((chgState, entity) => {
          const id = this.selectId(entity);
          const change = chgState[id];
          if (change) {
            if (!didMutate) {
              chgState = { ...chgState };
              didMutate = true;
            }
            chgState[id].originalValue = entity;
          } else {
            upsertEntities.push(entity);
          }
          return chgState;
        }, collection.changeState);

        collection = this.adapter.upsertMany(upsertEntities, collection);
        return didMutate ? { ...collection, changeState } : collection;
      }
    }
  }
開發者ID:RajeevSingh273,項目名稱:angular-ngrx-data,代碼行數:68,代碼來源:entity-change-tracker-base.ts

示例2: undoAll

  // #endregion track methods

  // #region undo methods
  /**
   * Revert the unsaved changes for all collection.
   * Harmless when there are no entity changes to undo.
   * @param collection The entity collection
   */
  undoAll(collection: EntityCollection<T>): EntityCollection<T> {
    const ids = Object.keys(collection.changeState);

    const { remove, upsert } = ids.reduce(
      (acc, id) => {
        const changeState = acc.chgState[id];
        switch (changeState.changeType) {
          case ChangeType.Added:
            acc.remove.push(id);
            break;
          case ChangeType.Deleted:
            const removed = changeState.originalValue;
            if (removed) {
              acc.upsert.push(removed);
            }
            break;
          case ChangeType.Updated:
            acc.upsert.push(changeState.originalValue);
            break;
        }
        return acc;
      },
      // entitiesToUndo
      {
        remove: [] as (number | string)[],
        upsert: [] as T[],
        chgState: collection.changeState
      }
    );

    collection = this.adapter.removeMany(remove as string[], collection);
    collection = this.adapter.upsertMany(upsert, collection);

    return { ...collection, changeState: {} };
  }
開發者ID:RajeevSingh273,項目名稱:angular-ngrx-data,代碼行數:43,代碼來源:entity-change-tracker-base.ts

示例3: undoMany

  /**
   * Revert the unsaved changes for the given entities.
   * Harmless when there are no entity changes to undo.
   * @param entityOrIdList The entities to revert or their ids.
   * @param collection The entity collection
   */
  undoMany(entityOrIdList: (number | string | T)[], collection: EntityCollection<T>): EntityCollection<T> {
    if (entityOrIdList == null || entityOrIdList.length === 0) {
      return collection; // nothing to undo
    }
    let didMutate = false;

    const { changeState, remove, upsert } = entityOrIdList.reduce(
      (acc, entityOrId) => {
        let chgState = acc.changeState;
        const id = typeof entityOrId === 'object' ? this.selectId(entityOrId) : entityOrId;
        if (chgState[id]) {
          if (!didMutate) {
            chgState = { ...chgState };
            didMutate = true;
          }
          const change = chgState[id];
          delete chgState[id]; // clear tracking of this entity

          switch (change.changeType) {
            case ChangeType.Added:
              acc.remove.push(id);
              break;
            case ChangeType.Deleted:
              const removed = change.originalValue;
              if (removed) {
                acc.upsert.push(removed);
              }
              break;
            case ChangeType.Updated:
              acc.upsert.push(change.originalValue);
              break;
          }
        }
        return acc;
      },
      // entitiesToUndo
      {
        remove: [] as (number | string)[],
        upsert: [] as T[],
        changeState: collection.changeState
      }
    );

    collection = this.adapter.removeMany(remove as string[], collection);
    collection = this.adapter.upsertMany(upsert, collection);
    return didMutate ? collection : { ...collection, changeState };
  }
開發者ID:RajeevSingh273,項目名稱:angular-ngrx-data,代碼行數:53,代碼來源:entity-change-tracker-base.ts


注:本文中的@ngrx/entity.EntityAdapter.upsertMany方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。