epos-unit 1.0.4 → 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/epos-unit.d.ts +2 -1
- package/dist/epos-unit.js +23 -13
- package/package.json +1 -1
package/dist/epos-unit.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type Cls, type Log } from '@eposlabs/utils';
|
|
2
2
|
import { epos } from 'epos';
|
|
3
3
|
export declare const _root_: unique symbol;
|
|
4
4
|
export declare const _parent_: unique symbol;
|
|
5
5
|
export declare const _disposers_: unique symbol;
|
|
6
|
+
export type Descriptors = Record<string | symbol, PropertyDescriptor>;
|
|
6
7
|
export declare class Unit<TRoot = unknown> {
|
|
7
8
|
'@': string;
|
|
8
9
|
log: Log;
|
package/dist/epos-unit.js
CHANGED
|
@@ -5,6 +5,7 @@ export const _parent_ = Symbol('parent');
|
|
|
5
5
|
export const _disposers_ = Symbol('disposers');
|
|
6
6
|
export class Unit {
|
|
7
7
|
constructor(parent) {
|
|
8
|
+
// Define parent for not-yet-attached units
|
|
8
9
|
Reflect.defineProperty(this, _parent_, { get: () => parent });
|
|
9
10
|
}
|
|
10
11
|
// ---------------------------------------------------------------------------
|
|
@@ -12,23 +13,32 @@ export class Unit {
|
|
|
12
13
|
// ---------------------------------------------------------------------------
|
|
13
14
|
[epos.state.symbols.model.init]() {
|
|
14
15
|
const Unit = this.constructor;
|
|
15
|
-
const
|
|
16
|
-
|
|
16
|
+
const descriptors = Object.getOwnPropertyDescriptors(Unit.prototype);
|
|
17
|
+
const keys = Reflect.ownKeys(descriptors);
|
|
18
|
+
// Setup disposers container
|
|
17
19
|
const disposers = new Set();
|
|
18
20
|
Reflect.defineProperty(this, _disposers_, { get: () => disposers });
|
|
19
21
|
// Bind all methods
|
|
20
|
-
for (const key of
|
|
21
|
-
if (typeof this[key] !== 'function')
|
|
22
|
-
continue;
|
|
22
|
+
for (const key of keys) {
|
|
23
23
|
if (key === 'constructor')
|
|
24
24
|
continue;
|
|
25
|
-
|
|
25
|
+
const descriptor = descriptors[key];
|
|
26
|
+
if (descriptor.get || descriptor.set)
|
|
27
|
+
continue;
|
|
28
|
+
if (typeof descriptor.value !== 'function')
|
|
29
|
+
continue;
|
|
30
|
+
this[key] = descriptor.value.bind(this);
|
|
26
31
|
}
|
|
27
32
|
// Wrap UI methods to components
|
|
28
|
-
for (const key of
|
|
29
|
-
if (typeof
|
|
33
|
+
for (const key of keys) {
|
|
34
|
+
if (typeof key === 'symbol')
|
|
35
|
+
continue;
|
|
36
|
+
if (!key.startsWith('ui'))
|
|
30
37
|
continue;
|
|
31
|
-
|
|
38
|
+
const descriptor = descriptors[key];
|
|
39
|
+
if (descriptor.get || descriptor.set)
|
|
40
|
+
continue;
|
|
41
|
+
if (typeof this[key] !== 'function')
|
|
32
42
|
continue;
|
|
33
43
|
const componentName = [this['@'], key.replace('ui', '')].filter(Boolean).join('-');
|
|
34
44
|
this[key] = epos.component(componentName, this[key]);
|
|
@@ -63,12 +73,15 @@ export class Unit {
|
|
|
63
73
|
return versioner;
|
|
64
74
|
}
|
|
65
75
|
// ---------------------------------------------------------------------------
|
|
66
|
-
//
|
|
76
|
+
// ROOT
|
|
67
77
|
// ---------------------------------------------------------------------------
|
|
68
78
|
get $() {
|
|
69
79
|
this[_root_] ??= findRoot(this);
|
|
70
80
|
return this[_root_];
|
|
71
81
|
}
|
|
82
|
+
// ---------------------------------------------------------------------------
|
|
83
|
+
// METHODS
|
|
84
|
+
// ---------------------------------------------------------------------------
|
|
72
85
|
up(Ancestor) {
|
|
73
86
|
let cursor = getParent(this);
|
|
74
87
|
while (cursor) {
|
|
@@ -102,9 +115,6 @@ export class Unit {
|
|
|
102
115
|
// ---------------------------------------------------------------------------
|
|
103
116
|
// HELPERS
|
|
104
117
|
// ---------------------------------------------------------------------------
|
|
105
|
-
function isUiKey(key) {
|
|
106
|
-
return key.startsWith('ui');
|
|
107
|
-
}
|
|
108
118
|
function getParent(child) {
|
|
109
119
|
return child[_parent_] ?? child[epos.state.symbols.model.parent];
|
|
110
120
|
}
|