epos-unit 1.0.6 → 1.1.1

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/package.json CHANGED
@@ -1,27 +1,26 @@
1
1
  {
2
2
  "name": "epos-unit",
3
- "version": "1.0.6",
3
+ "version": "1.1.1",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "author": "imkost",
7
7
  "description": "",
8
8
  "keywords": [],
9
9
  "scripts": {
10
- "dev": "rm -rf dist && tsc -w",
11
- "build": "rm -rf dist && tsc",
10
+ "build": "tsup src --format esm",
12
11
  "lint": "tsc --noEmit",
13
12
  "release": "sh -c 'npm version ${1:-patch} && npm run build && npm publish' --"
14
13
  },
15
14
  "exports": {
16
15
  ".": {
17
- "import": "./dist/index.js"
16
+ "import": "./src/epos-unit.ts"
18
17
  }
19
18
  },
20
19
  "files": [
21
- "dist"
20
+ "src"
22
21
  ],
23
22
  "dependencies": {
24
- "@eposlabs/utils": "^1.0.13",
25
- "epos": "^1.7.1"
23
+ "@eposlabs/utils": "^1.1.1",
24
+ "epos": "^1.8.0"
26
25
  }
27
26
  }
@@ -0,0 +1,155 @@
1
+ import { createLog, type Cls, type Log } from '@eposlabs/utils'
2
+ import { epos } from 'epos'
3
+ import type { FC } from 'react'
4
+
5
+ export const _root_ = Symbol('root')
6
+ export const _parent_ = Symbol('parent')
7
+ export const _disposers_ = Symbol('disposers')
8
+ export type Descriptors = Record<string | symbol, PropertyDescriptor>
9
+
10
+ export class Unit<TRoot = unknown> {
11
+ declare '@': string
12
+ declare log: Log
13
+ declare private [_root_]: TRoot
14
+ declare private [_parent_]: Unit<TRoot> | null // Parent reference for not-yet-attached units
15
+ declare private [_disposers_]: Set<() => void>;
16
+ [key: PropertyKey]: unknown
17
+
18
+ constructor(parent: Unit<TRoot> | null) {
19
+ // Define parent for not-yet-attached units
20
+ Reflect.defineProperty(this, _parent_, { get: () => parent })
21
+ }
22
+
23
+ // ---------------------------------------------------------------------------
24
+ // INIT
25
+ // ---------------------------------------------------------------------------
26
+
27
+ [epos.state.symbols.model.init]() {
28
+ const Unit = this.constructor
29
+ const descriptors: Descriptors = Object.getOwnPropertyDescriptors(Unit.prototype)
30
+ const keys = Reflect.ownKeys(descriptors)
31
+
32
+ // Setup disposers container
33
+ const disposers = new Set<() => void>()
34
+ Reflect.defineProperty(this, _disposers_, { get: () => disposers })
35
+
36
+ // Bind all methods
37
+ for (const key of keys) {
38
+ if (key === 'constructor') continue
39
+ const descriptor = descriptors[key]
40
+ if (descriptor.get || descriptor.set) continue
41
+ if (typeof descriptor.value !== 'function') continue
42
+ this[key] = descriptor.value.bind(this)
43
+ }
44
+
45
+ // Wrap UI methods to components
46
+ for (const key of keys) {
47
+ if (typeof key === 'symbol') continue
48
+ if (!key.startsWith('ui')) continue
49
+ const descriptor = descriptors[key]
50
+ if (descriptor.get || descriptor.set) continue
51
+ if (typeof this[key] !== 'function') continue
52
+ const componentName = [this['@'], key.replace('ui', '')].filter(Boolean).join('-')
53
+ this[key] = epos.component(componentName, this[key] as FC)
54
+ }
55
+
56
+ // Define log method
57
+ const log = createLog(this['@'])
58
+ Reflect.defineProperty(this, 'log', { get: () => log })
59
+
60
+ // Call init method
61
+ if (typeof this.init === 'function') this.init()
62
+ }
63
+
64
+ // ---------------------------------------------------------------------------
65
+ // CLEANUP
66
+ // ---------------------------------------------------------------------------
67
+
68
+ [epos.state.symbols.model.cleanup]() {
69
+ // Call disposers
70
+ this[_disposers_].forEach(disposer => disposer())
71
+ this[_disposers_].clear()
72
+
73
+ // Call cleanup method
74
+ if (typeof this.cleanup === 'function') this.cleanup()
75
+ }
76
+
77
+ // ---------------------------------------------------------------------------
78
+ // VERSIONER
79
+ // ---------------------------------------------------------------------------
80
+
81
+ static get [epos.state.symbols.model.versioner]() {
82
+ if (!('versioner' in this)) return null
83
+ return this.versioner
84
+ }
85
+
86
+ static defineVersioner(versioner: Record<number, (this: any, unit: any) => void>) {
87
+ return versioner
88
+ }
89
+
90
+ // ---------------------------------------------------------------------------
91
+ // ROOT
92
+ // ---------------------------------------------------------------------------
93
+
94
+ get $() {
95
+ this[_root_] ??= findRoot(this) as TRoot
96
+ return this[_root_]
97
+ }
98
+
99
+ // ---------------------------------------------------------------------------
100
+ // METHODS
101
+ // ---------------------------------------------------------------------------
102
+
103
+ up<T extends Unit>(Ancestor: Cls<T>): T | null {
104
+ let cursor: unknown = getParent(this)
105
+ while (cursor) {
106
+ if (cursor instanceof Ancestor) return cursor
107
+ cursor = getParent(cursor)
108
+ }
109
+ return null
110
+ }
111
+
112
+ autorun(...args: Parameters<typeof epos.libs.mobx.autorun>) {
113
+ const disposer = epos.libs.mobx.autorun(...args)
114
+ this[_disposers_].add(disposer)
115
+ return disposer
116
+ }
117
+
118
+ reaction(...args: Parameters<typeof epos.libs.mobx.reaction>) {
119
+ const disposer = epos.libs.mobx.reaction(...args)
120
+ this[_disposers_].add(disposer)
121
+ return disposer
122
+ }
123
+
124
+ setTimeout(...args: Parameters<typeof self.setTimeout>) {
125
+ const id = self.setTimeout(...args)
126
+ this[_disposers_].add(() => self.clearTimeout(id))
127
+ return id
128
+ }
129
+
130
+ setInterval(...args: Parameters<typeof self.setInterval>) {
131
+ const id = self.setInterval(...args)
132
+ this[_disposers_].add(() => self.clearInterval(id))
133
+ return id
134
+ }
135
+ }
136
+
137
+ // ---------------------------------------------------------------------------
138
+ // HELPERS
139
+ // ---------------------------------------------------------------------------
140
+
141
+ function getParent(child: any) {
142
+ return child[_parent_] ?? child[epos.state.symbols.model.parent]
143
+ }
144
+
145
+ function findRoot(unit: Unit) {
146
+ let root = unit
147
+ let cursor: any = unit
148
+
149
+ while (cursor) {
150
+ if (cursor instanceof Unit) root = cursor
151
+ cursor = getParent(cursor)
152
+ }
153
+
154
+ return root
155
+ }
@@ -1,25 +0,0 @@
1
- import { type Cls, type Log } from '@eposlabs/utils';
2
- import { epos } from 'epos';
3
- export declare const _root_: unique symbol;
4
- export declare const _parent_: unique symbol;
5
- export declare const _disposers_: unique symbol;
6
- export type Descriptors = Record<string | symbol, PropertyDescriptor>;
7
- export declare class Unit<TRoot = unknown> {
8
- '@': string;
9
- log: Log;
10
- private [_root_];
11
- private [_parent_];
12
- private [_disposers_];
13
- [key: PropertyKey]: unknown;
14
- constructor(parent: Unit<TRoot> | null);
15
- [epos.state.symbols.model.init](): void;
16
- [epos.state.symbols.model.cleanup](): void;
17
- static get [epos.state.symbols.model.versioner](): unknown;
18
- static defineVersioner(versioner: Record<number, (this: any, unit: any) => void>): Record<number, (this: any, unit: any) => void>;
19
- get $(): TRoot;
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
- }
package/dist/epos-unit.js DELETED
@@ -1,130 +0,0 @@
1
- import { createLog } from '@eposlabs/utils';
2
- import { epos } from 'epos';
3
- export const _root_ = Symbol('root');
4
- export const _parent_ = Symbol('parent');
5
- export const _disposers_ = Symbol('disposers');
6
- export class Unit {
7
- constructor(parent) {
8
- // Define parent for not-yet-attached units
9
- Reflect.defineProperty(this, _parent_, { get: () => parent });
10
- }
11
- // ---------------------------------------------------------------------------
12
- // INIT
13
- // ---------------------------------------------------------------------------
14
- [epos.state.symbols.model.init]() {
15
- const Unit = this.constructor;
16
- const descriptors = Object.getOwnPropertyDescriptors(Unit.prototype);
17
- const keys = Reflect.ownKeys(descriptors);
18
- // Setup disposers container
19
- const disposers = new Set();
20
- Reflect.defineProperty(this, _disposers_, { get: () => disposers });
21
- // Bind all methods
22
- for (const key of keys) {
23
- if (key === 'constructor')
24
- continue;
25
- const descriptor = descriptors[key];
26
- if (descriptor.get || descriptor.set)
27
- continue;
28
- if (typeof descriptor.value !== 'function')
29
- continue;
30
- this[key] = descriptor.value.bind(this);
31
- }
32
- // Wrap UI methods to components
33
- for (const key of keys) {
34
- if (typeof key === 'symbol')
35
- continue;
36
- if (!key.startsWith('ui'))
37
- continue;
38
- const descriptor = descriptors[key];
39
- if (descriptor.get || descriptor.set)
40
- continue;
41
- if (typeof this[key] !== 'function')
42
- continue;
43
- const componentName = [this['@'], key.replace('ui', '')].filter(Boolean).join('-');
44
- this[key] = epos.component(componentName, this[key]);
45
- }
46
- // Define log method
47
- const log = createLog(this['@']);
48
- Reflect.defineProperty(this, 'log', { get: () => log });
49
- // Call init method
50
- if (typeof this.init === 'function')
51
- this.init();
52
- }
53
- // ---------------------------------------------------------------------------
54
- // CLEANUP
55
- // ---------------------------------------------------------------------------
56
- [epos.state.symbols.model.cleanup]() {
57
- // Call disposers
58
- this[_disposers_].forEach(disposer => disposer());
59
- this[_disposers_].clear();
60
- // Call cleanup method
61
- if (typeof this.cleanup === 'function')
62
- this.cleanup();
63
- }
64
- // ---------------------------------------------------------------------------
65
- // VERSIONER
66
- // ---------------------------------------------------------------------------
67
- static get [epos.state.symbols.model.versioner]() {
68
- if (!('versioner' in this))
69
- return null;
70
- return this.versioner;
71
- }
72
- static defineVersioner(versioner) {
73
- return versioner;
74
- }
75
- // ---------------------------------------------------------------------------
76
- // ROOT
77
- // ---------------------------------------------------------------------------
78
- get $() {
79
- this[_root_] ??= findRoot(this);
80
- return this[_root_];
81
- }
82
- // ---------------------------------------------------------------------------
83
- // METHODS
84
- // ---------------------------------------------------------------------------
85
- up(Ancestor) {
86
- let cursor = getParent(this);
87
- while (cursor) {
88
- if (cursor instanceof Ancestor)
89
- return cursor;
90
- cursor = getParent(cursor);
91
- }
92
- return null;
93
- }
94
- autorun(...args) {
95
- const disposer = epos.libs.mobx.autorun(...args);
96
- this[_disposers_].add(disposer);
97
- return disposer;
98
- }
99
- reaction(...args) {
100
- const disposer = epos.libs.mobx.reaction(...args);
101
- this[_disposers_].add(disposer);
102
- return disposer;
103
- }
104
- setTimeout(...args) {
105
- const id = self.setTimeout(...args);
106
- this[_disposers_].add(() => self.clearTimeout(id));
107
- return id;
108
- }
109
- setInterval(...args) {
110
- const id = self.setInterval(...args);
111
- this[_disposers_].add(() => self.clearInterval(id));
112
- return id;
113
- }
114
- }
115
- // ---------------------------------------------------------------------------
116
- // HELPERS
117
- // ---------------------------------------------------------------------------
118
- function getParent(child) {
119
- return child[_parent_] ?? child[epos.state.symbols.model.parent];
120
- }
121
- function findRoot(unit) {
122
- let root = unit;
123
- let cursor = unit;
124
- while (cursor) {
125
- if (cursor instanceof Unit)
126
- root = cursor;
127
- cursor = getParent(cursor);
128
- }
129
- return root;
130
- }
package/dist/index.d.ts DELETED
@@ -1,3 +0,0 @@
1
- import { Unit } from './epos-unit';
2
- export { Unit };
3
- export default Unit;
package/dist/index.js DELETED
@@ -1,3 +0,0 @@
1
- import { Unit } from './epos-unit';
2
- export { Unit };
3
- export default Unit;