@itrocks/lazy-loading 0.0.10 → 0.0.12

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.
@@ -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
- function defineCollectionProperty(type, property, builtClass) {
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.type;
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,25 +89,41 @@ 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
94
  const propertyType = property.type;
77
95
  if (!propertyType)
78
96
  continue;
79
- if ((propertyType instanceof property_type_1.CollectionType) && (0, class_type_1.isAnyType)(propertyType.elementType.type)) {
80
- properties.push(defineCollectionProperty(propertyType, property.name, BuiltClass));
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
+ }
105
+ }
106
+ else if (defineObjectPropertyAction(BuiltClass, properties, property)) {
107
+ resultingBuiltClass = BuiltClass;
81
108
  }
82
- else if ((0, class_type_1.isAnyType)(propertyType?.type)) {
83
- properties.push(defineObjectProperty(propertyType.type, property.name, BuiltClass));
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 properties.length ? BuiltClass : undefined;
114
+ return resultingBuiltClass;
87
115
  }
88
116
  function initLazyLoading() {
117
+ const already = new Map;
89
118
  const Module = require('module');
90
119
  const superRequire = Module.prototype.require;
91
120
  Module.prototype.require = function () {
92
121
  const original = superRequire.call(this, ...arguments);
93
- let module;
122
+ let module = already.get(original);
123
+ if (module) {
124
+ return module;
125
+ }
126
+ already.set(original, original);
94
127
  let replacements;
95
128
  for (const [name, type] of Object.entries(original)) {
96
129
  if (!(0, class_type_1.isAnyType)(type))
@@ -108,11 +141,18 @@ function initLazyLoading() {
108
141
  if (!replacements) {
109
142
  module = { ...original };
110
143
  replacements = new Map();
144
+ already.set(original, module);
111
145
  }
112
146
  replacements.set(type, withORM);
113
147
  // @ts-ignore TS18048 but module is always initialized
114
148
  module[name] = withORM;
115
149
  }
150
+ while (deferredActions.length) {
151
+ if (deferredActions[deferredActions.length - 1]())
152
+ deferredActions.pop();
153
+ else
154
+ break;
155
+ }
116
156
  return module ?? original;
117
157
  };
118
158
  }
@@ -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
- function defineCollectionProperty(type, property, builtClass) {
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.type;
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,25 +86,41 @@ 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
91
  const propertyType = property.type;
74
92
  if (!propertyType)
75
93
  continue;
76
- if ((propertyType instanceof CollectionType) && isAnyType(propertyType.elementType.type)) {
77
- properties.push(defineCollectionProperty(propertyType, property.name, BuiltClass));
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
+ }
102
+ }
103
+ else if (defineObjectPropertyAction(BuiltClass, properties, property)) {
104
+ resultingBuiltClass = BuiltClass;
78
105
  }
79
- else if (isAnyType(propertyType?.type)) {
80
- properties.push(defineObjectProperty(propertyType.type, property.name, BuiltClass));
106
+ else if (propertyType?.type instanceof DeferredType) {
107
+ resultingBuiltClass = BuiltClass;
108
+ deferredActions.push(() => defineObjectPropertyAction(BuiltClass, properties, property));
81
109
  }
82
110
  }
83
- return properties.length ? BuiltClass : undefined;
111
+ return resultingBuiltClass;
84
112
  }
85
113
  export function initLazyLoading() {
114
+ const already = new Map;
86
115
  const Module = require('module');
87
116
  const superRequire = Module.prototype.require;
88
117
  Module.prototype.require = function () {
89
118
  const original = superRequire.call(this, ...arguments);
90
- let module;
119
+ let module = already.get(original);
120
+ if (module) {
121
+ return module;
122
+ }
123
+ already.set(original, original);
91
124
  let replacements;
92
125
  for (const [name, type] of Object.entries(original)) {
93
126
  if (!isAnyType(type))
@@ -105,11 +138,18 @@ export function initLazyLoading() {
105
138
  if (!replacements) {
106
139
  module = { ...original };
107
140
  replacements = new Map();
141
+ already.set(original, module);
108
142
  }
109
143
  replacements.set(type, withORM);
110
144
  // @ts-ignore TS18048 but module is always initialized
111
145
  module[name] = withORM;
112
146
  }
147
+ while (deferredActions.length) {
148
+ if (deferredActions[deferredActions.length - 1]())
149
+ deferredActions.pop();
150
+ else
151
+ break;
152
+ }
113
153
  return module ?? original;
114
154
  };
115
155
  }
package/package.json CHANGED
@@ -54,5 +54,5 @@
54
54
  "build:esm": "tsc -p tsconfig.esm.json && node esm/esm"
55
55
  },
56
56
  "types": "./esm/lazy-loading.d.ts",
57
- "version": "0.0.10"
57
+ "version": "0.0.12"
58
58
  }