mnemonica 1.3.9 → 1.4.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.
Files changed (39) hide show
  1. package/FOR_HUMANS.md +83 -76
  2. package/README.md +2 -2
  3. package/build/api/hooks/HookInvocation.d.ts +2 -2
  4. package/build/api/hooks/HookInvocation.js +1 -1
  5. package/build/api/index.d.ts +1 -1
  6. package/build/api/types/InstanceCreator.js +14 -3
  7. package/build/api/types/Mnemosyne.js +4 -1
  8. package/build/api/types/TypeProxy.js +30 -5
  9. package/build/api/types/compileNewModificatorFunctionBody.d.ts +4 -1
  10. package/build/api/types/compileNewModificatorFunctionBody.js +12 -7
  11. package/build/api/types/index.d.ts +2 -6
  12. package/build/api/types/index.js +103 -77
  13. package/build/api/types/lazyDefinition.d.ts +22 -0
  14. package/build/api/types/lazyDefinition.js +49 -0
  15. package/build/descriptors/types/index.js +3 -21
  16. package/build/index.d.ts +1 -4
  17. package/build/index.js +8 -11
  18. package/build/types/index.d.ts +14 -11
  19. package/build/types/index.js +1 -1
  20. package/build/utils/index.js +10 -8
  21. package/build/utils/parse.js +5 -2
  22. package/docs/UTILS.md +3 -2
  23. package/docs/async-constructors.md +13 -9
  24. package/docs/empathy-in-ai.md +2 -2
  25. package/docs/prototype-chain.md +1 -1
  26. package/docs/typed-lookup.md +6 -3
  27. package/package.json +17 -14
  28. package/src/api/hooks/HookInvocation.ts +4 -4
  29. package/src/api/types/InstanceCreator.ts +56 -4
  30. package/src/api/types/Mnemosyne.ts +9 -0
  31. package/src/api/types/TypeProxy.ts +63 -7
  32. package/src/api/types/compileNewModificatorFunctionBody.ts +18 -4
  33. package/src/api/types/index.ts +247 -154
  34. package/src/api/types/lazyDefinition.ts +104 -0
  35. package/src/descriptors/types/index.ts +13 -34
  36. package/src/index.ts +13 -32
  37. package/src/types/index.ts +62 -43
  38. package/src/utils/index.ts +15 -14
  39. package/src/utils/parse.ts +8 -1
package/src/index.ts CHANGED
@@ -46,6 +46,7 @@ export type {
46
46
  Proto,
47
47
  ProtoFlat,
48
48
  constructorOptions,
49
+ deliveredHookOpts,
49
50
  hooksOpts,
50
51
  hook,
51
52
  hooksTypes,
@@ -228,18 +229,12 @@ export function define <
228
229
  }
229
230
 
230
231
  export function lazy<T extends object>(
231
- // explicit-source named form: lazy(source, TypeName, getter, config?)
232
+ // explicit-source form: lazy(source, TypeName, getter, config?)
232
233
  source: RegistryHolderBase<object, object, string>,
233
234
  TypeName: string,
234
235
  getter: () => IDEF<T>,
235
236
  config?: constructorOptions
236
237
  ): IDefinitorInstance<T>;
