epos-unit 1.5.0 → 1.7.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 +28 -0
- package/dist/epos-unit.js +116 -0
- package/package.json +13 -6
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import * as mobx from 'mobx';
|
|
2
|
+
import { Log, Cls } from '@eposlabs/utils';
|
|
3
|
+
|
|
4
|
+
declare const _root_: unique symbol;
|
|
5
|
+
declare const _parent_: unique symbol;
|
|
6
|
+
declare const _disposers_: unique symbol;
|
|
7
|
+
type Descriptors = Record<string | symbol, PropertyDescriptor>;
|
|
8
|
+
declare class Unit<TRoot = unknown> {
|
|
9
|
+
'@': string;
|
|
10
|
+
log: Log;
|
|
11
|
+
[':version']?: number;
|
|
12
|
+
private [_root_];
|
|
13
|
+
private [_parent_];
|
|
14
|
+
private [_disposers_];
|
|
15
|
+
constructor(parent: Unit<TRoot> | null);
|
|
16
|
+
[epos.state.symbols.modelInit](): void;
|
|
17
|
+
[epos.state.symbols.modelCleanup](): void;
|
|
18
|
+
static get [epos.state.symbols.modelVersioner](): unknown;
|
|
19
|
+
static defineVersioner(versioner: Record<number, (this: any, unit: any) => void>): Record<number, (this: any, unit: any) => void>;
|
|
20
|
+
get $(): TRoot;
|
|
21
|
+
up<T extends Unit>(Ancestor: Cls<T>): T | null;
|
|
22
|
+
autorun(...args: Parameters<typeof epos.libs.mobx.autorun>): mobx.IReactionDisposer;
|
|
23
|
+
reaction(...args: Parameters<typeof epos.libs.mobx.reaction>): mobx.IReactionDisposer;
|
|
24
|
+
setTimeout(...args: Parameters<typeof self.setTimeout>): number;
|
|
25
|
+
setInterval(...args: Parameters<typeof self.setInterval>): number;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export { type Descriptors, Unit, _disposers_, _parent_, _root_ };
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
// src/epos-unit.ts
|
|
2
|
+
import "epos";
|
|
3
|
+
import { createLog } from "@eposlabs/utils";
|
|
4
|
+
var _root_ = Symbol("root");
|
|
5
|
+
var _parent_ = Symbol("parent");
|
|
6
|
+
var _disposers_ = Symbol("disposers");
|
|
7
|
+
var Unit = class {
|
|
8
|
+
constructor(parent) {
|
|
9
|
+
Reflect.defineProperty(this, _parent_, { get: () => parent });
|
|
10
|
+
}
|
|
11
|
+
// ---------------------------------------------------------------------------
|
|
12
|
+
// INIT
|
|
13
|
+
// ---------------------------------------------------------------------------
|
|
14
|
+
[epos.state.symbols.modelInit]() {
|
|
15
|
+
const _this = this;
|
|
16
|
+
const Unit2 = this.constructor;
|
|
17
|
+
const descriptors = Object.getOwnPropertyDescriptors(Unit2.prototype);
|
|
18
|
+
const keys = Reflect.ownKeys(descriptors);
|
|
19
|
+
const disposers = /* @__PURE__ */ new Set();
|
|
20
|
+
Reflect.defineProperty(this, _disposers_, { get: () => disposers });
|
|
21
|
+
for (const key of keys) {
|
|
22
|
+
if (key === "constructor") continue;
|
|
23
|
+
const descriptor = descriptors[key];
|
|
24
|
+
if (descriptor.get || descriptor.set) continue;
|
|
25
|
+
if (typeof descriptor.value !== "function") continue;
|
|
26
|
+
_this[key] = descriptor.value.bind(this);
|
|
27
|
+
}
|
|
28
|
+
for (const key of keys) {
|
|
29
|
+
if (typeof key === "symbol") continue;
|
|
30
|
+
if (!key.startsWith("ui")) continue;
|
|
31
|
+
const descriptor = descriptors[key];
|
|
32
|
+
if (descriptor.get || descriptor.set) continue;
|
|
33
|
+
if (typeof _this[key] !== "function") continue;
|
|
34
|
+
const componentName = [this["@"], key.replace("ui", "")].filter(Boolean).join("-");
|
|
35
|
+
_this[key] = epos.component(componentName, _this[key]);
|
|
36
|
+
}
|
|
37
|
+
const log = createLog(this["@"]);
|
|
38
|
+
Reflect.defineProperty(this, "log", { get: () => log });
|
|
39
|
+
if (typeof _this.init === "function") _this.init();
|
|
40
|
+
}
|
|
41
|
+
// ---------------------------------------------------------------------------
|
|
42
|
+
// CLEANUP
|
|
43
|
+
// ---------------------------------------------------------------------------
|
|
44
|
+
[epos.state.symbols.modelCleanup]() {
|
|
45
|
+
const _this = this;
|
|
46
|
+
this[_disposers_].forEach((disposer) => disposer());
|
|
47
|
+
this[_disposers_].clear();
|
|
48
|
+
if (typeof _this.cleanup === "function") _this.cleanup();
|
|
49
|
+
}
|
|
50
|
+
// ---------------------------------------------------------------------------
|
|
51
|
+
// VERSIONER
|
|
52
|
+
// ---------------------------------------------------------------------------
|
|
53
|
+
static get [epos.state.symbols.modelVersioner]() {
|
|
54
|
+
if (!("versioner" in this)) return null;
|
|
55
|
+
return this.versioner;
|
|
56
|
+
}
|
|
57
|
+
static defineVersioner(versioner) {
|
|
58
|
+
return versioner;
|
|
59
|
+
}
|
|
60
|
+
// ---------------------------------------------------------------------------
|
|
61
|
+
// ROOT
|
|
62
|
+
// ---------------------------------------------------------------------------
|
|
63
|
+
get $() {
|
|
64
|
+
this[_root_] ??= findRoot(this);
|
|
65
|
+
return this[_root_];
|
|
66
|
+
}
|
|
67
|
+
// ---------------------------------------------------------------------------
|
|
68
|
+
// METHODS
|
|
69
|
+
// ---------------------------------------------------------------------------
|
|
70
|
+
up(Ancestor) {
|
|
71
|
+
let cursor = getParent(this);
|
|
72
|
+
while (cursor) {
|
|
73
|
+
if (cursor instanceof Ancestor) 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_].add(disposer);
|
|
81
|
+
return disposer;
|
|
82
|
+
}
|
|
83
|
+
reaction(...args) {
|
|
84
|
+
const disposer = epos.libs.mobx.reaction(...args);
|
|
85
|
+
this[_disposers_].add(disposer);
|
|
86
|
+
return disposer;
|
|
87
|
+
}
|
|
88
|
+
setTimeout(...args) {
|
|
89
|
+
const id = self.setTimeout(...args);
|
|
90
|
+
this[_disposers_].add(() => self.clearTimeout(id));
|
|
91
|
+
return id;
|
|
92
|
+
}
|
|
93
|
+
setInterval(...args) {
|
|
94
|
+
const id = self.setInterval(...args);
|
|
95
|
+
this[_disposers_].add(() => self.clearInterval(id));
|
|
96
|
+
return id;
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
function getParent(child) {
|
|
100
|
+
return child[_parent_] ?? child[epos.state.symbols.parent];
|
|
101
|
+
}
|
|
102
|
+
function findRoot(unit) {
|
|
103
|
+
let root = unit;
|
|
104
|
+
let cursor = unit;
|
|
105
|
+
while (cursor) {
|
|
106
|
+
if (cursor instanceof Unit) root = cursor;
|
|
107
|
+
cursor = getParent(cursor);
|
|
108
|
+
}
|
|
109
|
+
return root;
|
|
110
|
+
}
|
|
111
|
+
export {
|
|
112
|
+
Unit,
|
|
113
|
+
_disposers_,
|
|
114
|
+
_parent_,
|
|
115
|
+
_root_
|
|
116
|
+
};
|
package/package.json
CHANGED
|
@@ -1,23 +1,30 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "epos-unit",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.7.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "imkost",
|
|
7
7
|
"description": "",
|
|
8
8
|
"keywords": [],
|
|
9
9
|
"scripts": {
|
|
10
|
+
"dev": "tsup --config ../tsup.config.ts --watch",
|
|
11
|
+
"build": "tsup --config ../tsup.config.ts",
|
|
10
12
|
"lint": "tsc --noEmit",
|
|
11
|
-
"release": "sh -c 'npm version ${1:-minor} && npm publish' --"
|
|
13
|
+
"release": "sh -c 'npm version ${1:-minor} && npm run build && npm publish' --"
|
|
12
14
|
},
|
|
13
15
|
"exports": {
|
|
14
|
-
".":
|
|
16
|
+
".": {
|
|
17
|
+
"source": "./src/epos-unit.ts",
|
|
18
|
+
"import": "./dist/epos-unit.js",
|
|
19
|
+
"types": "./dist/epos-unit.d.ts"
|
|
20
|
+
}
|
|
15
21
|
},
|
|
16
22
|
"files": [
|
|
17
|
-
"src"
|
|
23
|
+
"src",
|
|
24
|
+
"dist"
|
|
18
25
|
],
|
|
19
26
|
"dependencies": {
|
|
20
|
-
"@eposlabs/utils": "^1.
|
|
21
|
-
"epos": "^1.
|
|
27
|
+
"@eposlabs/utils": "^1.9.0",
|
|
28
|
+
"epos": "^1.17.0"
|
|
22
29
|
}
|
|
23
30
|
}
|