mnemonica 1.2.0 → 1.2.1
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/.ai/ONBOARDING.md +1 -1
- package/.ai/PROTOTYPE-CHAIN.md +12 -2
- package/.ai/rules-async-constructors.md +81 -1
- package/.ai/rules-define-patterns.md +39 -0
- package/AGENTS.md +14 -1
- package/CONTRIBUTING.md +4 -18
- package/FOR_HUMANS.md +103 -11
- package/README.md +42 -7
- package/SKILL.md +7 -2
- package/build/api/errors/index.js +7 -5
- package/build/api/index.d.ts +1 -0
- package/build/api/index.js +2 -1
- package/build/api/types/InstanceCreator.js +16 -5
- package/build/api/types/Mnemosyne.d.ts +3 -14
- package/build/api/types/Mnemosyne.js +21 -8
- package/build/api/types/Props.js +1 -2
- package/build/api/types/TypeProxy.d.ts +1 -1
- package/build/api/types/TypeProxy.js +9 -9
- package/build/api/types/compileNewModificatorFunctionBody.js +3 -5
- package/build/api/types/index.d.ts +5 -0
- package/build/api/types/index.js +70 -31
- package/build/api/utils/index.d.ts +1 -0
- package/build/api/utils/index.js +13 -3
- package/build/descriptors/types/index.js +46 -2
- package/build/index.d.ts +6 -2
- package/build/index.js +18 -3
- package/build/types/index.d.ts +22 -4
- package/build/types/index.js +1 -1
- package/build/utils/parent.js +35 -5
- package/docs/async-constructors.md +1 -1
- package/docs/empathy-in-ai.md +2 -3
- package/docs/prototype-chain.md +4 -0
- package/docs/typed-lookup.md +19 -2
- package/package.json +8 -8
- package/src/api/errors/index.ts +10 -4
- package/src/api/index.ts +2 -8
- package/src/api/types/InstanceCreator.ts +28 -6
- package/src/api/types/Mnemosyne.ts +34 -15
- package/src/api/types/Props.ts +4 -4
- package/src/api/types/TypeProxy.ts +10 -9
- package/src/api/types/compileNewModificatorFunctionBody.ts +12 -4
- package/src/api/types/index.ts +122 -65
- package/src/api/utils/index.ts +27 -9
- package/src/descriptors/types/index.ts +81 -3
- package/src/index.ts +66 -0
- package/src/types/index.ts +105 -10
- package/src/utils/parent.ts +48 -4
package/src/api/types/Props.ts
CHANGED
|
@@ -185,6 +185,10 @@ export const _getProps = (instance: object, base?: object): PropsType | undefine
|
|
|
185
185
|
export const _setSelf = (instance: object): void => {
|
|
186
186
|
// const props = __props__.get(instance);
|
|
187
187
|
const props = _getProps(instance);
|
|
188
|
+
// __self__ is installed here, not in _addProps, because it is the
|
|
189
|
+
// only prop whose value is the instance itself — which does not
|
|
190
|
+
// exist yet when _addProps runs. It serves as the async completion
|
|
191
|
+
// marker (makeAwaiter) and the self-call detector (fork).
|
|
188
192
|
Object.defineProperty(
|
|
189
193
|
props,
|
|
190
194
|
'__self__',
|
|
@@ -194,10 +198,6 @@ export const _setSelf = (instance: object): void => {
|
|
|
194
198
|
}
|
|
195
199
|
}
|
|
196
200
|
);
|
|
197
|
-
__props__.set(
|
|
198
|
-
instance,
|
|
199
|
-
props
|
|
200
|
-
);
|
|
201
201
|
};
|
|
202
202
|
|
|
203
203
|
export const getProps = (instance: object): PropsType | undefined => {
|
|
@@ -40,17 +40,18 @@ interface TypeProxyConstructHandler {
|
|
|
40
40
|
// Type for TypeProxy instance — data + traps
|
|
41
41
|
export interface TypeProxyInstance extends TypeProxyGetHandler, TypeProxySetHandler, TypeProxyConstructHandler {
|
|
42
42
|
__type__: TypeProxyType;
|
|
43
|
-
|
|
43
|
+
// the ancestor new instances descend from (root proto or apply-trap entity)
|
|
44
|
+
ancestor: unknown;
|
|
44
45
|
apply: typeof subTypeApply;
|
|
45
46
|
new (...args: unknown[]): object;
|
|
46
47
|
}
|
|
47
48
|
|
|
48
|
-
export const TypeProxy = function (__type__: TypeProxyType,
|
|
49
|
+
export const TypeProxy = function (__type__: TypeProxyType, ancestor: unknown) {
|
|
49
50
|
Object.assign(
|
|
50
51
|
this,
|
|
51
52
|
{
|
|
52
53
|
__type__,
|
|
53
|
-
|
|
54
|
+
ancestor
|
|
54
55
|
}
|
|
55
56
|
);
|
|
56
57
|
const typeProxy = new Proxy(
|
|
@@ -150,12 +151,12 @@ const primaryTypeApply = function (
|
|
|
150
151
|
this: TypeProxyInstance,
|
|
151
152
|
// proxy target
|
|
152
153
|
__: unknown,
|
|
153
|
-
|
|
154
|
+
thisArg: unknown,
|
|
154
155
|
args: [constructorOptions, ...unknown[]],
|
|
155
156
|
) {
|
|
156
157
|
const type = this.__type__;
|
|
157
158
|
// case of decorator like usage
|
|
158
|
-
if (
|
|
159
|
+
if (thisArg === undefined) {
|
|
159
160
|
const decorator = subTypeApply(
|
|
160
161
|
type,
|
|
161
162
|
args[ 0 ]
|
|
@@ -167,7 +168,7 @@ const primaryTypeApply = function (
|
|
|
167
168
|
// our PrimaryType whick is === instance of current TypeProxy
|
|
168
169
|
const InstanceCreatorProxy = new TypeProxy(
|
|
169
170
|
type,
|
|
170
|
-
|
|
171
|
+
thisArg
|
|
171
172
|
);
|
|
172
173
|
const instance = new InstanceCreatorProxy(...args);
|
|
173
174
|
return instance;
|
|
@@ -191,15 +192,15 @@ TypeProxy.prototype.construct = function (this: TypeProxyInstance, _target: unkn
|
|
|
191
192
|
|
|
192
193
|
const {
|
|
193
194
|
__type__: type,
|
|
194
|
-
|
|
195
|
+
ancestor
|
|
195
196
|
} = this;
|
|
196
197
|
|
|
197
198
|
// so this is a direct Sub-Type invocation
|
|
198
199
|
// having no existentInstance created earlier
|
|
199
200
|
// then we should rely on that somehow
|
|
200
|
-
const
|
|
201
|
+
const parentProto = type.isSubType ? getDefaultPrototype() : ancestor;
|
|
201
202
|
|
|
202
|
-
const mnemosyneProxy = createMnemosyne(
|
|
203
|
+
const mnemosyneProxy = createMnemosyne(parentProto);
|
|
203
204
|
const instance = new InstanceCreator(
|
|
204
205
|
type,
|
|
205
206
|
mnemosyneProxy,
|
|
@@ -92,12 +92,20 @@ const getFunctionConstructor = (
|
|
|
92
92
|
...args
|
|
93
93
|
);
|
|
94
94
|
} else {
|
|
95
|
-
|
|
95
|
+
// historical prototype swap kept for reference:
|
|
96
|
+
// const _proto = ConstructHandler.prototype;
|
|
96
97
|
// !!! it MUST be strict replacement !!!
|
|
97
98
|
// !!! this is the only way to keep Prototype Chain correct !!!
|
|
98
|
-
ConstructHandler.prototype = this.constructor.prototype;
|
|
99
|
-
answer = new (ConstructHandler as unknown as new (...args: unknown[]) => object)( ...args );
|
|
100
|
-
ConstructHandler.prototype = _proto;
|
|
99
|
+
// ConstructHandler.prototype = this.constructor.prototype;
|
|
100
|
+
// answer = new (ConstructHandler as unknown as new (...args: unknown[]) => object)( ...args );
|
|
101
|
+
// ConstructHandler.prototype = _proto;
|
|
102
|
+
|
|
103
|
+
const constructResult = Reflect.construct(
|
|
104
|
+
ConstructHandler,
|
|
105
|
+
args,
|
|
106
|
+
this.constructor
|
|
107
|
+
);
|
|
108
|
+
answer = constructResult;
|
|
101
109
|
}
|
|
102
110
|
const result = CreationHandler.call(
|
|
103
111
|
this,
|
package/src/api/types/index.ts
CHANGED
|
@@ -35,7 +35,7 @@ import {
|
|
|
35
35
|
|
|
36
36
|
// Lazy getter: a function that, when called, returns the actual constructor function.
|
|
37
37
|
// Used when define() receives an anonymous function wrapping the real constructor.
|
|
38
|
-
interface LazyTypeGetter extends CallableFunction {
|
|
38
|
+
export interface LazyTypeGetter extends CallableFunction {
|
|
39
39
|
(): ConstructHandler;
|
|
40
40
|
}
|
|
41
41
|
|
|
@@ -76,7 +76,7 @@ import compileNewModificatorFunctionBody, { ConstructHandler } from './compileNe
|
|
|
76
76
|
|
|
77
77
|
import TypesUtils, { CreationHandler } from '../utils';
|
|
78
78
|
const {
|
|
79
|
-
|
|
79
|
+
getCachedTypeChecker,
|
|
80
80
|
getTypeSplitPath,
|
|
81
81
|
checkTypeName,
|
|
82
82
|
isClass,
|
|
@@ -149,7 +149,22 @@ const TypeDescriptor = function (
|
|
|
149
149
|
|
|
150
150
|
config,
|
|
151
151
|
|
|
152
|
-
hooks : Object.create(null)
|
|
152
|
+
hooks : Object.create(null),
|
|
153
|
+
|
|
154
|
+
decorate : function (options?: object) {
|
|
155
|
+
const self = type;
|
|
156
|
+
const decorator = function (cstr: CallableFunction) {
|
|
157
|
+
const { name } = cstr;
|
|
158
|
+
const defineResult = self.define(
|
|
159
|
+
name,
|
|
160
|
+
cstr,
|
|
161
|
+
options
|
|
162
|
+
);
|
|
163
|
+
const decoratedResult = defineResult as unknown as CallableFunction;
|
|
164
|
+
return decoratedResult;
|
|
165
|
+
};
|
|
166
|
+
return decorator;
|
|
167
|
+
},
|
|
153
168
|
|
|
154
169
|
}
|
|
155
170
|
);
|
|
@@ -170,13 +185,13 @@ const TypeDescriptor = function (
|
|
|
170
185
|
}
|
|
171
186
|
);
|
|
172
187
|
|
|
173
|
-
// const
|
|
174
|
-
const
|
|
188
|
+
// const ancestor = isSubType ? Object.create(null) : proto;
|
|
189
|
+
const ancestor = isSubType ? undefined : proto;
|
|
175
190
|
types.set(
|
|
176
191
|
TypeName,
|
|
177
192
|
new TypeProxy(
|
|
178
193
|
type,
|
|
179
|
-
|
|
194
|
+
ancestor
|
|
180
195
|
)
|
|
181
196
|
);
|
|
182
197
|
|
|
@@ -208,6 +223,43 @@ TypeDescriptor.prototype.define = function (
|
|
|
208
223
|
return result;
|
|
209
224
|
};
|
|
210
225
|
|
|
226
|
+
TypeDescriptor.prototype.lazy = function (
|
|
227
|
+
this: TypeDescriptorInstance,
|
|
228
|
+
arg1: string | CallableFunction,
|
|
229
|
+
arg2?: CallableFunction | object,
|
|
230
|
+
arg3?: object
|
|
231
|
+
) {
|
|
232
|
+
let name: string | undefined;
|
|
233
|
+
let getter: LazyTypeGetter;
|
|
234
|
+
let config: object | undefined;
|
|
235
|
+
if (typeof arg1 === 'string') {
|
|
236
|
+
name = arg1;
|
|
237
|
+
getter = arg2 as LazyTypeGetter;
|
|
238
|
+
config = arg3;
|
|
239
|
+
} else {
|
|
240
|
+
getter = arg1 as LazyTypeGetter;
|
|
241
|
+
config = arg2 as object;
|
|
242
|
+
}
|
|
243
|
+
let result: TypeClass;
|
|
244
|
+
if (name) {
|
|
245
|
+
result = lazy.call(
|
|
246
|
+
this,
|
|
247
|
+
this.subtypes as TypesMap,
|
|
248
|
+
name,
|
|
249
|
+
getter as LazyTypeGetter,
|
|
250
|
+
config
|
|
251
|
+
);
|
|
252
|
+
} else {
|
|
253
|
+
result = lazy.call(
|
|
254
|
+
this,
|
|
255
|
+
this.subtypes as TypesMap,
|
|
256
|
+
getter as LazyTypeGetter,
|
|
257
|
+
config
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
return result;
|
|
261
|
+
};
|
|
262
|
+
|
|
211
263
|
TypeDescriptor.prototype.lookup = function (
|
|
212
264
|
this: TypeDescriptorInstance,
|
|
213
265
|
TypeNestedPath: string
|
|
@@ -233,7 +285,7 @@ odp(
|
|
|
233
285
|
TypeDescriptor.prototype,
|
|
234
286
|
Symbol.hasInstance, {
|
|
235
287
|
get (this: TypeDescriptorInstance) {
|
|
236
|
-
const result =
|
|
288
|
+
const result = getCachedTypeChecker(this.TypeName);
|
|
237
289
|
return result;
|
|
238
290
|
}
|
|
239
291
|
}
|
|
@@ -297,27 +349,6 @@ const checkDuplicate = function (target: TypesMap, name: string): void {
|
|
|
297
349
|
}
|
|
298
350
|
};
|
|
299
351
|
|
|
300
|
-
// ============================================================
|
|
301
|
-
// LAZY GETTER DETECTION
|
|
302
|
-
// A lazy getter is a function that, when called, returns a
|
|
303
|
-
// named constructor. Used for late-bound type definitions.
|
|
304
|
-
// If the call throws or returns a non-named function, it is
|
|
305
|
-
// treated as a direct handler instead.
|
|
306
|
-
// ============================================================
|
|
307
|
-
|
|
308
|
-
const isLazyGetter = function (handler: ConstructHandler | LazyTypeGetter): boolean {
|
|
309
|
-
try {
|
|
310
|
-
// Probe: try to call as a lazy getter. If it succeeds and returns a named
|
|
311
|
-
// function, it's a lazy getter. Otherwise (throws or returns non-function),
|
|
312
|
-
// it's a direct handler. The cast is the probe — runtime decides which it is.
|
|
313
|
-
const lazyResult = (handler as LazyTypeGetter)();
|
|
314
|
-
const result = lazyResult instanceof Function && lazyResult.name.length > 0;
|
|
315
|
-
return result;
|
|
316
|
-
} catch {
|
|
317
|
-
return false;
|
|
318
|
-
}
|
|
319
|
-
};
|
|
320
|
-
|
|
321
352
|
// ============================================================
|
|
322
353
|
// CONSTRUCTION STRATEGIES
|
|
323
354
|
// Two branches, same return type, no recursion.
|
|
@@ -374,25 +405,40 @@ const createFromDirectHandler = function (
|
|
|
374
405
|
const createFromLazyGetter = function (
|
|
375
406
|
defineOrigin: TypeAbsorber,
|
|
376
407
|
target: TypesMap,
|
|
377
|
-
name: string,
|
|
408
|
+
name: string | undefined,
|
|
378
409
|
getter: LazyTypeGetter,
|
|
379
410
|
config: constructorOptions
|
|
380
411
|
) {
|
|
412
|
+
|
|
381
413
|
const type = getter();
|
|
382
414
|
|
|
383
415
|
if (typeof type !== 'function') {
|
|
384
416
|
throw new HANDLER_MUST_BE_A_FUNCTION;
|
|
385
417
|
}
|
|
386
418
|
|
|
387
|
-
const TypeName =
|
|
419
|
+
const TypeName = name || type.name;
|
|
388
420
|
if (!TypeName) {
|
|
389
421
|
throw new TYPENAME_MUST_BE_A_STRING;
|
|
390
422
|
}
|
|
391
423
|
|
|
424
|
+
checkDuplicate(
|
|
425
|
+
target,
|
|
426
|
+
TypeName
|
|
427
|
+
);
|
|
428
|
+
|
|
392
429
|
const asClass = isClass(type);
|
|
393
430
|
|
|
431
|
+
const proto = (
|
|
432
|
+
hop(
|
|
433
|
+
type,
|
|
434
|
+
'prototype'
|
|
435
|
+
) &&
|
|
436
|
+
(typeof type.prototype === 'object')
|
|
437
|
+
) ? type.prototype : getDefaultPrototype();
|
|
438
|
+
|
|
394
439
|
const makeConstructHandler = () => {
|
|
395
440
|
const constructHandler = getter();
|
|
441
|
+
|
|
396
442
|
odp(
|
|
397
443
|
constructHandler,
|
|
398
444
|
SymbolConstructorName, {
|
|
@@ -411,7 +457,8 @@ const createFromLazyGetter = function (
|
|
|
411
457
|
constructHandler.prototype = getDefaultPrototype();
|
|
412
458
|
}
|
|
413
459
|
|
|
414
|
-
|
|
460
|
+
const handlerResult = constructHandler as MnemonicaConstructorFactory;
|
|
461
|
+
return handlerResult;
|
|
415
462
|
};
|
|
416
463
|
|
|
417
464
|
config = Object.assign(
|
|
@@ -426,7 +473,7 @@ const createFromLazyGetter = function (
|
|
|
426
473
|
target,
|
|
427
474
|
TypeName,
|
|
428
475
|
makeConstructHandler,
|
|
429
|
-
|
|
476
|
+
proto,
|
|
430
477
|
config
|
|
431
478
|
) as unknown as TypeClass;
|
|
432
479
|
return result;
|
|
@@ -468,18 +515,9 @@ export const define = function (
|
|
|
468
515
|
);
|
|
469
516
|
return defineResult;
|
|
470
517
|
}
|
|
471
|
-
// Anonymous function
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
: config as constructorOptions;
|
|
475
|
-
const lazyResult = createFromLazyGetter(
|
|
476
|
-
this as TypeAbsorber,
|
|
477
|
-
subtypes,
|
|
478
|
-
'',
|
|
479
|
-
fn as LazyTypeGetter,
|
|
480
|
-
lazyConfig
|
|
481
|
-
);
|
|
482
|
-
return lazyResult;
|
|
518
|
+
// Anonymous function as first arg is not a valid .define() form.
|
|
519
|
+
// Use .lazy(() => Constructor, config?) for lazy getters instead.
|
|
520
|
+
throw new TYPENAME_MUST_BE_A_STRING;
|
|
483
521
|
}
|
|
484
522
|
|
|
485
523
|
// --- Branch: string path passed as first arg ---
|
|
@@ -488,9 +526,9 @@ export const define = function (
|
|
|
488
526
|
}
|
|
489
527
|
checkTypeName(TypeOrTypeName);
|
|
490
528
|
|
|
491
|
-
let handler: ConstructHandler |
|
|
529
|
+
let handler: ConstructHandler | undefined;
|
|
492
530
|
if (typeof constructHandlerOrConfig === 'function') {
|
|
493
|
-
handler = constructHandlerOrConfig as ConstructHandler
|
|
531
|
+
handler = constructHandlerOrConfig as ConstructHandler;
|
|
494
532
|
} else if (typeof constructHandlerOrConfig === 'object') {
|
|
495
533
|
config = constructHandlerOrConfig as constructorOptions;
|
|
496
534
|
}
|
|
@@ -507,25 +545,6 @@ export const define = function (
|
|
|
507
545
|
throw new WRONG_TYPE_DEFINITION('definition is not provided');
|
|
508
546
|
}
|
|
509
547
|
|
|
510
|
-
// If the path resolved to an existing type and the handler
|
|
511
|
-
// is a lazy getter, define the getter's returned constructor
|
|
512
|
-
// as a subtype of the existing type.
|
|
513
|
-
if (parent && handler && isLazyGetter(handler)) {
|
|
514
|
-
const lazyType = (handler as LazyTypeGetter)();
|
|
515
|
-
checkDuplicate(
|
|
516
|
-
parent.subtypes as TypesMap,
|
|
517
|
-
lazyType.name
|
|
518
|
-
);
|
|
519
|
-
const lazyParentResult = createFromLazyGetter(
|
|
520
|
-
this as TypeAbsorber,
|
|
521
|
-
parent.subtypes as TypesMap,
|
|
522
|
-
lazyType.name,
|
|
523
|
-
handler as LazyTypeGetter,
|
|
524
|
-
config as constructorOptions
|
|
525
|
-
);
|
|
526
|
-
return lazyParentResult;
|
|
527
|
-
}
|
|
528
|
-
|
|
529
548
|
const result = createFromDirectHandler(
|
|
530
549
|
this as TypeAbsorber,
|
|
531
550
|
target,
|
|
@@ -536,6 +555,44 @@ lazyType.name
|
|
|
536
555
|
return result;
|
|
537
556
|
};
|
|
538
557
|
|
|
558
|
+
export const lazy = function (
|
|
559
|
+
this: unknown,
|
|
560
|
+
subtypes: TypesMap,
|
|
561
|
+
arg1: string | LazyTypeGetter | undefined,
|
|
562
|
+
arg2?: LazyTypeGetter | object,
|
|
563
|
+
arg3?: object
|
|
564
|
+
): TypeClass {
|
|
565
|
+
|
|
566
|
+
let name: string | undefined;
|
|
567
|
+
let getter: LazyTypeGetter;
|
|
568
|
+
let config: object | undefined;
|
|
569
|
+
if (typeof arg1 === 'string') {
|
|
570
|
+
name = arg1;
|
|
571
|
+
getter = arg2 as LazyTypeGetter;
|
|
572
|
+
config = arg3;
|
|
573
|
+
} else {
|
|
574
|
+
getter = arg1 as LazyTypeGetter;
|
|
575
|
+
config = arg2 as object;
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
if (typeof getter !== 'function') {
|
|
579
|
+
throw new HANDLER_MUST_BE_A_FUNCTION;
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
if (!config || (typeof config !== 'object' && typeof config !== 'function')) {
|
|
583
|
+
config = {};
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
const lazyResult = createFromLazyGetter(
|
|
587
|
+
this as TypeAbsorber,
|
|
588
|
+
subtypes,
|
|
589
|
+
name,
|
|
590
|
+
getter as LazyTypeGetter,
|
|
591
|
+
config as constructorOptions
|
|
592
|
+
);
|
|
593
|
+
return lazyResult;
|
|
594
|
+
};
|
|
595
|
+
|
|
539
596
|
export const lookup = function (
|
|
540
597
|
this: TypesMap,
|
|
541
598
|
TypeNestedPath: string
|
package/src/api/utils/index.ts
CHANGED
|
@@ -14,7 +14,7 @@ const {
|
|
|
14
14
|
const { WRONG_TYPE_DEFINITION, } = ErrorsTypes;
|
|
15
15
|
|
|
16
16
|
import {
|
|
17
|
-
_getProps, Props
|
|
17
|
+
_getProps, Props
|
|
18
18
|
} from '../types/Props';
|
|
19
19
|
import type { MnemonicaConstructor } from '../../types';
|
|
20
20
|
|
|
@@ -39,22 +39,22 @@ const getTypeChecker = (TypeName: string) => {
|
|
|
39
39
|
return false;
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
-
|
|
42
|
+
|
|
43
43
|
if (!instance!.constructor) {
|
|
44
44
|
return false;
|
|
45
45
|
}
|
|
46
|
-
|
|
46
|
+
|
|
47
47
|
// @ts-expect-error I'm too lazy for that
|
|
48
48
|
if (Reflect.getPrototypeOf(instance).constructor.name === 'Promise') {
|
|
49
49
|
// if ( instance instanceof Promise ) {
|
|
50
|
-
|
|
50
|
+
|
|
51
51
|
// @ts-expect-error I'm too lazy for that
|
|
52
52
|
const checkResult = instance[ SymbolConstructorName ] === TypeName;
|
|
53
53
|
return checkResult;
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
const constructors = collectConstructors(instance) as Record<string, new () => unknown>;
|
|
57
|
-
|
|
57
|
+
|
|
58
58
|
const constructorResult = constructors[ TypeName ] || false;
|
|
59
59
|
return constructorResult;
|
|
60
60
|
|
|
@@ -62,6 +62,19 @@ const getTypeChecker = (TypeName: string) => {
|
|
|
62
62
|
return seeker;
|
|
63
63
|
};
|
|
64
64
|
|
|
65
|
+
const typeCheckerCache = new Map<string, unknown>();
|
|
66
|
+
|
|
67
|
+
// getTypeChecker builds a fresh closure per call, and instanceof
|
|
68
|
+
// checks run per access — so the closures are cached per TypeName
|
|
69
|
+
const getCachedTypeChecker = (TypeName: string) => {
|
|
70
|
+
let checker = typeCheckerCache.get(TypeName);
|
|
71
|
+
if (!checker) {
|
|
72
|
+
checker = getTypeChecker(TypeName);
|
|
73
|
+
typeCheckerCache.set(TypeName, checker);
|
|
74
|
+
}
|
|
75
|
+
return checker;
|
|
76
|
+
};
|
|
77
|
+
|
|
65
78
|
const getTypeSplitPath = (path: string) => {
|
|
66
79
|
const split = path
|
|
67
80
|
// beautifull names
|
|
@@ -176,7 +189,7 @@ const findSubTypeFromParent = (instance: parentSub | object | undefined, subType
|
|
|
176
189
|
const props = _getProps(instance) as Props;
|
|
177
190
|
|
|
178
191
|
if (props) {
|
|
179
|
-
if(props.__type__.subtypes.has(subType)) {
|
|
192
|
+
if (props.__type__.subtypes.has(subType)) {
|
|
180
193
|
const _subtype = props.__type__.subtypes.get(subType);
|
|
181
194
|
subtype = _subtype;
|
|
182
195
|
} else {
|
|
@@ -186,7 +199,7 @@ const findSubTypeFromParent = (instance: parentSub | object | undefined, subType
|
|
|
186
199
|
);
|
|
187
200
|
}
|
|
188
201
|
}
|
|
189
|
-
|
|
202
|
+
|
|
190
203
|
// @ts-expect-error I'm too lazy for that
|
|
191
204
|
return subtype;
|
|
192
205
|
};
|
|
@@ -199,12 +212,12 @@ const findSubTypeFromParent = (instance: parentSub | object | undefined, subType
|
|
|
199
212
|
// accordingly to the gist from here:
|
|
200
213
|
// https://gist.github.com/wentout/ea3afe9c822a6b6ef32f9e4f3e98b1ba
|
|
201
214
|
const isClass = (fn: ConstructHandler) => {
|
|
202
|
-
|
|
215
|
+
|
|
216
|
+
/*
|
|
203
217
|
const str = String(fn);
|
|
204
218
|
const result = str.indexOf('class ') === 0;
|
|
205
219
|
return result;
|
|
206
220
|
|
|
207
|
-
/*
|
|
208
221
|
|
|
209
222
|
// not necessary to check fn for typeof
|
|
210
223
|
// because of other checks made before
|
|
@@ -234,6 +247,10 @@ const isClass = (fn: ConstructHandler) => {
|
|
|
234
247
|
return Reflect.getOwnPropertyDescriptor(fn, 'prototype')!.writable === false;
|
|
235
248
|
|
|
236
249
|
*/
|
|
250
|
+
|
|
251
|
+
const protoDesc = Reflect.getOwnPropertyDescriptor(fn, 'prototype');
|
|
252
|
+
const result = protoDesc !== undefined && protoDesc.writable === false;
|
|
253
|
+
return result;
|
|
237
254
|
};
|
|
238
255
|
|
|
239
256
|
const makeErrorModificatorType = (
|
|
@@ -304,6 +321,7 @@ const TypesUtils = {
|
|
|
304
321
|
isClass,
|
|
305
322
|
checkProto,
|
|
306
323
|
getTypeChecker,
|
|
324
|
+
getCachedTypeChecker,
|
|
307
325
|
getTypeSplitPath,
|
|
308
326
|
getExistentAsyncStack,
|
|
309
327
|
checkTypeName,
|
|
@@ -4,6 +4,8 @@ import type {
|
|
|
4
4
|
_Internal_TC_,
|
|
5
5
|
CreateTypesCollectionFunction,
|
|
6
6
|
TypesCollection,
|
|
7
|
+
TypeClass,
|
|
8
|
+
IDEF,
|
|
7
9
|
hooksOpts,
|
|
8
10
|
hook
|
|
9
11
|
} from '../../types';
|
|
@@ -22,7 +24,7 @@ const {
|
|
|
22
24
|
|
|
23
25
|
// here is TypesCollection.define() method
|
|
24
26
|
import {
|
|
25
|
-
define, lookup, type TypesMap
|
|
27
|
+
define, lazy, lookup, type TypesMap, type LazyTypeGetter
|
|
26
28
|
} from '../../api/types';
|
|
27
29
|
|
|
28
30
|
import * as hooksAPI from '../../api/hooks';
|
|
@@ -154,9 +156,11 @@ odp(
|
|
|
154
156
|
constructHandlerOrConfig?: CallableFunction | object,
|
|
155
157
|
config?: object
|
|
156
158
|
) {
|
|
157
|
-
//
|
|
159
|
+
// pass `result` itself as the stack-capture boundary (StackBoundary):
|
|
160
|
+
// it is a real callable on the stack, so captureStackTrace
|
|
161
|
+
// truncates at the user's call site instead of keeping internal frames
|
|
158
162
|
const defineResult = define.call(
|
|
159
|
-
|
|
163
|
+
result,
|
|
160
164
|
subtypes as TypesMap,
|
|
161
165
|
TypeOrTypeName,
|
|
162
166
|
constructHandlerOrConfig,
|
|
@@ -170,6 +174,80 @@ odp(
|
|
|
170
174
|
}
|
|
171
175
|
);
|
|
172
176
|
|
|
177
|
+
odp(
|
|
178
|
+
TypesCollection.prototype,
|
|
179
|
+
'lazy',
|
|
180
|
+
{
|
|
181
|
+
get (this: { subtypes: Map<string, object> }) {
|
|
182
|
+
const { subtypes } = this;
|
|
183
|
+
const result = function (
|
|
184
|
+
this: CallableFunction,
|
|
185
|
+
arg1: string | CallableFunction,
|
|
186
|
+
arg2?: CallableFunction | object,
|
|
187
|
+
arg3?: object
|
|
188
|
+
) {
|
|
189
|
+
let name: string | undefined;
|
|
190
|
+
let getter: LazyTypeGetter;
|
|
191
|
+
let config: object | undefined;
|
|
192
|
+
if (typeof arg1 === 'string') {
|
|
193
|
+
name = arg1;
|
|
194
|
+
getter = arg2 as LazyTypeGetter;
|
|
195
|
+
config = arg3;
|
|
196
|
+
} else {
|
|
197
|
+
getter = arg1 as LazyTypeGetter;
|
|
198
|
+
config = arg2 as object;
|
|
199
|
+
}
|
|
200
|
+
let lazyResult: TypeClass;
|
|
201
|
+
// same as in `define` above: pass `result` itself as the
|
|
202
|
+
// stack-capture boundary, not the collection object
|
|
203
|
+
if (name) {
|
|
204
|
+
lazyResult = lazy.call(
|
|
205
|
+
result,
|
|
206
|
+
subtypes as TypesMap,
|
|
207
|
+
name,
|
|
208
|
+
getter as LazyTypeGetter,
|
|
209
|
+
config
|
|
210
|
+
);
|
|
211
|
+
} else {
|
|
212
|
+
lazyResult = lazy.call(
|
|
213
|
+
result,
|
|
214
|
+
subtypes as TypesMap,
|
|
215
|
+
getter as LazyTypeGetter,
|
|
216
|
+
config
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
return lazyResult;
|
|
220
|
+
};
|
|
221
|
+
return result;
|
|
222
|
+
},
|
|
223
|
+
enumerable : true
|
|
224
|
+
}
|
|
225
|
+
);
|
|
226
|
+
|
|
227
|
+
odp(
|
|
228
|
+
TypesCollection.prototype,
|
|
229
|
+
'decorate',
|
|
230
|
+
{
|
|
231
|
+
get (this: TypesCollection) {
|
|
232
|
+
const self = this;
|
|
233
|
+
const result = function (config?: object) {
|
|
234
|
+
const decorator = function (cstr: CallableFunction) {
|
|
235
|
+
const { name } = cstr;
|
|
236
|
+
const defineResult = self.define(
|
|
237
|
+
name,
|
|
238
|
+
cstr as IDEF<object>,
|
|
239
|
+
config
|
|
240
|
+
);
|
|
241
|
+
return defineResult;
|
|
242
|
+
};
|
|
243
|
+
return decorator;
|
|
244
|
+
};
|
|
245
|
+
return result;
|
|
246
|
+
},
|
|
247
|
+
enumerable : true
|
|
248
|
+
}
|
|
249
|
+
);
|
|
250
|
+
|
|
173
251
|
odp(
|
|
174
252
|
TypesCollection.prototype,
|
|
175
253
|
'lookup',
|