epos-unit 1.1.2 → 1.1.4

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.
@@ -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
+ private [_root_];
12
+ private [_parent_];
13
+ private [_disposers_];
14
+ [key: PropertyKey]: unknown;
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,114 @@
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 Unit2 = this.constructor;
16
+ const descriptors = Object.getOwnPropertyDescriptors(Unit2.prototype);
17
+ const keys = Reflect.ownKeys(descriptors);
18
+ const disposers = /* @__PURE__ */ new Set();
19
+ Reflect.defineProperty(this, _disposers_, { get: () => disposers });
20
+ for (const key of keys) {
21
+ if (key === "constructor") continue;
22
+ const descriptor = descriptors[key];
23
+ if (descriptor.get || descriptor.set) continue;
24
+ if (typeof descriptor.value !== "function") continue;
25
+ this[key] = descriptor.value.bind(this);
26
+ }
27
+ for (const key of keys) {
28
+ if (typeof key === "symbol") continue;
29
+ if (!key.startsWith("ui")) continue;
30
+ const descriptor = descriptors[key];
31
+ if (descriptor.get || descriptor.set) continue;
32
+ if (typeof this[key] !== "function") continue;
33
+ const componentName = [this["@"], key.replace("ui", "")].filter(Boolean).join("-");
34
+ this[key] = epos.component(componentName, this[key]);
35
+ }
36
+ const log = createLog(this["@"]);
37
+ Reflect.defineProperty(this, "log", { get: () => log });
38
+ if (typeof this.init === "function") this.init();
39
+ }
40
+ // ---------------------------------------------------------------------------
41
+ // CLEANUP
42
+ // ---------------------------------------------------------------------------
43
+ [epos.state.symbols.modelCleanup]() {
44
+ this[_disposers_].forEach((disposer) => disposer());
45
+ this[_disposers_].clear();
46
+ if (typeof this.cleanup === "function") this.cleanup();
47
+ }
48
+ // ---------------------------------------------------------------------------
49
+ // VERSIONER
50
+ // ---------------------------------------------------------------------------
51
+ static get [epos.state.symbols.modelVersioner]() {
52
+ if (!("versioner" in this)) return null;
53
+ return this.versioner;
54
+ }
55
+ static defineVersioner(versioner) {
56
+ return versioner;
57
+ }
58
+ // ---------------------------------------------------------------------------
59
+ // ROOT
60
+ // ---------------------------------------------------------------------------
61
+ get $() {
62
+ this[_root_] ??= findRoot(this);
63
+ return this[_root_];
64
+ }
65
+ // ---------------------------------------------------------------------------
66
+ // METHODS
67
+ // ---------------------------------------------------------------------------
68
+ up(Ancestor) {
69
+ let cursor = getParent(this);
70
+ while (cursor) {
71
+ if (cursor instanceof Ancestor) return cursor;
72
+ cursor = getParent(cursor);
73
+ }
74
+ return null;
75
+ }
76
+ autorun(...args) {
77
+ const disposer = epos.libs.mobx.autorun(...args);
78
+ this[_disposers_].add(disposer);
79
+ return disposer;
80
+ }
81
+ reaction(...args) {
82
+ const disposer = epos.libs.mobx.reaction(...args);
83
+ this[_disposers_].add(disposer);
84
+ return disposer;
85
+ }
86
+ setTimeout(...args) {
87
+ const id = self.setTimeout(...args);
88
+ this[_disposers_].add(() => self.clearTimeout(id));
89
+ return id;
90
+ }
91
+ setInterval(...args) {
92
+ const id = self.setInterval(...args);
93
+ this[_disposers_].add(() => self.clearInterval(id));
94
+ return id;
95
+ }
96
+ };
97
+ function getParent(child) {
98
+ return child[_parent_] ?? child[epos.state.symbols.parent];
99
+ }
100
+ function findRoot(unit) {
101
+ let root = unit;
102
+ let cursor = unit;
103
+ while (cursor) {
104
+ if (cursor instanceof Unit) root = cursor;
105
+ cursor = getParent(cursor);
106
+ }
107
+ return root;
108
+ }
109
+ export {
110
+ Unit,
111
+ _disposers_,
112
+ _parent_,
113
+ _root_
114
+ };
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "epos-unit",
3
- "version": "1.1.2",
3
+ "version": "1.1.4",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "author": "imkost",
7
7
  "description": "",
8
8
  "keywords": [],
9
9
  "scripts": {
10
- "build": "tsup src --format esm --dts --clean",
10
+ "build": "tsup src --format esm --dts --clean --no-splitting",
11
11
  "lint": "tsc --noEmit",
12
12
  "release": "sh -c 'npm version ${1:-patch} && npm run build && npm publish' --"
13
13
  },
@@ -20,10 +20,11 @@
20
20
  }
21
21
  },
22
22
  "files": [
23
- "src"
23
+ "src",
24
+ "dist"
24
25
  ],
25
26
  "dependencies": {
26
- "@eposlabs/utils": "^1.1.1",
27
- "epos": "^1.8.0"
27
+ "@eposlabs/utils": "^1.2.1",
28
+ "epos": "^1.8.4"
28
29
  }
29
30
  }
package/src/epos-unit.ts CHANGED
@@ -24,7 +24,7 @@ export class Unit<TRoot = unknown> {
24
24
  // INIT
25
25
  // ---------------------------------------------------------------------------
26
26
 
27
- [epos.state.symbols.model.init]() {
27
+ [epos.state.symbols.modelInit]() {
28
28
  const Unit = this.constructor
29
29
  const descriptors: Descriptors = Object.getOwnPropertyDescriptors(Unit.prototype)
30
30
  const keys = Reflect.ownKeys(descriptors)
@@ -65,7 +65,7 @@ export class Unit<TRoot = unknown> {
65
65
  // CLEANUP
66
66
  // ---------------------------------------------------------------------------
67
67
 
68
- [epos.state.symbols.model.cleanup]() {
68
+ [epos.state.symbols.modelCleanup]() {
69
69
  // Call disposers
70
70
  this[_disposers_].forEach(disposer => disposer())
71
71
  this[_disposers_].clear()
@@ -78,7 +78,7 @@ export class Unit<TRoot = unknown> {
78
78
  // VERSIONER
79
79
  // ---------------------------------------------------------------------------
80
80
 
81
- static get [epos.state.symbols.model.versioner]() {
81
+ static get [epos.state.symbols.modelVersioner]() {
82
82
  if (!('versioner' in this)) return null
83
83
  return this.versioner
84
84
  }
@@ -139,7 +139,7 @@ export class Unit<TRoot = unknown> {
139
139
  // ---------------------------------------------------------------------------
140
140
 
141
141
  function getParent(child: any) {
142
- return child[_parent_] ?? child[epos.state.symbols.model.parent]
142
+ return child[_parent_] ?? child[epos.state.symbols.parent]
143
143
  }
144
144
 
145
145
  function findRoot(unit: Unit) {