1- import { DerivedPropsGate , SetupComponent , SetupComponentHost } from "@mendix/widget-plugin-mobx-kit/main" ;
1+ import { DerivedPropsGate , Emitter } from "@mendix/widget-plugin-mobx-kit/main" ;
22import { ObjectItem , SelectionMultiValue , SelectionSingleValue } from "mendix" ;
33import { action , computed , makeObservable , observable , when } from "mobx" ;
44import { QueryService } from "../interfaces/QueryService" ;
5- import { TaskProgressService } from "../interfaces/TaskProgressService " ;
5+ import { ServiceEvents } from "./select-all.model " ;
66
77interface DynamicProps {
88 itemSelection ?: SelectionMultiValue | SelectionSingleValue ;
99}
1010
11- export class SelectAllService implements SetupComponent {
11+ export class SelectAllService {
1212 private locked = false ;
1313 private abortController ?: AbortController ;
1414 private readonly pageSize = 1024 ;
1515
1616 constructor (
17- host : SetupComponentHost ,
1817 private gate : DerivedPropsGate < DynamicProps > ,
1918 private query : QueryService ,
20- private progress : TaskProgressService
19+ private progress : Emitter < ServiceEvents >
2120 ) {
22- host . add ( this ) ;
23- type PrivateMembers = "setIsLocked" | "locked" ;
21+ type PrivateMembers = "locked" ;
2422 makeObservable < this, PrivateMembers > ( this , {
25- setIsLocked : action ,
2623 canExecute : computed ,
27- isExecuting : computed ,
2824 selection : computed ,
2925 locked : observable ,
3026 selectAllPages : action ,
@@ -33,10 +29,6 @@ export class SelectAllService implements SetupComponent {
3329 } ) ;
3430 }
3531
36- setup ( ) : ( ) => void {
37- return ( ) => this . abort ( ) ;
38- }
39-
4032 get selection ( ) : SelectionMultiValue | undefined {
4133 const selection = this . gate . props . itemSelection ;
4234 if ( selection === undefined ) return ;
@@ -48,14 +40,6 @@ export class SelectAllService implements SetupComponent {
4840 return this . gate . props . itemSelection ?. type === "Multi" && ! this . locked ;
4941 }
5042
51- get isExecuting ( ) : boolean {
52- return this . locked ;
53- }
54-
55- private setIsLocked ( value : boolean ) : void {
56- this . locked = value ;
57- }
58-
5943 private beforeRunChecks ( ) : boolean {
6044 const selection = this . gate . props . itemSelection ;
6145
@@ -94,8 +78,10 @@ export class SelectAllService implements SetupComponent {
9478 return { success : false } ;
9579 }
9680
97- this . setIsLocked ( true ) ;
81+ this . locked = true ;
82+ this . abortController = new AbortController ( ) ;
9883
84+ const signal = this . abortController . signal ;
9985 const { offset : initOffset , limit : initLimit } = this . query ;
10086 const initSelection = this . selection ?. selection ?? [ ] ;
10187 const hasTotal = typeof this . query . totalCount === "number" ;
@@ -107,12 +93,10 @@ export class SelectAllService implements SetupComponent {
10793 new ProgressEvent ( type , { loaded, total : totalCount , lengthComputable : hasTotal } ) ;
10894 // We should avoid duplicates, so, we start with clean array.
10995 const allItems : ObjectItem [ ] = [ ] ;
110- this . abortController = new AbortController ( ) ;
111- const signal = this . abortController . signal ;
11296
11397 performance . mark ( "SelectAll_Start" ) ;
11498 try {
115- this . progress . onloadstart ( pe ( "loadstart" ) ) ;
99+ this . progress . emit ( "loadstart" , pe ( "loadstart" ) ) ;
116100 let loading = true ;
117101 while ( loading ) {
118102 const loadedItems = await this . query . fetchPage ( {
@@ -124,7 +108,7 @@ export class SelectAllService implements SetupComponent {
124108 allItems . push ( ...loadedItems ) ;
125109 loaded += loadedItems . length ;
126110 offset += this . pageSize ;
127- this . progress . onprogress ( pe ( "progress" ) ) ;
111+ this . progress . emit ( "progress" , pe ( "progress" ) ) ;
128112 loading = ! signal . aborted && this . query . hasMoreItems ;
129113 }
130114 success = true ;
@@ -141,9 +125,7 @@ export class SelectAllService implements SetupComponent {
141125 offset : initOffset
142126 } ) ;
143127 await this . reloadSelection ( ) ;
144- this . progress . onloadend ( ) ;
145-
146- // const selectionBeforeReload = this.selection?.selection ?? [];
128+ this . progress . emit ( "loadend" ) ;
147129 // Reload selection to make sure setSelection is working as expected.
148130 this . selection ?. setSelection ( success ? allItems : initSelection ) ;
149131 this . locked = false ;
0 commit comments