1
1
import { findElementsForInit , isElementWithinLazyParent } from './dc-dom' ;
2
2
import { DcBaseComponent } from 'src/dc-base-component'
3
-
4
- /**
5
- * @typedef {string } ComponentState
6
- * */
7
-
8
- /**
9
- * @type {ComponentState }
10
- */
11
- const STATE_NOT_INITED = 'not-inited' ;
12
- /**
13
- * @type {ComponentState }
14
- */
15
- const STATE_INITIALIZING = 'initializing' ;
16
- /**
17
- * @type {ComponentState }
18
- */
19
- const STATE_LAZY_WAITING = 'lazy-waiting' ;
20
- /**
21
- * @type {ComponentState }
22
- */
23
- const STATE_CREATED = 'created' ;
24
- /**
25
- * @type {ComponentState }
26
- */
27
- const STATE_ERROR = 'error' ;
3
+ import { states } from "./enums" ;
28
4
29
5
interface StatedComponent {
30
6
element : HTMLElement ;
@@ -74,7 +50,7 @@ class DcFactory {
74
50
this . _registeredComponents . forEach ( ( rComponent ) => {
75
51
rComponent . components = rComponent . components . filter ( ( { element, instance, state } ) => {
76
52
if ( root . contains ( element ) ) {
77
- if ( state === STATE_CREATED && instance ) instance . destroy ( ) ;
53
+ if ( state === states . CREATED && instance ) instance . destroy ( ) ;
78
54
return false ;
79
55
}
80
56
@@ -90,7 +66,7 @@ class DcFactory {
90
66
private _getState = ( element : HTMLElement , rComponent : RegisteredComponent ) : string => {
91
67
const component = this . _getComponent ( element , rComponent ) ;
92
68
if ( component ) return component . state ;
93
- return STATE_NOT_INITED ;
69
+ return states . NOT_INITED ;
94
70
}
95
71
96
72
private _setState = ( element : HTMLElement , rComponent : RegisteredComponent , state : string , instance ?: DcBaseComponent | undefined ) : void => {
@@ -120,11 +96,11 @@ class DcFactory {
120
96
const state = this . _getState ( element , rComponent ) ;
121
97
switch ( state ) {
122
98
// ignore components which are already created or in the middle of that process
123
- case STATE_CREATED :
124
- case STATE_ERROR :
125
- case STATE_INITIALIZING :
99
+ case states . CREATED :
100
+ case states . ERROR :
101
+ case states . INITIALIZING :
126
102
return ;
127
- case STATE_LAZY_WAITING :
103
+ case states . LAZY_WAITING :
128
104
if ( ! withLazy ) {
129
105
return ;
130
106
}
@@ -133,17 +109,17 @@ class DcFactory {
133
109
// if component is lazy but we should not instantiate it according to withLazy = false
134
110
// we need to mark this component and wait until withLazy = true
135
111
if ( ! withLazy && isElementWithinLazyParent ( element ) ) {
136
- this . _setState ( element , rComponent , STATE_LAZY_WAITING ) ;
112
+ this . _setState ( element , rComponent , states . LAZY_WAITING ) ;
137
113
return ;
138
114
}
139
115
140
116
// finally init component on element
141
- this . _setState ( element , rComponent , STATE_INITIALIZING ) ;
117
+ this . _setState ( element , rComponent , states . INITIALIZING ) ;
142
118
requestAnimationFrame ( ( ) => {
143
119
try {
144
120
this . _createComponent ( element , rComponent ) ;
145
121
} catch ( error ) {
146
- this . _setState ( element , rComponent , STATE_ERROR ) ;
122
+ this . _setState ( element , rComponent , states . ERROR ) ;
147
123
console . error ( `Component ${ _Class . name } hasn't been created due to error: ${ error . message } ` , element ) ;
148
124
console . error ( error ) ;
149
125
}
@@ -154,9 +130,8 @@ class DcFactory {
154
130
private _createComponent = ( element : HTMLElement , rComponent : RegisteredComponent ) : void => {
155
131
const { _Class } = rComponent ;
156
132
const instance = new _Class ( element , _Class . namespace , _Class . requiredRefs ) ;
157
- console . log ( instance ) ;
158
133
instance . init ( ) ;
159
- this . _setState ( element , rComponent , STATE_CREATED , instance ) ;
134
+ this . _setState ( element , rComponent , states . CREATED , instance ) ;
160
135
}
161
136
}
162
137
0 commit comments