4
4
Comparer ,
5
5
EntityStateAdapter ,
6
6
Update ,
7
- EntityMap
7
+ EntityMap ,
8
+ EntityId
8
9
} from './models'
9
10
import { createStateOperator } from './state_adapter'
10
11
import { createUnsortedStateAdapter } from './unsorted_state_adapter'
@@ -13,21 +14,18 @@ import { selectIdValue } from './utils'
13
14
export function createSortedStateAdapter < T > (
14
15
selectId : IdSelector < T > ,
15
16
sort : Comparer < T >
16
- ) : EntityStateAdapter < T >
17
- export function createSortedStateAdapter < T > ( selectId : any , sort : any ) : any {
17
+ ) : EntityStateAdapter < T > {
18
18
type R = EntityState < T >
19
19
20
20
const { removeOne, removeMany, removeAll } = createUnsortedStateAdapter (
21
21
selectId
22
22
)
23
23
24
- function addOneMutably ( entity : T , state : R ) : void
25
- function addOneMutably ( entity : any , state : any ) : void {
24
+ function addOneMutably ( entity : T , state : R ) : void {
26
25
return addManyMutably ( [ entity ] , state )
27
26
}
28
27
29
- function addManyMutably ( newModels : T [ ] , state : R ) : void
30
- function addManyMutably ( newModels : any [ ] , state : any ) : void {
28
+ function addManyMutably ( newModels : T [ ] , state : R ) : void {
31
29
const models = newModels . filter (
32
30
model => ! ( selectIdValue ( model , selectId ) in state . entities )
33
31
)
@@ -37,21 +35,18 @@ export function createSortedStateAdapter<T>(selectId: any, sort: any): any {
37
35
}
38
36
}
39
37
40
- function setAllMutably ( models : T [ ] , state : R ) : void
41
- function setAllMutably ( models : any [ ] , state : any ) : void {
38
+ function setAllMutably ( models : T [ ] , state : R ) : void {
42
39
state . entities = { }
43
40
state . ids = [ ]
44
41
45
42
addManyMutably ( models , state )
46
43
}
47
44
48
- function updateOneMutably ( update : Update < T > , state : R ) : void
49
- function updateOneMutably ( update : any , state : any ) : void {
45
+ function updateOneMutably ( update : Update < T > , state : R ) : void {
50
46
return updateManyMutably ( [ update ] , state )
51
47
}
52
48
53
- function takeUpdatedModel ( models : T [ ] , update : Update < T > , state : R ) : boolean
54
- function takeUpdatedModel ( models : any [ ] , update : any , state : any ) : boolean {
49
+ function takeUpdatedModel ( models : T [ ] , update : Update < T > , state : R ) : boolean {
55
50
if ( ! ( update . id in state . entities ) ) {
56
51
return false
57
52
}
@@ -67,8 +62,7 @@ export function createSortedStateAdapter<T>(selectId: any, sort: any): any {
67
62
return newKey !== update . id
68
63
}
69
64
70
- function updateManyMutably ( updates : Update < T > [ ] , state : R ) : void
71
- function updateManyMutably ( updates : any [ ] , state : any ) : void {
65
+ function updateManyMutably ( updates : Update < T > [ ] , state : R ) : void {
72
66
const models : T [ ] = [ ]
73
67
74
68
updates . forEach ( update => takeUpdatedModel ( models , update , state ) )
@@ -78,11 +72,10 @@ export function createSortedStateAdapter<T>(selectId: any, sort: any): any {
78
72
}
79
73
}
80
74
81
- function mapMutably ( map : EntityMap < T > , state : R ) : void
82
- function mapMutably ( updatesOrMap : any , state : any ) : void {
75
+ function mapMutably ( updatesOrMap : EntityMap < T > , state : R ) : void {
83
76
const updates : Update < T > [ ] = state . ids . reduce (
84
- ( changes : any [ ] , id : string | number ) => {
85
- const change = updatesOrMap ( state . entities [ id ] )
77
+ ( changes : Update < T > [ ] , id : EntityId ) => {
78
+ const change = updatesOrMap ( state . entities [ id ] ! )
86
79
if ( change !== state . entities [ id ] ) {
87
80
changes . push ( { id, changes : change } )
88
81
}
@@ -94,15 +87,13 @@ export function createSortedStateAdapter<T>(selectId: any, sort: any): any {
94
87
updateManyMutably ( updates , state )
95
88
}
96
89
97
- function upsertOneMutably ( entity : T , state : R ) : void
98
- function upsertOneMutably ( entity : any , state : any ) : void {
90
+ function upsertOneMutably ( entity : T , state : R ) : void {
99
91
return upsertManyMutably ( [ entity ] , state )
100
92
}
101
93
102
- function upsertManyMutably ( entities : T [ ] , state : R ) : void
103
- function upsertManyMutably ( entities : any [ ] , state : any ) : void {
104
- const added : any [ ] = [ ]
105
- const updated : any [ ] = [ ]
94
+ function upsertManyMutably ( entities : T [ ] , state : R ) : void {
95
+ const added : T [ ] = [ ]
96
+ const updated : Update < T > [ ] = [ ]
106
97
107
98
for ( const entity of entities ) {
108
99
const id = selectIdValue ( entity , selectId )
@@ -117,7 +108,7 @@ export function createSortedStateAdapter<T>(selectId: any, sort: any): any {
117
108
addManyMutably ( added , state )
118
109
}
119
110
120
- function areArraysEqual ( a : any [ ] , b : any [ ] ) {
111
+ function areArraysEqual ( a : unknown [ ] , b : unknown [ ] ) {
121
112
if ( a . length !== b . length ) {
122
113
return false
123
114
}
@@ -131,8 +122,7 @@ export function createSortedStateAdapter<T>(selectId: any, sort: any): any {
131
122
return true
132
123
}
133
124
134
- function merge ( models : T [ ] , state : R ) : void
135
- function merge ( models : any [ ] , state : any ) : void {
125
+ function merge ( models : T [ ] , state : R ) : void {
136
126
models . sort ( sort )
137
127
138
128
// Insert/overwrite all new/updated
0 commit comments