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
|
@@ -9,6 +9,15 @@ const { checkProto, } = TypesUtils;
|
|
|
9
9
|
|
|
10
10
|
import { hop } from '../../utils/hop';
|
|
11
11
|
|
|
12
|
+
import { constants } from '../../constants';
|
|
13
|
+
const { odp } = constants;
|
|
14
|
+
|
|
15
|
+
import {
|
|
16
|
+
readLazyDefinition,
|
|
17
|
+
gatherLazyDefinition,
|
|
18
|
+
setLazyDefinition
|
|
19
|
+
} from './lazyDefinition';
|
|
20
|
+
|
|
12
21
|
import { ErrorsTypes } from '../../descriptors/errors';
|
|
13
22
|
const { WRONG_TYPE_DEFINITION, } = ErrorsTypes;
|
|
14
23
|
|
|
@@ -50,14 +59,49 @@ export const TypeProxy = function (__type__: TypeProxyType, ancestor: unknown) {
|
|
|
50
59
|
Object.assign(
|
|
51
60
|
this,
|
|
52
61
|
{
|
|
53
|
-
__type__
|
|
54
|
-
ancestor
|
|
62
|
+
__type__
|
|
55
63
|
}
|
|
56
64
|
);
|
|
65
|
+
// for a lazy ROOT type the ancestor is not fixed at define time: it is
|
|
66
|
+
// the current construction's gathered prototype, so it is a facade over
|
|
67
|
+
// the lazy cell. An explicit ancestor (the .call/.apply/.bind path)
|
|
68
|
+
// keeps winning as a plain value, exactly as for direct types
|
|
69
|
+
const lazy = readLazyDefinition(__type__);
|
|
70
|
+
if (lazy !== undefined && ancestor === undefined && !__type__.isSubType) {
|
|
71
|
+
const { cell } = lazy;
|
|
72
|
+
odp(
|
|
73
|
+
this,
|
|
74
|
+
'ancestor',
|
|
75
|
+
{
|
|
76
|
+
get () {
|
|
77
|
+
const result = cell.proto;
|
|
78
|
+
return result;
|
|
79
|
+
},
|
|
80
|
+
enumerable : true,
|
|
81
|
+
configurable : true
|
|
82
|
+
}
|
|
83
|
+
);
|
|
84
|
+
} else {
|
|
85
|
+
Object.assign(
|
|
86
|
+
this,
|
|
87
|
+
{
|
|
88
|
+
ancestor
|
|
89
|
+
}
|
|
90
|
+
);
|
|
91
|
+
}
|
|
57
92
|
const typeProxy = new Proxy(
|
|
58
93
|
InstanceCreator,
|
|
59
94
|
this
|
|
60
95
|
);
|
|
96
|
+
if (lazy !== undefined) {
|
|
97
|
+
// the subtype construction entries (makeSubTypeProxy, addThen) hold
|
|
98
|
+
// this proxy, not the descriptor — register it against the same
|
|
99
|
+
// definition so both keys resolve the one cell
|
|
100
|
+
setLazyDefinition(
|
|
101
|
+
typeProxy,
|
|
102
|
+
lazy
|
|
103
|
+
);
|
|
104
|
+
}
|
|
61
105
|
return typeProxy;
|
|
62
106
|
} as _Internal_TC_<TypeProxyInstance>;
|
|
63
107
|
|
|
@@ -199,17 +243,29 @@ TypeProxy.prototype.construct = function (this: TypeProxyInstance, _target: unkn
|
|
|
199
243
|
|
|
200
244
|
// new.target id equal with target here
|
|
201
245
|
|
|
202
|
-
const {
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
} = this;
|
|
246
|
+
const { __type__: type } = this;
|
|
247
|
+
|
|
248
|
+
const { ancestor } = this;
|
|
206
249
|
|
|
207
250
|
// so this is a direct Sub-Type invocation
|
|
208
251
|
// having no existentInstance created earlier
|
|
209
252
|
// then we should rely on that somehow
|
|
210
253
|
const parentProto = type.isSubType ? getDefaultPrototype() : ancestor;
|
|
211
254
|
|
|
212
|
-
|
|
255
|
+
// the lazy callback runs exactly ONCE per construction, here at the
|
|
256
|
+
// entry — unless the construction is about to be blocked (an errored
|
|
257
|
+
// parent with blockErrors on): the body path never runs user code for
|
|
258
|
+
// a blocked construction, so the callback must not run either. The
|
|
259
|
+
// pre-gather parentProto is enough for that check: on this path the
|
|
260
|
+
// facade still serves the define-time prototype (never an Error); only
|
|
261
|
+
// an explicit apply-path ancestor can be one
|
|
262
|
+
gatherLazyDefinition(type, parentProto);
|
|
263
|
+
|
|
264
|
+
// re-read after the gather: for a lazy root the ancestor facade now
|
|
265
|
+
// serves THIS construction's gathered prototype
|
|
266
|
+
const gatheredParentProto = type.isSubType ? getDefaultPrototype() : this.ancestor;
|
|
267
|
+
|
|
268
|
+
const mnemosyneProxy = createMnemosyne(gatheredParentProto);
|
|
213
269
|
const instance = new InstanceCreator(
|
|
214
270
|
type,
|
|
215
271
|
mnemosyneProxy,
|
|
@@ -50,6 +50,16 @@ export interface CreationHandler extends CallableFunction {
|
|
|
50
50
|
(this: object, answer: unknown): unknown;
|
|
51
51
|
}
|
|
52
52
|
|
|
53
|
+
// what a typeof === 'function' check proves — callability and nothing
|
|
54
|
+
// more; the SHAPE checks in classifyConstructHandler below are what make
|
|
55
|
+
// a candidate a ConstructHandler
|
|
56
|
+
export interface CallableCandidate extends CallableFunction {}
|
|
57
|
+
|
|
58
|
+
export const isCallableCandidate = ( value: unknown ): value is CallableCandidate => {
|
|
59
|
+
const result = typeof value === 'function';
|
|
60
|
+
return result;
|
|
61
|
+
};
|
|
62
|
+
|
|
53
63
|
// Classify the construct handler ONCE at define time from two cheap
|
|
54
64
|
// facts only — constructor.name and own prototype — NO toString()
|
|
55
65
|
// (overhead, fragile under transpilers). Async forms are NOT
|
|
@@ -59,10 +69,14 @@ export interface CreationHandler extends CallableFunction {
|
|
|
59
69
|
// function — the three are indistinguishable here and all are
|
|
60
70
|
// rejected with the readable error (the docs: write constructors as
|
|
61
71
|
// regular functions or classes).
|
|
62
|
-
|
|
63
|
-
|
|
72
|
+
// a function declaration: assertion signatures require one (TS2775)
|
|
73
|
+
export function classifyConstructHandler (
|
|
74
|
+
FunctionName: string,
|
|
75
|
+
handler: CallableCandidate
|
|
76
|
+
): asserts handler is ConstructHandler {
|
|
77
|
+
const handlerKind = handler.constructor.name;
|
|
64
78
|
const hasOwnPrototype = hop(
|
|
65
|
-
|
|
79
|
+
handler,
|
|
66
80
|
'prototype'
|
|
67
81
|
);
|
|
68
82
|
|
|
@@ -79,7 +93,7 @@ export const classifyConstructHandler = ( FunctionName: string, ConstructHandler
|
|
|
79
93
|
'(arrow functions, methods and bound functions are not supported)';
|
|
80
94
|
throw new WRONG_MODIFICATION_PATTERN( msg );
|
|
81
95
|
}
|
|
82
|
-
}
|
|
96
|
+
}
|
|
83
97
|
|
|
84
98
|
const getClassConstructor = (
|
|
85
99
|
ConstructHandler: ClassConstructHandler,
|
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,108 +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
|
-
// the getter's raw function cannot construct directly: it lacks the
|
|
507
|
-
// compiled modificator body — the type's name on prototype.constructor,
|
|
508
|
-
// the CreationHandler call, the Reflect.construct with new.target —
|
|
509
|
-
// which is why named lazy with an anonymous or differently named
|
|
510
|
-
// handler failed to construct, and async lazy handlers broke. Build
|
|
511
|
-
// through the SAME wrapper as the direct path, per construction: the
|
|
512
|
-
// getter may return a NEW function each time, so neither the shape
|
|
513
|
-
// check nor the wrapper can be cached. This is what makes lazy slower
|
|
514
|
-
// than a direct define
|
|
515
|
-
const modificatorBody = compileNewModificatorFunctionBody(
|
|
516
|
-
TypeName,
|
|
517
|
-
asClass
|
|
518
|
-
);
|
|
519
|
-
|
|
520
|
-
const makeConstructHandler = () => {
|
|
521
|
-
const constructHandler = getter();
|
|
522
|
-
|
|
523
|
-
classifyConstructHandler(
|
|
524
|
-
TypeName,
|
|
525
|
-
constructHandler as ConstructHandler
|
|
526
|
-
);
|
|
527
|
-
|
|
528
|
-
const wrappedBody = modificatorBody(
|
|
529
|
-
constructHandler as ConstructHandler,
|
|
530
|
-
CreationHandler,
|
|
531
|
-
SymbolConstructorName
|
|
532
|
-
);
|
|
533
|
-
const handlerResult = wrappedBody();
|
|
534
|
-
return handlerResult;
|
|
535
|
-
};
|
|
536
|
-
|
|
537
|
-
// 'name' is a COLLECTION option only (same rejection as the direct path)
|
|
538
|
-
if ( config !== undefined && typeof config === 'object' && 'name' in config ) {
|
|
539
|
-
const nameError = '\"name\" is a collection option ' +
|
|
540
|
-
'(createTypesCollection({ name })) — it cannot be set on a type';
|
|
541
|
-
throw new OPTIONS_ERROR( nameError );
|
|
542
|
-
}
|
|
543
|
-
config = Object.assign(
|
|
544
|
-
{},
|
|
545
|
-
config
|
|
546
|
-
);
|
|
547
|
-
|
|
548
|
-
config.asClass = asClass;
|
|
549
|
-
|
|
550
|
-
const result = new TypeDescriptor(
|
|
628
|
+
const result = createFromDirectHandler(
|
|
551
629
|
defineOrigin,
|
|
552
630
|
target,
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
// TypeClass contract is materialized dynamically by the Proxy
|
|
559
|
-
) as unknown as TypeClass;
|
|
631
|
+
name,
|
|
632
|
+
undefined,
|
|
633
|
+
config,
|
|
634
|
+
getter
|
|
635
|
+
);
|
|
560
636
|
return result;
|
|
561
637
|
};
|
|
562
638
|
|
|
@@ -639,36 +715,55 @@ export const define = function (
|
|
|
639
715
|
export const lazy = function (
|
|
640
716
|
this: unknown,
|
|
641
717
|
subtypes: TypesMap,
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
718
|
+
TypeName: string,
|
|
719
|
+
getter?: LazyDef<object>,
|
|
720
|
+
config?: object
|
|
645
721
|
): TypeClass {
|
|
646
722
|
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
if (typeof
|
|
651
|
-
|
|
652
|
-
getter = getterOrConfig as LazyTypeGetter;
|
|
653
|
-
config = namedFormConfig;
|
|
654
|
-
} else {
|
|
655
|
-
getter = TypeNameOrGetter as LazyTypeGetter;
|
|
656
|
-
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;
|
|
657
728
|
}
|
|
658
729
|
|
|
659
730
|
if (typeof getter !== 'function') {
|
|
660
731
|
throw new HANDLER_MUST_BE_A_FUNCTION;
|
|
661
732
|
}
|
|
662
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
|
+
|
|
663
748
|
if (!config || (typeof config !== 'object' && typeof config !== 'function')) {
|
|
664
749
|
config = {};
|
|
665
750
|
}
|
|
666
751
|
|
|
667
|
-
|
|
668
|
-
|
|
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(
|
|
669
758
|
subtypes,
|
|
759
|
+
TypeName
|
|
760
|
+
);
|
|
761
|
+
|
|
762
|
+
const lazyResult = createFromLazyDefinition(
|
|
763
|
+
this as TypeAbsorber,
|
|
764
|
+
target,
|
|
670
765
|
name,
|
|
671
|
-
getter
|
|
766
|
+
getter,
|
|
672
767
|
config as constructorOptions
|
|
673
768
|
);
|
|
674
769
|
return lazyResult;
|