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/index.ts
CHANGED
|
@@ -13,6 +13,7 @@ import type {
|
|
|
13
13
|
DecoratedClass,
|
|
14
14
|
TypeClass,
|
|
15
15
|
TypeAbsorber,
|
|
16
|
+
LazyAbsorber,
|
|
16
17
|
MnemonicaModule,
|
|
17
18
|
InstanceResult,
|
|
18
19
|
Merge,
|
|
@@ -30,6 +31,7 @@ export const {
|
|
|
30
31
|
|
|
31
32
|
export type {
|
|
32
33
|
IDEF,
|
|
34
|
+
LazyDef,
|
|
33
35
|
TypeConstructor,
|
|
34
36
|
TypeConstructorBase,
|
|
35
37
|
Proto,
|
|
@@ -177,6 +179,68 @@ export function define <
|
|
|
177
179
|
return defineResult;
|
|
178
180
|
}
|
|
179
181
|
|
|
182
|
+
export function lazy<T extends object>(
|
|
183
|
+
// explicit-source named form: lazy(source, TypeName, getter, config?)
|
|
184
|
+
source: RegistryHolderBase<object, object, string>,
|
|
185
|
+
TypeName: string,
|
|
186
|
+
getter: () => IDEF<T>,
|
|
187
|
+
config?: constructorOptions
|
|
188
|
+
): IDefinitorInstance<T>;
|
|
189
|
+
export function lazy<T extends object>(
|
|
190
|
+
// explicit-source unnamed form: lazy(source, getter, config?)
|
|
191
|
+
source: RegistryHolderBase<object, object, string>,
|
|
192
|
+
getter: () => IDEF<T>,
|
|
193
|
+
config?: constructorOptions
|
|
194
|
+
): IDefinitorInstance<T>;
|
|
195
|
+
export function lazy<T extends object>(
|
|
196
|
+
// named form: lazy(TypeName, getter, config?)
|
|
197
|
+
this: unknown,
|
|
198
|
+
TypeName: string,
|
|
199
|
+
getter: () => IDEF<T>,
|
|
200
|
+
config?: constructorOptions
|
|
201
|
+
): IDefinitorInstance<T>;
|
|
202
|
+
export function lazy<T extends object>(
|
|
203
|
+
// unnamed form: lazy(getter, config?)
|
|
204
|
+
this: unknown,
|
|
205
|
+
getter: () => IDEF<T>,
|
|
206
|
+
config?: constructorOptions
|
|
207
|
+
): IDefinitorInstance<T>;
|
|
208
|
+
export function lazy(
|
|
209
|
+
this: unknown,
|
|
210
|
+
arg1: unknown,
|
|
211
|
+
arg2?: unknown,
|
|
212
|
+
arg3?: unknown,
|
|
213
|
+
arg4?: unknown
|
|
214
|
+
): unknown {
|
|
215
|
+
|
|
216
|
+
// explicit-source form: lazy(source, name?, getter, config?)
|
|
217
|
+
// the source may be a TypesCollection (object) or a TypeProxy (function),
|
|
218
|
+
// so detection is: has a callable .lazy
|
|
219
|
+
const mayBeSource = (
|
|
220
|
+
(typeof arg1 === 'object' && arg1 !== null) ||
|
|
221
|
+
typeof arg1 === 'function'
|
|
222
|
+
) && typeof (arg1 as { lazy?: unknown }).lazy === 'function';
|
|
223
|
+
if (mayBeSource) {
|
|
224
|
+
const source = arg1 as unknown as { lazy: LazyAbsorber };
|
|
225
|
+
const sourceLazyResult = source.lazy(
|
|
226
|
+
arg2 as string | (() => IDEF<object>),
|
|
227
|
+
arg3 as (() => IDEF<object>) | constructorOptions,
|
|
228
|
+
arg4 as constructorOptions
|
|
229
|
+
) as unknown;
|
|
230
|
+
return sourceLazyResult;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
const types = checkThis(this) ? defaultTypes : this || defaultTypes;
|
|
234
|
+
// Type assertion needed because TypesCollectionProxy is a Proxy
|
|
235
|
+
const lazyResult = (types as { lazy: LazyAbsorber })
|
|
236
|
+
.lazy(
|
|
237
|
+
arg1 as string | (() => IDEF<object>),
|
|
238
|
+
arg2 as (() => IDEF<object>) | constructorOptions,
|
|
239
|
+
arg3 as constructorOptions
|
|
240
|
+
) as unknown;
|
|
241
|
+
return lazyResult;
|
|
242
|
+
}
|
|
243
|
+
|
|
180
244
|
export function lookup<const K extends keyof TypeRegistry>(
|
|
181
245
|
this: unknown,
|
|
182
246
|
TypeNestedPath: K
|
|
@@ -336,6 +400,7 @@ export const registerHook = function <T extends Constructor<T>>(
|
|
|
336
400
|
export const mnemonica = Object.entries({
|
|
337
401
|
|
|
338
402
|
define,
|
|
403
|
+
lazy,
|
|
339
404
|
lookup,
|
|
340
405
|
apply,
|
|
341
406
|
call,
|
|
@@ -370,6 +435,7 @@ import * as api from './api';
|
|
|
370
435
|
|
|
371
436
|
export const {
|
|
372
437
|
define: _define,
|
|
438
|
+
lazy: _lazy,
|
|
373
439
|
lookup: _lookup
|
|
374
440
|
} = api.types;
|
|
375
441
|
|
package/src/types/index.ts
CHANGED
|
@@ -13,6 +13,10 @@ export type PropsType = Record<string, unknown>;
|
|
|
13
13
|
// supply Args to get typed constructor params: IDEF<MyType, [string, number]>
|
|
14
14
|
export type IDEF<T, Args extends unknown[] = unknown[]> = { new(): T } | { (this: T, ...args: Args): void };
|
|
15
15
|
|
|
16
|
+
// Lazy getter type - a zero-arg factory that returns a constructor.
|
|
17
|
+
// Used by the explicit .lazy() API.
|
|
18
|
+
export type LazyDef<T, Args extends unknown[] = unknown[]> = () => IDEF<T, Args>;
|
|
19
|
+
|
|
16
20
|
// Error message types - all error messages are strings
|
|
17
21
|
export type ErrorMessageKey =
|
|
18
22
|
| 'BASE_ERROR_MESSAGE'
|
|
@@ -26,8 +30,8 @@ export type ErrorMessageKey =
|
|
|
26
30
|
| 'MISSING_HOOK_CALLBACK'
|
|
27
31
|
| 'MISSING_CALLBACK_ARGUMENT'
|
|
28
32
|
| 'OPTIONS_ERROR'
|
|
29
|
-
| 'WRONG_STACK_CLEANER'
|
|
30
|
-
|
|
33
|
+
| 'WRONG_STACK_CLEANER'
|
|
34
|
+
| 'TYPENAME_MUST_BE_A_STRING';
|
|
31
35
|
// | 'FLOW_CHECKER_REDEFINITION'
|
|
32
36
|
|
|
33
37
|
// Error messages object type
|
|
@@ -581,6 +585,31 @@ export interface RegistryHolderBase<
|
|
|
581
585
|
T & Record<ChildPath, StoredConstructor<F, ChildPath>>,
|
|
582
586
|
ChildPath
|
|
583
587
|
>;
|
|
588
|
+
|
|
589
|
+
// Explicit lazy getter: .lazy(() => Constructor, config?)
|
|
590
|
+
lazy<SubType extends object>(
|
|
591
|
+
this: RegistryHolderBase<T, Parent, Path>,
|
|
592
|
+
getter: LazyDef<SubType>,
|
|
593
|
+
config?: constructorOptions
|
|
594
|
+
): IDefinitorInstance<SubType>;
|
|
595
|
+
|
|
596
|
+
// Named lazy getter: .lazy(TypeName, () => Constructor, config?)
|
|
597
|
+
lazy<
|
|
598
|
+
const Name extends string,
|
|
599
|
+
N extends object,
|
|
600
|
+
F extends Proto<Parent, N> = Proto<Parent, N>,
|
|
601
|
+
ChildPath extends string = Path extends '' ? Name : `${Path}.${Name}`
|
|
602
|
+
>(
|
|
603
|
+
this: RegistryHolderBase<T, Parent, Path>,
|
|
604
|
+
TypeName: Name,
|
|
605
|
+
getter: LazyDef<N>,
|
|
606
|
+
config?: constructorOptions
|
|
607
|
+
): IDefinitorInstance<
|
|
608
|
+
F,
|
|
609
|
+
InstanceResult<F>,
|
|
610
|
+
T & Record<ChildPath, StoredConstructor<F, ChildPath>>,
|
|
611
|
+
ChildPath
|
|
612
|
+
>;
|
|
584
613
|
}
|
|
585
614
|
|
|
586
615
|
// Full registry holder - adds typed .lookup() for collections that are not
|
|
@@ -624,6 +653,8 @@ export interface IDefinitorInstance<
|
|
|
624
653
|
|
|
625
654
|
lookup: TypeLookup<Registry>;
|
|
626
655
|
|
|
656
|
+
decorate: (config?: constructorOptions) => <U extends Constructor<object>>(cstr: U) => DecoratedClass<U, Registry>;
|
|
657
|
+
|
|
627
658
|
registerHook(hookType: hooksTypes, cb: hook): void;
|
|
628
659
|
|
|
629
660
|
subtypes: SubtypesMap;
|
|
@@ -652,6 +683,47 @@ export interface TypeAbsorber extends CallableFunction {
|
|
|
652
683
|
): IDefinitorInstance<T>;
|
|
653
684
|
}
|
|
654
685
|
|
|
686
|
+
// Lazy absorber function type - used for explicit lazy getter definitions
|
|
687
|
+
export interface LazyAbsorber extends CallableFunction {
|
|
688
|
+
<T extends object>(
|
|
689
|
+
this: unknown,
|
|
690
|
+
getter: LazyDef<T>,
|
|
691
|
+
config?: constructorOptions
|
|
692
|
+
): IDefinitorInstance<T>;
|
|
693
|
+
<T extends object>(
|
|
694
|
+
this: unknown,
|
|
695
|
+
TypeName: string,
|
|
696
|
+
getter: LazyDef<T>,
|
|
697
|
+
config?: constructorOptions
|
|
698
|
+
): IDefinitorInstance<T>;
|
|
699
|
+
<T extends object>(
|
|
700
|
+
this: unknown,
|
|
701
|
+
arg1: string | LazyDef<T>,
|
|
702
|
+
arg2: LazyDef<T> | constructorOptions,
|
|
703
|
+
arg3?: constructorOptions
|
|
704
|
+
): IDefinitorInstance<T>;
|
|
705
|
+
<T extends object>(
|
|
706
|
+
this: unknown,
|
|
707
|
+
source: RegistryHolderBase<object, object, string>,
|
|
708
|
+
getter: LazyDef<T>,
|
|
709
|
+
config?: constructorOptions
|
|
710
|
+
): IDefinitorInstance<T>;
|
|
711
|
+
<T extends object>(
|
|
712
|
+
this: unknown,
|
|
713
|
+
source: RegistryHolderBase<object, object, string>,
|
|
714
|
+
TypeName: string,
|
|
715
|
+
getter: LazyDef<T>,
|
|
716
|
+
config?: constructorOptions
|
|
717
|
+
): IDefinitorInstance<T>;
|
|
718
|
+
<T extends object>(
|
|
719
|
+
this: unknown,
|
|
720
|
+
source: RegistryHolderBase<object, object, string>,
|
|
721
|
+
arg1: string | LazyDef<T>,
|
|
722
|
+
arg2: LazyDef<T> | constructorOptions,
|
|
723
|
+
arg3?: constructorOptions
|
|
724
|
+
): IDefinitorInstance<T>;
|
|
725
|
+
}
|
|
726
|
+
|
|
655
727
|
// TypesCollection interface for createTypesCollection
|
|
656
728
|
// This represents the actual return type of createTypesCollection
|
|
657
729
|
export interface TypesCollection<
|
|
@@ -661,6 +733,16 @@ export interface TypesCollection<
|
|
|
661
733
|
>
|
|
662
734
|
extends RegistryHolder<T, Parent, Path>, Hookable {
|
|
663
735
|
subtypes: SubtypesMap;
|
|
736
|
+
|
|
737
|
+
// Decorator factory: .decorate() or .decorate(config)
|
|
738
|
+
// Returns a class decorator that registers the class in this collection's registry.
|
|
739
|
+
decorate(
|
|
740
|
+
config?: constructorOptions
|
|
741
|
+
): <U extends Constructor<object>>(cstr: U) => DecoratedClass<
|
|
742
|
+
U,
|
|
743
|
+
T & Record<ConstructorName<U>, StoredConstructor<InstanceType<U>, ConstructorName<U>>>
|
|
744
|
+
>;
|
|
745
|
+
|
|
664
746
|
[key: string]: unknown;
|
|
665
747
|
}
|
|
666
748
|
|
|
@@ -702,12 +784,21 @@ export interface TypeDescriptorDefine extends CallableFunction {
|
|
|
702
784
|
): TypeClass;
|
|
703
785
|
}
|
|
704
786
|
|
|
787
|
+
export interface TypeDescriptorLazy extends CallableFunction {
|
|
788
|
+
(
|
|
789
|
+
getter: CallableFunction,
|
|
790
|
+
config?: object
|
|
791
|
+
): TypeClass;
|
|
792
|
+
}
|
|
793
|
+
|
|
705
794
|
export interface TypeDescriptorLookup extends CallableFunction {
|
|
706
795
|
(TypeNestedPath: string): TypeClass | undefined;
|
|
707
796
|
}
|
|
708
797
|
|
|
709
798
|
export type TypeDescriptorInstance = {
|
|
710
799
|
define: TypeDescriptorDefine;
|
|
800
|
+
lazy: TypeDescriptorLazy;
|
|
801
|
+
decorate: (config?: constructorOptions) => <U extends Constructor<object>>(cstr: U) => DecoratedClass<U>;
|
|
711
802
|
lookup: TypeDescriptorLookup;
|
|
712
803
|
subtypes: Map<string, object>;
|
|
713
804
|
TypeName: string;
|
|
@@ -723,18 +814,22 @@ export type ConstructorName<T extends Constructor<object>> =
|
|
|
723
814
|
|
|
724
815
|
// Decorated class type - includes call signature for decorator pattern and
|
|
725
816
|
// aligns with `IDefinitorInstance` so decorated classes expose the same
|
|
726
|
-
// constructor/lookup surface as builder-created types.
|
|
727
|
-
//
|
|
728
|
-
// local
|
|
729
|
-
|
|
817
|
+
// constructor/lookup surface as builder-created types.
|
|
818
|
+
// `Registry` defaults to the global `TypeRegistry`; for custom collections it
|
|
819
|
+
// is the collection's local registry so `lookup()` and nested `.decorate()`
|
|
820
|
+
// stay inside that collection.
|
|
821
|
+
export type DecoratedClass<
|
|
822
|
+
T extends Constructor<object>,
|
|
823
|
+
Registry extends object = TypeRegistry
|
|
824
|
+
> =
|
|
730
825
|
T &
|
|
731
826
|
Omit<
|
|
732
|
-
IDefinitorInstance<InstanceType<T>, InstanceType<T>,
|
|
827
|
+
IDefinitorInstance<InstanceType<T>, InstanceType<T>, Registry, ConstructorName<T>>,
|
|
733
828
|
'define' | 'lookup'
|
|
734
829
|
> &
|
|
735
|
-
(<U extends Constructor<object>>(target: U) => DecoratedClass<U>) & {
|
|
830
|
+
(<U extends Constructor<object>>(target: U) => DecoratedClass<U, Registry>) & {
|
|
736
831
|
define: TypeAbsorber;
|
|
737
|
-
lookup: TypeLookup<
|
|
832
|
+
lookup: TypeLookup<Registry>;
|
|
738
833
|
};
|
|
739
834
|
|
|
740
835
|
// Helper: merge parent entity (E) and child constructor instance (T) into a
|
|
@@ -809,7 +904,7 @@ export interface MnemonicaModule<Registry extends object = {}>
|
|
|
809
904
|
decorate: <T extends Constructor<object> | constructorOptions | undefined = undefined>(
|
|
810
905
|
target?: T,
|
|
811
906
|
config?: constructorOptions
|
|
812
|
-
) => <U extends Constructor<object>>(cstr: U) => DecoratedClass<U>;
|
|
907
|
+
) => <U extends Constructor<object>>(cstr: U) => DecoratedClass<U, Registry>;
|
|
813
908
|
registerHook: <T extends object>(Constructor: IDEF<T>, hookType: hooksTypes, cb: hook) => void;
|
|
814
909
|
|
|
815
910
|
// Descriptors
|
package/src/utils/parent.ts
CHANGED
|
@@ -40,12 +40,56 @@ export function parent <T extends object> (instance: T, path?: string): object |
|
|
|
40
40
|
return p;
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
-
const
|
|
43
|
+
const segments = path.split('.');
|
|
44
|
+
const last = segments.length - 1;
|
|
44
45
|
|
|
45
46
|
// seek throuh parent instances
|
|
46
47
|
// about the fist constructor with this name
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
48
|
+
// dotted paths must match contiguously upwards:
|
|
49
|
+
// each leading segment must be the direct parent
|
|
50
|
+
// of the instance matched by the next one,
|
|
51
|
+
// and the instance itself is never a candidate
|
|
52
|
+
let current = p as object;
|
|
53
|
+
for ( ;; ) {
|
|
54
|
+
|
|
55
|
+
const { constructor: { name } } = current as { constructor: { name: string } };
|
|
56
|
+
|
|
57
|
+
if ( name === segments[ last ] ) {
|
|
58
|
+
|
|
59
|
+
if ( last === 0 ) {
|
|
60
|
+
return current;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
let ancestor = current;
|
|
64
|
+
let matched = true;
|
|
65
|
+
for ( let i = last - 1; i >= 0; i-- ) {
|
|
66
|
+
const ancestorProps = _getProps( ancestor ) as Props | undefined;
|
|
67
|
+
if ( !ancestorProps ) {
|
|
68
|
+
matched = false;
|
|
69
|
+
break;
|
|
70
|
+
}
|
|
71
|
+
ancestor = ancestorProps.__parent__ as object;
|
|
72
|
+
const { constructor: { name: ancestorName } } = ancestor as { constructor: { name: string } };
|
|
73
|
+
if ( ancestorName !== segments[ i ] ) {
|
|
74
|
+
matched = false;
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if ( matched ) {
|
|
80
|
+
return current;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// every props-bearing instance chains up to an object,
|
|
86
|
+
// the props-less root instance terminates the scan
|
|
87
|
+
const currentProps = _getProps( current ) as Props | undefined;
|
|
88
|
+
if ( !currentProps ) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
current = currentProps.__parent__ as object;
|
|
92
|
+
|
|
93
|
+
}
|
|
50
94
|
|
|
51
95
|
}
|