mnemonica 1.3.8 → 1.4.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.
Files changed (45) hide show
  1. package/FOR_HUMANS.md +101 -31
  2. package/README.md +2 -2
  3. package/build/api/errors/index.d.ts +3 -7
  4. package/build/api/errors/index.js +3 -5
  5. package/build/api/errors/throwModificationError.js +2 -4
  6. package/build/api/hooks/HookInvocation.d.ts +2 -2
  7. package/build/api/hooks/HookInvocation.js +1 -1
  8. package/build/api/hooks/flowCheckers.d.ts +3 -3
  9. package/build/api/hooks/flowCheckers.js +1 -1
  10. package/build/api/index.d.ts +3 -3
  11. package/build/api/types/index.d.ts +3 -3
  12. package/build/api/types/index.js +18 -26
  13. package/build/api/utils/index.d.ts +2 -2
  14. package/build/api/utils/index.js +31 -18
  15. package/build/descriptors/errors/index.js +1 -1
  16. package/build/descriptors/types/index.js +8 -8
  17. package/build/index.d.ts +10 -13
  18. package/build/index.js +13 -16
  19. package/build/types/index.d.ts +89 -25
  20. package/build/types/index.js +1 -1
  21. package/build/utils/collectConstructors.js +1 -1
  22. package/build/utils/fork.d.ts +2 -1
  23. package/build/utils/fork.js +1 -1
  24. package/build/utils/index.js +10 -8
  25. package/build/utils/parse.js +5 -2
  26. package/docs/UTILS.md +3 -2
  27. package/docs/async-constructors.md +5 -3
  28. package/docs/empathy-in-ai.md +2 -2
  29. package/docs/prototype-chain.md +1 -1
  30. package/docs/typed-lookup.md +3 -1
  31. package/package.json +12 -12
  32. package/src/api/errors/index.ts +12 -15
  33. package/src/api/errors/throwModificationError.ts +3 -4
  34. package/src/api/hooks/HookInvocation.ts +6 -5
  35. package/src/api/hooks/flowCheckers.ts +6 -3
  36. package/src/api/types/index.ts +45 -44
  37. package/src/api/utils/index.ts +60 -24
  38. package/src/descriptors/errors/index.ts +4 -5
  39. package/src/descriptors/types/index.ts +20 -16
  40. package/src/index.ts +55 -47
  41. package/src/types/index.ts +252 -39
  42. package/src/utils/collectConstructors.ts +0 -9
  43. package/src/utils/fork.ts +2 -1
  44. package/src/utils/index.ts +15 -14
  45. package/src/utils/parse.ts +9 -1
