mnemonica 1.4.5 → 1.4.7

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/src/utils/fork.ts CHANGED
@@ -4,6 +4,7 @@ import {
4
4
  getProps, Props
5
5
  } from '../api/types/Props';
6
6
  import { InstanceCreator } from '../api/types/InstanceCreator';
7
+ import { gatherLazyDefinition } from '../api/types/lazyDefinition';
7
8
  import TypesUtils from '../api/utils/index';
8
9
  import type { ForkInvoker } from '../types';
9
10
 
@@ -26,7 +27,7 @@ export const fork = <T extends object>(instance: T): ForkInvoker<T> => {
26
27
  TypeName
27
28
  } = type;
28
29
 
29
- const result = function (this: object, ...forkArgs: unknown[]) {
30
+ const result = function (this: object | void, ...forkArgs: unknown[]) {
30
31
 
31
32
  let forked;
32
33
  const Constructor = isSubType ?
@@ -36,15 +37,21 @@ export const fork = <T extends object>(instance: T): ForkInvoker<T> => {
36
37
  const args = forkArgs.length ? forkArgs : __args__;
37
38
 
38
39
 
39
- if (this === __self__) {
40
+ if (this === __self__ || this === undefined) {
40
41
 
41
- // @ts-expect-error this is definitely a constructor
42
+ // Constructor is the parent instance (subtype proxy) or the collection,
43
+ // both indexed dynamically, so Constructor[TypeName] is unknown statically.
44
+ // At runtime it is always the registered type, since TypeName comes from
45
+ // this instance's own type; a guard would be an unreachable branch.
46
+ // @ts-expect-error -- the registered subtype is only dynamically indexed
42
47
  forked = new (Constructor[ TypeName ])(...args);
43
48
  } else {
44
49
  // fork.call ? let's do it !
50
+ const wrappedInstance = reflectPrimitiveWrappers(this);
51
+ gatherLazyDefinition(type, wrappedInstance);
45
52
  forked = new InstanceCreator(
46
53
  type,
47
- reflectPrimitiveWrappers(this),
54
+ wrappedInstance,
48
55
  args
49
56
  );
50
57
  }
@@ -50,12 +50,11 @@ const wrapThis = ( collection: object, method: WrappableMethod ) => {
50
50
  : ( this !== collection ? this : undefined );
51
51
  let wrapResult: unknown;
52
52
  if ( new.target ) {
53
- // wrapThis forwards `new` to the util; WrappableMethod (a plain
54
- // function type) declares no construct signature, so the cast
55
- // bridges that construct call
56
- wrapResult = new (method as unknown as new (...a: unknown[]) => unknown)(
57
- instanceContext,
58
- ...args
53
+ // wrapThis forwards `new` with Reflect.construct: without a third
54
+ // argument new.target is method, exactly as in new method(...args)
55
+ wrapResult = Reflect.construct(
56
+ method,
57
+ [ instanceContext, ...args ]
59
58
  );
60
59
  } else {
61
60
  wrapResult = method(
@@ -13,8 +13,6 @@ import {
13
13
  } from '../api/types/Props';
14
14
  import { constants } from '../constants';
15
15
 
16
- const { SymbolConfig } = constants;
17
-
18
16
  import type {
19
17
  LineageGraph,
20
18
  LineageNode,
@@ -181,11 +179,12 @@ export const lineage = ( instances: object[], options?: LineageOptions ): Lineag
181
179
  const { __collection__: collection } = baseProps;
182
180
  // every collection carries a name by construction (the default
183
181
  // collection is 'defaultTypes', unnamed customs are auto-named), so
184
- // no fallback is needed here
185
- const collectionConfig = ( collection as unknown as { [ SymbolConfig ]?: { name?: string } } )[
186
- SymbolConfig
187
- ]!;
188
- const collectionName: string = collectionConfig.name!;
182
+ // no fallback is needed here; CollectionDef types its SymbolConfig
183
+ // entry as CollectionConfig with that name. Read through constants
184
+ // directly: a local alias (const or destructured) widens the unique
185
+ // symbol to symbol and loses the typed key
186
+ const collectionConfig = collection[ constants.SymbolConfig ];
187
+ const collectionName: string = collectionConfig.name;
189
188
 
190
189
  const node: LineageNode = {
191
190
  type : {