@itrocks/lazy-loading 0.2.1 → 0.2.2
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 +7 -14
- package/esm/lazy-loading.d.ts +0 -7
- package/esm/lazy-loading.js +0 -214
package/package.json
CHANGED
|
@@ -13,21 +13,17 @@
|
|
|
13
13
|
"description": "Integrates lazy loading for objects and collections in TypeScript classes",
|
|
14
14
|
"devDependencies": {
|
|
15
15
|
"@types/node": "^24.10",
|
|
16
|
-
"typescript": "
|
|
16
|
+
"typescript": "^6.0"
|
|
17
17
|
},
|
|
18
18
|
"engines": {
|
|
19
|
-
"node": ">=
|
|
20
|
-
},
|
|
21
|
-
"exports": {
|
|
22
|
-
"import": "./esm/lazy-loading.js",
|
|
23
|
-
"require": "./cjs/lazy-loading.js"
|
|
19
|
+
"node": ">=24"
|
|
24
20
|
},
|
|
21
|
+
"exports": "./cjs/lazy-loading.js",
|
|
25
22
|
"files": [
|
|
26
23
|
"LICENSE",
|
|
27
24
|
"README.md",
|
|
28
|
-
"
|
|
29
|
-
"
|
|
30
|
-
"!*/esm.*"
|
|
25
|
+
"cjs",
|
|
26
|
+
"!*.map"
|
|
31
27
|
],
|
|
32
28
|
"homepage": "https://it.rocks",
|
|
33
29
|
"keywords": [
|
|
@@ -51,10 +47,7 @@
|
|
|
51
47
|
"url": "git+https://github.com/itrocks-ts/lazy-loading.git"
|
|
52
48
|
},
|
|
53
49
|
"scripts": {
|
|
54
|
-
"build": "
|
|
55
|
-
"build:cjs": "tsc -p tsconfig.cjs.json",
|
|
56
|
-
"build:esm": "tsc -p tsconfig.esm.json && node esm/esm"
|
|
50
|
+
"build": "tsc"
|
|
57
51
|
},
|
|
58
|
-
"
|
|
59
|
-
"version": "0.2.1"
|
|
52
|
+
"version": "0.2.2"
|
|
60
53
|
}
|
package/esm/lazy-loading.d.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { Type } from '@itrocks/class-type';
|
|
2
|
-
export declare const PROTECT_GET: unique symbol;
|
|
3
|
-
export type PropertyDescriptorWithProtectGet = PropertyDescriptor & ThisType<any> & {
|
|
4
|
-
[PROTECT_GET]?: true;
|
|
5
|
-
};
|
|
6
|
-
export declare function initClass<T extends object>(classType: Type<T>): Type<T> | undefined;
|
|
7
|
-
export declare function initLazyLoading(): void;
|
package/esm/lazy-loading.js
DELETED
|
@@ -1,214 +0,0 @@
|
|
|
1
|
-
import { createRequire } from 'module'
|
|
2
|
-
const require = createRequire(import.meta.url)
|
|
3
|
-
import { isAnyType } from '@itrocks/class-type';
|
|
4
|
-
import { metadataNameOf } from '@itrocks/decorator/property';
|
|
5
|
-
import { CollectionType } from '@itrocks/property-type';
|
|
6
|
-
import { DeferredType } from '@itrocks/property-type';
|
|
7
|
-
import { ReflectClass } from '@itrocks/reflect';
|
|
8
|
-
import { dataSource } from '@itrocks/storage';
|
|
9
|
-
import { storeOf } from '@itrocks/store';
|
|
10
|
-
export const PROTECT_GET = Symbol('protectGet');
|
|
11
|
-
const deferredActions = new Array;
|
|
12
|
-
const deferredModules = new Array;
|
|
13
|
-
function defineCollectionProperty(elementType, property, builtClass) {
|
|
14
|
-
const metadataName = metadataNameOf(property);
|
|
15
|
-
const propertyIds = property.toString() + 'Ids';
|
|
16
|
-
const descriptor = {
|
|
17
|
-
configurable: true,
|
|
18
|
-
enumerable: true,
|
|
19
|
-
async get() {
|
|
20
|
-
const ids = this[propertyIds];
|
|
21
|
-
return this[property] = ids
|
|
22
|
-
? await dataSource().readMultiple(elementType, ids)
|
|
23
|
-
: await dataSource().readCollection(this, property, elementType);
|
|
24
|
-
},
|
|
25
|
-
set(value) {
|
|
26
|
-
delete this[propertyIds];
|
|
27
|
-
Object.defineProperty(this, property, { configurable: true, enumerable: true, value, writable: true });
|
|
28
|
-
Reflect.defineMetadata(PROTECT_GET, false, this, metadataName);
|
|
29
|
-
}
|
|
30
|
-
};
|
|
31
|
-
Object.defineProperty(builtClass.prototype, property, descriptor);
|
|
32
|
-
Reflect.defineMetadata(PROTECT_GET, true, builtClass.prototype, metadataName);
|
|
33
|
-
return property;
|
|
34
|
-
}
|
|
35
|
-
function defineObjectProperty(type, property, builtClass) {
|
|
36
|
-
const metadataName = metadataNameOf(property);
|
|
37
|
-
const propertyId = property.toString() + 'Id';
|
|
38
|
-
const descriptor = {
|
|
39
|
-
configurable: true,
|
|
40
|
-
enumerable: true,
|
|
41
|
-
async get() {
|
|
42
|
-
const id = this[propertyId];
|
|
43
|
-
return this[property] = id ? await dataSource().read(type, id) : undefined;
|
|
44
|
-
},
|
|
45
|
-
set(value) {
|
|
46
|
-
delete this[propertyId];
|
|
47
|
-
Object.defineProperty(this, property, { configurable: true, enumerable: true, value, writable: true });
|
|
48
|
-
Reflect.defineMetadata(PROTECT_GET, false, this, metadataName);
|
|
49
|
-
}
|
|
50
|
-
};
|
|
51
|
-
Object.defineProperty(builtClass.prototype, property, descriptor);
|
|
52
|
-
Reflect.defineMetadata(PROTECT_GET, true, builtClass.prototype, metadataName);
|
|
53
|
-
return property;
|
|
54
|
-
}
|
|
55
|
-
function defineCollectionPropertyAction(BuiltClass, properties, property) {
|
|
56
|
-
const type = property.collectionType.elementType.lead;
|
|
57
|
-
if (isAnyType(type)) {
|
|
58
|
-
properties.push(defineCollectionProperty(type, property.name, BuiltClass));
|
|
59
|
-
return true;
|
|
60
|
-
}
|
|
61
|
-
return false;
|
|
62
|
-
}
|
|
63
|
-
function defineObjectPropertyAction(BuiltClass, properties, property) {
|
|
64
|
-
const type = property.type.lead;
|
|
65
|
-
if (isAnyType(type)) {
|
|
66
|
-
properties.push(defineObjectProperty(type, property.name, BuiltClass));
|
|
67
|
-
return true;
|
|
68
|
-
}
|
|
69
|
-
return false;
|
|
70
|
-
}
|
|
71
|
-
export function initClass(classType) {
|
|
72
|
-
try {
|
|
73
|
-
if (!storeOf(classType))
|
|
74
|
-
return;
|
|
75
|
-
}
|
|
76
|
-
catch {
|
|
77
|
-
return;
|
|
78
|
-
}
|
|
79
|
-
const properties = [];
|
|
80
|
-
// @ts-ignore TS2415 classType is always a heritable class, not a function.
|
|
81
|
-
const BuiltClass = (() => class extends classType {
|
|
82
|
-
constructor(...args) {
|
|
83
|
-
super(...args);
|
|
84
|
-
for (const property of properties) {
|
|
85
|
-
const value = Object.getOwnPropertyDescriptor(this, property)?.value;
|
|
86
|
-
if ((value === undefined) || (Array.isArray(value) && !value.length)) {
|
|
87
|
-
// @ts-ignore TS2536 Type 'keyof T' cannot be used to index type 'this'.
|
|
88
|
-
delete this[property];
|
|
89
|
-
}
|
|
90
|
-
else {
|
|
91
|
-
Reflect.defineMetadata(PROTECT_GET, false, this, metadataNameOf(property));
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
})();
|
|
96
|
-
let resultingBuiltClass = undefined;
|
|
97
|
-
for (const property of new ReflectClass(classType).properties) {
|
|
98
|
-
const propertyType = property.type;
|
|
99
|
-
if (!propertyType)
|
|
100
|
-
continue;
|
|
101
|
-
if (propertyType instanceof CollectionType) {
|
|
102
|
-
if (defineCollectionPropertyAction(BuiltClass, properties, property)) {
|
|
103
|
-
resultingBuiltClass = BuiltClass;
|
|
104
|
-
}
|
|
105
|
-
else if (propertyType.elementType.type instanceof DeferredType) {
|
|
106
|
-
resultingBuiltClass = BuiltClass;
|
|
107
|
-
deferredActions.push(() => defineCollectionPropertyAction(BuiltClass, properties, property));
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
else if (defineObjectPropertyAction(BuiltClass, properties, property)) {
|
|
111
|
-
resultingBuiltClass = BuiltClass;
|
|
112
|
-
}
|
|
113
|
-
else if (propertyType?.type instanceof DeferredType) {
|
|
114
|
-
resultingBuiltClass = BuiltClass;
|
|
115
|
-
deferredActions.push(() => defineObjectPropertyAction(BuiltClass, properties, property));
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
return resultingBuiltClass;
|
|
119
|
-
}
|
|
120
|
-
export function initLazyLoading() {
|
|
121
|
-
const already = new Map;
|
|
122
|
-
let defer;
|
|
123
|
-
const Module = require('module');
|
|
124
|
-
const superRequire = Module.prototype.require;
|
|
125
|
-
function initModule(module, original) {
|
|
126
|
-
defer = undefined;
|
|
127
|
-
let replacements = new Map;
|
|
128
|
-
for (const [name, type] of Object.entries(original)) {
|
|
129
|
-
defer = (defer === undefined)
|
|
130
|
-
? (type === undefined)
|
|
131
|
-
: (defer && (type === undefined));
|
|
132
|
-
if (!isAnyType(type))
|
|
133
|
-
continue;
|
|
134
|
-
if (module && replacements) {
|
|
135
|
-
const replacement = replacements.get(type);
|
|
136
|
-
if (replacement) {
|
|
137
|
-
module[name] = replacement;
|
|
138
|
-
continue;
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
const withORM = initClass(type);
|
|
142
|
-
if (!withORM)
|
|
143
|
-
continue;
|
|
144
|
-
replacements.set(type, withORM);
|
|
145
|
-
if (!module) {
|
|
146
|
-
module = { ...original };
|
|
147
|
-
already.set(original, module);
|
|
148
|
-
}
|
|
149
|
-
module[name] = withORM;
|
|
150
|
-
}
|
|
151
|
-
return (defer || replacements.size) ? (module ?? { ...original }) : original;
|
|
152
|
-
}
|
|
153
|
-
function resolveDeferredActions() {
|
|
154
|
-
while (deferredActions.length) {
|
|
155
|
-
if (deferredActions[deferredActions.length - 1]())
|
|
156
|
-
deferredActions.pop();
|
|
157
|
-
else
|
|
158
|
-
break;
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
function resolveDeferredModules() {
|
|
162
|
-
while (deferredModules.length) {
|
|
163
|
-
const [original, module, _file] = deferredModules[deferredModules.length - 1];
|
|
164
|
-
if (initModule(module, original) === original) {
|
|
165
|
-
Object.assign(module, original);
|
|
166
|
-
}
|
|
167
|
-
if (defer)
|
|
168
|
-
break;
|
|
169
|
-
else {
|
|
170
|
-
deferredModules.pop();
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
Module.prototype.require = function () {
|
|
175
|
-
const original = superRequire.call(this, ...arguments);
|
|
176
|
-
const alreadyModule = already.get(original);
|
|
177
|
-
if (alreadyModule) {
|
|
178
|
-
return alreadyModule;
|
|
179
|
-
}
|
|
180
|
-
already.set(original, original);
|
|
181
|
-
const module = initModule(alreadyModule, original);
|
|
182
|
-
if (defer) {
|
|
183
|
-
// { ...original } into module may be enough. \/ this allows resolving during accessing value, if it is late.
|
|
184
|
-
for (const [name, value] of Object.entries(original)) {
|
|
185
|
-
Object.defineProperty(module, name, {
|
|
186
|
-
configurable: true,
|
|
187
|
-
enumerable: true,
|
|
188
|
-
get() {
|
|
189
|
-
if (original[name] !== undefined) {
|
|
190
|
-
resolveDeferredModules();
|
|
191
|
-
resolveDeferredActions();
|
|
192
|
-
return module[name];
|
|
193
|
-
}
|
|
194
|
-
return value;
|
|
195
|
-
},
|
|
196
|
-
set(value) {
|
|
197
|
-
let descriptor = Object.getOwnPropertyDescriptor(original, name)
|
|
198
|
-
?? { configurable: true, enumerable: true };
|
|
199
|
-
delete descriptor.get;
|
|
200
|
-
delete descriptor.set;
|
|
201
|
-
descriptor.value = value;
|
|
202
|
-
Object.defineProperty(module, name, descriptor);
|
|
203
|
-
}
|
|
204
|
-
});
|
|
205
|
-
}
|
|
206
|
-
already.set(original, module);
|
|
207
|
-
deferredModules.push([original, module, arguments[0]]);
|
|
208
|
-
}
|
|
209
|
-
resolveDeferredModules();
|
|
210
|
-
resolveDeferredActions();
|
|
211
|
-
return module ?? original;
|
|
212
|
-
};
|
|
213
|
-
}
|
|
214
|
-
//# sourceMappingURL=lazy-loading.js.map
|