epos-unit 1.0.3 → 1.0.5

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.
@@ -1,16 +1,16 @@
1
- import { type Log, type Cls } from '@eposlabs/utils';
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
6
  export declare class Unit<TRoot = unknown> {
7
7
  '@': string;
8
- log: Log;
8
+ 'log': Log;
9
9
  private [_root_];
10
10
  private [_parent_];
11
11
  private [_disposers_];
12
12
  [key: PropertyKey]: unknown;
13
- constructor(parent?: Unit<TRoot> | null);
13
+ constructor(parent: Unit<TRoot> | null);
14
14
  [epos.state.symbols.model.init](): void;
15
15
  [epos.state.symbols.model.cleanup](): void;
16
16
  static get [epos.state.symbols.model.versioner](): unknown;
package/dist/epos-unit.js CHANGED
@@ -4,7 +4,8 @@ export const _root_ = Symbol('root');
4
4
  export const _parent_ = Symbol('parent');
5
5
  export const _disposers_ = Symbol('disposers');
6
6
  export class Unit {
7
- constructor(parent = null) {
7
+ constructor(parent) {
8
+ // Define parent for not-yet-attached units
8
9
  Reflect.defineProperty(this, _parent_, { get: () => parent });
9
10
  }
10
11
  // ---------------------------------------------------------------------------
@@ -12,26 +13,32 @@ export class Unit {
12
13
  // ---------------------------------------------------------------------------
13
14
  [epos.state.symbols.model.init]() {
14
15
  const Unit = this.constructor;
15
- const prototypeKeys = Object.getOwnPropertyNames(Unit.prototype);
16
+ const descriptors = Object.getOwnPropertyDescriptors(Unit.prototype);
16
17
  // Set disposers container
17
18
  const disposers = new Set();
18
19
  Reflect.defineProperty(this, _disposers_, { get: () => disposers });
19
20
  // Bind all methods
20
- for (const key of prototypeKeys) {
21
- if (typeof this[key] !== 'function')
22
- continue;
21
+ for (const key in descriptors) {
22
+ const descriptor = descriptors[key];
23
23
  if (key === 'constructor')
24
24
  continue;
25
- this[key] = this[key].bind(this);
25
+ if (descriptor.get || descriptor.set)
26
+ continue;
27
+ if (typeof descriptor.value !== 'function')
28
+ continue;
29
+ this[key] = descriptor.value.bind(this);
26
30
  }
27
31
  // Wrap UI methods to components
28
- for (const key of prototypeKeys) {
29
- if (typeof this[key] !== 'function')
32
+ for (const key in descriptors) {
33
+ const descriptor = descriptors[key];
34
+ if (!key.startsWith('ui'))
35
+ continue;
36
+ if (descriptor.get || descriptor.set)
30
37
  continue;
31
- if (!isUiKey(key))
38
+ if (typeof descriptor.value !== 'function')
32
39
  continue;
33
40
  const componentName = [this['@'], key.replace('ui', '')].filter(Boolean).join('-');
34
- this[key] = epos.component(componentName, this[key]);
41
+ this[key] = epos.component(componentName, descriptor.value);
35
42
  }
36
43
  // Define log method
37
44
  const log = createLog(this['@']);
@@ -63,12 +70,15 @@ export class Unit {
63
70
  return versioner;
64
71
  }
65
72
  // ---------------------------------------------------------------------------
66
- // GETTERS & METHODS
73
+ // ROOT
67
74
  // ---------------------------------------------------------------------------
68
75
  get $() {
69
76
  this[_root_] ??= findRoot(this);
70
77
  return this[_root_];
71
78
  }
79
+ // ---------------------------------------------------------------------------
80
+ // METHODS
81
+ // ---------------------------------------------------------------------------
72
82
  up(Ancestor) {
73
83
  let cursor = getParent(this);
74
84
  while (cursor) {
@@ -102,9 +112,6 @@ export class Unit {
102
112
  // ---------------------------------------------------------------------------
103
113
  // HELPERS
104
114
  // ---------------------------------------------------------------------------
105
- function isUiKey(key) {
106
- return key.startsWith('ui');
107
- }
108
115
  function getParent(child) {
109
116
  return child[_parent_] ?? child[epos.state.symbols.model.parent];
110
117
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "epos-unit",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "author": "imkost",