epos-unit 1.0.0 → 1.0.3
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 +13 -15
- package/dist/epos-unit.js +21 -18
- package/package.json +1 -1
package/dist/epos-unit.d.ts
CHANGED
|
@@ -1,26 +1,24 @@
|
|
|
1
|
+
import { type Log, type Cls } from '@eposlabs/utils';
|
|
1
2
|
import { epos } from 'epos';
|
|
2
|
-
|
|
3
|
-
declare const
|
|
4
|
-
declare const _disposers_: unique symbol;
|
|
5
|
-
declare
|
|
6
|
-
type Versioner = Record<number, (this: any, unit: any) => void>;
|
|
7
|
-
export declare class Unit {
|
|
3
|
+
export declare const _root_: unique symbol;
|
|
4
|
+
export declare const _parent_: unique symbol;
|
|
5
|
+
export declare const _disposers_: unique symbol;
|
|
6
|
+
export declare class Unit<TRoot = unknown> {
|
|
8
7
|
'@': string;
|
|
8
|
+
log: Log;
|
|
9
9
|
private [_root_];
|
|
10
|
+
private [_parent_];
|
|
10
11
|
private [_disposers_];
|
|
11
|
-
private [_tempParent_];
|
|
12
|
-
[epos.store.symbols.model.parent]: Unit | null;
|
|
13
12
|
[key: PropertyKey]: unknown;
|
|
14
|
-
constructor(parent?: Unit | null);
|
|
15
|
-
[epos.
|
|
16
|
-
[epos.
|
|
17
|
-
static get [epos.
|
|
18
|
-
static defineVersioner(versioner:
|
|
19
|
-
get $():
|
|
13
|
+
constructor(parent?: Unit<TRoot> | null);
|
|
14
|
+
[epos.state.symbols.model.init](): void;
|
|
15
|
+
[epos.state.symbols.model.cleanup](): void;
|
|
16
|
+
static get [epos.state.symbols.model.versioner](): unknown;
|
|
17
|
+
static defineVersioner(versioner: Record<number, (this: any, unit: any) => void>): Record<number, (this: any, unit: any) => void>;
|
|
18
|
+
get $(): TRoot;
|
|
20
19
|
up<T extends Unit>(Ancestor: Cls<T>): T | null;
|
|
21
20
|
autorun(...args: Parameters<typeof epos.libs.mobx.autorun>): import("mobx").IReactionDisposer;
|
|
22
21
|
reaction(...args: Parameters<typeof epos.libs.mobx.reaction>): import("mobx").IReactionDisposer;
|
|
23
22
|
setTimeout(...args: Parameters<typeof self.setTimeout>): number;
|
|
24
23
|
setInterval(...args: Parameters<typeof self.setInterval>): number;
|
|
25
24
|
}
|
|
26
|
-
export {};
|
package/dist/epos-unit.js
CHANGED
|
@@ -1,24 +1,27 @@
|
|
|
1
|
-
import { epos } from 'epos';
|
|
2
1
|
import { createLog } from '@eposlabs/utils';
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
const
|
|
2
|
+
import { epos } from 'epos';
|
|
3
|
+
export const _root_ = Symbol('root');
|
|
4
|
+
export const _parent_ = Symbol('parent');
|
|
5
|
+
export const _disposers_ = Symbol('disposers');
|
|
6
6
|
export class Unit {
|
|
7
7
|
constructor(parent = null) {
|
|
8
|
-
this
|
|
8
|
+
Reflect.defineProperty(this, _parent_, { get: () => parent });
|
|
9
9
|
}
|
|
10
10
|
// ---------------------------------------------------------------------------
|
|
11
11
|
// INIT
|
|
12
12
|
// ---------------------------------------------------------------------------
|
|
13
|
-
[epos.
|
|
13
|
+
[epos.state.symbols.model.init]() {
|
|
14
14
|
const Unit = this.constructor;
|
|
15
15
|
const prototypeKeys = Object.getOwnPropertyNames(Unit.prototype);
|
|
16
16
|
// Set disposers container
|
|
17
|
-
|
|
17
|
+
const disposers = new Set();
|
|
18
|
+
Reflect.defineProperty(this, _disposers_, { get: () => disposers });
|
|
18
19
|
// Bind all methods
|
|
19
20
|
for (const key of prototypeKeys) {
|
|
20
21
|
if (typeof this[key] !== 'function')
|
|
21
22
|
continue;
|
|
23
|
+
if (key === 'constructor')
|
|
24
|
+
continue;
|
|
22
25
|
this[key] = this[key].bind(this);
|
|
23
26
|
}
|
|
24
27
|
// Wrap UI methods to components
|
|
@@ -27,7 +30,7 @@ export class Unit {
|
|
|
27
30
|
continue;
|
|
28
31
|
if (!isUiKey(key))
|
|
29
32
|
continue;
|
|
30
|
-
const componentName =
|
|
33
|
+
const componentName = [this['@'], key.replace('ui', '')].filter(Boolean).join('-');
|
|
31
34
|
this[key] = epos.component(componentName, this[key]);
|
|
32
35
|
}
|
|
33
36
|
// Define log method
|
|
@@ -40,10 +43,10 @@ export class Unit {
|
|
|
40
43
|
// ---------------------------------------------------------------------------
|
|
41
44
|
// CLEANUP
|
|
42
45
|
// ---------------------------------------------------------------------------
|
|
43
|
-
[epos.
|
|
46
|
+
[epos.state.symbols.model.cleanup]() {
|
|
44
47
|
// Call disposers
|
|
45
48
|
this[_disposers_].forEach(disposer => disposer());
|
|
46
|
-
this[_disposers_]
|
|
49
|
+
this[_disposers_].clear();
|
|
47
50
|
// Call cleanup method
|
|
48
51
|
if (typeof this.cleanup === 'function')
|
|
49
52
|
this.cleanup();
|
|
@@ -51,7 +54,7 @@ export class Unit {
|
|
|
51
54
|
// ---------------------------------------------------------------------------
|
|
52
55
|
// VERSIONER
|
|
53
56
|
// ---------------------------------------------------------------------------
|
|
54
|
-
static get [epos.
|
|
57
|
+
static get [epos.state.symbols.model.versioner]() {
|
|
55
58
|
if (!('versioner' in this))
|
|
56
59
|
return null;
|
|
57
60
|
return this.versioner;
|
|
@@ -77,22 +80,22 @@ export class Unit {
|
|
|
77
80
|
}
|
|
78
81
|
autorun(...args) {
|
|
79
82
|
const disposer = epos.libs.mobx.autorun(...args);
|
|
80
|
-
this[_disposers_].
|
|
83
|
+
this[_disposers_].add(disposer);
|
|
81
84
|
return disposer;
|
|
82
85
|
}
|
|
83
86
|
reaction(...args) {
|
|
84
87
|
const disposer = epos.libs.mobx.reaction(...args);
|
|
85
|
-
this[_disposers_].
|
|
88
|
+
this[_disposers_].add(disposer);
|
|
86
89
|
return disposer;
|
|
87
90
|
}
|
|
88
91
|
setTimeout(...args) {
|
|
89
92
|
const id = self.setTimeout(...args);
|
|
90
|
-
this[_disposers_].
|
|
93
|
+
this[_disposers_].add(() => self.clearTimeout(id));
|
|
91
94
|
return id;
|
|
92
95
|
}
|
|
93
96
|
setInterval(...args) {
|
|
94
97
|
const id = self.setInterval(...args);
|
|
95
|
-
this[_disposers_].
|
|
98
|
+
this[_disposers_].add(() => self.clearInterval(id));
|
|
96
99
|
return id;
|
|
97
100
|
}
|
|
98
101
|
}
|
|
@@ -100,10 +103,10 @@ export class Unit {
|
|
|
100
103
|
// HELPERS
|
|
101
104
|
// ---------------------------------------------------------------------------
|
|
102
105
|
function isUiKey(key) {
|
|
103
|
-
return key
|
|
106
|
+
return key.startsWith('ui');
|
|
104
107
|
}
|
|
105
|
-
function getParent(
|
|
106
|
-
return
|
|
108
|
+
function getParent(child) {
|
|
109
|
+
return child[_parent_] ?? child[epos.state.symbols.model.parent];
|
|
107
110
|
}
|
|
108
111
|
function findRoot(unit) {
|
|
109
112
|
let root = unit;
|