@itrocks/lazy-loading 0.0.17 → 0.2.0
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 +17 -11
- package/esm/lazy-loading.js +17 -11
- package/package.json +1 -1
package/cjs/lazy-loading.js
CHANGED
|
@@ -4,6 +4,7 @@ exports.PROTECT_GET = void 0;
|
|
|
4
4
|
exports.initClass = initClass;
|
|
5
5
|
exports.initLazyLoading = initLazyLoading;
|
|
6
6
|
const class_type_1 = require("@itrocks/class-type");
|
|
7
|
+
const property_1 = require("@itrocks/decorator/property");
|
|
7
8
|
const property_type_1 = require("@itrocks/property-type");
|
|
8
9
|
const property_type_2 = require("@itrocks/property-type");
|
|
9
10
|
const reflect_1 = require("@itrocks/reflect");
|
|
@@ -13,41 +14,45 @@ exports.PROTECT_GET = Symbol('protectGet');
|
|
|
13
14
|
const deferredActions = new Array;
|
|
14
15
|
const deferredModules = new Array;
|
|
15
16
|
function defineCollectionProperty(elementType, property, builtClass) {
|
|
17
|
+
const metadataName = (0, property_1.metadataNameOf)(property);
|
|
18
|
+
const propertyIds = property.toString() + 'Ids';
|
|
16
19
|
const descriptor = {
|
|
17
20
|
configurable: true,
|
|
18
21
|
enumerable: true,
|
|
19
22
|
async get() {
|
|
20
|
-
const ids = this[
|
|
23
|
+
const ids = this[propertyIds];
|
|
21
24
|
return this[property] = ids
|
|
22
25
|
? await (0, storage_1.dataSource)().readMultiple(elementType, ids)
|
|
23
26
|
: await (0, storage_1.dataSource)().readCollection(this, property, elementType);
|
|
24
27
|
},
|
|
25
28
|
set(value) {
|
|
26
|
-
delete this[
|
|
29
|
+
delete this[propertyIds];
|
|
27
30
|
Object.defineProperty(this, property, { configurable: true, enumerable: true, value, writable: true });
|
|
28
|
-
Reflect.defineMetadata(exports.PROTECT_GET, false, this,
|
|
31
|
+
Reflect.defineMetadata(exports.PROTECT_GET, false, this, metadataName);
|
|
29
32
|
}
|
|
30
33
|
};
|
|
31
34
|
Object.defineProperty(builtClass.prototype, property, descriptor);
|
|
32
|
-
Reflect.defineMetadata(exports.PROTECT_GET, true, builtClass.prototype,
|
|
35
|
+
Reflect.defineMetadata(exports.PROTECT_GET, true, builtClass.prototype, metadataName);
|
|
33
36
|
return property;
|
|
34
37
|
}
|
|
35
38
|
function defineObjectProperty(type, property, builtClass) {
|
|
39
|
+
const metadataName = (0, property_1.metadataNameOf)(property);
|
|
40
|
+
const propertyId = property.toString() + 'Id';
|
|
36
41
|
const descriptor = {
|
|
37
42
|
configurable: true,
|
|
38
43
|
enumerable: true,
|
|
39
44
|
async get() {
|
|
40
|
-
const id = this[
|
|
45
|
+
const id = this[propertyId];
|
|
41
46
|
return this[property] = id ? await (0, storage_1.dataSource)().read(type, id) : undefined;
|
|
42
47
|
},
|
|
43
48
|
set(value) {
|
|
44
|
-
delete this[
|
|
49
|
+
delete this[propertyId];
|
|
45
50
|
Object.defineProperty(this, property, { configurable: true, enumerable: true, value, writable: true });
|
|
46
|
-
Reflect.defineMetadata(exports.PROTECT_GET, false, this,
|
|
51
|
+
Reflect.defineMetadata(exports.PROTECT_GET, false, this, metadataName);
|
|
47
52
|
}
|
|
48
53
|
};
|
|
49
54
|
Object.defineProperty(builtClass.prototype, property, descriptor);
|
|
50
|
-
Reflect.defineMetadata(exports.PROTECT_GET, true, builtClass.prototype,
|
|
55
|
+
Reflect.defineMetadata(exports.PROTECT_GET, true, builtClass.prototype, metadataName);
|
|
51
56
|
return property;
|
|
52
57
|
}
|
|
53
58
|
function defineCollectionPropertyAction(BuiltClass, properties, property) {
|
|
@@ -76,20 +81,21 @@ function initClass(classType) {
|
|
|
76
81
|
}
|
|
77
82
|
const properties = [];
|
|
78
83
|
// @ts-ignore TS2415 classType is always a heritable class, not a function.
|
|
79
|
-
const BuiltClass =
|
|
84
|
+
const BuiltClass = class extends classType {
|
|
80
85
|
constructor(...args) {
|
|
81
86
|
super(...args);
|
|
82
87
|
for (const property of properties) {
|
|
83
88
|
const value = Object.getOwnPropertyDescriptor(this, property)?.value;
|
|
84
89
|
if ((value === undefined) || (Array.isArray(value) && !value.length)) {
|
|
90
|
+
// @ts-ignore TS2536 Type 'keyof T' cannot be used to index type 'this'.
|
|
85
91
|
delete this[property];
|
|
86
92
|
}
|
|
87
93
|
else {
|
|
88
|
-
Reflect.defineMetadata(exports.PROTECT_GET, false, this, property);
|
|
94
|
+
Reflect.defineMetadata(exports.PROTECT_GET, false, this, (0, property_1.metadataNameOf)(property));
|
|
89
95
|
}
|
|
90
96
|
}
|
|
91
97
|
}
|
|
92
|
-
}
|
|
98
|
+
};
|
|
93
99
|
let resultingBuiltClass = undefined;
|
|
94
100
|
for (const property of new reflect_1.ReflectClass(classType).properties) {
|
|
95
101
|
const propertyType = property.type;
|
package/esm/lazy-loading.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { createRequire } from 'module'
|
|
2
2
|
const require = createRequire(import.meta.url)
|
|
3
3
|
import { isAnyType } from '@itrocks/class-type';
|
|
4
|
+
import { metadataNameOf } from '@itrocks/decorator/property';
|
|
4
5
|
import { CollectionType } from '@itrocks/property-type';
|
|
5
6
|
import { DeferredType } from '@itrocks/property-type';
|
|
6
7
|
import { ReflectClass } from '@itrocks/reflect';
|
|
@@ -10,41 +11,45 @@ export const PROTECT_GET = Symbol('protectGet');
|
|
|
10
11
|
const deferredActions = new Array;
|
|
11
12
|
const deferredModules = new Array;
|
|
12
13
|
function defineCollectionProperty(elementType, property, builtClass) {
|
|
14
|
+
const metadataName = metadataNameOf(property);
|
|
15
|
+
const propertyIds = property.toString() + 'Ids';
|
|
13
16
|
const descriptor = {
|
|
14
17
|
configurable: true,
|
|
15
18
|
enumerable: true,
|
|
16
19
|
async get() {
|
|
17
|
-
const ids = this[
|
|
20
|
+
const ids = this[propertyIds];
|
|
18
21
|
return this[property] = ids
|
|
19
22
|
? await dataSource().readMultiple(elementType, ids)
|
|
20
23
|
: await dataSource().readCollection(this, property, elementType);
|
|
21
24
|
},
|
|
22
25
|
set(value) {
|
|
23
|
-
delete this[
|
|
26
|
+
delete this[propertyIds];
|
|
24
27
|
Object.defineProperty(this, property, { configurable: true, enumerable: true, value, writable: true });
|
|
25
|
-
Reflect.defineMetadata(PROTECT_GET, false, this,
|
|
28
|
+
Reflect.defineMetadata(PROTECT_GET, false, this, metadataName);
|
|
26
29
|
}
|
|
27
30
|
};
|
|
28
31
|
Object.defineProperty(builtClass.prototype, property, descriptor);
|
|
29
|
-
Reflect.defineMetadata(PROTECT_GET, true, builtClass.prototype,
|
|
32
|
+
Reflect.defineMetadata(PROTECT_GET, true, builtClass.prototype, metadataName);
|
|
30
33
|
return property;
|
|
31
34
|
}
|
|
32
35
|
function defineObjectProperty(type, property, builtClass) {
|
|
36
|
+
const metadataName = metadataNameOf(property);
|
|
37
|
+
const propertyId = property.toString() + 'Id';
|
|
33
38
|
const descriptor = {
|
|
34
39
|
configurable: true,
|
|
35
40
|
enumerable: true,
|
|
36
41
|
async get() {
|
|
37
|
-
const id = this[
|
|
42
|
+
const id = this[propertyId];
|
|
38
43
|
return this[property] = id ? await dataSource().read(type, id) : undefined;
|
|
39
44
|
},
|
|
40
45
|
set(value) {
|
|
41
|
-
delete this[
|
|
46
|
+
delete this[propertyId];
|
|
42
47
|
Object.defineProperty(this, property, { configurable: true, enumerable: true, value, writable: true });
|
|
43
|
-
Reflect.defineMetadata(PROTECT_GET, false, this,
|
|
48
|
+
Reflect.defineMetadata(PROTECT_GET, false, this, metadataName);
|
|
44
49
|
}
|
|
45
50
|
};
|
|
46
51
|
Object.defineProperty(builtClass.prototype, property, descriptor);
|
|
47
|
-
Reflect.defineMetadata(PROTECT_GET, true, builtClass.prototype,
|
|
52
|
+
Reflect.defineMetadata(PROTECT_GET, true, builtClass.prototype, metadataName);
|
|
48
53
|
return property;
|
|
49
54
|
}
|
|
50
55
|
function defineCollectionPropertyAction(BuiltClass, properties, property) {
|
|
@@ -73,20 +78,21 @@ export function initClass(classType) {
|
|
|
73
78
|
}
|
|
74
79
|
const properties = [];
|
|
75
80
|
// @ts-ignore TS2415 classType is always a heritable class, not a function.
|
|
76
|
-
const BuiltClass =
|
|
81
|
+
const BuiltClass = class extends classType {
|
|
77
82
|
constructor(...args) {
|
|
78
83
|
super(...args);
|
|
79
84
|
for (const property of properties) {
|
|
80
85
|
const value = Object.getOwnPropertyDescriptor(this, property)?.value;
|
|
81
86
|
if ((value === undefined) || (Array.isArray(value) && !value.length)) {
|
|
87
|
+
// @ts-ignore TS2536 Type 'keyof T' cannot be used to index type 'this'.
|
|
82
88
|
delete this[property];
|
|
83
89
|
}
|
|
84
90
|
else {
|
|
85
|
-
Reflect.defineMetadata(PROTECT_GET, false, this, property);
|
|
91
|
+
Reflect.defineMetadata(PROTECT_GET, false, this, metadataNameOf(property));
|
|
86
92
|
}
|
|
87
93
|
}
|
|
88
94
|
}
|
|
89
|
-
}
|
|
95
|
+
};
|
|
90
96
|
let resultingBuiltClass = undefined;
|
|
91
97
|
for (const property of new ReflectClass(classType).properties) {
|
|
92
98
|
const propertyType = property.type;
|
package/package.json
CHANGED