@itrocks/lazy-loading 0.0.11 → 0.0.13
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 +79 -17
- package/esm/lazy-loading.js +79 -17
- package/package.json +1 -1
package/cjs/lazy-loading.js
CHANGED
|
@@ -11,6 +11,7 @@ const storage_1 = require("@itrocks/storage");
|
|
|
11
11
|
const store_1 = require("@itrocks/store");
|
|
12
12
|
exports.PROTECT_GET = Symbol('protectGet');
|
|
13
13
|
const deferredActions = new Array;
|
|
14
|
+
const deferredModules = new Array;
|
|
14
15
|
function defineCollectionProperty(elementType, property, builtClass) {
|
|
15
16
|
const descriptor = {
|
|
16
17
|
configurable: true,
|
|
@@ -29,6 +30,7 @@ function defineCollectionProperty(elementType, property, builtClass) {
|
|
|
29
30
|
};
|
|
30
31
|
Object.defineProperty(builtClass.prototype, property, descriptor);
|
|
31
32
|
Reflect.defineMetadata(exports.PROTECT_GET, true, builtClass.prototype, property);
|
|
33
|
+
console.log('defineCollectionProperty', new reflect_1.ReflectClass(builtClass).name + '.' + property);
|
|
32
34
|
return property;
|
|
33
35
|
}
|
|
34
36
|
function defineObjectProperty(type, property, builtClass) {
|
|
@@ -47,6 +49,7 @@ function defineObjectProperty(type, property, builtClass) {
|
|
|
47
49
|
};
|
|
48
50
|
Object.defineProperty(builtClass.prototype, property, descriptor);
|
|
49
51
|
Reflect.defineMetadata(exports.PROTECT_GET, true, builtClass.prototype, property);
|
|
52
|
+
console.log('defineObjectProperty', new reflect_1.ReflectClass(builtClass).name + '.' + property);
|
|
50
53
|
return property;
|
|
51
54
|
}
|
|
52
55
|
function defineCollectionPropertyAction(BuiltClass, properties, property) {
|
|
@@ -114,13 +117,17 @@ function initClass(classType) {
|
|
|
114
117
|
return resultingBuiltClass;
|
|
115
118
|
}
|
|
116
119
|
function initLazyLoading() {
|
|
120
|
+
const already = new Map;
|
|
121
|
+
let defer;
|
|
117
122
|
const Module = require('module');
|
|
118
123
|
const superRequire = Module.prototype.require;
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
let
|
|
122
|
-
let replacements;
|
|
124
|
+
function initModule(module, original) {
|
|
125
|
+
defer = undefined;
|
|
126
|
+
let replacements = new Map;
|
|
123
127
|
for (const [name, type] of Object.entries(original)) {
|
|
128
|
+
defer = (defer === undefined)
|
|
129
|
+
? (type === undefined)
|
|
130
|
+
: (defer && (type === undefined));
|
|
124
131
|
if (!(0, class_type_1.isAnyType)(type))
|
|
125
132
|
continue;
|
|
126
133
|
if (module && replacements) {
|
|
@@ -133,25 +140,80 @@ function initLazyLoading() {
|
|
|
133
140
|
const withORM = initClass(type);
|
|
134
141
|
if (!withORM)
|
|
135
142
|
continue;
|
|
136
|
-
|
|
143
|
+
replacements.set(type, withORM);
|
|
144
|
+
if (!module) {
|
|
137
145
|
module = { ...original };
|
|
138
|
-
|
|
146
|
+
already.set(original, module);
|
|
139
147
|
}
|
|
140
|
-
replacements.set(type, withORM);
|
|
141
|
-
// @ts-ignore TS18048 but module is always initialized
|
|
142
148
|
module[name] = withORM;
|
|
143
149
|
}
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
150
|
+
return (defer || replacements.size) ? (module ?? { ...original }) : original;
|
|
151
|
+
}
|
|
152
|
+
function resolveDeferredActions() {
|
|
153
|
+
while (deferredActions.length) {
|
|
154
|
+
if (deferredActions[deferredActions.length - 1]())
|
|
155
|
+
deferredActions.pop();
|
|
156
|
+
else
|
|
157
|
+
break;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
function resolveDeferredModules() {
|
|
161
|
+
while (deferredModules.length) {
|
|
162
|
+
const [original, module, file] = deferredModules[deferredModules.length - 1];
|
|
163
|
+
console.log('? deferred', file);
|
|
164
|
+
if (initModule(module, original) === original) {
|
|
165
|
+
console.log('> no ORM on deferred', file);
|
|
166
|
+
Object.assign(module, original);
|
|
167
|
+
}
|
|
168
|
+
if (defer)
|
|
169
|
+
break;
|
|
170
|
+
else {
|
|
171
|
+
console.log('> resolved', file);
|
|
172
|
+
deferredModules.pop();
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
Module.prototype.require = function () {
|
|
177
|
+
const original = superRequire.call(this, ...arguments);
|
|
178
|
+
const alreadyModule = already.get(original);
|
|
179
|
+
if (alreadyModule) {
|
|
180
|
+
return alreadyModule;
|
|
181
|
+
}
|
|
182
|
+
already.set(original, original);
|
|
183
|
+
const module = initModule(alreadyModule, original);
|
|
184
|
+
if (defer) {
|
|
185
|
+
console.log('> defer', arguments[0]);
|
|
186
|
+
// { ...original } into module may be enough. \/ this allows resolving during accessing value, if it is late.
|
|
187
|
+
for (const [name, value] of Object.entries(original)) {
|
|
188
|
+
Object.defineProperty(module, name, {
|
|
189
|
+
configurable: true,
|
|
190
|
+
enumerable: true,
|
|
191
|
+
get() {
|
|
192
|
+
console.log('? resolve during getter');
|
|
193
|
+
if (original[name] !== undefined) {
|
|
194
|
+
resolveDeferredModules();
|
|
195
|
+
resolveDeferredActions();
|
|
196
|
+
console.log('! resolved during getter');
|
|
197
|
+
return module[name];
|
|
198
|
+
}
|
|
199
|
+
return value;
|
|
200
|
+
},
|
|
201
|
+
set(value) {
|
|
202
|
+
console.log(' set value for', name);
|
|
203
|
+
let descriptor = Object.getOwnPropertyDescriptor(original, name)
|
|
204
|
+
?? { configurable: true, enumerable: true };
|
|
205
|
+
delete descriptor.get;
|
|
206
|
+
delete descriptor.set;
|
|
207
|
+
descriptor.value = value;
|
|
208
|
+
Object.defineProperty(module, name, descriptor);
|
|
209
|
+
}
|
|
210
|
+
});
|
|
153
211
|
}
|
|
212
|
+
already.set(original, module);
|
|
213
|
+
deferredModules.push([original, module, arguments[0]]);
|
|
154
214
|
}
|
|
215
|
+
resolveDeferredModules();
|
|
216
|
+
resolveDeferredActions();
|
|
155
217
|
return module ?? original;
|
|
156
218
|
};
|
|
157
219
|
}
|
package/esm/lazy-loading.js
CHANGED
|
@@ -8,6 +8,7 @@ import { dataSource } from '@itrocks/storage';
|
|
|
8
8
|
import { storeOf } from '@itrocks/store';
|
|
9
9
|
export const PROTECT_GET = Symbol('protectGet');
|
|
10
10
|
const deferredActions = new Array;
|
|
11
|
+
const deferredModules = new Array;
|
|
11
12
|
function defineCollectionProperty(elementType, property, builtClass) {
|
|
12
13
|
const descriptor = {
|
|
13
14
|
configurable: true,
|
|
@@ -26,6 +27,7 @@ function defineCollectionProperty(elementType, property, builtClass) {
|
|
|
26
27
|
};
|
|
27
28
|
Object.defineProperty(builtClass.prototype, property, descriptor);
|
|
28
29
|
Reflect.defineMetadata(PROTECT_GET, true, builtClass.prototype, property);
|
|
30
|
+
console.log('defineCollectionProperty', new ReflectClass(builtClass).name + '.' + property);
|
|
29
31
|
return property;
|
|
30
32
|
}
|
|
31
33
|
function defineObjectProperty(type, property, builtClass) {
|
|
@@ -44,6 +46,7 @@ function defineObjectProperty(type, property, builtClass) {
|
|
|
44
46
|
};
|
|
45
47
|
Object.defineProperty(builtClass.prototype, property, descriptor);
|
|
46
48
|
Reflect.defineMetadata(PROTECT_GET, true, builtClass.prototype, property);
|
|
49
|
+
console.log('defineObjectProperty', new ReflectClass(builtClass).name + '.' + property);
|
|
47
50
|
return property;
|
|
48
51
|
}
|
|
49
52
|
function defineCollectionPropertyAction(BuiltClass, properties, property) {
|
|
@@ -111,13 +114,17 @@ export function initClass(classType) {
|
|
|
111
114
|
return resultingBuiltClass;
|
|
112
115
|
}
|
|
113
116
|
export function initLazyLoading() {
|
|
117
|
+
const already = new Map;
|
|
118
|
+
let defer;
|
|
114
119
|
const Module = require('module');
|
|
115
120
|
const superRequire = Module.prototype.require;
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
let
|
|
119
|
-
let replacements;
|
|
121
|
+
function initModule(module, original) {
|
|
122
|
+
defer = undefined;
|
|
123
|
+
let replacements = new Map;
|
|
120
124
|
for (const [name, type] of Object.entries(original)) {
|
|
125
|
+
defer = (defer === undefined)
|
|
126
|
+
? (type === undefined)
|
|
127
|
+
: (defer && (type === undefined));
|
|
121
128
|
if (!isAnyType(type))
|
|
122
129
|
continue;
|
|
123
130
|
if (module && replacements) {
|
|
@@ -130,25 +137,80 @@ export function initLazyLoading() {
|
|
|
130
137
|
const withORM = initClass(type);
|
|
131
138
|
if (!withORM)
|
|
132
139
|
continue;
|
|
133
|
-
|
|
140
|
+
replacements.set(type, withORM);
|
|
141
|
+
if (!module) {
|
|
134
142
|
module = { ...original };
|
|
135
|
-
|
|
143
|
+
already.set(original, module);
|
|
136
144
|
}
|
|
137
|
-
replacements.set(type, withORM);
|
|
138
|
-
// @ts-ignore TS18048 but module is always initialized
|
|
139
145
|
module[name] = withORM;
|
|
140
146
|
}
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
147
|
+
return (defer || replacements.size) ? (module ?? { ...original }) : original;
|
|
148
|
+
}
|
|
149
|
+
function resolveDeferredActions() {
|
|
150
|
+
while (deferredActions.length) {
|
|
151
|
+
if (deferredActions[deferredActions.length - 1]())
|
|
152
|
+
deferredActions.pop();
|
|
153
|
+
else
|
|
154
|
+
break;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
function resolveDeferredModules() {
|
|
158
|
+
while (deferredModules.length) {
|
|
159
|
+
const [original, module, file] = deferredModules[deferredModules.length - 1];
|
|
160
|
+
console.log('? deferred', file);
|
|
161
|
+
if (initModule(module, original) === original) {
|
|
162
|
+
console.log('> no ORM on deferred', file);
|
|
163
|
+
Object.assign(module, original);
|
|
164
|
+
}
|
|
165
|
+
if (defer)
|
|
166
|
+
break;
|
|
167
|
+
else {
|
|
168
|
+
console.log('> resolved', file);
|
|
169
|
+
deferredModules.pop();
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
Module.prototype.require = function () {
|
|
174
|
+
const original = superRequire.call(this, ...arguments);
|
|
175
|
+
const alreadyModule = already.get(original);
|
|
176
|
+
if (alreadyModule) {
|
|
177
|
+
return alreadyModule;
|
|
178
|
+
}
|
|
179
|
+
already.set(original, original);
|
|
180
|
+
const module = initModule(alreadyModule, original);
|
|
181
|
+
if (defer) {
|
|
182
|
+
console.log('> defer', arguments[0]);
|
|
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
|
+
console.log('? resolve during getter');
|
|
190
|
+
if (original[name] !== undefined) {
|
|
191
|
+
resolveDeferredModules();
|
|
192
|
+
resolveDeferredActions();
|
|
193
|
+
console.log('! resolved during getter');
|
|
194
|
+
return module[name];
|
|
195
|
+
}
|
|
196
|
+
return value;
|
|
197
|
+
},
|
|
198
|
+
set(value) {
|
|
199
|
+
console.log(' set value for', name);
|
|
200
|
+
let descriptor = Object.getOwnPropertyDescriptor(original, name)
|
|
201
|
+
?? { configurable: true, enumerable: true };
|
|
202
|
+
delete descriptor.get;
|
|
203
|
+
delete descriptor.set;
|
|
204
|
+
descriptor.value = value;
|
|
205
|
+
Object.defineProperty(module, name, descriptor);
|
|
206
|
+
}
|
|
207
|
+
});
|
|
150
208
|
}
|
|
209
|
+
already.set(original, module);
|
|
210
|
+
deferredModules.push([original, module, arguments[0]]);
|
|
151
211
|
}
|
|
212
|
+
resolveDeferredModules();
|
|
213
|
+
resolveDeferredActions();
|
|
152
214
|
return module ?? original;
|
|
153
215
|
};
|
|
154
216
|
}
|
package/package.json
CHANGED