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.
- package/FOR_HUMANS.md +83 -76
- package/README.md +2 -2
- package/build/api/hooks/HookInvocation.d.ts +2 -2
- package/build/api/hooks/HookInvocation.js +1 -1
- package/build/api/index.d.ts +1 -1
- package/build/api/types/InstanceCreator.js +14 -3
- 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 -77
- 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 +1 -4
- package/build/index.js +8 -11
- package/build/types/index.d.ts +14 -11
- package/build/types/index.js +1 -1
- package/build/utils/index.js +10 -8
- package/build/utils/parse.js +5 -2
- package/docs/UTILS.md +3 -2
- package/docs/async-constructors.md +13 -9
- package/docs/empathy-in-ai.md +2 -2
- package/docs/prototype-chain.md +1 -1
- package/docs/typed-lookup.md +6 -3
- package/package.json +17 -14
- package/src/api/hooks/HookInvocation.ts +4 -4
- package/src/api/types/InstanceCreator.ts +56 -4
- 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 -154
- package/src/api/types/lazyDefinition.ts +104 -0
- package/src/descriptors/types/index.ts +13 -34
- package/src/index.ts +13 -32
- package/src/types/index.ts +62 -43
- package/src/utils/index.ts +15 -14
- package/src/utils/parse.ts +8 -1
package/src/api/types/index.ts
CHANGED
|
@@ -36,12 +36,6 @@ import {
|
|
|
36
36
|
LazyDef,
|
|
37
37
|
} from '../../types';
|
|
38
38
|
|
|
39
|
-
// Lazy getter: a function that, when called, returns the actual constructor function.
|
|
40
|
-
// Used when define() receives an anonymous function wrapping the real constructor.
|
|
41
|
-
export interface LazyTypeGetter extends CallableFunction {
|
|
42
|
-
(): ConstructHandler;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
39
|
import { hop } from '../../utils/hop';
|
|
46
40
|
|
|
47
41
|
import { constants } from '../../constants';
|
|
@@ -65,6 +59,7 @@ const { getDefaultPrototype } = mnemosynes;
|
|
|
65
59
|
const {
|
|
66
60
|
ALREADY_DECLARED,
|
|
67
61
|
WRONG_TYPE_DEFINITION,
|
|
62
|
+
WRONG_MODIFICATION_PATTERN,
|
|
68
63
|
TYPENAME_MUST_BE_A_STRING,
|
|
69
64
|
HANDLER_MUST_BE_A_FUNCTION,
|
|
70
65
|
OPTIONS_ERROR,
|
|
@@ -78,9 +73,15 @@ import { TypeProxy } from './TypeProxy';
|
|
|
78
73
|
|
|
79
74
|
import compileNewModificatorFunctionBody, {
|
|
80
75
|
classifyConstructHandler,
|
|
76
|
+
isCallableCandidate,
|
|
81
77
|
ConstructHandler
|
|
82
78
|
} from './compileNewModificatorFunctionBody';
|
|
83
79
|
|
|
80
|
+
import {
|
|
81
|
+
setLazyDefinition,
|
|
82
|
+
LazyDefinitionCell,
|
|
83
|
+
} from './lazyDefinition';
|
|
84
|
+
|
|
84
85
|
import TypesUtils, { CreationHandler } from '../utils';
|
|
85
86
|
const {
|
|
86
87
|
getCachedTypeChecker,
|
|
@@ -99,6 +100,159 @@ export type TypesMap = Map<string, object> & {
|
|
|
99
100
|
[MNEMOSYNE]?: CollectionDef;
|
|
100
101
|
};
|
|
101
102
|
|
|
103
|
+
// Lazy type facades.
|
|
104
|
+
// A lazy type is a regular define whose handler is the empty shim; the real
|
|
105
|
+
// constructor is gathered from the callback once per construction (see
|
|
106
|
+
// lazyDefinition.ts). The type's proto / config / constructHandler read the
|
|
107
|
+
// gather cell, so the same type object serves every construction without
|
|
108
|
+
// ever calling the callback at define time.
|
|
109
|
+
const installLazyDefinition = function (
|
|
110
|
+
type: TypeDescriptorInstance,
|
|
111
|
+
TypeName: string,
|
|
112
|
+
shimConstructHandler: MnemonicaConstructorFactory,
|
|
113
|
+
shimProto: { [index: string]: unknown },
|
|
114
|
+
mergedConfig: { [index: string]: unknown },
|
|
115
|
+
getter: LazyDef<object>
|
|
116
|
+
) {
|
|
117
|
+
|
|
118
|
+
// outside any construction the cell holds the define-time fallback:
|
|
119
|
+
// no handler, the shim's empty prototype, asClass off, no failure
|
|
120
|
+
const cell: LazyDefinitionCell = {
|
|
121
|
+
handler : undefined,
|
|
122
|
+
asClass : false,
|
|
123
|
+
proto : shimProto,
|
|
124
|
+
failed : false,
|
|
125
|
+
error : undefined
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
// back to the define-time state: no handler retained, the shim's empty
|
|
129
|
+
// prototype, asClass off, no error
|
|
130
|
+
const reset = function () {
|
|
131
|
+
cell.handler = undefined;
|
|
132
|
+
cell.asClass = false;
|
|
133
|
+
cell.proto = shimProto;
|
|
134
|
+
cell.failed = false;
|
|
135
|
+
cell.error = undefined;
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
// the single callback call per construction — invoked ONLY by
|
|
139
|
+
// gatherLazyDefinition at the construction entries
|
|
140
|
+
const gather = function () {
|
|
141
|
+
reset();
|
|
142
|
+
try {
|
|
143
|
+
const gathered = getter();
|
|
144
|
+
if (!isCallableCandidate(gathered)) {
|
|
145
|
+
throw new HANDLER_MUST_BE_A_FUNCTION;
|
|
146
|
+
}
|
|
147
|
+
// the shape checks are what make it a ConstructHandler
|
|
148
|
+
classifyConstructHandler(
|
|
149
|
+
TypeName,
|
|
150
|
+
gathered
|
|
151
|
+
);
|
|
152
|
+
const handler = gathered;
|
|
153
|
+
cell.handler = handler;
|
|
154
|
+
cell.asClass = isClass(handler);
|
|
155
|
+
// same fallback as the direct path: an own NON-OBJECT prototype
|
|
156
|
+
// (prototype = 123) is replaced by the default
|
|
157
|
+
cell.proto = (
|
|
158
|
+
hop(
|
|
159
|
+
handler,
|
|
160
|
+
'prototype'
|
|
161
|
+
) &&
|
|
162
|
+
(typeof handler.prototype === 'object')
|
|
163
|
+
) ? handler.prototype : getDefaultPrototype();
|
|
164
|
+
} catch (error) {
|
|
165
|
+
// passthrough, exactly like runBuild's catch: whatever the
|
|
166
|
+
// callback threw reaches throwModificationError as-is (a thrown
|
|
167
|
+
// non-Error breaks the prototype splice there the same way it
|
|
168
|
+
// does for a constructor body). The flag is separate from the
|
|
169
|
+
// value: a callback may throw undefined
|
|
170
|
+
cell.failed = true;
|
|
171
|
+
cell.error = error;
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
// keyed by the type descriptor here; TypeProxy registers its proxy
|
|
176
|
+
// against the same definition when it wraps this type
|
|
177
|
+
setLazyDefinition(
|
|
178
|
+
type,
|
|
179
|
+
{
|
|
180
|
+
cell,
|
|
181
|
+
// the merged config value at define time — the callback must
|
|
182
|
+
// not run when the construction will be blocked
|
|
183
|
+
blockErrors : mergedConfig.blockErrors !== false,
|
|
184
|
+
gather,
|
|
185
|
+
reset
|
|
186
|
+
}
|
|
187
|
+
);
|
|
188
|
+
|
|
189
|
+
// zero-arg factory, the same contract as a direct type's
|
|
190
|
+
// constructHandler: builds the wrapper from the gathered handler,
|
|
191
|
+
// rebuilt on every construction (the callback may return a new function
|
|
192
|
+
// each time); before the gather — and after a failed one — it builds
|
|
193
|
+
// the shim's wrapper, which the error path then replaces
|
|
194
|
+
const lazyConstructHandler = function () {
|
|
195
|
+
if (cell.handler === undefined) {
|
|
196
|
+
const shimResult = shimConstructHandler();
|
|
197
|
+
return shimResult;
|
|
198
|
+
}
|
|
199
|
+
const { handler } = cell;
|
|
200
|
+
const modificatorBody = compileNewModificatorFunctionBody(
|
|
201
|
+
TypeName,
|
|
202
|
+
cell.asClass
|
|
203
|
+
);
|
|
204
|
+
const wrappedBody = modificatorBody(
|
|
205
|
+
handler,
|
|
206
|
+
CreationHandler,
|
|
207
|
+
SymbolConstructorName
|
|
208
|
+
);
|
|
209
|
+
const handlerResult = wrappedBody();
|
|
210
|
+
return handlerResult;
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
odp(
|
|
214
|
+
type,
|
|
215
|
+
'constructHandler', {
|
|
216
|
+
get () {
|
|
217
|
+
return lazyConstructHandler;
|
|
218
|
+
},
|
|
219
|
+
enumerable : true,
|
|
220
|
+
configurable : true
|
|
221
|
+
}
|
|
222
|
+
);
|
|
223
|
+
|
|
224
|
+
odp(
|
|
225
|
+
type,
|
|
226
|
+
'proto', {
|
|
227
|
+
get () {
|
|
228
|
+
const result = cell.proto;
|
|
229
|
+
return result;
|
|
230
|
+
},
|
|
231
|
+
enumerable : true,
|
|
232
|
+
configurable : true
|
|
233
|
+
}
|
|
234
|
+
);
|
|
235
|
+
|
|
236
|
+
// a fresh copy per read: asClass is per-construction and must never
|
|
237
|
+
// leak into the shared merged config
|
|
238
|
+
odp(
|
|
239
|
+
type,
|
|
240
|
+
'config', {
|
|
241
|
+
get () {
|
|
242
|
+
const result = Object.assign(
|
|
243
|
+
{},
|
|
244
|
+
mergedConfig,
|
|
245
|
+
{ asClass : cell.asClass }
|
|
246
|
+
);
|
|
247
|
+
return result;
|
|
248
|
+
},
|
|
249
|
+
enumerable : true,
|
|
250
|
+
configurable : true
|
|
251
|
+
}
|
|
252
|
+
);
|
|
253
|
+
|
|
254
|
+
};
|
|
255
|
+
|
|
102
256
|
const typeDescriptorCreator = function (
|
|
103
257
|
this: TypeDescriptorInstance,
|
|
104
258
|
defineOrigin: TypeAbsorber,
|
|
@@ -107,6 +261,7 @@ const typeDescriptorCreator = function (
|
|
|
107
261
|
constructHandler: MnemonicaConstructorFactory,
|
|
108
262
|
proto: { [index: string]: unknown },
|
|
109
263
|
config: { [index: string]: unknown },
|
|
264
|
+
lazyGetter?: LazyDef<object>,
|
|
110
265
|
) {
|
|
111
266
|
|
|
112
267
|
// here "types" refers to subtypes of type or collection object {}
|
|
@@ -177,6 +332,20 @@ const typeDescriptorCreator = function (
|
|
|
177
332
|
defineOrigin
|
|
178
333
|
);
|
|
179
334
|
|
|
335
|
+
// lazy types: proto / config / constructHandler become facades over the
|
|
336
|
+
// per-construction gather cell — see installLazyDefinition above and
|
|
337
|
+
// lazyDefinition.ts
|
|
338
|
+
if (lazyGetter !== undefined) {
|
|
339
|
+
installLazyDefinition(
|
|
340
|
+
type,
|
|
341
|
+
TypeName,
|
|
342
|
+
constructHandler,
|
|
343
|
+
proto,
|
|
344
|
+
config,
|
|
345
|
+
lazyGetter
|
|
346
|
+
);
|
|
347
|
+
}
|
|
348
|
+
|
|
180
349
|
odp(
|
|
181
350
|
subtypes,
|
|
182
351
|
SymbolParentType, {
|
|
@@ -187,7 +356,10 @@ const typeDescriptorCreator = function (
|
|
|
187
356
|
);
|
|
188
357
|
|
|
189
358
|
// const ancestor = isSubType ? Object.create(null) : proto;
|
|
190
|
-
|
|
359
|
+
// a lazy root's ancestor is not known at define time either: TypeProxy
|
|
360
|
+
// installs a facade over the gather cell when it receives undefined for
|
|
361
|
+
// a non-subtype here
|
|
362
|
+
const ancestor = (isSubType || lazyGetter !== undefined) ? undefined : proto;
|
|
191
363
|
types.set(
|
|
192
364
|
TypeName,
|
|
193
365
|
new TypeProxy(
|
|
@@ -232,38 +404,17 @@ TypeDescriptor.prototype.define = function (
|
|
|
232
404
|
|
|
233
405
|
TypeDescriptor.prototype.lazy = function (
|
|
234
406
|
this: TypeDescriptorInstance,
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
407
|
+
TypeName: string,
|
|
408
|
+
getter?: LazyDef<object>,
|
|
409
|
+
config?: object
|
|
238
410
|
) {
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
} else {
|
|
247
|
-
getter = TypeNameOrGetter as LazyTypeGetter;
|
|
248
|
-
config = getterOrConfig as object;
|
|
249
|
-
}
|
|
250
|
-
let result: TypeClass;
|
|
251
|
-
if (name) {
|
|
252
|
-
result = lazy.call(
|
|
253
|
-
this,
|
|
254
|
-
this.subtypes as TypesMap,
|
|
255
|
-
name,
|
|
256
|
-
getter as LazyTypeGetter,
|
|
257
|
-
config
|
|
258
|
-
);
|
|
259
|
-
} else {
|
|
260
|
-
result = lazy.call(
|
|
261
|
-
this,
|
|
262
|
-
this.subtypes as TypesMap,
|
|
263
|
-
getter as LazyTypeGetter,
|
|
264
|
-
config
|
|
265
|
-
);
|
|
266
|
-
}
|
|
411
|
+
const result = lazy.call(
|
|
412
|
+
this,
|
|
413
|
+
this.subtypes as TypesMap,
|
|
414
|
+
TypeName,
|
|
415
|
+
getter,
|
|
416
|
+
config
|
|
417
|
+
);
|
|
267
418
|
return result;
|
|
268
419
|
};
|
|
269
420
|
|
|
@@ -398,8 +549,12 @@ const createFromDirectHandler = function (
|
|
|
398
549
|
target: TypesMap,
|
|
399
550
|
name: string,
|
|
400
551
|
handler?: ConstructHandler,
|
|
401
|
-
config?: constructorOptions
|
|
552
|
+
config?: constructorOptions,
|
|
553
|
+
lazyGetter?: LazyDef<object>
|
|
402
554
|
) {
|
|
555
|
+
// for a lazy type the handler stays the empty shim: the real constructor
|
|
556
|
+
// is gathered from the callback once per construction (see
|
|
557
|
+
// installLazyDefinition above and lazyDefinition.ts)
|
|
403
558
|
handler = handler || function () { };
|
|
404
559
|
const asClass = isClass(handler);
|
|
405
560
|
const modificatorBody = compileNewModificatorFunctionBody(
|
|
@@ -437,7 +592,7 @@ const createFromDirectHandler = function (
|
|
|
437
592
|
if ( !target.has( name ) ) {
|
|
438
593
|
classifyConstructHandler(
|
|
439
594
|
name,
|
|
440
|
-
handler
|
|
595
|
+
handler
|
|
441
596
|
);
|
|
442
597
|
}
|
|
443
598
|
|
|
@@ -447,7 +602,8 @@ const createFromDirectHandler = function (
|
|
|
447
602
|
name,
|
|
448
603
|
makeConstructHandler,
|
|
449
604
|
proto,
|
|
450
|
-
config
|
|
605
|
+
config,
|
|
606
|
+
lazyGetter
|
|
451
607
|
// new TypeDescriptor() returns the TypeProxy it registers in the
|
|
452
608
|
// subtypes map — a Proxy materializing the TypeClass surface
|
|
453
609
|
// dynamically — so the public constructor contract is named here
|
|
@@ -455,110 +611,28 @@ const createFromDirectHandler = function (
|
|
|
455
611
|
return result;
|
|
456
612
|
};
|
|
457
613
|
|
|
458
|
-
|
|
614
|
+
// .lazy() is a regular define whose handler is the empty shim; the real
|
|
615
|
+
// constructor is gathered from the callback once per construction — see
|
|
616
|
+
// installLazyDefinition above and lazyDefinition.ts. NOTHING is derived
|
|
617
|
+
// from the callback here: it is never called at define time, so forward
|
|
618
|
+
// references and circular imports work — the callback's target only has to
|
|
619
|
+
// exist by the first `new`
|
|
620
|
+
const createFromLazyDefinition = function (
|
|
459
621
|
defineOrigin: TypeAbsorber,
|
|
460
622
|
target: TypesMap,
|
|
461
|
-
name: string
|
|
462
|
-
getter:
|
|
623
|
+
name: string,
|
|
624
|
+
getter: LazyDef<object>,
|
|
463
625
|
config: constructorOptions
|
|
464
626
|
) {
|
|
465
627
|
|
|
466
|
-
const
|
|
467
|
-
|
|
468
|
-
if (typeof type !== 'function') {
|
|
469
|
-
throw new HANDLER_MUST_BE_A_FUNCTION;
|
|
470
|
-
}
|
|
471
|
-
|
|
472
|
-
const TypeName = name || type.name;
|
|
473
|
-
if (!TypeName) {
|
|
474
|
-
throw new TYPENAME_MUST_BE_A_STRING;
|
|
475
|
-
}
|
|
476
|
-
|
|
477
|
-
checkDuplicate(
|
|
478
|
-
target,
|
|
479
|
-
TypeName
|
|
480
|
-
);
|
|
481
|
-
|
|
482
|
-
// reject the unsupported handler shapes at DEFINE time too — the lazy
|
|
483
|
-
// path used to check only typeof/name, so a getter returning an arrow,
|
|
484
|
-
// a method, a bound function or a generator defined fine and failed
|
|
485
|
-
// (or misbehaved) at construction. Same readable errors as the direct
|
|
486
|
-
// path; checkDuplicate runs first so a taken name still wins with the
|
|
487
|
-
// canonical ALREADY_DECLARED
|
|
488
|
-
classifyConstructHandler(
|
|
489
|
-
TypeName,
|
|
490
|
-
type as ConstructHandler
|
|
491
|
-
);
|
|
492
|
-
|
|
493
|
-
const asClass = isClass(type);
|
|
494
|
-
|
|
495
|
-
// classifyConstructHandler above rejects only no-own-prototype
|
|
496
|
-
// functions; a function with an own NON-OBJECT prototype (prototype = 123)
|
|
497
|
-
// still passes and needs the default-prototype fallback
|
|
498
|
-
const proto = (
|
|
499
|
-
hop(
|
|
500
|
-
type,
|
|
501
|
-
'prototype'
|
|
502
|
-
) &&
|
|
503
|
-
(typeof type.prototype === 'object')
|
|
504
|
-
) ? type.prototype : getDefaultPrototype();
|
|
505
|
-
|
|
506
|
-
const makeConstructHandler = () => {
|
|
507
|
-
const constructHandler = getter();
|
|
508
|
-
|
|
509
|
-
// and on EVERY construction: the getter may return a new function
|
|
510
|
-
// each time, so the shape check cannot be cached from define time.
|
|
511
|
-
// This is what makes lazy slower than a direct define
|
|
512
|
-
classifyConstructHandler(
|
|
513
|
-
TypeName,
|
|
514
|
-
constructHandler as ConstructHandler
|
|
515
|
-
);
|
|
516
|
-
|
|
517
|
-
odp(
|
|
518
|
-
constructHandler,
|
|
519
|
-
SymbolConstructorName, {
|
|
520
|
-
get () {
|
|
521
|
-
return TypeName;
|
|
522
|
-
}
|
|
523
|
-
}
|
|
524
|
-
);
|
|
525
|
-
|
|
526
|
-
const protoDesc = Object
|
|
527
|
-
.getOwnPropertyDescriptor(
|
|
528
|
-
constructHandler,
|
|
529
|
-
'prototype'
|
|
530
|
-
) as PropertyDescriptor | undefined;
|
|
531
|
-
if (protoDesc && protoDesc.writable) {
|
|
532
|
-
constructHandler.prototype = getDefaultPrototype();
|
|
533
|
-
}
|
|
534
|
-
|
|
535
|
-
const handlerResult = constructHandler as MnemonicaConstructorFactory;
|
|
536
|
-
return handlerResult;
|
|
537
|
-
};
|
|
538
|
-
|
|
539
|
-
// 'name' is a COLLECTION option only (same rejection as the direct path)
|
|
540
|
-
if ( config !== undefined && typeof config === 'object' && 'name' in config ) {
|
|
541
|
-
const nameError = '\"name\" is a collection option ' +
|
|
542
|
-
'(createTypesCollection({ name })) — it cannot be set on a type';
|
|
543
|
-
throw new OPTIONS_ERROR( nameError );
|
|
544
|
-
}
|
|
545
|
-
config = Object.assign(
|
|
546
|
-
{},
|
|
547
|
-
config
|
|
548
|
-
);
|
|
549
|
-
|
|
550
|
-
config.asClass = asClass;
|
|
551
|
-
|
|
552
|
-
const result = new TypeDescriptor(
|
|
628
|
+
const result = createFromDirectHandler(
|
|
553
629
|
defineOrigin,
|
|
554
630
|
target,
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
// TypeClass contract is materialized dynamically by the Proxy
|
|
561
|
-
) as unknown as TypeClass;
|
|
631
|
+
name,
|
|
632
|
+
undefined,
|
|
633
|
+
config,
|
|
634
|
+
getter
|
|
635
|
+
);
|
|
562
636
|
return result;
|
|
563
637
|
};
|
|
564
638
|
|
|
@@ -641,36 +715,55 @@ export const define = function (
|
|
|
641
715
|
export const lazy = function (
|
|
642
716
|
this: unknown,
|
|
643
717
|
subtypes: TypesMap,
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
718
|
+
TypeName: string,
|
|
719
|
+
getter?: LazyDef<object>,
|
|
720
|
+
config?: object
|
|
647
721
|
): TypeClass {
|
|
648
722
|
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
if (typeof
|
|
653
|
-
|
|
654
|
-
getter = getterOrConfig as LazyTypeGetter;
|
|
655
|
-
config = namedFormConfig;
|
|
656
|
-
} else {
|
|
657
|
-
getter = TypeNameOrGetter as LazyTypeGetter;
|
|
658
|
-
config = getterOrConfig as object;
|
|
723
|
+
// the unnamed lazy(() => Constructor) form is dropped: the callback is
|
|
724
|
+
// never called at define time, so there is nothing to derive the name
|
|
725
|
+
// from — the name must come in explicitly
|
|
726
|
+
if (typeof TypeName !== 'string') {
|
|
727
|
+
throw new TYPENAME_MUST_BE_A_STRING;
|
|
659
728
|
}
|
|
660
729
|
|
|
661
730
|
if (typeof getter !== 'function') {
|
|
662
731
|
throw new HANDLER_MUST_BE_A_FUNCTION;
|
|
663
732
|
}
|
|
664
733
|
|
|
734
|
+
// the callback runs per construction and must deliver the constructor
|
|
735
|
+
// synchronously: an async or generator callback would produce a
|
|
736
|
+
// Promise / iterator instead
|
|
737
|
+
const callbackKind = getter.constructor.name;
|
|
738
|
+
if (
|
|
739
|
+
callbackKind === 'AsyncFunction' ||
|
|
740
|
+
callbackKind === 'GeneratorFunction' ||
|
|
741
|
+
callbackKind === 'AsyncGeneratorFunction'
|
|
742
|
+
) {
|
|
743
|
+
const msg = `${TypeName}: lazy callback must be a sync function ` +
|
|
744
|
+
'(async and generator callbacks are not supported)';
|
|
745
|
+
throw new WRONG_MODIFICATION_PATTERN( msg );
|
|
746
|
+
}
|
|
747
|
+
|
|
665
748
|
if (!config || (typeof config !== 'object' && typeof config !== 'function')) {
|
|
666
749
|
config = {};
|
|
667
750
|
}
|
|
668
751
|
|
|
669
|
-
|
|
670
|
-
|
|
752
|
+
// the same dotted-path resolution as define's string branch:
|
|
753
|
+
// lazy('Parent.Child', ...) lands under Parent, and a taken name fails
|
|
754
|
+
// with the descriptor's ALREADY_DECLARED, exactly like define
|
|
755
|
+
checkTypeName(TypeName);
|
|
756
|
+
|
|
757
|
+
const { target, name } = resolveDefinitionContext(
|
|
671
758
|
subtypes,
|
|
759
|
+
TypeName
|
|
760
|
+
);
|
|
761
|
+
|
|
762
|
+
const lazyResult = createFromLazyDefinition(
|
|
763
|
+
this as TypeAbsorber,
|
|
764
|
+
target,
|
|
672
765
|
name,
|
|
673
|
-
getter
|
|
766
|
+
getter,
|
|
674
767
|
config as constructorOptions
|
|
675
768
|
);
|
|
676
769
|
return lazyResult;
|
|
@@ -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;
|