Skip to content

Commit

Permalink
Extract common utils from both adapter files (#952)
Browse files Browse the repository at this point in the history
* Extract common utils from both adapter files

* Remove unused import

* Use array return for better minification
  • Loading branch information
markerikson authored Mar 28, 2021
1 parent 7db3e48 commit 61ec24c
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 56 deletions.
47 changes: 20 additions & 27 deletions src/entities/sorted_state_adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(
selectId: IdSelector<T>,
Expand All @@ -25,14 +29,12 @@ export function createSortedStateAdapter<T>(
}

function addManyMutably(
newModels: T[] | Record<EntityId, T>,
newEntities: T[] | Record<EntityId, T>,
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)
)

Expand All @@ -41,14 +43,15 @@ export function createSortedStateAdapter<T>(
}
}

function setAllMutably(models: T[] | Record<EntityId, T>, state: R): void {
if (!Array.isArray(models)) {
models = Object.values(models)
}
function setAllMutably(
newEntities: T[] | Record<EntityId, T>,
state: R
): void {
newEntities = ensureEntitiesArray(newEntities)
state.entities = {}
state.ids = []

addManyMutably(models, state)
addManyMutably(newEntities, state)
}

function updateOneMutably(update: Update<T>, state: R): void {
Expand Down Expand Up @@ -86,24 +89,14 @@ export function createSortedStateAdapter<T>(
}

function upsertManyMutably(
entities: T[] | Record<EntityId, T>,
newEntities: T[] | Record<EntityId, T>,
state: R
): void {
if (!Array.isArray(entities)) {
entities = Object.values(entities)
}

const added: T[] = []
const updated: Update<T>[] = []

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<T>(
newEntities,
selectId,
state
)

updateManyMutably(updated, state)
addManyMutably(added, state)
Expand Down
50 changes: 23 additions & 27 deletions src/entities/unsorted_state_adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import {
createStateOperator,
createSingleArgumentStateOperator
} from './state_adapter'
import { selectIdValue } from './utils'
import {
selectIdValue,
ensureEntitiesArray,
splitAddedUpdatedEntities
} from './utils'

export function createUnsortedStateAdapter<T>(
selectId: IdSelector<T>
Expand All @@ -27,25 +31,27 @@ export function createUnsortedStateAdapter<T>(
state.entities[key] = entity
}

function addManyMutably(entities: T[] | Record<EntityId, T>, state: R): void {
if (!Array.isArray(entities)) {
entities = Object.values(entities)
}
function addManyMutably(
newEntities: T[] | Record<EntityId, T>,
state: R
): void {
newEntities = ensureEntitiesArray(newEntities)

for (const entity of entities) {
for (const entity of newEntities) {
addOneMutably(entity, state)
}
}

function setAllMutably(entities: T[] | Record<EntityId, T>, state: R): void {
if (!Array.isArray(entities)) {
entities = Object.values(entities)
}
function setAllMutably(
newEntities: T[] | Record<EntityId, T>,
state: R
): void {
newEntities = ensureEntitiesArray(newEntities)

state.ids = []
state.entities = {}

addManyMutably(entities, state)
addManyMutably(newEntities, state)
}

function removeOneMutably(key: EntityId, state: R): void {
Expand Down Expand Up @@ -140,24 +146,14 @@ export function createUnsortedStateAdapter<T>(
}

function upsertManyMutably(
entities: T[] | Record<EntityId, T>,
newEntities: T[] | Record<EntityId, T>,
state: R
): void {
if (!Array.isArray(entities)) {
entities = Object.values(entities)
}

const added: T[] = []
const updated: Update<T>[] = []

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<T>(
newEntities,
selectId,
state
)

updateManyMutably(updated, state)
addManyMutably(added, state)
Expand Down
33 changes: 32 additions & 1 deletion src/entities/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IdSelector } from './models'
import { EntityState, IdSelector, Update, EntityId } from './models'

export function selectIdValue<T>(entity: T, selectId: IdSelector<T>) {
const key = selectId(entity)
Expand All @@ -16,3 +16,34 @@ export function selectIdValue<T>(entity: T, selectId: IdSelector<T>) {

return key
}

export function ensureEntitiesArray<T>(
entities: T[] | Record<EntityId, T>
): T[] {
if (!Array.isArray(entities)) {
entities = Object.values(entities)
}

return entities
}

export function splitAddedUpdatedEntities<T>(
newEntities: T[] | Record<EntityId, T>,
selectId: IdSelector<T>,
state: EntityState<T>
): [T[], Update<T>[]] {
newEntities = ensureEntitiesArray(newEntities)

const added: T[] = []
const updated: Update<T>[] = []

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]
}
2 changes: 1 addition & 1 deletion src/serializableStateInvariantMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 61ec24c

Please sign in to comment.