本文整理汇总了TypeScript中@ngrx/entity.EntityAdapter类的典型用法代码示例。如果您正苦于以下问题:TypeScript EntityAdapter类的具体用法?TypeScript EntityAdapter怎么用?TypeScript EntityAdapter使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了EntityAdapter类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: reducer
export function reducer(state = initialState, action: BookActions | CollectionActions) {
switch (action.type) {
case BookActionTypes.SearchComplete:
case CollectionActionTypes.LoadSuccess: {
return {
/**
* 将许多记录添加到实体字典,并返回一个新的状态,包括那些记录。
* 如果要对集合进行排序,则适配器将在进入已排序的数组时对每个记录进行排序。
*/
...adapter.addMany(action.payload, state),
selectedBookId: state.selectedBookId
};
}
case BookActionTypes.Load: {
return {
/**
* 将一个记录添加到实体字典,并返回一个新的状态,包括该记录如果不存在的话。
* 如果要对集合进行排序,则适配器将把新记录插入到已排序的数组中。
*/
...adapter.addOne(action.payload, state),
selectedBookId: state.selectedBookId
};
}
case BookActionTypes.Select: {
return {
...state,
selectedBookId: action.payload
};
}
default: {
return state;
}
}
}
示例2: reducer
export function reducer(state = initialState, action: contentActions.Union | eventActions.Union | discussionActions.Union): State {
switch (action.type) {
case contentActions.ActionTypes.CreateContentSuccess: {
return adapter.addOne(action.content, state);
}
case contentActions.ActionTypes.AddManyContentItems: {
return adapter.addMany(action.contents, state);
}
// TODO: move to own reducer
case eventActions.ActionTypes.ParticipateSuccess: {
return adapter.updateOne({id: action.event.contentId, changes: {event: action.event}}, state);
}
case eventActions.ActionTypes.UnparticipateSuccess: {
return adapter.updateOne({id: action.event.contentId, changes: {event: action.event}}, state);
}
case discussionActions.ActionTypes.CreateCommentSuccess: {
const discussion = {...state.entities[action.contentId].discussion} as Discussion;
discussion.comments = discussion.comments.concat(action.comment);
return adapter.updateOne({id: action.contentId, changes: {discussion}}, state);
}
}
return state;
}
示例3: reducer
export function reducer(state = initialState, action: topicsActions.Union): State {
switch (action.type) {
case topicsActions.ActionTypes.FetchTopicsSuccess: {
return adapter.addMany(action.topics, state);
}
// case TopicsActions.ActionTypes.AddNormalizedTopics: {
// return adapter.addMany(action.topics, state);
// }
// case topicsActions.ActionTypes.AddContentRefToTopic: {
// const topic = {...state.entities[action.topicId]} as any;
//
// return adapter.updateOne({id: action.topicId, changes: {content: topic.content.concat(action.contentId)}}, state);
// }
case topicsActions.ActionTypes.FetchTopicSuccess: {
return adapter.addOne(action.topic, state);
}
case topicsActions.ActionTypes.CreateTopicSuccess: {
return adapter.addOne(action.topic, state);
}
}
return state;
}
示例4: 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: {} };
}
示例5: 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;
}
}
}
示例6: bookReducer
export function bookReducer(
state: BookState = initialState,
action: BookActions
): BookState {
switch (action.type) {
case BookActionTypes.UPSERT_ONE:
return bookAdapter.upsertOne(action.payload.book, state);
case BookActionTypes.DELETE_ONE:
return bookAdapter.removeOne(action.payload.id, state);
default:
return state;
}
}
示例7: lessonsReducer
export function lessonsReducer(state = initialLessonsState,
action: CourseActions): LessonsState {
switch(action.type) {
case CourseActionTypes.LessonsPageCancelled:
return {
...state,
loading:false
};
case CourseActionTypes.LessonsPageRequested:
return {
...state,
loading:true
};
case CourseActionTypes.LessonsPageLoaded:
return adapter.addMany(action.payload.lessons, {...state, loading:false});
default:
return state;
}
}
示例8: mergeSaveDeletes
/**
* Merge successful result of deleting entities on the server that have the given primary keys
* Clears the entity changeState for those keys unless the MergeStrategy is ignoreChanges.
* @param entities keys primary keys of the entities to remove/delete.
* @param collection The entity collection
* @param [mergeStrategy] How to adjust change tracking when the corresponding entity in the collection has an unsaved change.
* Defaults to MergeStrategy.OverwriteChanges.
* @returns The merged EntityCollection.
*/
mergeSaveDeletes(keys: (number | string)[], collection: EntityCollection<T>, mergeStrategy?: MergeStrategy): EntityCollection<T> {
mergeStrategy = mergeStrategy == null ? MergeStrategy.OverwriteChanges : mergeStrategy;
// same logic for all non-ignore merge strategies: always clear (commit) the changes
const deleteIds = keys as string[]; // make TypeScript happy
collection = mergeStrategy === MergeStrategy.IgnoreChanges ? collection : this.commitMany(deleteIds, collection);
return this.adapter.removeMany(deleteIds, collection);
}
示例9: 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 };
}
示例10: reducer
export function reducer(
state = initialState,
action: BookActionsUnion | CollectionActionsUnion
): State {
switch (action.type) {
case BookActionTypes.SearchComplete:
case CollectionActionTypes.LoadSuccess: {
/**
* The addMany function provided by the created adapter
* adds many records to the entity dictionary
* and returns a new state including those records. If
* the collection is to be sorted, the adapter will
* sort each record upon entry into the sorted array.
*/
return adapter.addMany(action.payload, {
...state,
selectedBookId: state.selectedBookId,
});
}
case BookActionTypes.Load: {
/**
* The addOne function provided by the created adapter
* adds one record to the entity dictionary
* and returns a new state including that records if it doesn't
* exist already. If the collection is to be sorted, the adapter will
* insert the new record into the sorted array.
*/
return adapter.addOne(action.payload, {
...state,
selectedBookId: state.selectedBookId,
});
}
case BookActionTypes.Select: {
return {
...state,
selectedBookId: action.payload,
};
}
default: {
return state;
}
}
}