Skip to content

Commit

Permalink
remove any where applicable
Browse files Browse the repository at this point in the history
  • Loading branch information
Lenz Weber committed Feb 20, 2020
1 parent 191e22e commit 65d66dc
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 118 deletions.
17 changes: 12 additions & 5 deletions src/createAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,11 +296,18 @@ export function createAction(type: string, prepareAction?: Function): any {
return actionCreator
}

export function isFSA<
Payload = undefined,
Type extends string = string,
Meta = undefined
>(action: any): action is PayloadAction<Payload, Type, Meta> {
/**
* The previous typings implied some assertion for the types of Payload/Error/Meta, which was just not the case.
* I'd suggest we just do something like this - assert the shape of a FSA, but make no assumptions about the contents.
*/
export function isFSA(
action: unknown
): action is {
type: string
payload?: unknown
error?: unknown
meta?: unknown
} {
return (
isPlainObject(action) &&
typeof (action as any).type === 'string' &&
Expand Down
56 changes: 16 additions & 40 deletions src/entities/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@ export interface DictionaryNum<T> {

/**
* @alpha
* why was this a class, not an interface?
*/
export abstract class Dictionary<T> implements DictionaryNum<T> {
[id: string]: T | undefined
// I get why you're adding "undefined" here, but there is not one check for it actually not being undefined down the line
// on the contrary, some casts to `any` were necessary because `Object.entities(dictionary).sort()` were not possible any more
// due to the `sort` assuming parameters of T.
[id: string]: T
}

/**
Expand All @@ -39,11 +43,6 @@ export type Update<T> = { id: EntityId; changes: Partial<T> }
*/
export type EntityMap<T> = (entity: T) => T

/**
* @alpha
*/
export type TypeOrPayloadAction<T> = T | PayloadAction<T>

/**
* @alpha
*/
Expand All @@ -58,71 +57,48 @@ export interface EntityDefinition<T> {
}

export interface EntityStateAdapter<T> {
addOne<S extends EntityState<T>>(state: S, entity: TypeOrPayloadAction<T>): S
addOne<S extends EntityState<T>>(state: S, entity: T): S
addOne<S extends EntityState<T>>(state: S, action: PayloadAction<T>): S

addMany<S extends EntityState<T>>(
state: S,
entities: TypeOrPayloadAction<T[]>
): S
addMany<S extends EntityState<T>>(state: S, entities: T[]): S
addMany<S extends EntityState<T>>(state: S, entities: PayloadAction<T[]>): S

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

removeOne<S extends EntityState<T>>(
state: S,
key: TypeOrPayloadAction<EntityId>
): S
removeOne<S extends EntityState<T>>(state: S, key: EntityId): S
removeOne<S extends EntityState<T>>(state: S, key: PayloadAction<EntityId>): S

removeMany<S extends EntityState<T>>(state: S, keys: EntityId[]): S
removeMany<S extends EntityState<T>>(
state: S,
keys: TypeOrPayloadAction<EntityId[]>
keys: PayloadAction<EntityId[]>
): S

removeAll<S extends EntityState<T>>(state: S): S

updateOne<S extends EntityState<T>>(
state: S,
update: TypeOrPayloadAction<Update<T>>
): S
updateOne<S extends EntityState<T>>(state: S, update: Update<T>): S
updateOne<S extends EntityState<T>>(
state: S,
update: PayloadAction<Update<T>>
): S

updateMany<S extends EntityState<T>>(
state: S,
updates: TypeOrPayloadAction<Update<T>[]>
): S
updateMany<S extends EntityState<T>>(state: S, updates: Update<T>[]): S
updateMany<S extends EntityState<T>>(
state: S,
updates: PayloadAction<Update<T>[]>
): S

upsertOne<S extends EntityState<T>>(
state: S,
entity: TypeOrPayloadAction<T>
): S
upsertOne<S extends EntityState<T>>(state: S, entity: T): S
upsertOne<S extends EntityState<T>>(state: S, entity: PayloadAction<T>): S

upsertMany<S extends EntityState<T>>(
state: S,
entities: TypeOrPayloadAction<T[]>
): S
upsertMany<S extends EntityState<T>>(state: S, entities: T[]): S
upsertMany<S extends EntityState<T>>(
state: S,
entities: PayloadAction<T[]>
): S

map<S extends EntityState<T>>(
state: S,
map: TypeOrPayloadAction<EntityMap<T>>
): S
map<S extends EntityState<T>>(state: S, map: EntityMap<T>): S
map<S extends EntityState<T>>(state: S, map: PayloadAction<EntityMap<T>>): S
}

Expand Down
2 changes: 1 addition & 1 deletion src/entities/sorted_state_adapter.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EntityStateAdapter, EntityState, Update } from './models'
import { EntityStateAdapter, EntityState } from './models'
import { createEntityAdapter } from './create_adapter'
import { createAction } from '../createAction'
import {
Expand Down
46 changes: 18 additions & 28 deletions src/entities/sorted_state_adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
Comparer,
EntityStateAdapter,
Update,
EntityMap
EntityMap,
EntityId
} from './models'
import { createStateOperator } from './state_adapter'
import { createUnsortedStateAdapter } from './unsorted_state_adapter'
Expand All @@ -13,21 +14,18 @@ import { selectIdValue } from './utils'
export function createSortedStateAdapter<T>(
selectId: IdSelector<T>,
sort: Comparer<T>
): EntityStateAdapter<T>
export function createSortedStateAdapter<T>(selectId: any, sort: any): any {
): EntityStateAdapter<T> {
type R = EntityState<T>

const { removeOne, removeMany, removeAll } = createUnsortedStateAdapter(
selectId
)

function addOneMutably(entity: T, state: R): void
function addOneMutably(entity: any, state: any): void {
function addOneMutably(entity: T, state: R): void {
return addManyMutably([entity], state)
}

function addManyMutably(newModels: T[], state: R): void
function addManyMutably(newModels: any[], state: any): void {
function addManyMutably(newModels: T[], state: R): void {
const models = newModels.filter(
model => !(selectIdValue(model, selectId) in state.entities)
)
Expand All @@ -37,21 +35,18 @@ export function createSortedStateAdapter<T>(selectId: any, sort: any): any {
}
}

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

addManyMutably(models, state)
}

function updateOneMutably(update: Update<T>, state: R): void
function updateOneMutably(update: any, state: any): void {
function updateOneMutably(update: Update<T>, state: R): void {
return updateManyMutably([update], state)
}

function takeUpdatedModel(models: T[], update: Update<T>, state: R): boolean
function takeUpdatedModel(models: any[], update: any, state: any): boolean {
function takeUpdatedModel(models: T[], update: Update<T>, state: R): boolean {
if (!(update.id in state.entities)) {
return false
}
Expand All @@ -67,8 +62,7 @@ export function createSortedStateAdapter<T>(selectId: any, sort: any): any {
return newKey !== update.id
}

function updateManyMutably(updates: Update<T>[], state: R): void
function updateManyMutably(updates: any[], state: any): void {
function updateManyMutably(updates: Update<T>[], state: R): void {
const models: T[] = []

updates.forEach(update => takeUpdatedModel(models, update, state))
Expand All @@ -78,11 +72,10 @@ export function createSortedStateAdapter<T>(selectId: any, sort: any): any {
}
}

function mapMutably(map: EntityMap<T>, state: R): void
function mapMutably(updatesOrMap: any, state: any): void {
function mapMutably(updatesOrMap: EntityMap<T>, state: R): void {
const updates: Update<T>[] = state.ids.reduce(
(changes: any[], id: string | number) => {
const change = updatesOrMap(state.entities[id])
(changes: Update<T>[], id: EntityId) => {
const change = updatesOrMap(state.entities[id]!)
if (change !== state.entities[id]) {
changes.push({ id, changes: change })
}
Expand All @@ -94,15 +87,13 @@ export function createSortedStateAdapter<T>(selectId: any, sort: any): any {
updateManyMutably(updates, state)
}

function upsertOneMutably(entity: T, state: R): void
function upsertOneMutably(entity: any, state: any): void {
function upsertOneMutably(entity: T, state: R): void {
return upsertManyMutably([entity], state)
}

function upsertManyMutably(entities: T[], state: R): void
function upsertManyMutably(entities: any[], state: any): void {
const added: any[] = []
const updated: any[] = []
function upsertManyMutably(entities: T[], state: R): void {
const added: T[] = []
const updated: Update<T>[] = []

for (const entity of entities) {
const id = selectIdValue(entity, selectId)
Expand All @@ -117,7 +108,7 @@ export function createSortedStateAdapter<T>(selectId: any, sort: any): any {
addManyMutably(added, state)
}

function areArraysEqual(a: any[], b: any[]) {
function areArraysEqual(a: unknown[], b: unknown[]) {
if (a.length !== b.length) {
return false
}
Expand All @@ -131,8 +122,7 @@ export function createSortedStateAdapter<T>(selectId: any, sort: any): any {
return true
}

function merge(models: T[], state: R): void
function merge(models: any[], state: any): void {
function merge(models: T[], state: R): void {
models.sort(sort)

// Insert/overwrite all new/updated
Expand Down
19 changes: 11 additions & 8 deletions src/entities/state_adapter.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import createNextState, { Draft, isDraft } from 'immer'
import createNextState, { isDraft } from 'immer'
import { EntityState } from './models'
import { PayloadAction, isFSA } from '../createAction'

export function createStateOperator<V, R>(
mutator: (arg: R, state: EntityState<V>) => void
): EntityState<V>
export function createStateOperator<V, R>(
mutator: (arg: any, state: any) => void
): any {
) {
return function operation<S extends EntityState<V>>(
state: any,
state: S,
arg: R | PayloadAction<R>
): S {
const runMutator = (draft: Draft<EntityState<V>>) => {
if (isFSA(arg)) {
function isPayloadActionArgument(
arg: R | PayloadAction<R>
): arg is PayloadAction<R> {
return isFSA(arg)
}

const runMutator = (draft: EntityState<V>) => {
if (isPayloadActionArgument(arg)) {
mutator(arg.payload, draft)
} else {
mutator(arg, draft)
Expand Down
Loading

0 comments on commit 65d66dc

Please sign in to comment.