@@ -14,15 +14,15 @@ interface SelectAllControllerSpec {
14
14
15
15
type SelectAllEventType = "loadstart" | "progress" | "abort" | "loadend" ;
16
16
17
- export class SelectAllController extends EventTarget implements ReactiveController {
17
+ export class SelectAllController implements ReactiveController {
18
18
private readonly gate : Gate ;
19
19
private readonly query : QueryController ;
20
20
private abortController ?: AbortController ;
21
21
private locked = false ;
22
22
readonly pageSize : number = 100 ;
23
+ private readonly emitter = new EventTarget ( ) ;
23
24
24
25
constructor ( host : ReactiveControllerHost , spec : SelectAllControllerSpec ) {
25
- super ( ) ;
26
26
host . addController ( this ) ;
27
27
this . gate = spec . gate ;
28
28
this . query = spec . query ;
@@ -44,12 +44,12 @@ export class SelectAllController extends EventTarget implements ReactiveControll
44
44
return ( ) => this . abort ( ) ;
45
45
}
46
46
47
- addEventListener (
48
- type : SelectAllEventType ,
49
- callback : ( pe : ProgressEvent < SelectAllController > ) => void ,
50
- options ?: AddEventListenerOptions | boolean
51
- ) : void {
52
- super . addEventListener ( type , callback , options ) ;
47
+ on ( type : SelectAllEventType , listener : ( pe : ProgressEvent ) => void ) : void {
48
+ this . emitter . addEventListener ( type , listener ) ;
49
+ }
50
+
51
+ off ( type : SelectAllEventType , listener : ( pe : ProgressEvent ) => void ) : void {
52
+ this . emitter . removeEventListener ( type , listener ) ;
53
53
}
54
54
55
55
get selection ( ) : SelectionMultiValue | undefined {
@@ -96,6 +96,7 @@ export class SelectAllController extends EventTarget implements ReactiveControll
96
96
this . setIsLocked ( true ) ;
97
97
98
98
const { offset : initOffset , limit : initLimit } = this . query ;
99
+ const initSelection = this . selection ?. selection ;
99
100
const hasTotal = typeof this . query . totalCount === "number" ;
100
101
const totalCount = this . query . totalCount ?? 0 ;
101
102
let loaded = 0 ;
@@ -104,36 +105,39 @@ export class SelectAllController extends EventTarget implements ReactiveControll
104
105
new ProgressEvent ( type , { loaded, total : totalCount , lengthComputable : hasTotal } ) ;
105
106
// We should avoid duplicates, so, we start with clean array.
106
107
const allItems : ObjectItem [ ] = [ ] ;
108
+ this . abortController = new AbortController ( ) ;
109
+ const signal = this . abortController . signal ;
107
110
108
111
try {
109
- this . abortController = new AbortController ( ) ;
110
- this . dispatchEvent ( pe ( "loadstart" ) ) ;
112
+ this . emitter . dispatchEvent ( pe ( "loadstart" ) ) ;
111
113
let loading = true ;
112
114
while ( loading ) {
113
115
const loadedItems = await this . query . fetchPage ( {
114
116
limit : this . pageSize ,
115
117
offset,
116
- signal : this . abortController . signal
118
+ signal
117
119
} ) ;
118
120
119
121
allItems . push ( ...loadedItems ) ;
120
122
loaded += loadedItems . length ;
121
123
offset += this . pageSize ;
122
- this . dispatchEvent ( pe ( "progress" ) ) ;
123
- loading = ! this . abortController . signal . aborted && this . query . hasMoreItems ;
124
+ this . emitter . dispatchEvent ( pe ( "progress" ) ) ;
125
+ loading = ! signal . aborted && this . query . hasMoreItems ;
124
126
}
127
+ // Set allItems on success
128
+ this . selection ?. setSelection ( allItems ) ;
125
129
} catch ( error ) {
126
- const aborted = this . abortController ?. signal . aborted ;
127
- if ( ! aborted ) {
130
+ if ( ! signal . aborted ) {
128
131
throw error ;
129
132
}
133
+ // Restore selection on abort
134
+ this . selection ?. setSelection ( initSelection ?? [ ] ) ;
130
135
} finally {
131
136
this . query . setOffset ( initOffset ) ;
132
137
this . query . setLimit ( initLimit ) ;
133
- this . selection ?. setSelection ( allItems ) ;
134
138
this . locked = false ;
139
+ this . emitter . dispatchEvent ( pe ( "loadend" ) ) ;
135
140
this . abortController = undefined ;
136
- this . dispatchEvent ( pe ( "loadend" ) ) ;
137
141
}
138
142
}
139
143
@@ -147,6 +151,6 @@ export class SelectAllController extends EventTarget implements ReactiveControll
147
151
148
152
abort ( ) : void {
149
153
this . abortController ?. abort ( ) ;
150
- this . dispatchEvent ( new ProgressEvent ( "abort" ) ) ;
154
+ this . emitter . dispatchEvent ( new ProgressEvent ( "abort" ) ) ;
151
155
}
152
156
}
0 commit comments