本文整理汇总了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;
}
}
}
示例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: {} };
}
示例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 };
}