mnemonica 1.4.0 → 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.
@@ -0,0 +1,104 @@
1
+ 'use strict';
2
+
3
+ // Per-type lazy definition cell.
4
+ //
5
+ // .lazy(TypeName, callback) is a regular define whose construct handler is
6
+ // resolved on every `new`: the callback runs exactly ONCE per construction,
7
+ // at its entry point (TypeProxy.construct for a direct `new`, the
8
+ // SubTypeProxy in Mnemosyne.ts for `new instance.SubType()`, addThen in
9
+ // InstanceCreator.ts for an async chain link), and the gathered handler
10
+ // lives in this cell until InstanceCreator's setup has snapshotted it —
11
+ // then the cell returns to its define-time state, so between constructions
12
+ // every read (the proto / config / constructHandler facades installed by
13
+ // typeDescriptorCreator in api/types/index.ts, the ancestor facade in
14
+ // TypeProxy.ts) sees the define-time state: no handler retained, the shim's
15
+ // empty prototype, asClass off.
16
+ //
17
+ // The cells live in a module-level WeakMap keyed by the type DESCRIPTOR —
18
+ // and by its TypeProxy too (TypeProxy registers itself at creation),
19
+ // because the subtype construction entries (makeSubTypeProxy, addThen)
20
+ // hold the proxy, not the descriptor. Both keys resolve to the same
21
+ // LazyDefinition.
22
+
23
+ import type { ConstructHandler } from './compileNewModificatorFunctionBody';
24
+
25
+ export interface LazyDefinitionCell {
26
+ handler: ConstructHandler | undefined;
27
+ asClass: boolean;
28
+ proto: object;
29
+ // the gather outcome flag: distinct from the error VALUE, because a
30
+ // callback may legitimately throw undefined
31
+ failed: boolean;
32
+ // whatever the callback threw or whatever rejected shape it produced —
33
+ // unknown on purpose: the error path gets it exactly as the
34
+ // constructor-body path gets a thrown non-Error
35
+ error: unknown;
36
+ }
37
+
38
+ export interface LazyDefinition {
39
+ cell: LazyDefinitionCell;
40
+ // the type's merged config value at define time (handler-independent):
41
+ // a blocked construction (errored parent + blockErrors on) never runs
42
+ // user code on the body path — the callback must not run either
43
+ blockErrors: boolean;
44
+ gather: () => void;
45
+ reset: () => void;
46
+ }
47
+
48
+ const lazyDefinitions = new WeakMap<object, LazyDefinition>();
49
+
50
+ export const setLazyDefinition = function ( key: object, definition: LazyDefinition ) {
51
+ lazyDefinitions.set(
52
+ key,
53
+ definition
54
+ );
55
+ };
56
+
57
+ export const readLazyDefinition = function ( type: unknown ): LazyDefinition | undefined {
58
+ if (type === null || (typeof type !== 'object' && typeof type !== 'function')) {
59
+ return undefined;
60
+ }
61
+ const result = lazyDefinitions.get( type );
62
+ return result;
63
+ };
64
+
65
+ // runs the callback at a construction entry; a no-op for direct types, and
66
+ // skipped when the construction is about to be blocked (an errored parent
67
+ // with blockErrors on): the circuit breaker in InstanceCreator then throws
68
+ // the parent's error exactly as on the body path
69
+ export const gatherLazyDefinition = function ( type: unknown, existentInstance?: unknown ) {
70
+ const lazy = readLazyDefinition( type );
71
+ if ( lazy === undefined ) {
72
+ return;
73
+ }
74
+ if ( lazy.blockErrors && existentInstance instanceof Error ) {
75
+ return;
76
+ }
77
+ lazy.gather();
78
+ };
79
+
80
+ // returns the cell to the define-time state; called by InstanceCreator's
81
+ // setup once the construction has snapshotted everything it needs
82
+ export const resetLazyDefinition = function ( type: unknown ) {
83
+ const lazy = readLazyDefinition( type );
84
+ if ( lazy === undefined ) {
85
+ return;
86
+ }
87
+ lazy.reset();
88
+ };
89
+
90
+ // the gather failure snapshotted by InstanceCreator's setup: `failed` is
91
+ // the explicit outcome flag (a callback may throw undefined), `error` the
92
+ // thrown value itself
93
+ export const readLazyDefinitionFailure = function ( type: unknown ): { failed: boolean; error: unknown } | undefined {
94
+ const lazy = readLazyDefinition( type );
95
+ if ( lazy === undefined ) {
96
+ return undefined;
97
+ }
98
+ const { cell } = lazy;
99
+ const result = {
100
+ failed : cell.failed,
101
+ error : cell.error
102
+ };
103
+ return result;
104
+ };
@@ -4,14 +4,14 @@ import type {
4
4
  _Internal_TC_,
5
5
  CreateTypesCollectionFunction,
6
6
  TypesCollection,
7
- TypeClass,
8
7
  IDEF,
9
8
  hooksOpts,
10
9
  hook,
11
10
  StackBoundary,
12
11
  DefineNewable,
13
12
  DefineNewableOrCallable,
14
- FlowChecker
13
+ FlowChecker,
14
+ LazyDef
15
15
  } from '../../types';
