Skip to content

Commit

Permalink
feat(entity): deprecate addAll and rename it to setAll (#2348)
Browse files Browse the repository at this point in the history
Closes #2330
  • Loading branch information
elvisun authored Feb 6, 2020
1 parent 0dabfc4 commit 27f5059
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 29 deletions.
3 changes: 2 additions & 1 deletion docs/entity/adapter.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ state if no changes were made.

- `addOne`: Add one entity to the collection
- `addMany`: Add multiple entities to the collection
- `addAll`: Replace current collection with provided collection
- ~~`addAll`~~: (Deprecated and renamed to `setAll`). ~~Replace current collection with provided collection~~
- `setAll`: Replace current collection with provided collection
- `removeOne`: Remove one entity from the collection
- `removeMany`: Remove multiple entities from the collection
- `removeAll`: Clear entity collection
Expand Down
39 changes: 28 additions & 11 deletions modules/entity/spec/sorted_state_adapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,27 @@ describe('Sorted State Adapter', () => {
});
});

it('should let you add all entities to the state', () => {
it('should remove existing and add new ones on setAll', () => {
const withOneEntity = adapter.addOne(TheGreatGatsby, state);

const withAll = adapter.addAll(
const withAll = adapter.setAll(
[AClockworkOrange, AnimalFarm],
withOneEntity
);

expect(withAll).toEqual({
ids: [AClockworkOrange.id, AnimalFarm.id],
entities: {
[AClockworkOrange.id]: AClockworkOrange,
[AnimalFarm.id]: AnimalFarm,
},
});
});

it('should remove existing and add new ones on addAll (deprecated)', () => {
const withOneEntity = adapter.addOne(TheGreatGatsby, state);

const withAll = adapter.setAll(
[AClockworkOrange, AnimalFarm],
withOneEntity
);
Expand All @@ -98,7 +115,7 @@ describe('Sorted State Adapter', () => {
});

it('should let you remove many entities by id from the state', () => {
const withAll = adapter.addAll(
const withAll = adapter.setAll(
[TheGreatGatsby, AClockworkOrange, AnimalFarm],
state
);
Expand All @@ -117,7 +134,7 @@ describe('Sorted State Adapter', () => {
});

it('should let you remove many entities by a predicate from the state', () => {
const withAll = adapter.addAll(
const withAll = adapter.setAll(
[TheGreatGatsby, AClockworkOrange, AnimalFarm],
state
);
Expand All @@ -133,7 +150,7 @@ describe('Sorted State Adapter', () => {
});

it('should let you remove all entities from the state', () => {
const withAll = adapter.addAll(
const withAll = adapter.setAll(
[TheGreatGatsby, AClockworkOrange, AnimalFarm],
state
);
Expand Down Expand Up @@ -182,7 +199,7 @@ describe('Sorted State Adapter', () => {
});

it('should not change ids state if you attempt to update an entity that does not impact sorting', () => {
const withAll = adapter.addAll(
const withAll = adapter.setAll(
[TheGreatGatsby, AClockworkOrange, AnimalFarm],
state
);
Expand Down Expand Up @@ -223,7 +240,7 @@ describe('Sorted State Adapter', () => {
});

it('should resort correctly if same id but sort key update', () => {
const withAll = adapter.addAll(
const withAll = adapter.setAll(
[TheGreatGatsby, AnimalFarm, AClockworkOrange],
state
);
Expand Down Expand Up @@ -251,7 +268,7 @@ describe('Sorted State Adapter', () => {
});

it('should resort correctly if the id and sort key update', () => {
const withOne = adapter.addAll(
const withOne = adapter.setAll(
[TheGreatGatsby, AnimalFarm, AClockworkOrange],
state
);
Expand Down Expand Up @@ -281,7 +298,7 @@ describe('Sorted State Adapter', () => {
it('should let you update many entities by id in the state', () => {
const firstChange = { title: 'Zack' };
const secondChange = { title: 'Aaron' };
const withMany = adapter.addAll([TheGreatGatsby, AClockworkOrange], state);
const withMany = adapter.setAll([TheGreatGatsby, AClockworkOrange], state);

const withUpdates = adapter.updateMany(
[
Expand Down Expand Up @@ -310,7 +327,7 @@ describe('Sorted State Adapter', () => {
const firstChange = { ...TheGreatGatsby, title: 'First change' };
const secondChange = { ...AClockworkOrange, title: 'Second change' };

const withMany = adapter.addAll(
const withMany = adapter.setAll(
[TheGreatGatsby, AClockworkOrange, AnimalFarm],
state
);
Expand Down Expand Up @@ -372,7 +389,7 @@ describe('Sorted State Adapter', () => {

it('should let you upsert many entities in the state', () => {
const firstChange = { title: 'Zack' };
const withMany = adapter.addAll([TheGreatGatsby], state);
const withMany = adapter.setAll([TheGreatGatsby], state);

const withUpserts = adapter.upsertMany(
[{ ...TheGreatGatsby, ...firstChange }, AClockworkOrange],
Expand Down
4 changes: 2 additions & 2 deletions modules/entity/spec/state_selectors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('Entity State Selectors', () => {
});

state = {
books: adapter.addAll(
books: adapter.setAll(
[AClockworkOrange, AnimalFarm, TheGreatGatsby],
adapter.getInitialState()
),
Expand Down Expand Up @@ -70,7 +70,7 @@ describe('Entity State Selectors', () => {
selectId: (book: BookModel) => book.id,
});

state = adapter.addAll(
state = adapter.setAll(
[AClockworkOrange, AnimalFarm, TheGreatGatsby],
adapter.getInitialState()
);
Expand Down
31 changes: 24 additions & 7 deletions modules/entity/spec/unsorted_state_adapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,24 @@ describe('Unsorted State Adapter', () => {
});
});

it('should let you add all entities to the state', () => {
it('should remove existing and add new ones on setAll', () => {
const withOneEntity = adapter.addOne(TheGreatGatsby, state);

const withAll = adapter.setAll(
[AClockworkOrange, AnimalFarm],
withOneEntity
);

expect(withAll).toEqual({
ids: [AClockworkOrange.id, AnimalFarm.id],
entities: {
[AClockworkOrange.id]: AClockworkOrange,
[AnimalFarm.id]: AnimalFarm,
},
});
});

it('should remove existing and add new ones on addAll (deprecated)', () => {
const withOneEntity = adapter.addOne(TheGreatGatsby, state);

const withAll = adapter.addAll(
Expand Down Expand Up @@ -97,7 +114,7 @@ describe('Unsorted State Adapter', () => {
});

it('should let you remove many entities by id from the state', () => {
const withAll = adapter.addAll(
const withAll = adapter.setAll(
[TheGreatGatsby, AClockworkOrange, AnimalFarm],
state
);
Expand All @@ -116,7 +133,7 @@ describe('Unsorted State Adapter', () => {
});

it('should let you remove many entities by a predicate from the state', () => {
const withAll = adapter.addAll(
const withAll = adapter.setAll(
[TheGreatGatsby, AClockworkOrange, AnimalFarm],
state
);
Expand All @@ -132,7 +149,7 @@ describe('Unsorted State Adapter', () => {
});

it('should let you remove all entities from the state', () => {
const withAll = adapter.addAll(
const withAll = adapter.setAll(
[TheGreatGatsby, AClockworkOrange, AnimalFarm],
state
);
Expand Down Expand Up @@ -221,7 +238,7 @@ describe('Unsorted State Adapter', () => {
it('should let you update many entities by id in the state', () => {
const firstChange = { title: 'First Change' };
const secondChange = { title: 'Second Change' };
const withMany = adapter.addAll([TheGreatGatsby, AClockworkOrange], state);
const withMany = adapter.setAll([TheGreatGatsby, AClockworkOrange], state);

const withUpdates = adapter.updateMany(
[
Expand Down Expand Up @@ -250,7 +267,7 @@ describe('Unsorted State Adapter', () => {
const firstChange = { ...TheGreatGatsby, title: 'First change' };
const secondChange = { ...AClockworkOrange, title: 'Second change' };

const withMany = adapter.addAll(
const withMany = adapter.setAll(
[TheGreatGatsby, AClockworkOrange, AnimalFarm],
state
);
Expand Down Expand Up @@ -312,7 +329,7 @@ describe('Unsorted State Adapter', () => {

it('should let you upsert many entities in the state', () => {
const firstChange = { title: 'First Change' };
const withMany = adapter.addAll([TheGreatGatsby], state);
const withMany = adapter.setAll([TheGreatGatsby], state);

const withUpserts = adapter.upsertMany(
[{ ...TheGreatGatsby, ...firstChange }, AClockworkOrange],
Expand Down
4 changes: 4 additions & 0 deletions modules/entity/src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,12 @@ export interface EntityDefinition<T> {
export interface EntityStateAdapter<T> {
addOne<S extends EntityState<T>>(entity: T, state: S): S;
addMany<S extends EntityState<T>>(entities: T[], state: S): S;

/** @deprecated addAll has been renamed. Use setAll instead. */
addAll<S extends EntityState<T>>(entities: T[], state: S): S;

setAll<S extends EntityState<T>>(entities: T[], state: S): S;

removeOne<S extends EntityState<T>>(key: string, state: S): S;
removeOne<S extends EntityState<T>>(key: number, state: S): S;

Expand Down
7 changes: 4 additions & 3 deletions modules/entity/src/sorted_state_adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ export function createSortedStateAdapter<T>(selectId: any, sort: any): any {
}
}

function addAllMutably(models: T[], state: R): DidMutate;
function addAllMutably(models: any[], state: any): DidMutate {
function setAllMutably(models: T[], state: R): DidMutate;
function setAllMutably(models: any[], state: any): DidMutate {
state.entities = {};
state.ids = [];

Expand Down Expand Up @@ -199,7 +199,8 @@ export function createSortedStateAdapter<T>(selectId: any, sort: any): any {
addOne: createStateOperator(addOneMutably),
updateOne: createStateOperator(updateOneMutably),
upsertOne: createStateOperator(upsertOneMutably),
addAll: createStateOperator(addAllMutably),
addAll: createStateOperator(setAllMutably),
setAll: createStateOperator(setAllMutably),
addMany: createStateOperator(addManyMutably),
updateMany: createStateOperator(updateManyMutably),
upsertMany: createStateOperator(upsertManyMutably),
Expand Down
7 changes: 4 additions & 3 deletions modules/entity/src/unsorted_state_adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ export function createUnsortedStateAdapter<T>(selectId: IdSelector<T>): any {
return didMutate ? DidMutate.Both : DidMutate.None;
}

function addAllMutably(entities: T[], state: R): DidMutate;
function addAllMutably(entities: any[], state: any): DidMutate {
function setAllMutably(entities: T[], state: R): DidMutate;
function setAllMutably(entities: any[], state: any): DidMutate {
state.ids = [];
state.entities = {};

Expand Down Expand Up @@ -194,7 +194,8 @@ export function createUnsortedStateAdapter<T>(selectId: IdSelector<T>): any {
removeAll,
addOne: createStateOperator(addOneMutably),
addMany: createStateOperator(addManyMutably),
addAll: createStateOperator(addAllMutably),
addAll: createStateOperator(setAllMutably),
setAll: createStateOperator(setAllMutably),
updateOne: createStateOperator(updateOneMutably),
updateMany: createStateOperator(updateManyMutably),
upsertOne: createStateOperator(upsertOneMutably),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const <%= camelize(name) %>Reducer = createReducer(
(state, action) => adapter.removeMany(action.ids, state)
),
on(<%= classify(name) %>Actions.load<%= classify(name) %>s,
(state, action) => adapter.addAll(action.<%= camelize(name) %>s, state)
(state, action) => adapter.setAll(action.<%= camelize(name) %>s, state)
),
on(<%= classify(name) %>Actions.clear<%= classify(name) %>s,
state => adapter.removeAll(state)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export function reducer(
}

case <%= classify(name) %>ActionTypes.Load<%= classify(name) %>s: {
return adapter.addAll(action.payload.<%= camelize(name) %>s, state);
return adapter.setAll(action.payload.<%= camelize(name) %>s, state);
}

case <%= classify(name) %>ActionTypes.Clear<%= classify(name) %>s: {
Expand Down

0 comments on commit 27f5059

Please sign in to comment.