package/src/index.ts CHANGED
@@ -5,7 +5,6 @@ import type {
5
5
  CreateTypesCollectionFunction,
6
6
  CtorParameter,
7
7
  IDEF,
8
- hook,
9
8
  hooksTypes,
10
9
  constructorOptions,
11
10
  Proto,
@@ -22,7 +21,13 @@ import type {
22
21
  TypeLookup,
23
22
  RegistryHolderBase,
24
23
  RegistryEntry,
25
- LookedUpConstructor
24
+ LookedUpConstructor,
25
+ WithHookRegister,
26
+ typedHook,
27
+ HookableConstructor,
28
+ DefineNewableOrCallable,
29
+ LookupSource,
30
+ LazyDef
26
31
  } from './types';
27
32
 
28
33
  import TypesUtils from './api/utils/index';
@@ -41,6 +46,7 @@ export type {
41
46
  Proto,
42
47
  ProtoFlat,
43
48
  constructorOptions,
49
+ deliveredHookOpts,
44
50
  hooksOpts,
45
51
  hook,
46
52
  hooksTypes,
@@ -62,6 +68,13 @@ export type {
62
68
  RegistryEntry,
63
69
  LookedUpConstructor,
64
70
  LookedUpInstance,
71
+ typedHook,
72
+ typedHookOpts,
73
+ WithHookRegister,
74
+ HookableConstructor,
75
+ // what a failed construction throws — the catch side of `new T()`
76
+ CreationError,
77
+ ErrorProps,
65
78
  } from './types';
66
79
 
67
80
  /**
@@ -149,8 +162,9 @@ export function define <
149
162
  ): IDefinitorInstance<
150
163
  F,
151
164
  InstanceResult<F>,
152
- Reg & Record<ChildPath, RegistryEntry<F, ChildPath>>,
153
- ChildPath
165
+ Reg & Record<ChildPath, RegistryEntry<F, ChildPath, Parent>>,
166
+ ChildPath,
167
+ Parent
154
168
  >;
155
169
  export function define <
156
170
  T extends object,
@@ -160,9 +174,9 @@ export function define <
160
174
  R extends IDefinitorInstance<N> = IDefinitorInstance<N>,
161
175
  >(
162
176
  this: unknown,
163
- TypeName?: string | CallableFunction | NewableFunction,
177
+ TypeName?: string | DefineNewableOrCallable,
164
178
  // Allow both strict IDEF and more flexible function signatures
165
- constructHandler?: IDEF<T> | CallableFunction | NewableFunction | object | boolean,
179
+ constructHandler?: IDEF<T> | DefineNewableOrCallable | object | boolean,
166
180
  config?: constructorOptions,
167
181
  ): R;
168
182
  export function define <
@@ -172,9 +186,9 @@ export function define <
172
186
  R extends IDefinitorInstance<N> = IDefinitorInstance<N>,
173
187
  >(
174
188
  this: unknown,
175
- TypeNameOrSource?: string | CallableFunction | NewableFunction | RegistryHolderBase<object, object, string>,
176
- constructHandlerOrName?: IDEF<T> | CallableFunction | NewableFunction | object | boolean | string,
177
- configOrHandler?: constructorOptions | IDEF<T> | CallableFunction | NewableFunction | object | boolean,
189
+ TypeNameOrSource?: string | DefineNewableOrCallable | RegistryHolderBase<object, object, string>,
190
+ constructHandlerOrName?: IDEF<T> | DefineNewableOrCallable | object | boolean | string,
191
+ configOrHandler?: constructorOptions | IDEF<T> | DefineNewableOrCallable | object | boolean,
178
192
  config?: constructorOptions,
179
193
  ): R {
180
194
 
@@ -231,36 +245,36 @@ export function lazy<T extends object>(
231
245
  // named form: lazy(TypeName, getter, config?)
232
246
  this: unknown,
233
247
  TypeName: string,
234
- getter: () => IDEF<T>,
248
+ getter: LazyDef<T>,
235
249
  config?: constructorOptions
236
250
  ): IDefinitorInstance<T>;
237
251
  export function lazy<T extends object>(
238
252
  // unnamed form: lazy(getter, config?)
239
253
  this: unknown,
240
- getter: () => IDEF<T>,
254
+ getter: LazyDef<T>,
241
255
  config?: constructorOptions
242
256
  ): IDefinitorInstance<T>;
243
257
  export function lazy(
244
258
  this: unknown,
245
- arg1: unknown,
246
- arg2?: unknown,
247
- arg3?: unknown,
248
- arg4?: unknown
259
+ sourceOrTypeNameOrGetter: unknown,
260
+ TypeNameOrGetterOrConfig?: unknown,
261
+ getterOrConfig?: unknown,
262
+ config?: unknown
249
263
  ): unknown {
250
264
 
251
265
  // explicit-source form: lazy(source, name?, getter, config?)
252
266
  // the source may be a TypesCollection (object) or a TypeProxy (function),
253
267
  // so detection is: has a callable .lazy
254
268
  const mayBeSource = (
255
- (typeof arg1 === 'object' && arg1 !== null) ||
256
- typeof arg1 === 'function'
257
- ) && typeof (arg1 as { lazy?: unknown }).lazy === 'function';
269
+ (typeof sourceOrTypeNameOrGetter === 'object' && sourceOrTypeNameOrGetter !== null) ||
270
+ typeof sourceOrTypeNameOrGetter === 'function'
271
+ ) && typeof (sourceOrTypeNameOrGetter as { lazy?: unknown }).lazy === 'function';
258
272
  if (mayBeSource) {
259
- const source = arg1 as { lazy: LazyAbsorber };
273
+ const source = sourceOrTypeNameOrGetter as { lazy: LazyAbsorber };
260
274
  const sourceLazyResult = source.lazy(
261
- arg2 as string | (() => IDEF<object>),
262
- arg3 as (() => IDEF<object>) | constructorOptions,
263
- arg4 as constructorOptions
275
+ TypeNameOrGetterOrConfig as string | LazyDef<object>,
276
+ getterOrConfig as LazyDef<object> | constructorOptions,
277
+ config as constructorOptions
264
278
  ) as unknown;
265
279
  return sourceLazyResult;
266
280
  }
@@ -269,9 +283,9 @@ export function lazy(
269
283
  // Type assertion needed because TypesCollectionProxy is a Proxy
270
284
  const lazyResult = (types as { lazy: LazyAbsorber })
271
285
  .lazy(
272
- arg1 as string | (() => IDEF<object>),
273
- arg2 as (() => IDEF<object>) | constructorOptions,
274
- arg3 as constructorOptions
286
+ sourceOrTypeNameOrGetter as string | LazyDef<object>,
287
+ TypeNameOrGetterOrConfig as LazyDef<object> | constructorOptions,
288
+ getterOrConfig as constructorOptions
275
289
  ) as unknown;
276
290
  return lazyResult;
277
291
  }
@@ -279,34 +293,34 @@ export function lazy(
279
293
  export function lookup<const K extends keyof TypeRegistry>(
280
294
  this: unknown,
281
295
  TypeNestedPath: K
282
- ): TypeRegistry[K];
296
+ ): WithHookRegister<TypeRegistry[K]>;
283
297
  export function lookup<Reg extends object, const K extends keyof Reg & string>(
284
298
  // explicit-source form: lookup(source, path)
285
299
  // resolves against the registry carried by a builder/collection value
286
300
  source: { lookup: TypeLookup<Reg> },
287
301
  TypeNestedPath: K
288
- ): LookedUpConstructor<Reg, K>;
302
+ ): WithHookRegister<LookedUpConstructor<Reg, K>>;
289
303
  export function lookup(this: unknown, TypeNestedPath: string): TypeClass | undefined;
290
304
  export function lookup(
291
- source: { lookup: (path: string) => TypeClass | undefined },
305
+ source: LookupSource,
292
306
  TypeNestedPath: string
293
307
  ): TypeClass | undefined;
294
308
  export function lookup(
295
309
  this: unknown,
296
- arg1: unknown,
297
- arg2?: unknown
310
+ sourceOrPath: unknown,
311
+ TypeNestedPath?: unknown
298
312
  ): unknown {
299
313
 
300
314
  // explicit-source form: lookup(source, path)
301
- if (typeof arg1 !== 'string' && typeof arg2 === 'string') {
302
- const source = arg1 as { lookup: (path: string) => TypeClass | undefined };
303
- const sourceResult = source.lookup(arg2);
315
+ if (typeof sourceOrPath !== 'string' && typeof TypeNestedPath === 'string') {
316
+ const source = sourceOrPath as LookupSource;
317
+ const sourceResult = source.lookup(TypeNestedPath);
304
318
  return sourceResult;
305
319
  }
306
320
 
307
321
  const types = checkThis(this) ? defaultTypes : this || defaultTypes;
308
322
  // Type assertion needed because TypesCollectionProxy is a Proxy
309
- const lookupResult = (types as { lookup: (path: string) => TypeClass | undefined }).lookup(arg1 as string);
323
+ const lookupResult = (types as LookupSource).lookup(sourceOrPath as string);
310
324
  return lookupResult;
311
325
  }
312
326
 
@@ -329,7 +343,6 @@ const $run = function <E extends object, T extends object, S extends Proto<E, T>
329
343
  TypeName,
330
344
  entity
331
345
  ) as TypeConstructorBase;
332
- // TODO: check lines below and if Constructor is not mnemonized ...
333
346
  if (Cstr === undefined) {
334
347
  throw new WRONG_MODIFICATION_PATTERN(`[ ${TypeName} ] is not defined as a Type Constructor on used instance`);
335
348
  }
@@ -431,10 +444,13 @@ export const decorate = function <
431
444
  };
432
445
 
433
446
 
434
- export const registerHook = function <T extends Constructor<T>>(
435
- Ctor: DecoratedClass<T>,
436
- hookType: hooksTypes,
437
- cb: hook
447
+ export const registerHook = function <
448
+ T extends object,
449
+ HT extends hooksTypes = hooksTypes,
450
+ >(
451
+ Ctor: HookableConstructor<T>,
452
+ hookType: HT,
453
+ cb: typedHook<HT, object, T>
438
454
  ): void {
439
455
  Ctor.registerHook(
440
456
  hookType,
@@ -476,14 +492,6 @@ export const mnemonica = Object.entries({
476
492
  {}
477
493
  ) as MnemonicaModule;
478
494
 
479
- import * as api from './api';
480
-
481
- export const {
482
- define: _define,
483
- lazy: _lazy,
484
- lookup: _lookup
485
- } = api.types;
486
-
487
495
  export const {
488
496
 
489
497
  SymbolParentType,