diff --git a/src/entities/sorted_state_adapter.ts b/src/entities/sorted_state_adapter.ts index 4315308a7d..dedd10a11e 100644 --- a/src/entities/sorted_state_adapter.ts +++ b/src/entities/sorted_state_adapter.ts @@ -8,7 +8,11 @@ import { } from './models' import { createStateOperator } from './state_adapter' import { createUnsortedStateAdapter } from './unsorted_state_adapter' -import { selectIdValue } from './utils' +import { + selectIdValue, + ensureEntitiesArray, + splitAddedUpdatedEntities +} from './utils' export function createSortedStateAdapter( selectId: IdSelector, @@ -25,14 +29,12 @@ export function createSortedStateAdapter( } function addManyMutably( - newModels: T[] | Record, + newEntities: T[] | Record, state: R ): void { - if (!Array.isArray(newModels)) { - newModels = Object.values(newModels) - } + newEntities = ensureEntitiesArray(newEntities) - const models = newModels.filter( + const models = newEntities.filter( model => !(selectIdValue(model, selectId) in state.entities) ) @@ -41,14 +43,15 @@ export function createSortedStateAdapter( } } - function setAllMutably(models: T[] | Record, state: R): void { - if (!Array.isArray(models)) { - models = Object.values(models) - } + function setAllMutably( + newEntities: T[] | Record, + state: R + ): void { + newEntities = ensureEntitiesArray(newEntities) state.entities = {} state.ids = [] - addManyMutably(models, state) + addManyMutably(newEntities, state) } function updateOneMutably(update: Update, state: R): void { @@ -86,24 +89,14 @@ export function createSortedStateAdapter( } function upsertManyMutably( - entities: T[] | Record, + newEntities: T[] | Record, state: R ): void { - if (!Array.isArray(entities)) { - entities = Object.values(entities) - } - - const added: T[] = [] - const updated: Update[] = [] - - for (const entity of entities) { - const id = selectIdValue(entity, selectId) - if (id in state.entities) { - updated.push({ id, changes: entity }) - } else { - added.push(entity) - } - } + const [added, updated] = splitAddedUpdatedEntities( + newEntities, + selectId, + state + ) updateManyMutably(updated, state) addManyMutably(added, state) diff --git a/src/entities/unsorted_state_adapter.ts b/src/entities/unsorted_state_adapter.ts index 4fec5c1fdc..7afde13939 100644 --- a/src/entities/unsorted_state_adapter.ts +++ b/src/entities/unsorted_state_adapter.ts @@ -9,7 +9,11 @@ import { createStateOperator, createSingleArgumentStateOperator } from './state_adapter' -import { selectIdValue } from './utils' +import { + selectIdValue, + ensureEntitiesArray, + splitAddedUpdatedEntities +} from './utils' export function createUnsortedStateAdapter( selectId: IdSelector @@ -27,25 +31,27 @@ export function createUnsortedStateAdapter( state.entities[key] = entity } - function addManyMutably(entities: T[] | Record, state: R): void { - if (!Array.isArray(entities)) { - entities = Object.values(entities) - } + function addManyMutably( + newEntities: T[] | Record, + state: R + ): void { + newEntities = ensureEntitiesArray(newEntities) - for (const entity of entities) { + for (const entity of newEntities) { addOneMutably(entity, state) } } - function setAllMutably(entities: T[] | Record, state: R): void { - if (!Array.isArray(entities)) { - entities = Object.values(entities) - } + function setAllMutably( + newEntities: T[] | Record, + state: R + ): void { + newEntities = ensureEntitiesArray(newEntities) state.ids = [] state.entities = {} - addManyMutably(entities, state) + addManyMutably(newEntities, state) } function removeOneMutably(key: EntityId, state: R): void { @@ -140,24 +146,14 @@ export function createUnsortedStateAdapter( } function upsertManyMutably( - entities: T[] | Record, + newEntities: T[] | Record, state: R ): void { - if (!Array.isArray(entities)) { - entities = Object.values(entities) - } - - const added: T[] = [] - const updated: Update[] = [] - - for (const entity of entities) { - const id = selectIdValue(entity, selectId) - if (id in state.entities) { - updated.push({ id, changes: entity }) - } else { - added.push(entity) - } - } + const [added, updated] = splitAddedUpdatedEntities( + newEntities, + selectId, + state + ) updateManyMutably(updated, state) addManyMutably(added, state) diff --git a/src/entities/utils.ts b/src/entities/utils.ts index 426c3bf042..b79a69ebc3 100644 --- a/src/entities/utils.ts +++ b/src/entities/utils.ts @@ -1,4 +1,4 @@ -import { IdSelector } from './models' +import { EntityState, IdSelector, Update, EntityId } from './models' export function selectIdValue(entity: T, selectId: IdSelector) { const key = selectId(entity) @@ -16,3 +16,34 @@ export function selectIdValue(entity: T, selectId: IdSelector) { return key } + +export function ensureEntitiesArray( + entities: T[] | Record +): T[] { + if (!Array.isArray(entities)) { + entities = Object.values(entities) + } + + return entities +} + +export function splitAddedUpdatedEntities( + newEntities: T[] | Record, + selectId: IdSelector, + state: EntityState +): [T[], Update[]] { + newEntities = ensureEntitiesArray(newEntities) + + const added: T[] = [] + const updated: Update[] = [] + + for (const entity of newEntities) { + const id = selectIdValue(entity, selectId) + if (id in state.entities) { + updated.push({ id, changes: entity }) + } else { + added.push(entity) + } + } + return [added, updated] +} diff --git a/src/serializableStateInvariantMiddleware.ts b/src/serializableStateInvariantMiddleware.ts index dd076b17ce..5ce10ad197 100644 --- a/src/serializableStateInvariantMiddleware.ts +++ b/src/serializableStateInvariantMiddleware.ts @@ -57,7 +57,7 @@ export function findNonSerializableValue( const hasIgnoredPaths = ignoredPaths.length > 0 for (const [key, nestedValue] of entries) { - const nestedPath = path ? path + '.' + key : key // path.concat(property) + const nestedPath = path ? path + '.' + key : key if (hasIgnoredPaths && ignoredPaths.indexOf(nestedPath) >= 0) { continue