237
- export function lazy<T extends object>(
238
- // explicit-source unnamed form: lazy(source, getter, config?)
239
- source: RegistryHolderBase<object, object, string>,
240
- getter: () => IDEF<T>,
241
- config?: constructorOptions
242
- ): IDefinitorInstance<T>;
243
238
  export function lazy<T extends object>(
244
239
  // named form: lazy(TypeName, getter, config?)
245
240
  this: unknown,
@@ -247,32 +242,26 @@ export function lazy<T extends object>(
247
242
  getter: LazyDef<T>,
248
243
  config?: constructorOptions
249
244
  ): IDefinitorInstance<T>;
250
- export function lazy<T extends object>(
251
- // unnamed form: lazy(getter, config?)
252
- this: unknown,
253
- getter: LazyDef<T>,
254
- config?: constructorOptions
255
- ): IDefinitorInstance<T>;
256
245
  export function lazy(
257
246
  this: unknown,
258
- sourceOrTypeNameOrGetter: unknown,
259
- TypeNameOrGetterOrConfig?: unknown,
247
+ sourceOrTypeName: unknown,
248
+ TypeNameOrGetter?: unknown,
260
249
  getterOrConfig?: unknown,
261
250
  config?: unknown
262
251
  ): unknown {
263
252
 
264
- // explicit-source form: lazy(source, name?, getter, config?)
253
+ // explicit-source form: lazy(source, TypeName, getter, config?)
265
254
  // the source may be a TypesCollection (object) or a TypeProxy (function),
266
255
  // so detection is: has a callable .lazy
267
256
  const mayBeSource = (
268
- (typeof sourceOrTypeNameOrGetter === 'object' && sourceOrTypeNameOrGetter !== null) ||
269
- typeof sourceOrTypeNameOrGetter === 'function'
270
- ) && typeof (sourceOrTypeNameOrGetter as { lazy?: unknown }).lazy === 'function';
257
+ (typeof sourceOrTypeName === 'object' && sourceOrTypeName !== null) ||
258
+ typeof sourceOrTypeName === 'function'
259
+ ) && typeof (sourceOrTypeName as { lazy?: unknown }).lazy === 'function';
271
260
  if (mayBeSource) {
272
- const source = sourceOrTypeNameOrGetter as { lazy: LazyAbsorber };
261
+ const source = sourceOrTypeName as { lazy: LazyAbsorber };
273
262
  const sourceLazyResult = source.lazy(
274
- TypeNameOrGetterOrConfig as string | LazyDef<object>,
275
- getterOrConfig as LazyDef<object> | constructorOptions,
263
+ TypeNameOrGetter as string,
264
+ getterOrConfig as LazyDef<object>,
276
265
  config as constructorOptions
277
266
  ) as unknown;
278
267
  return sourceLazyResult;
@@ -282,8 +271,8 @@ export function lazy(
282
271
  // Type assertion needed because TypesCollectionProxy is a Proxy
283
272
  const lazyResult = (types as { lazy: LazyAbsorber })
284
273
  .lazy(
285
- sourceOrTypeNameOrGetter as string | LazyDef<object>,
286
- TypeNameOrGetterOrConfig as LazyDef<object> | constructorOptions,
274
+ sourceOrTypeName as string,
275
+ TypeNameOrGetter as LazyDef<object>,
287
276
  getterOrConfig as constructorOptions
288
277
  ) as unknown;
289
278
  return lazyResult;
@@ -491,14 +480,6 @@ export const mnemonica = Object.entries({
491
480
  {}
492
481
  ) as MnemonicaModule;
493
482
 
494
- import * as api from './api';
495
-
496
- export const {
497
- define: _define,
498
- lazy: _lazy,
499
- lookup: _lookup
500
- } = api.types;
501
-
502
483
  export const {
503
484
 
504
485
  SymbolParentType,
@@ -108,9 +108,12 @@ export type ErrorMessageKey =
108
108
  // Error messages object type
109
109
  export type ErrorMessages = Record<ErrorMessageKey, string>;
110
110
 
111
- // Hook callback — passed to registerHook and invoked by invokeHook
111
+ // Hook callback as STORED and invoked by invokeHook — its parameter is the
112
+ // DELIVERED payload (deliveredHookOpts: HookInvocation.build()'s output).
113
+ // The registration surface (registerHook) types callbacks per hookType via
114
+ // typedHook instead.
112
115
  export interface hook extends CallableFunction {
113
- (opts: hooksOpts): unknown;
116
+ (opts: deliveredHookOpts): unknown;
114
117
  }
115
118
 
116
119
  // Flow checker callback registered via registerFlowChecker: invoked with
@@ -288,6 +291,15 @@ export type hooksTypes = 'preCreation' | 'postCreation' | 'creationError';
288
291
  // Hook options passed to hook callbacks
289
292
  // P = parent / existent instance (proto)
290
293
  // T = child / inherited instance (type being created)
294
+ //
295
+ // This is the payload at invokeHook's INPUT boundary: InstanceCreator's
296
+ // hookData carries `creator` (the InstanceCreator itself), and so may a
297
+ // manual invokeHook caller. HookInvocation converts creator into the bound
298
+ // throwModificationError of the DELIVERED payload (deliveredHookOpts below)
299
+ // — callbacks never see `creator`. Merged with optional post-hook fields on
300
+ // purpose: invokePreHooks' input has no instance yet, and a manual
301
+ // invokeHook caller may pass a partial payload. The CONSUMPTION side
302
+ // (registerHook callbacks) is typed per hookType by typedHookOpts below.
291
303
  export type hooksOpts<P = object, T = P> = {
292
304
  TypeName: string;
293
305
  type: TypeDef;
@@ -297,11 +309,29 @@ export type hooksOpts<P = object, T = P> = {
297
309
  creator?: { throwModificationError(error: Error): void };
298
310
  };
299
311
 
300
- // Hook options typed per hookType — the compile-time face of the runtime
301
- // hookData built in InstanceCreator (invokePreHooks / invokePostHooks).
312
+ // The DELIVERED hook payload — what invokeHook's HookInvocation rebuild
313
+ // actually hands to callbacks: the base quartet always; inheritedInstance
314
+ // and the bound throwModificationError only on post hooks. Both stay
315
+ // OPTIONAL here because a manual invokeHook caller may deliver a partial
316
+ // payload — the registration surface (typedHookOpts) types them per
317
+ // hookType instead.
318
+ export type deliveredHookOpts<P = object, T = P> = {
319
+ TypeName: string;
320
+ type: TypeDef;
321
+ args: unknown[];
322
+ existentInstance: P;
323
+ inheritedInstance?: T;
324
+ throwModificationError?: ThrowModificationError;
325
+ };
326
+
327
+ // Hook options typed per hookType — the compile-time face of the DELIVERED
328
+ // hook payload (HookInvocation's rebuild in invokeHook, which is what
329
+ // callbacks receive; InstanceCreator's own hookData is the input side,
330
+ // hooksOpts).
302
331
  // P = existent (parent) instance, T = inherited (created) instance.
303
332
  // preCreation fires BEFORE the created instance exists, so its opts carry
304
- // no inheritedInstance and no creator — accessing them is a compile error.
333
+ // no inheritedInstance and no throwModificationError — accessing them is a
334
+ // compile error.
305
335
  export type typedHookOpts<HT extends hooksTypes, P extends object, T extends object> =
306
336
  HT extends 'preCreation'
307
337
  ? {
@@ -316,13 +346,16 @@ export type typedHookOpts<HT extends hooksTypes, P extends object, T extends obj
316
346
  args: unknown[];
317
347
  existentInstance: P;
318
348
  inheritedInstance: T;
319
- creator: { throwModificationError(error: Error): void };
349
+ throwModificationError: ThrowModificationError;
320
350
  };
321
351
 
322
- // Typed hook callback — the cb of a registerHook call bound to a known type:
323
- // the callback's opts are inferred from the hookType and the parent/instance
324
- // pair the hook is registered on. Collection-wide (global) handlers stay on
325
- // the untyped `hook` above by design.
352
+ // Typed hook callback — the cb of EVERY registerHook call: the callback's
353
+ // opts are inferred from the hookType and the parent/instance pair the hook
354
+ // is registered on. On a TYPE (RegistryEntry, the free registerHook) P/T are
355
+ // the real parent/created instance types; on a COLLECTION (Hookable) they
356
+ // stay `object` — a collection mixes every registered type, so narrowing to
357
+ // a concrete instance is the callback's business (instanceof lookup('User'))
358
+ // — but the preCreation/post payload split holds at both levels.
326
359
  export interface typedHook<
327
360
  HT extends hooksTypes,
328
361
  P extends object = object,
@@ -650,6 +683,13 @@ export interface InstanceCreatorContext {
650
683
  proto: object;
651
684
  __proto_proto__?: object;
652
685
  stack?: string[];
686
+ // a lazy type's gather outcome, snapshotted by runSetup before the cell
687
+ // is reset (see lazyDefinition.ts): failed is the explicit flag (a
688
+ // callback may throw undefined), lazyError the thrown value itself —
689
+ // passed through to throwModificationError exactly like a
690
+ // constructor-body throw
691
+ lazyFailed?: boolean;
692
+ lazyError?: unknown;
653
693
 
654
694
  getExistentAsyncStack(existentInstance: object): unknown;
655
695
  postProcessing(continuationOf?: TypeDef): void;
@@ -906,14 +946,9 @@ export interface RegistryHolderBase<
906
946
  Parent
907
947
  >;
908
948
 
909
- // Explicit lazy getter: .lazy(() => Constructor, config?)
910
- lazy<SubType extends object>(
911
- this: RegistryHolderBase<T, Parent, Path>,
912
- getter: LazyDef<SubType>,
913
- config?: constructorOptions
914
- ): IDefinitorInstance<SubType>;
915
-
916
- // Named lazy getter: .lazy(TypeName, () => Constructor, config?)
949
+ // Lazy getter: .lazy(TypeName, () => Constructor, config?)
950
+ // (the unnamed form is dropped: the callback is never called at define
951
+ // time, so there is nothing to derive the name from)
917
952
  lazy<
918
953
  const Name extends string,
919
954
  N extends object,
@@ -1009,30 +1044,15 @@ export interface TypeAbsorber extends CallableFunction {
1009
1044
  }
1010
1045
 
1011
1046
  // Lazy absorber function type - used for explicit lazy getter definitions
1047
+ // (named form only: the callback is never called at define time, so the
1048
+ // name cannot be derived from it)
1012
1049
  export interface LazyAbsorber extends CallableFunction {
1013
- <T extends object>(
1014
- this: unknown,
1015
- getter: LazyDef<T>,
1016
- config?: constructorOptions
1017
- ): IDefinitorInstance<T>;
1018
1050
  <T extends object>(
1019
1051
  this: unknown,
1020
1052
  TypeName: string,
1021
1053
  getter: LazyDef<T>,
1022
1054
  config?: constructorOptions
1023
1055
  ): IDefinitorInstance<T>;
1024
- <T extends object>(
1025
- this: unknown,
1026
- TypeNameOrGetter: string | LazyDef<T>,
1027
- getterOrConfig: LazyDef<T> | constructorOptions,
1028
- namedFormConfig?: constructorOptions
1029
- ): IDefinitorInstance<T>;
1030
- <T extends object>(
1031
- this: unknown,
1032
- source: RegistryHolderBase<object, object, string>,
1033
- getter: LazyDef<T>,
1034
- config?: constructorOptions
1035
- ): IDefinitorInstance<T>;
1036
1056
  <T extends object>(
1037
1057
  this: unknown,
1038
1058
  source: RegistryHolderBase<object, object, string>,
@@ -1040,13 +1060,6 @@ export interface LazyAbsorber extends CallableFunction {
1040
1060
  getter: LazyDef<T>,
1041
1061
  config?: constructorOptions
1042
1062
  ): IDefinitorInstance<T>;
1043
- <T extends object>(
1044
- this: unknown,
1045
- source: RegistryHolderBase<object, object, string>,
1046
- TypeNameOrGetter: string | LazyDef<T>,
1047
- getterOrConfig: LazyDef<T> | constructorOptions,
1048
- namedFormConfig?: constructorOptions
1049
- ): IDefinitorInstance<T>;
1050
1063
  }
1051
1064
 
1052
1065
  // TypesCollection interface for createTypesCollection
@@ -1075,7 +1088,12 @@ export interface TypesCollection<
1075
1088
  export interface Hookable {
1076
1089
  hooks: Record<string, Set<hook>>;
1077
1090
  invokeHook(hookType: hooksTypes, opts: hooksOpts): Set<unknown>;
1078
- registerHook(hookType: hooksTypes, cb: hook): void;
1091
+ // Collection-level registration: the payload split by hookType holds
1092
+ // (preCreation has no inheritedInstance/throwModificationError; post
1093
+ // hooks carry both, required) but P/T stay `object` — a collection
1094
+ // mixes every registered type, so narrowing to a concrete instance
1095
+ // happens in the callback.
1096
+ registerHook<HT extends hooksTypes>(hookType: HT, cb: typedHook<HT>): void;
1079
1097
  registerFlowChecker(cb: FlowChecker): void;
1080
1098
  }
1081
1099
 
@@ -1111,6 +1129,7 @@ export interface TypeDescriptorDefine extends CallableFunction {
1111
1129
 
1112
1130
  export interface TypeDescriptorLazy extends CallableFunction {
1113
1131
  (
1132
+ TypeName: string,
1114
1133
  getter: LazyDef<object>,
1115
1134
  config?: object
1116
1135
  ): TypeClass;
@@ -41,9 +41,13 @@ const utilsUnWrapped = {
41
41
 
42
42
  };
43
43
 
44
- const wrapThis = ( method: WrappableMethod ) => {
44
+ const wrapThis = ( collection: object, method: WrappableMethod ) => {
45
45
  const result = function ( this: object, instance: object | undefined, ...args: unknown[] ) {
46
- const instanceContext = instance !== undefined ? instance : this;
46
+ // utils.X(undefined) must not silently operate on the utils
47
+ // collection itself: `this` substitutes only for a genuine method
48
+ // call on some OTHER object (the prototype opt-in form)
49
+ const instanceContext = instance !== undefined ? instance
50
+ : ( this !== collection ? this : undefined );
47
51
  let wrapResult: unknown;
48
52
  if ( new.target ) {
49
53
  // wrapThis forwards `new` to the util; WrappableMethod (a plain
@@ -64,18 +68,15 @@ const wrapThis = ( method: WrappableMethod ) => {
64
68
  return result;
65
69
  };
66
70
 
67
- export const utils = {
71
+ // one object serves both roles: the exported collection AND the identity
72
+ // wrapThis compares `this` against (a spread copy would defeat the check)
73
+ const wrappedUtils: { [ index: string ]: WrappableMethod } = {};
74
+ Object.entries( utilsUnWrapped )
75
+ .forEach(( util ) => {
76
+ const [ name, fn ] = util;
77
+ wrappedUtils[ name ] = wrapThis( wrappedUtils, fn );
78
+ });
68
79
 
69
- ...Object.entries( utilsUnWrapped )
70
- .reduce(
71
- ( methods: { [ index: string ]: WrappableMethod }, util ) => {
72
- const [ name, fn ] = util;
73
- methods[ name ] = wrapThis( fn );
74
- return methods;
75
- },
76
- {}
77
- ),
78
-
79
- } as UtilsCollection;
80
+ export const utils = wrappedUtils as UtilsCollection;
80
81
 
81
82
  export { defineStackCleaner } from './defineStackCleaner';
@@ -42,7 +42,14 @@ export function parse<T extends object> ( self: T | null ): Parsed<T> | EmptyPar
42
42
  return nullResult;
43
43
  }
44
44
 
45
- if ( !self || !( self as { constructor?: InstanceConstructor } ).constructor ) {
45
+ if ( typeof self !== 'object' && typeof self !== 'function' ) {
46
+ // every non-object except null (handled above): undefined,
47
+ // primitives — these used to die on a plain TypeError from
48
+ // Reflect.getPrototypeOf instead of the documented error
49
+ throw new WRONG_MODIFICATION_PATTERN;
50
+ }
51
+
52
+ if ( !( self as { constructor?: InstanceConstructor } ).constructor ) {
46
53
  throw new WRONG_MODIFICATION_PATTERN;
47
54
  }
48
55