Skip to content

Commit f056359

Browse files
committed
moved states in enum
1 parent 5a8522b commit f056359

File tree

2 files changed

+18
-36
lines changed

2 files changed

+18
-36
lines changed

src/dc-factory.ts

+11-36
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,6 @@
11
import { findElementsForInit, isElementWithinLazyParent } from './dc-dom';
22
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";
284

295
interface StatedComponent {
306
element: HTMLElement;
@@ -74,7 +50,7 @@ class DcFactory {
7450
this._registeredComponents.forEach((rComponent) => {
7551
rComponent.components = rComponent.components.filter(({ element, instance, state }) => {
7652
if (root.contains(element)) {
77-
if (state === STATE_CREATED && instance) instance.destroy();
53+
if (state === states.CREATED && instance) instance.destroy();
7854
return false;
7955
}
8056

@@ -90,7 +66,7 @@ class DcFactory {
9066
private _getState = (element: HTMLElement, rComponent: RegisteredComponent): string => {
9167
const component = this._getComponent(element, rComponent);
9268
if (component) return component.state;
93-
return STATE_NOT_INITED;
69+
return states.NOT_INITED;
9470
}
9571

9672
private _setState = (element: HTMLElement, rComponent: RegisteredComponent, state: string, instance?: DcBaseComponent | undefined): void => {
@@ -120,11 +96,11 @@ class DcFactory {
12096
const state = this._getState(element, rComponent);
12197
switch (state) {
12298
// 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:
126102
return;
127-
case STATE_LAZY_WAITING:
103+
case states.LAZY_WAITING:
128104
if (!withLazy) {
129105
return;
130106
}
@@ -133,17 +109,17 @@ class DcFactory {
133109
// if component is lazy but we should not instantiate it according to withLazy = false
134110
// we need to mark this component and wait until withLazy = true
135111
if (!withLazy && isElementWithinLazyParent(element)) {
136-
this._setState(element, rComponent, STATE_LAZY_WAITING);
112+
this._setState(element, rComponent, states.LAZY_WAITING);
137113
return;
138114
}
139115

140116
// finally init component on element
141-
this._setState(element, rComponent, STATE_INITIALIZING);
117+
this._setState(element, rComponent, states.INITIALIZING);
142118
requestAnimationFrame(() => {
143119
try {
144120
this._createComponent(element, rComponent);
145121
} catch (error) {
146-
this._setState(element, rComponent, STATE_ERROR);
122+
this._setState(element, rComponent, states.ERROR);
147123
console.error(`Component ${_Class.name} hasn't been created due to error: ${error.message}`, element);
148124
console.error(error);
149125
}
@@ -154,9 +130,8 @@ class DcFactory {
154130
private _createComponent = (element: HTMLElement, rComponent: RegisteredComponent): void => {
155131
const { _Class } = rComponent;
156132
const instance = new _Class(element, _Class.namespace, _Class.requiredRefs);
157-
console.log(instance);
158133
instance.init();
159-
this._setState(element, rComponent, STATE_CREATED, instance);
134+
this._setState(element, rComponent, states.CREATED, instance);
160135
}
161136
}
162137

src/enums.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export const states = Object.freeze({
2+
NOT_INITED: 'not-inited',
3+
INITIALIZING: 'initializing',
4+
LAZY_WAITING: 'lazy-waiting',
5+
CREATED: 'created',
6+
ERROR: 'error',
7+
});

0 commit comments

Comments
 (0)