16
16
 
17
17
  import { constants } from '../../constants';
@@ -28,7 +28,7 @@ const {
28
28
 
29
29
  // here is TypesCollection.define() method
30
30
  import {
31
- define, lazy, lookup, type TypesMap, type LazyTypeGetter
31
+ define, lazy, lookup, type TypesMap
32
32
  } from '../../api/types';
33
33
 
34
34
  import * as hooksAPI from '../../api/hooks';
@@ -186,40 +186,19 @@ odp(
186
186
  const { subtypes } = this;
187
187
  const result = function (
188
188
  this: StackBoundary,
189
- TypeNameOrGetter: string | LazyTypeGetter,
190
- getterOrConfig?: LazyTypeGetter | object,
191
- namedFormConfig?: object
189
+ TypeName: string,
190
+ getter?: LazyDef<object>,
191
+ config?: object
192
192
  ) {
193
- let name: string | undefined;
194
- let getter: LazyTypeGetter;
195
- let config: object | undefined;
196
- if (typeof TypeNameOrGetter === 'string') {
197
- name = TypeNameOrGetter;
198
- getter = getterOrConfig as LazyTypeGetter;
199
- config = namedFormConfig;
200
- } else {
201
- getter = TypeNameOrGetter as LazyTypeGetter;
202
- config = getterOrConfig as object;
203
- }
204
- let lazyResult: TypeClass;
205
193
  // same as in `define` above: pass `result` itself as the
206
194
  // stack-capture boundary, not the collection object
207
- if (name) {
208
- lazyResult = lazy.call(
209
- result,
210
- subtypes as TypesMap,
211
- name,
212
- getter as LazyTypeGetter,
213
- config
214
- );
215
- } else {
216
- lazyResult = lazy.call(
217
- result,
218
- subtypes as TypesMap,
219
- getter as LazyTypeGetter,
220
- config
221
- );
222
- }
195
+ const lazyResult = lazy.call(
196
+ result,
197
+ subtypes as TypesMap,
198
+ TypeName,
199
+ getter,
200
+ config
201
+ );
223
202
  return lazyResult;
224
203
  };
225
204
  return result;
package/src/index.ts CHANGED
@@ -229,18 +229,12 @@ export function define <
229
229
  }
230
230
 
231
231
  export function lazy<T extends object>(
232
- // explicit-source named form: lazy(source, TypeName, getter, config?)
232
+ // explicit-source form: lazy(source, TypeName, getter, config?)
233
233
  source: RegistryHolderBase<object, object, string>,
234
234
  TypeName: string,
235
235
  getter: () => IDEF<T>,
236
236
  config?: constructorOptions
237
237
  ): IDefinitorInstance<T>;
238
- export function lazy<T extends object>(
239
- // explicit-source unnamed form: lazy(source, getter, config?)
240
- source: RegistryHolderBase<object, object, string>,
241
- getter: () => IDEF<T>,
242
- config?: constructorOptions
243
- ): IDefinitorInstance<T>;
244
238
  export function lazy<T extends object>(
245
239
  // named form: lazy(TypeName, getter, config?)
246
240
  this: unknown,
@@ -248,32 +242,26 @@ export function lazy<T extends object>(
248
242
  getter: LazyDef<T>,
249
243
  config?: constructorOptions
250
244
  ): IDefinitorInstance<T>;
251
- export function lazy<T extends object>(
252
- // unnamed form: lazy(getter, config?)
253
- this: unknown,
254
- getter: LazyDef<T>,
255
- config?: constructorOptions
256
- ): IDefinitorInstance<T>;
257
245
  export function lazy(
258
246
  this: unknown,
259
- sourceOrTypeNameOrGetter: unknown,
260
- TypeNameOrGetterOrConfig?: unknown,
247
+ sourceOrTypeName: unknown,
248
+ TypeNameOrGetter?: unknown,
261
249
  getterOrConfig?: unknown,
262
250
  config?: unknown
263
251
  ): unknown {
264
252
 
265
- // explicit-source form: lazy(source, name?, getter, config?)
253
+ // explicit-source form: lazy(source, TypeName, getter, config?)
266
254
  // the source may be a TypesCollection (object) or a TypeProxy (function),
267
255
  // so detection is: has a callable .lazy
268
256
  const mayBeSource = (
269
- (typeof sourceOrTypeNameOrGetter === 'object' && sourceOrTypeNameOrGetter !== null) ||
270
- typeof sourceOrTypeNameOrGetter === 'function'
271
- ) && 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';
272
260
  if (mayBeSource) {
273
- const source = sourceOrTypeNameOrGetter as { lazy: LazyAbsorber };
261
+ const source = sourceOrTypeName as { lazy: LazyAbsorber };
274
262
  const sourceLazyResult = source.lazy(
275
- TypeNameOrGetterOrConfig as string | LazyDef<object>,
276
- getterOrConfig as LazyDef<object> | constructorOptions,
263
+ TypeNameOrGetter as string,
264
+ getterOrConfig as LazyDef<object>,
277
265
  config as constructorOptions
278
266
  ) as unknown;
279
267
  return sourceLazyResult;
@@ -283,8 +271,8 @@ export function lazy(
283
271
  // Type assertion needed because TypesCollectionProxy is a Proxy
284
272
  const lazyResult = (types as { lazy: LazyAbsorber })
285
273
  .lazy(
286
- sourceOrTypeNameOrGetter as string | LazyDef<object>,
287
- TypeNameOrGetterOrConfig as LazyDef<object> | constructorOptions,
274
+ sourceOrTypeName as string,
275
+ TypeNameOrGetter as LazyDef<object>,
288
276
  getterOrConfig as constructorOptions
289
277
  ) as unknown;
290
278
  return lazyResult;
@@ -683,6 +683,13 @@ export interface InstanceCreatorContext {
683
683
  proto: object;
684
684
  __proto_proto__?: object;
685
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;
686
693
 
687
694
  getExistentAsyncStack(existentInstance: object): unknown;
688
695
  postProcessing(continuationOf?: TypeDef): void;
@@ -939,14 +946,9 @@ export interface RegistryHolderBase<
939
946
  Parent
940
947
  >;
941
948
 
942
- // Explicit lazy getter: .lazy(() => Constructor, config?)
943
- lazy<SubType extends object>(
944
- this: RegistryHolderBase<T, Parent, Path>,
945
- getter: LazyDef<SubType>,
946
- config?: constructorOptions
947
- ): IDefinitorInstance<SubType>;
948
-
949
- // 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)
950
952
  lazy<
951
953
  const Name extends string,
952
954
  N extends object,
@@ -1042,30 +1044,15 @@ export interface TypeAbsorber extends CallableFunction {
1042
1044
  }
1043
1045
 
1044
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)
1045
1049
  export interface LazyAbsorber extends CallableFunction {
1046
- <T extends object>(
1047
- this: unknown,
1048
- getter: LazyDef<T>,
1049
- config?: constructorOptions
1050
- ): IDefinitorInstance<T>;
1051
1050
  <T extends object>(
1052
1051
  this: unknown,
1053
1052
  TypeName: string,
1054
1053
  getter: LazyDef<T>,
1055
1054
  config?: constructorOptions
1056
1055
  ): IDefinitorInstance<T>;
1057
- <T extends object>(
1058
- this: unknown,
1059
- TypeNameOrGetter: string | LazyDef<T>,
1060
- getterOrConfig: LazyDef<T> | constructorOptions,
1061
- namedFormConfig?: constructorOptions
1062
- ): IDefinitorInstance<T>;
1063
- <T extends object>(
1064
- this: unknown,
1065
- source: RegistryHolderBase<object, object, string>,
1066
- getter: LazyDef<T>,
1067
- config?: constructorOptions
1068
- ): IDefinitorInstance<T>;
1069
1056
  <T extends object>(
1070
1057
  this: unknown,
1071
1058
  source: RegistryHolderBase<object, object, string>,
@@ -1073,13 +1060,6 @@ export interface LazyAbsorber extends CallableFunction {
1073
1060
  getter: LazyDef<T>,
1074
1061
  config?: constructorOptions
1075
1062
  ): IDefinitorInstance<T>;
1076
- <T extends object>(
1077
- this: unknown,
1078
- source: RegistryHolderBase<object, object, string>,
1079
- TypeNameOrGetter: string | LazyDef<T>,
1080
- getterOrConfig: LazyDef<T> | constructorOptions,
1081
- namedFormConfig?: constructorOptions
1082
- ): IDefinitorInstance<T>;
1083
1063
  }
1084
1064
 
1085
1065
  // TypesCollection interface for createTypesCollection
@@ -1149,6 +1129,7 @@ export interface TypeDescriptorDefine extends CallableFunction {
1149
1129
 
1150
1130
  export interface TypeDescriptorLazy extends CallableFunction {
1151
1131
  (
1132
+ TypeName: string,
1152
1133
  getter: LazyDef<object>,
1153
1134
  config?: object
1154
1135
  ): TypeClass;