@itrocks/lazy-loading 0.0.9 → 0.0.11
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/cjs/lazy-loading.js +48 -9
- package/esm/lazy-loading.js +48 -9
- package/package.json +1 -1
package/cjs/lazy-loading.js
CHANGED
|
@@ -5,16 +5,17 @@ exports.initClass = initClass;
|
|
|
5
5
|
exports.initLazyLoading = initLazyLoading;
|
|
6
6
|
const class_type_1 = require("@itrocks/class-type");
|
|
7
7
|
const property_type_1 = require("@itrocks/property-type");
|
|
8
|
+
const property_type_2 = require("@itrocks/property-type");
|
|
8
9
|
const reflect_1 = require("@itrocks/reflect");
|
|
9
10
|
const storage_1 = require("@itrocks/storage");
|
|
10
11
|
const store_1 = require("@itrocks/store");
|
|
11
12
|
exports.PROTECT_GET = Symbol('protectGet');
|
|
12
|
-
|
|
13
|
+
const deferredActions = new Array;
|
|
14
|
+
function defineCollectionProperty(elementType, property, builtClass) {
|
|
13
15
|
const descriptor = {
|
|
14
16
|
configurable: true,
|
|
15
17
|
enumerable: true,
|
|
16
18
|
async get() {
|
|
17
|
-
const elementType = type.elementType;
|
|
18
19
|
const ids = this[property + 'Ids'];
|
|
19
20
|
return this[property] = ids
|
|
20
21
|
? await (0, storage_1.dataSource)().readMultiple(elementType, ids)
|
|
@@ -48,6 +49,22 @@ function defineObjectProperty(type, property, builtClass) {
|
|
|
48
49
|
Reflect.defineMetadata(exports.PROTECT_GET, true, builtClass.prototype, property);
|
|
49
50
|
return property;
|
|
50
51
|
}
|
|
52
|
+
function defineCollectionPropertyAction(BuiltClass, properties, property) {
|
|
53
|
+
const type = property.collectionType.elementType.lead;
|
|
54
|
+
if ((0, class_type_1.isAnyType)(type)) {
|
|
55
|
+
properties.push(defineCollectionProperty(type, property.name, BuiltClass));
|
|
56
|
+
return true;
|
|
57
|
+
}
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
function defineObjectPropertyAction(BuiltClass, properties, property) {
|
|
61
|
+
const type = property.type.lead;
|
|
62
|
+
if ((0, class_type_1.isAnyType)(type)) {
|
|
63
|
+
properties.push(defineObjectProperty(type, property.name, BuiltClass));
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
51
68
|
function initClass(classType) {
|
|
52
69
|
try {
|
|
53
70
|
if (!(0, store_1.storeOf)(classType))
|
|
@@ -72,18 +89,29 @@ function initClass(classType) {
|
|
|
72
89
|
}
|
|
73
90
|
}
|
|
74
91
|
})();
|
|
92
|
+
let resultingBuiltClass = undefined;
|
|
75
93
|
for (const property of new reflect_1.ReflectClass(classType).properties) {
|
|
76
|
-
const
|
|
77
|
-
if (!
|
|
94
|
+
const propertyType = property.type;
|
|
95
|
+
if (!propertyType)
|
|
78
96
|
continue;
|
|
79
|
-
if (
|
|
80
|
-
|
|
97
|
+
if (propertyType instanceof property_type_1.CollectionType) {
|
|
98
|
+
if (defineCollectionPropertyAction(BuiltClass, properties, property)) {
|
|
99
|
+
resultingBuiltClass = BuiltClass;
|
|
100
|
+
}
|
|
101
|
+
else if (propertyType.elementType.type instanceof property_type_2.DeferredType) {
|
|
102
|
+
resultingBuiltClass = BuiltClass;
|
|
103
|
+
deferredActions.push(() => defineCollectionPropertyAction(BuiltClass, properties, property));
|
|
104
|
+
}
|
|
81
105
|
}
|
|
82
|
-
else if ((
|
|
83
|
-
|
|
106
|
+
else if (defineObjectPropertyAction(BuiltClass, properties, property)) {
|
|
107
|
+
resultingBuiltClass = BuiltClass;
|
|
108
|
+
}
|
|
109
|
+
else if (propertyType?.type instanceof property_type_2.DeferredType) {
|
|
110
|
+
resultingBuiltClass = BuiltClass;
|
|
111
|
+
deferredActions.push(() => defineObjectPropertyAction(BuiltClass, properties, property));
|
|
84
112
|
}
|
|
85
113
|
}
|
|
86
|
-
return
|
|
114
|
+
return resultingBuiltClass;
|
|
87
115
|
}
|
|
88
116
|
function initLazyLoading() {
|
|
89
117
|
const Module = require('module');
|
|
@@ -113,6 +141,17 @@ function initLazyLoading() {
|
|
|
113
141
|
// @ts-ignore TS18048 but module is always initialized
|
|
114
142
|
module[name] = withORM;
|
|
115
143
|
}
|
|
144
|
+
if (deferredActions.length) {
|
|
145
|
+
let deferredAction;
|
|
146
|
+
while (deferredAction = deferredActions[deferredActions.length - 1]) {
|
|
147
|
+
if (deferredAction()) {
|
|
148
|
+
deferredActions.pop();
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
break;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
116
155
|
return module ?? original;
|
|
117
156
|
};
|
|
118
157
|
}
|
package/esm/lazy-loading.js
CHANGED
|
@@ -2,16 +2,17 @@ import { createRequire } from 'module'
|
|
|
2
2
|
const require = createRequire(import.meta.url)
|
|
3
3
|
import { isAnyType } from '@itrocks/class-type';
|
|
4
4
|
import { CollectionType } from '@itrocks/property-type';
|
|
5
|
+
import { DeferredType } from '@itrocks/property-type';
|
|
5
6
|
import { ReflectClass } from '@itrocks/reflect';
|
|
6
7
|
import { dataSource } from '@itrocks/storage';
|
|
7
8
|
import { storeOf } from '@itrocks/store';
|
|
8
9
|
export const PROTECT_GET = Symbol('protectGet');
|
|
9
|
-
|
|
10
|
+
const deferredActions = new Array;
|
|
11
|
+
function defineCollectionProperty(elementType, property, builtClass) {
|
|
10
12
|
const descriptor = {
|
|
11
13
|
configurable: true,
|
|
12
14
|
enumerable: true,
|
|
13
15
|
async get() {
|
|
14
|
-
const elementType = type.elementType;
|
|
15
16
|
const ids = this[property + 'Ids'];
|
|
16
17
|
return this[property] = ids
|
|
17
18
|
? await dataSource().readMultiple(elementType, ids)
|
|
@@ -45,6 +46,22 @@ function defineObjectProperty(type, property, builtClass) {
|
|
|
45
46
|
Reflect.defineMetadata(PROTECT_GET, true, builtClass.prototype, property);
|
|
46
47
|
return property;
|
|
47
48
|
}
|
|
49
|
+
function defineCollectionPropertyAction(BuiltClass, properties, property) {
|
|
50
|
+
const type = property.collectionType.elementType.lead;
|
|
51
|
+
if (isAnyType(type)) {
|
|
52
|
+
properties.push(defineCollectionProperty(type, property.name, BuiltClass));
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
function defineObjectPropertyAction(BuiltClass, properties, property) {
|
|
58
|
+
const type = property.type.lead;
|
|
59
|
+
if (isAnyType(type)) {
|
|
60
|
+
properties.push(defineObjectProperty(type, property.name, BuiltClass));
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
48
65
|
export function initClass(classType) {
|
|
49
66
|
try {
|
|
50
67
|
if (!storeOf(classType))
|
|
@@ -69,18 +86,29 @@ export function initClass(classType) {
|
|
|
69
86
|
}
|
|
70
87
|
}
|
|
71
88
|
})();
|
|
89
|
+
let resultingBuiltClass = undefined;
|
|
72
90
|
for (const property of new ReflectClass(classType).properties) {
|
|
73
|
-
const
|
|
74
|
-
if (!
|
|
91
|
+
const propertyType = property.type;
|
|
92
|
+
if (!propertyType)
|
|
75
93
|
continue;
|
|
76
|
-
if (
|
|
77
|
-
|
|
94
|
+
if (propertyType instanceof CollectionType) {
|
|
95
|
+
if (defineCollectionPropertyAction(BuiltClass, properties, property)) {
|
|
96
|
+
resultingBuiltClass = BuiltClass;
|
|
97
|
+
}
|
|
98
|
+
else if (propertyType.elementType.type instanceof DeferredType) {
|
|
99
|
+
resultingBuiltClass = BuiltClass;
|
|
100
|
+
deferredActions.push(() => defineCollectionPropertyAction(BuiltClass, properties, property));
|
|
101
|
+
}
|
|
78
102
|
}
|
|
79
|
-
else if (
|
|
80
|
-
|
|
103
|
+
else if (defineObjectPropertyAction(BuiltClass, properties, property)) {
|
|
104
|
+
resultingBuiltClass = BuiltClass;
|
|
105
|
+
}
|
|
106
|
+
else if (propertyType?.type instanceof DeferredType) {
|
|
107
|
+
resultingBuiltClass = BuiltClass;
|
|
108
|
+
deferredActions.push(() => defineObjectPropertyAction(BuiltClass, properties, property));
|
|
81
109
|
}
|
|
82
110
|
}
|
|
83
|
-
return
|
|
111
|
+
return resultingBuiltClass;
|
|
84
112
|
}
|
|
85
113
|
export function initLazyLoading() {
|
|
86
114
|
const Module = require('module');
|
|
@@ -110,6 +138,17 @@ export function initLazyLoading() {
|
|
|
110
138
|
// @ts-ignore TS18048 but module is always initialized
|
|
111
139
|
module[name] = withORM;
|
|
112
140
|
}
|
|
141
|
+
if (deferredActions.length) {
|
|
142
|
+
let deferredAction;
|
|
143
|
+
while (deferredAction = deferredActions[deferredActions.length - 1]) {
|
|
144
|
+
if (deferredAction()) {
|
|
145
|
+
deferredActions.pop();
|
|
146
|
+
}
|
|
147
|
+
else {
|
|
148
|
+
break;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
113
152
|
return module ?? original;
|
|
114
153
|
};
|
|
115
154
|
}
|
package/package.json
CHANGED