epos-unit 1.0.0
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 +26 -0
- package/dist/epos-unit.js +117 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/package.json +27 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { epos } from 'epos';
|
|
2
|
+
import { type Cls } from '@eposlabs/utils';
|
|
3
|
+
declare const _root_: unique symbol;
|
|
4
|
+
declare const _disposers_: unique symbol;
|
|
5
|
+
declare const _tempParent_: unique symbol;
|
|
6
|
+
type Versioner = Record<number, (this: any, unit: any) => void>;
|
|
7
|
+
export declare class Unit {
|
|
8
|
+
'@': string;
|
|
9
|
+
private [_root_];
|
|
10
|
+
private [_disposers_];
|
|
11
|
+
private [_tempParent_];
|
|
12
|
+
[epos.store.symbols.model.parent]: Unit | null;
|
|
13
|
+
[key: PropertyKey]: unknown;
|
|
14
|
+
constructor(parent?: Unit | null);
|
|
15
|
+
[epos.store.symbols.model.init](): void;
|
|
16
|
+
[epos.store.symbols.model.cleanup](): void;
|
|
17
|
+
static get [epos.store.symbols.model.versioner](): unknown;
|
|
18
|
+
static defineVersioner(versioner: Versioner): Versioner;
|
|
19
|
+
get $(): Unit;
|
|
20
|
+
up<T extends Unit>(Ancestor: Cls<T>): T | null;
|
|
21
|
+
autorun(...args: Parameters<typeof epos.libs.mobx.autorun>): import("mobx").IReactionDisposer;
|
|
22
|
+
reaction(...args: Parameters<typeof epos.libs.mobx.reaction>): import("mobx").IReactionDisposer;
|
|
23
|
+
setTimeout(...args: Parameters<typeof self.setTimeout>): number;
|
|
24
|
+
setInterval(...args: Parameters<typeof self.setInterval>): number;
|
|
25
|
+
}
|
|
26
|
+
export {};
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { epos } from 'epos';
|
|
2
|
+
import { createLog } from '@eposlabs/utils';
|
|
3
|
+
const _root_ = Symbol('root');
|
|
4
|
+
const _disposers_ = Symbol('disposers');
|
|
5
|
+
const _tempParent_ = Symbol('parent');
|
|
6
|
+
export class Unit {
|
|
7
|
+
constructor(parent = null) {
|
|
8
|
+
this[_tempParent_] = parent;
|
|
9
|
+
}
|
|
10
|
+
// ---------------------------------------------------------------------------
|
|
11
|
+
// INIT
|
|
12
|
+
// ---------------------------------------------------------------------------
|
|
13
|
+
[epos.store.symbols.model.init]() {
|
|
14
|
+
const Unit = this.constructor;
|
|
15
|
+
const prototypeKeys = Object.getOwnPropertyNames(Unit.prototype);
|
|
16
|
+
// Set disposers container
|
|
17
|
+
this[_disposers_] = [];
|
|
18
|
+
// Bind all methods
|
|
19
|
+
for (const key of prototypeKeys) {
|
|
20
|
+
if (typeof this[key] !== 'function')
|
|
21
|
+
continue;
|
|
22
|
+
this[key] = this[key].bind(this);
|
|
23
|
+
}
|
|
24
|
+
// Wrap UI methods to components
|
|
25
|
+
for (const key of prototypeKeys) {
|
|
26
|
+
if (typeof this[key] !== 'function')
|
|
27
|
+
continue;
|
|
28
|
+
if (!isUiKey(key))
|
|
29
|
+
continue;
|
|
30
|
+
const componentName = key === 'ui' ? this['@'] : [this['@'], key].join('-');
|
|
31
|
+
this[key] = epos.component(componentName, this[key]);
|
|
32
|
+
}
|
|
33
|
+
// Define log method
|
|
34
|
+
const log = createLog(this['@']);
|
|
35
|
+
Reflect.defineProperty(this, 'log', { get: () => log });
|
|
36
|
+
// Call init method
|
|
37
|
+
if (typeof this.init === 'function')
|
|
38
|
+
this.init();
|
|
39
|
+
}
|
|
40
|
+
// ---------------------------------------------------------------------------
|
|
41
|
+
// CLEANUP
|
|
42
|
+
// ---------------------------------------------------------------------------
|
|
43
|
+
[epos.store.symbols.model.cleanup]() {
|
|
44
|
+
// Call disposers
|
|
45
|
+
this[_disposers_].forEach(disposer => disposer());
|
|
46
|
+
this[_disposers_] = [];
|
|
47
|
+
// Call cleanup method
|
|
48
|
+
if (typeof this.cleanup === 'function')
|
|
49
|
+
this.cleanup();
|
|
50
|
+
}
|
|
51
|
+
// ---------------------------------------------------------------------------
|
|
52
|
+
// VERSIONER
|
|
53
|
+
// ---------------------------------------------------------------------------
|
|
54
|
+
static get [epos.store.symbols.model.versioner]() {
|
|
55
|
+
if (!('versioner' in this))
|
|
56
|
+
return null;
|
|
57
|
+
return this.versioner;
|
|
58
|
+
}
|
|
59
|
+
static defineVersioner(versioner) {
|
|
60
|
+
return versioner;
|
|
61
|
+
}
|
|
62
|
+
// ---------------------------------------------------------------------------
|
|
63
|
+
// GETTERS & METHODS
|
|
64
|
+
// ---------------------------------------------------------------------------
|
|
65
|
+
get $() {
|
|
66
|
+
this[_root_] ??= findRoot(this);
|
|
67
|
+
return this[_root_];
|
|
68
|
+
}
|
|
69
|
+
up(Ancestor) {
|
|
70
|
+
let cursor = getParent(this);
|
|
71
|
+
while (cursor) {
|
|
72
|
+
if (cursor instanceof Ancestor)
|
|
73
|
+
return cursor;
|
|
74
|
+
cursor = getParent(cursor);
|
|
75
|
+
}
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
autorun(...args) {
|
|
79
|
+
const disposer = epos.libs.mobx.autorun(...args);
|
|
80
|
+
this[_disposers_].push(disposer);
|
|
81
|
+
return disposer;
|
|
82
|
+
}
|
|
83
|
+
reaction(...args) {
|
|
84
|
+
const disposer = epos.libs.mobx.reaction(...args);
|
|
85
|
+
this[_disposers_].push(disposer);
|
|
86
|
+
return disposer;
|
|
87
|
+
}
|
|
88
|
+
setTimeout(...args) {
|
|
89
|
+
const id = self.setTimeout(...args);
|
|
90
|
+
this[_disposers_].push(() => self.clearTimeout(id));
|
|
91
|
+
return id;
|
|
92
|
+
}
|
|
93
|
+
setInterval(...args) {
|
|
94
|
+
const id = self.setInterval(...args);
|
|
95
|
+
this[_disposers_].push(() => self.clearInterval(id));
|
|
96
|
+
return id;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
// ---------------------------------------------------------------------------
|
|
100
|
+
// HELPERS
|
|
101
|
+
// ---------------------------------------------------------------------------
|
|
102
|
+
function isUiKey(key) {
|
|
103
|
+
return key === 'ui' || /^[A-Z][a-zA-Z0-9]*$/.test(key);
|
|
104
|
+
}
|
|
105
|
+
function getParent(unit) {
|
|
106
|
+
return unit[_tempParent_] ?? unit[epos.store.symbols.model.parent];
|
|
107
|
+
}
|
|
108
|
+
function findRoot(unit) {
|
|
109
|
+
let root = unit;
|
|
110
|
+
let cursor = unit;
|
|
111
|
+
while (cursor) {
|
|
112
|
+
if (cursor instanceof Unit)
|
|
113
|
+
root = cursor;
|
|
114
|
+
cursor = getParent(cursor);
|
|
115
|
+
}
|
|
116
|
+
return root;
|
|
117
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "epos-unit",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "imkost",
|
|
7
|
+
"description": "",
|
|
8
|
+
"keywords": [],
|
|
9
|
+
"scripts": {
|
|
10
|
+
"dev": "rm -rf dist && tsc -w",
|
|
11
|
+
"build": "rm -rf dist && tsc",
|
|
12
|
+
"lint": "tsc --noEmit",
|
|
13
|
+
"release": "sh -c 'npm version ${1:-patch} && npm run build && npm publish' --"
|
|
14
|
+
},
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"import": "./dist/index.js"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist"
|
|
22
|
+
],
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@eposlabs/utils": "^1.0.13",
|
|
25
|
+
"epos": "^1.7.1"
|
|
26
|
+
}
|
|
27
|
+
}
|