1+ /* eslint-disable max-classes-per-file */
12import eventsEngine from '@js/common/core/events/core/events_engine' ;
23import ArrayStore from '@js/common/data/array_store' ;
34import { errors } from '@js/common/data/errors' ;
4- import Class from '@js/core/class' ;
55import domAdapter from '@js/core/dom_adapter' ;
66import { getWindow } from '@js/core/utils/window' ;
77
8+ import Store from './m_abstract_store' ;
9+
810const window = getWindow ( ) ;
9- const { abstract } = Class ;
1011
11- const LocalStoreBackend = Class . inherit ( {
12+ class LocalStoreBackend {
13+ _store : any ;
14+
15+ _dirty : boolean ;
16+
17+ _immediate : boolean ;
1218
13- ctor ( store , storeOptions ) {
19+ _key : string ;
20+
21+ constructor ( store , storeOptions ) {
1422 this . _store = store ;
1523 this . _dirty = ! ! storeOptions . data ;
24+
25+ const { name } = storeOptions ;
26+ if ( ! name ) {
27+ throw errors . Error ( 'E4013' ) ;
28+ }
29+
30+ this . _key = `dx-data-localStore-${ name } ` ;
31+
1632 this . save ( ) ;
1733
1834 const immediate = this . _immediate = storeOptions . immediate ;
@@ -27,105 +43,92 @@ const LocalStoreBackend = Class.inherit({
2743 domAdapter . listen ( domAdapter . getDocument ( ) , 'pause' , saveProxy , false ) ;
2844 }
2945 }
30- } ,
46+ }
3147
32- notifyChanged ( ) {
48+ notifyChanged ( ) : void {
3349 this . _dirty = true ;
3450 if ( this . _immediate ) {
3551 this . save ( ) ;
3652 }
37- } ,
53+ }
3854
39- load ( ) {
55+ load ( ) : void {
4056 this . _store . _array = this . _loadImpl ( ) ;
4157 this . _dirty = false ;
42- } ,
58+ }
4359
44- save ( ) {
60+ save ( ) : void {
4561 if ( ! this . _dirty ) {
4662 return ;
4763 }
4864
4965 this . _saveImpl ( this . _store . _array ) ;
5066 this . _dirty = false ;
51- } ,
67+ }
5268
53- _loadImpl : abstract ,
54- _saveImpl : abstract ,
55- } ) ;
56-
57- const DomLocalStoreBackend = LocalStoreBackend . inherit ( {
58-
59- ctor ( store , storeOptions ) {
60- const { name } = storeOptions ;
61- if ( ! name ) {
62- throw errors . Error ( 'E4013' ) ;
63- }
64- this . _key = `dx-data-localStore-${ name } ` ;
65-
66- this . callBase ( store , storeOptions ) ;
67- } ,
68-
69- _loadImpl ( ) {
69+ _loadImpl ( ) : any {
7070 const raw = window . localStorage . getItem ( this . _key ) ;
7171
7272 if ( raw ) {
7373 return JSON . parse ( raw ) ;
7474 }
7575 return [ ] ;
76- } ,
76+ }
7777
78- _saveImpl ( array ) {
78+ _saveImpl ( array ) : void {
7979 if ( ! array . length ) {
8080 window . localStorage . removeItem ( this . _key ) ;
8181 } else {
8282 window . localStorage . setItem ( this . _key , JSON . stringify ( array ) ) ;
8383 }
84- } ,
84+ }
85+ }
86+ class LocalStore extends ArrayStore {
87+ _backend : LocalStoreBackend ;
8588
86- } ) ;
89+ _array : any ;
8790
88- const localStoreBackends = {
89- dom : DomLocalStoreBackend ,
90- } ;
91- const LocalStore = ArrayStore . inherit ( {
92-
93- ctor ( options ) {
91+ constructor ( options ) {
9492 if ( typeof options === 'string' ) {
9593 options = { name : options } ;
9694 } else {
9795 options = options || { } ;
9896 }
9997
100- this . callBase ( options ) ;
98+ super ( options ) ;
99+ this . _array = options . data || [ ] ;
101100
102- this . _backend = new localStoreBackends [ options . backend || 'dom' ] ( this , options ) ;
101+ this . _backend = new LocalStoreBackend ( this , options ) ;
103102 this . _backend . load ( ) ;
104- } ,
103+ }
105104
106- _clearCache ( ) {
105+ _clearCache ( ) : void {
107106 this . _backend . load ( ) ;
108- } ,
107+ }
109108
110- clear ( ) {
111- this . callBase ( ) ;
109+ clear ( ) : void {
110+ super . clear ( ) ;
112111 this . _backend . notifyChanged ( ) ;
113- } ,
112+ }
114113
115- _insertImpl ( values ) {
114+ _insertImpl ( values ) : any {
116115 const b = this . _backend ;
117- return this . callBase ( values ) . done ( b . notifyChanged . bind ( b ) ) ;
118- } ,
116+ return super . _insertImpl ( values ) . done ( b . notifyChanged . bind ( b ) ) ;
117+ }
119118
120- _updateImpl ( key , values ) {
119+ _updateImpl ( key , values ) : any {
121120 const b = this . _backend ;
122- return this . callBase ( key , values ) . done ( b . notifyChanged . bind ( b ) ) ;
123- } ,
121+ return super . _updateImpl ( key , values ) . done ( b . notifyChanged . bind ( b ) ) ;
122+ }
124123
125- _removeImpl ( key ) {
124+ _removeImpl ( key ) : any {
126125 const b = this . _backend ;
127- return this . callBase ( key ) . done ( b . notifyChanged . bind ( b ) ) ;
128- } ,
129- } , 'local' ) ;
126+ return super . _removeImpl ( key ) . done ( b . notifyChanged . bind ( b ) ) ;
127+ }
128+ }
129+
130+ // Preserve alias registration used by Store.create('local', ...)
131+ // @ts -expect-error register ES6 class with alias
132+ Store . registerClass ( LocalStore , 'local' ) ;
130133
131134export default LocalStore ;
0 commit comments