1+ import type { DataSource } from '@js/common/data' ;
2+ import type { DeferredObj } from '@js/core/utils/deferred' ;
13import { Deferred } from '@js/core/utils/deferred' ;
4+ import type { StoreEventName } from '@js/data/store' ;
25
3- const STORE_EVENTS = {
6+ import type { SafeAppointment } from '../types' ;
7+
8+ const STORE_EVENTS : Record < string , StoreEventName > = {
49 updating : 'updating' ,
510 push : 'push' ,
611} ;
712
13+ interface UpdatedAppointmentKey {
14+ key : string ;
15+ value : unknown ;
16+ }
17+
818export class AppointmentDataSource {
9- protected updatedAppointmentKeys : any [ ] ;
19+ protected updatedAppointmentKeys : UpdatedAppointmentKey [ ] = [ ] ;
1020
11- protected dataSource : any ;
21+ protected dataSource : DataSource | null = null ;
1222
13- protected updatedAppointment : any ;
23+ protected updatedAppointment : SafeAppointment | null = null ;
1424
15- constructor ( dataSource ) {
25+ constructor ( dataSource : DataSource ) {
1626 this . setDataSource ( dataSource ) ;
1727 this . updatedAppointmentKeys = [ ] ;
1828 }
1929
20- get keyName ( ) {
21- const store = this . dataSource . store ( ) ;
22- return store . key ( ) ;
30+ get keyName ( ) : string {
31+ const store = this . dataSource ? .store ( ) ;
32+ return store ? .key ( ) as string ;
2333 }
2434
25- get isDataSourceInit ( ) {
35+ get isDataSourceInit ( ) : boolean {
2636 return Boolean ( this . dataSource ) ;
2737 }
2838
29- private getStoreKey ( target ) {
30- const store = this . dataSource . store ( ) ;
39+ private getStoreKey ( target : SafeAppointment ) : unknown {
40+ const store = this . dataSource ? .store ( ) ;
3141
32- return store . keyOf ( target ) ;
42+ return store ? .keyOf ( target ) ;
3343 }
3444
35- setDataSource ( dataSource ) {
45+ setDataSource ( dataSource : DataSource ) : void {
3646 this . dataSource = dataSource ;
3747
3848 this . cleanState ( ) ;
3949 this . initStoreChangeHandlers ( ) ;
4050 }
4151
42- private initStoreChangeHandlers ( ) {
52+ private initStoreChangeHandlers ( ) : void {
4353 const { dataSource } = this ;
4454 const store = dataSource ?. store ( ) ;
4555
4656 if ( store ) {
4757 store . on ( STORE_EVENTS . updating , ( key ) => {
48- const keyName = store . key ( ) ;
58+ const keyName = store . key ( ) as string ;
4959 if ( keyName ) {
5060 this . updatedAppointmentKeys . push ( {
5161 key : keyName ,
@@ -57,8 +67,8 @@ export class AppointmentDataSource {
5767 } ) ;
5868
5969 store . on ( STORE_EVENTS . push , ( pushItems ) => {
60- const items = dataSource . items ( ) ;
61- const keyName = store . key ( ) ;
70+ const items = dataSource ? .items ( ) ?? [ ] ;
71+ const keyName = store . key ( ) as string ;
6272
6373 pushItems . forEach ( ( pushItem ) => {
6474 const itemExists = items . filter ( ( item ) => item [ keyName ] === pushItem . key ) . length !== 0 ;
@@ -70,52 +80,62 @@ export class AppointmentDataSource {
7080 } ) ;
7181 } else {
7282 const { data } = pushItem ;
73- data && items . push ( data ) ;
83+ if ( data ) {
84+ items . push ( data ) ;
85+ }
7486 }
7587 } ) ;
7688
77- dataSource . load ( ) ;
89+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
90+ dataSource ?. load ( ) ;
7891 } ) ;
7992 }
8093 }
8194
82- getUpdatedAppointment ( ) {
95+ getUpdatedAppointment ( ) : SafeAppointment | null {
8396 return this . updatedAppointment ;
8497 }
8598
86- getUpdatedAppointmentKeys ( ) {
99+ getUpdatedAppointmentKeys ( ) : UpdatedAppointmentKey [ ] {
87100 return this . updatedAppointmentKeys ;
88101 }
89102
90- cleanState ( ) {
103+ cleanState ( ) : void {
91104 this . updatedAppointment = null ;
92105 this . updatedAppointmentKeys = [ ] ;
93106 }
94107
95- add ( rawAppointment ) {
96- return this . dataSource . store ( ) . insert ( rawAppointment ) . done ( ( ) => this . dataSource . load ( ) ) ;
108+ add ( rawAppointment : SafeAppointment ) : DeferredObj < SafeAppointment > {
109+ // @eslint -disable-next-line
110+ return this . dataSource ?. store ( ) . insert ( rawAppointment )
111+ // @ts -expect-error
112+ . done ( ( ) => this . dataSource . load ( ) ) ;
97113 }
98114
99- update ( target , data ) {
115+ update ( target : SafeAppointment , data : SafeAppointment ) : DeferredObj < SafeAppointment | undefined > {
100116 const key = this . getStoreKey ( target ) ;
101117 // @ts -expect-error
102118 const d = new Deferred ( ) ;
103119
104- this . dataSource . store ( ) . update ( key , data )
105- . done ( ( result ) => this . dataSource . load ( )
120+ this . dataSource ?. store ( ) . update ( key , data )
121+ // @ts -expect-error
122+ . done ( ( result ) => this . dataSource ?. load ( )
123+ // @ts -expect-error
106124 . done ( ( ) => d . resolve ( result ) )
107125 . fail ( d . reject ) )
108126 . fail ( d . reject ) ;
109127
110128 return d . promise ( ) ;
111129 }
112130
113- remove ( rawAppointment ) {
131+ remove ( rawAppointment : SafeAppointment ) : DeferredObj < SafeAppointment | undefined > {
114132 const key = this . getStoreKey ( rawAppointment ) ;
115- return this . dataSource . store ( ) . remove ( key ) . done ( ( ) => this . dataSource . load ( ) ) ;
133+ return this . dataSource ?. store ( ) . remove ( key )
134+ // @ts -expect-error
135+ . done ( ( ) => this . dataSource ?. load ( ) ) ;
116136 }
117137
118- destroy ( ) {
138+ destroy ( ) : void {
119139 const store = this . dataSource ?. store ( ) ;
120140
121141 if ( store ) {
0 commit comments