mnemonica 1.4.0 → 1.4.2
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/FOR_HUMANS.md +44 -42
- package/build/api/errors/throwModificationError.js +2 -1
- package/build/api/index.d.ts +1 -1
- package/build/api/types/InstanceCreator.js +24 -9
- package/build/api/types/Mnemosyne.js +4 -1
- package/build/api/types/TypeProxy.js +30 -5
- package/build/api/types/compileNewModificatorFunctionBody.d.ts +4 -1
- package/build/api/types/compileNewModificatorFunctionBody.js +12 -7
- package/build/api/types/index.d.ts +2 -6
- package/build/api/types/index.js +103 -69
- package/build/api/types/lazyDefinition.d.ts +22 -0
- package/build/api/types/lazyDefinition.js +49 -0
- package/build/descriptors/types/index.js +3 -21
- package/build/index.d.ts +0 -2
- package/build/index.js +7 -7
- package/build/types/index.d.ts +4 -6
- package/build/types/index.js +1 -1
- package/docs/async-constructors.md +8 -6
- package/docs/typed-lookup.md +3 -2
- package/package.json +6 -3
- package/src/api/errors/throwModificationError.ts +5 -0
- package/src/api/types/InstanceCreator.ts +77 -14
- package/src/api/types/Mnemosyne.ts +9 -0
- package/src/api/types/TypeProxy.ts +63 -7
- package/src/api/types/compileNewModificatorFunctionBody.ts +18 -4
- package/src/api/types/index.ts +247 -152
- package/src/api/types/lazyDefinition.ts +104 -0
- package/src/descriptors/types/index.ts +13 -34
- package/src/index.ts +12 -24
- package/src/types/index.ts +17 -32
|
@@ -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
|
|
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
|
-
|
|
190
|
-
|
|
191
|
-
|
|
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
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
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
|
|
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
|
-
|
|
260
|
-
|
|
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,
|
|
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
|
|
270
|
-
typeof
|
|
271
|
-
) && typeof (
|
|
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 =
|
|
261
|
+
const source = sourceOrTypeName as { lazy: LazyAbsorber };
|
|
274
262
|
const sourceLazyResult = source.lazy(
|
|
275
|
-
|
|
276
|
-
getterOrConfig as LazyDef<object
|
|
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
|
-
|
|
287
|
-
|
|
274
|
+
sourceOrTypeName as string,
|
|
275
|
+
TypeNameOrGetter as LazyDef<object>,
|
|
288
276
|
getterOrConfig as constructorOptions
|
|
289
277
|
) as unknown;
|
|
290
278
|
return lazyResult;
|
package/src/types/index.ts
CHANGED
|
@@ -683,6 +683,17 @@ 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;
|
|
693
|
+
// set by throwModificationError once the post hooks have fired for this
|
|
694
|
+
// construction; postProcessing reads it to avoid a second firing when a
|
|
695
|
+
// creationError hook suppressed the throw
|
|
696
|
+
postHooksFired?: boolean;
|
|
686
697
|
|
|
687
698
|
getExistentAsyncStack(existentInstance: object): unknown;
|
|
688
699
|
postProcessing(continuationOf?: TypeDef): void;
|
|
@@ -939,14 +950,9 @@ export interface RegistryHolderBase<
|
|
|
939
950
|
Parent
|
|
940
951
|
>;
|
|
941
952
|
|
|
942
|
-
//
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
getter: LazyDef<SubType>,
|
|
946
|
-
config?: constructorOptions
|
|
947
|
-
): IDefinitorInstance<SubType>;
|
|
948
|
-
|
|
949
|
-
// Named lazy getter: .lazy(TypeName, () => Constructor, config?)
|
|
953
|
+
// Lazy getter: .lazy(TypeName, () => Constructor, config?)
|
|
954
|
+
// (the unnamed form is dropped: the callback is never called at define
|
|
955
|
+
// time, so there is nothing to derive the name from)
|
|
950
956
|
lazy<
|
|
951
957
|
const Name extends string,
|
|
952
958
|
N extends object,
|
|
@@ -1042,30 +1048,15 @@ export interface TypeAbsorber extends CallableFunction {
|
|
|
1042
1048
|
}
|
|
1043
1049
|
|
|
1044
1050
|
// Lazy absorber function type - used for explicit lazy getter definitions
|
|
1051
|
+
// (named form only: the callback is never called at define time, so the
|
|
1052
|
+
// name cannot be derived from it)
|
|
1045
1053
|
export interface LazyAbsorber extends CallableFunction {
|
|
1046
|
-
<T extends object>(
|
|
1047
|
-
this: unknown,
|
|
1048
|
-
getter: LazyDef<T>,
|
|
1049
|
-
config?: constructorOptions
|
|
1050
|
-
): IDefinitorInstance<T>;
|
|
1051
1054
|
<T extends object>(
|
|
1052
1055
|
this: unknown,
|
|
1053
1056
|
TypeName: string,
|
|
1054
1057
|
getter: LazyDef<T>,
|
|
1055
1058
|
config?: constructorOptions
|
|
1056
1059
|
): 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
1060
|
<T extends object>(
|
|
1070
1061
|
this: unknown,
|
|
1071
1062
|
source: RegistryHolderBase<object, object, string>,
|
|
@@ -1073,13 +1064,6 @@ export interface LazyAbsorber extends CallableFunction {
|
|
|
1073
1064
|
getter: LazyDef<T>,
|
|
1074
1065
|
config?: constructorOptions
|
|
1075
1066
|
): 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
1067
|
}
|
|
1084
1068
|
|
|
1085
1069
|
// TypesCollection interface for createTypesCollection
|
|
@@ -1149,6 +1133,7 @@ export interface TypeDescriptorDefine extends CallableFunction {
|
|
|
1149
1133
|
|
|
1150
1134
|
export interface TypeDescriptorLazy extends CallableFunction {
|
|
1151
1135
|
(
|
|
1136
|
+
TypeName: string,
|
|
1152
1137
|
getter: LazyDef<object>,
|
|
1153
1138
|
config?: object
|
|
1154
1139
|
): TypeClass;
|