mnemonica 1.4.8 → 1.5.0
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 +95 -31
- package/README.md +10 -9
- package/SKILL.md +4 -4
- package/build/api/hooks/hookTypes.d.ts +2 -0
- package/build/api/hooks/hookTypes.js +25 -0
- package/build/api/hooks/index.d.ts +2 -1
- package/build/api/hooks/index.js +6 -2
- package/build/api/hooks/registerHook.d.ts +1 -1
- package/build/api/hooks/registerHook.js +4 -15
- package/build/api/hooks/unregisterHook.d.ts +2 -0
- package/build/api/hooks/unregisterHook.js +18 -0
- package/build/api/index.d.ts +4 -3
- package/build/api/index.js +2 -1
- package/build/api/types/compileNewModificatorFunctionBody.d.ts +4 -2
- package/build/api/types/compileNewModificatorFunctionBody.js +16 -7
- package/build/api/types/index.d.ts +2 -2
- package/build/api/types/index.js +21 -18
- package/build/api/utils/index.d.ts +1 -2
- package/build/api/utils/index.js +37 -13
- package/build/descriptors/types/index.js +19 -12
- package/build/index.d.ts +25 -17
- package/build/index.js +35 -50
- package/build/types/index.d.ts +242 -225
- package/build/types/index.js +1 -1
- package/docs/UTILS.md +4 -5
- package/docs/async-constructors.md +32 -13
- package/docs/hott-correspondence.md +1 -1
- package/docs/typed-lookup.md +31 -37
- package/module/index.js +2 -0
- package/package.json +2 -2
- package/src/api/hooks/hookTypes.ts +37 -0
- package/src/api/hooks/index.ts +8 -1
- package/src/api/hooks/registerHook.ts +6 -19
- package/src/api/hooks/unregisterHook.ts +36 -0
- package/src/api/index.ts +2 -0
- package/src/api/types/compileNewModificatorFunctionBody.ts +79 -18
- package/src/api/types/index.ts +64 -54
- package/src/api/utils/index.ts +5 -51
- package/src/descriptors/types/index.ts +31 -22
- package/src/index.ts +271 -161
- package/src/types/index.ts +812 -590
package/src/index.ts
CHANGED
|
@@ -6,7 +6,12 @@ import type {
|
|
|
6
6
|
CtorParameter,
|
|
7
7
|
IDEF,
|
|
8
8
|
hooksTypes,
|
|
9
|
-
|
|
9
|
+
hookName,
|
|
10
|
+
HookMethods,
|
|
11
|
+
AnyHookCallback,
|
|
12
|
+
AsyncIDEF,
|
|
13
|
+
DefineConfig,
|
|
14
|
+
NoHandlerConfig,
|
|
10
15
|
Proto,
|
|
11
16
|
IDefinitorInstance,
|
|
12
17
|
Constructor,
|
|
@@ -17,20 +22,15 @@ import type {
|
|
|
17
22
|
LazyAbsorber,
|
|
18
23
|
MnemonicaModule,
|
|
19
24
|
InstanceResult,
|
|
25
|
+
NotAny,
|
|
20
26
|
Merge,
|
|
21
|
-
TypeLookup,
|
|
22
|
-
RegistryHolderBase,
|
|
23
|
-
RegistryEntry,
|
|
24
27
|
GlobalRegistry,
|
|
25
|
-
LookedUpConstructor,
|
|
26
28
|
FreeLookupResult,
|
|
27
|
-
WithHookRegister,
|
|
28
29
|
typedHook,
|
|
29
30
|
HookableConstructor,
|
|
30
31
|
DefineNewableOrCallable,
|
|
31
32
|
LookupSource,
|
|
32
|
-
LazyDef
|
|
33
|
-
ApplyFunction
|
|
33
|
+
LazyDef
|
|
34
34
|
} from './types';
|
|
35
35
|
|
|
36
36
|
import TypesUtils from './api/utils/index';
|
|
@@ -49,10 +49,13 @@ export type {
|
|
|
49
49
|
Proto,
|
|
50
50
|
ProtoFlat,
|
|
51
51
|
constructorOptions,
|
|
52
|
+
DefineConfig,
|
|
52
53
|
deliveredHookOpts,
|
|
53
54
|
hooksOpts,
|
|
54
55
|
hook,
|
|
55
56
|
hooksTypes,
|
|
57
|
+
hookName,
|
|
58
|
+
HookMethods,
|
|
56
59
|
IDefinitorInstance,
|
|
57
60
|
InstanceResult,
|
|
58
61
|
Merge,
|
|
@@ -148,28 +151,27 @@ function checkThis(pointer: typeof mnemonica | typeof exports | unknown): boolea
|
|
|
148
151
|
|
|
149
152
|
|
|
150
153
|
// Define function using TypeAbsorber interface with proper type casting
|
|
154
|
+
// Three forms, the config is always the last argument:
|
|
155
|
+
// define(TypeName, constructHandler, config?)
|
|
156
|
+
// define(TypeName, config?) no handler
|
|
157
|
+
// define(constructHandler, config?) TypeName is its own name
|
|
158
|
+
// An async handler precedes IDEF, which accepts it too, its void result
|
|
159
|
+
// erasing the Promise. A `=> never` handler (one that only throws) precedes
|
|
160
|
+
// the async one, whose Promise it would match, and so would an `any` handler
|
|
151
161
|
export function define <
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
Path extends string,
|
|
155
|
-
const Name extends string,
|
|
156
|
-
N extends object,
|
|
157
|
-
Args extends unknown[] = unknown[],
|
|
158
|
-
F extends Proto<Parent, N> = Proto<Parent, N>,
|
|
159
|
-
ChildPath extends string = Path extends '' ? Name : `${Path}.${Name}`
|
|
162
|
+
T extends object,
|
|
163
|
+
Args extends unknown[] = unknown[]
|
|
160
164
|
>(
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
constructHandler?: IDEF<N, Args>,
|
|
166
|
-
config?: constructorOptions
|
|
165
|
+
this: unknown,
|
|
166
|
+
TypeName: string,
|
|
167
|
+
constructHandler: (this: T, ...args: Args) => never,
|
|
168
|
+
config?: DefineConfig
|
|
167
169
|
): IDefinitorInstance<
|
|
168
|
-
|
|
169
|
-
InstanceResult<
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
170
|
+
T,
|
|
171
|
+
InstanceResult<T>,
|
|
172
|
+
GlobalRegistry,
|
|
173
|
+
'',
|
|
174
|
+
object,
|
|
173
175
|
Args
|
|
174
176
|
>;
|
|
175
177
|
export function define <
|
|
@@ -177,9 +179,25 @@ export function define <
|
|
|
177
179
|
Args extends unknown[] = unknown[]
|
|
178
180
|
>(
|
|
179
181
|
this: unknown,
|
|
180
|
-
TypeName
|
|
181
|
-
constructHandler
|
|
182
|
-
config?:
|
|
182
|
+
TypeName: string,
|
|
183
|
+
constructHandler: AsyncIDEF<T, Args>,
|
|
184
|
+
config?: DefineConfig
|
|
185
|
+
): IDefinitorInstance<
|
|
186
|
+
T,
|
|
187
|
+
InstanceResult<T>,
|
|
188
|
+
GlobalRegistry,
|
|
189
|
+
'',
|
|
190
|
+
object,
|
|
191
|
+
Args,
|
|
192
|
+
Promise<InstanceResult<T>>
|
|
193
|
+
>;
|
|
194
|
+
export function define <
|
|
195
|
+
T extends object,
|
|
196
|
+
Args extends unknown[] = unknown[]
|
|
197
|
+
>(
|
|
198
|
+
this: unknown,
|
|
199
|
+
constructHandler: (this: T, ...args: Args) => never,
|
|
200
|
+
config?: DefineConfig
|
|
183
201
|
): IDefinitorInstance<
|
|
184
202
|
T,
|
|
185
203
|
InstanceResult<T>,
|
|
@@ -188,6 +206,69 @@ export function define <
|
|
|
188
206
|
object,
|
|
189
207
|
Args
|
|
190
208
|
>;
|
|
209
|
+
export function define <
|
|
210
|
+
T extends object,
|
|
211
|
+
Args extends unknown[] = unknown[]
|
|
212
|
+
>(
|
|
213
|
+
this: unknown,
|
|
214
|
+
constructHandler: AsyncIDEF<T, Args>,
|
|
215
|
+
config?: DefineConfig
|
|
216
|
+
): IDefinitorInstance<
|
|
217
|
+
T,
|
|
218
|
+
InstanceResult<T>,
|
|
219
|
+
GlobalRegistry,
|
|
220
|
+
'',
|
|
221
|
+
object,
|
|
222
|
+
Args,
|
|
223
|
+
Promise<InstanceResult<T>>
|
|
224
|
+
>;
|
|
225
|
+
export function define <
|
|
226
|
+
T extends object,
|
|
227
|
+
Args extends unknown[] = unknown[]
|
|
228
|
+
>(
|
|
229
|
+
this: unknown,
|
|
230
|
+
TypeName: string,
|
|
231
|
+
constructHandler: IDEF<T, Args> | undefined,
|
|
232
|
+
config?: DefineConfig
|
|
233
|
+
): IDefinitorInstance<
|
|
234
|
+
T,
|
|
235
|
+
InstanceResult<T>,
|
|
236
|
+
GlobalRegistry,
|
|
237
|
+
'',
|
|
238
|
+
object,
|
|
239
|
+
Args
|
|
240
|
+
>;
|
|
241
|
+
export function define <
|
|
242
|
+
T extends object,
|
|
243
|
+
Args extends unknown[] = unknown[]
|
|
244
|
+
>(
|
|
245
|
+
this: unknown,
|
|
246
|
+
TypeName: string,
|
|
247
|
+
config?: NoHandlerConfig
|
|
248
|
+
): IDefinitorInstance<
|
|
249
|
+
T,
|
|
250
|
+
InstanceResult<T>,
|
|
251
|
+
GlobalRegistry,
|
|
252
|
+
'',
|
|
253
|
+
object,
|
|
254
|
+
Args
|
|
255
|
+
>;
|
|
256
|
+
export function define <
|
|
257
|
+
T extends object,
|
|
258
|
+
Args extends unknown[] = unknown[]
|
|
259
|
+
>(
|
|
260
|
+
this: unknown,
|
|
261
|
+
constructHandler: IDEF<T, Args>,
|
|
262
|
+
config?: DefineConfig
|
|
263
|
+
): IDefinitorInstance<
|
|
264
|
+
T,
|
|
265
|
+
InstanceResult<T>,
|
|
266
|
+
GlobalRegistry,
|
|
267
|
+
'',
|
|
268
|
+
object,
|
|
269
|
+
Args
|
|
270
|
+
>;
|
|
271
|
+
// the same three forms for explicit generics and flexible handlers
|
|
191
272
|
export function define <
|
|
192
273
|
T extends object,
|
|
193
274
|
P extends object = object,
|
|
@@ -203,98 +284,112 @@ export function define <
|
|
|
203
284
|
> = IDefinitorInstance<N>,
|
|
204
285
|
>(
|
|
205
286
|
this: unknown,
|
|
206
|
-
TypeName
|
|
287
|
+
TypeName: string,
|
|
207
288
|
// Allow both strict IDEF and more flexible function signatures
|
|
208
|
-
constructHandler
|
|
209
|
-
config?:
|
|
289
|
+
constructHandler: IDEF<T> | DefineNewableOrCallable | undefined,
|
|
290
|
+
config?: DefineConfig,
|
|
291
|
+
): R;
|
|
292
|
+
export function define <
|
|
293
|
+
T extends object,
|
|
294
|
+
P extends object = object,
|
|
295
|
+
N extends Proto<P, T> = Proto<P, T>,
|
|
296
|
+
R extends IDefinitorInstance<
|
|
297
|
+
N,
|
|
298
|
+
InstanceResult<N>,
|
|
299
|
+
GlobalRegistry,
|
|
300
|
+
'',
|
|
301
|
+
object,
|
|
302
|
+
never[]
|
|
303
|
+
> = IDefinitorInstance<N>,
|
|
304
|
+
>(
|
|
305
|
+
this: unknown,
|
|
306
|
+
TypeName: string,
|
|
307
|
+
config?: NoHandlerConfig,
|
|
210
308
|
): R;
|
|
309
|
+
export function define <
|
|
310
|
+
T extends object,
|
|
311
|
+
P extends object = object,
|
|
312
|
+
N extends Proto<P, T> = Proto<P, T>,
|
|
313
|
+
R extends IDefinitorInstance<
|
|
314
|
+
N,
|
|
315
|
+
InstanceResult<N>,
|
|
316
|
+
GlobalRegistry,
|
|
317
|
+
'',
|
|
318
|
+
object,
|
|
319
|
+
never[]
|
|
320
|
+
> = IDefinitorInstance<N>,
|
|
321
|
+
>(
|
|
322
|
+
this: unknown,
|
|
323
|
+
constructHandler: IDEF<T> | DefineNewableOrCallable,
|
|
324
|
+
config?: DefineConfig,
|
|
325
|
+
): R;
|
|
326
|
+
// `this` is the collection: defaultTypes for a plain call, the given one for
|
|
327
|
+
// define.call(collection, …); the arguments pass through as they are
|
|
211
328
|
export function define <T extends object>(
|
|
212
329
|
this: unknown,
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
config?: constructorOptions,
|
|
330
|
+
TypeOrTypeName: string | DefineNewableOrCallable,
|
|
331
|
+
constructHandlerOrConfig?: IDEF<T> | DefineNewableOrCallable | DefineConfig,
|
|
332
|
+
config?: DefineConfig,
|
|
217
333
|
): IDefinitorInstance<T> {
|
|
218
|
-
|
|
219
|
-
// explicit-source form: define(source, name, handler, config?)
|
|
220
|
-
// the source may be a TypesCollection (object) or a TypeProxy (function),
|
|
221
|
-
// so detection is: has a callable .define AND the next arg is a string name
|
|
222
|
-
const sourceCandidate = TypeNameOrSource as { define?: unknown };
|
|
223
|
-
const mayBeSource = (
|
|
224
|
-
(typeof TypeNameOrSource === 'object' && TypeNameOrSource !== null) ||
|
|
225
|
-
typeof TypeNameOrSource === 'function'
|
|
226
|
-
) && typeof sourceCandidate.define === 'function';
|
|
227
|
-
if (mayBeSource && typeof constructHandlerOrName === 'string') {
|
|
228
|
-
// .define was verified callable above; the cast only adds its signature
|
|
229
|
-
const source = sourceCandidate as { define: TypeAbsorber };
|
|
230
|
-
// The implementation returns the delegated constructor directly;
|
|
231
|
-
// caller-specific R views live only on the overloads.
|
|
232
|
-
const sourceDefineResult = source.define(
|
|
233
|
-
constructHandlerOrName,
|
|
234
|
-
configOrHandler as IDEF<T>,
|
|
235
|
-
config
|
|
236
|
-
);
|
|
237
|
-
return sourceDefineResult;
|
|
238
|
-
}
|
|
239
|
-
|
|
240
334
|
const types = checkThis(this) ? defaultTypes : this || defaultTypes;
|
|
241
335
|
// Type assertion needed because TypesCollectionProxy is a Proxy
|
|
242
336
|
// The implementation returns the delegated constructor directly.
|
|
243
337
|
const defineResult = (types as { define: TypeAbsorber })
|
|
244
338
|
.define(
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
339
|
+
TypeOrTypeName as string,
|
|
340
|
+
constructHandlerOrConfig as IDEF<T>,
|
|
341
|
+
config
|
|
248
342
|
);
|
|
249
343
|
return defineResult;
|
|
250
344
|
}
|
|
251
345
|
|
|
346
|
+
// the same order as in define() above
|
|
252
347
|
export function lazy<T extends object, Args extends unknown[] = unknown[]>(
|
|
253
|
-
|
|
254
|
-
source: RegistryHolderBase<object, object, string>,
|
|
348
|
+
this: unknown,
|
|
255
349
|
TypeName: string,
|
|
256
|
-
getter:
|
|
257
|
-
config?:
|
|
258
|
-
): IDefinitorInstance<
|
|
350
|
+
getter: () => (this: T, ...args: Args) => never,
|
|
351
|
+
config?: DefineConfig
|
|
352
|
+
): IDefinitorInstance<
|
|
353
|
+
T,
|
|
354
|
+
InstanceResult<T>,
|
|
355
|
+
GlobalRegistry,
|
|
356
|
+
'',
|
|
357
|
+
object,
|
|
358
|
+
Args
|
|
359
|
+
>;
|
|
360
|
+
export function lazy<T extends object, Args extends unknown[] = unknown[]>(
|
|
361
|
+
this: unknown,
|
|
362
|
+
TypeName: string,
|
|
363
|
+
getter: () => AsyncIDEF<T, Args>,
|
|
364
|
+
config?: DefineConfig
|
|
365
|
+
): IDefinitorInstance<
|
|
366
|
+
T,
|
|
367
|
+
InstanceResult<T>,
|
|
368
|
+
GlobalRegistry,
|
|
369
|
+
'',
|
|
370
|
+
object,
|
|
371
|
+
Args,
|
|
372
|
+
Promise<InstanceResult<T>>
|
|
373
|
+
>;
|
|
259
374
|
export function lazy<T extends object, Args extends unknown[] = unknown[]>(
|
|
260
|
-
// named form: lazy(TypeName, getter, config?)
|
|
261
375
|
this: unknown,
|
|
262
376
|
TypeName: string,
|
|
263
377
|
getter: LazyDef<T, Args>,
|
|
264
|
-
config?:
|
|
378
|
+
config?: DefineConfig
|
|
265
379
|
): IDefinitorInstance<T, InstanceResult<T>, GlobalRegistry, '', object, Args>;
|
|
266
380
|
export function lazy(
|
|
267
381
|
this: unknown,
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
config?: unknown
|
|
382
|
+
TypeName: string,
|
|
383
|
+
getter: unknown,
|
|
384
|
+
config?: DefineConfig
|
|
272
385
|
): unknown {
|
|
273
|
-
|
|
274
|
-
// explicit-source form: lazy(source, TypeName, getter, config?)
|
|
275
|
-
// the source may be a TypesCollection (object) or a TypeProxy (function),
|
|
276
|
-
// so detection is: has a callable .lazy
|
|
277
|
-
const mayBeSource = (
|
|
278
|
-
(typeof sourceOrTypeName === 'object' && sourceOrTypeName !== null) ||
|
|
279
|
-
typeof sourceOrTypeName === 'function'
|
|
280
|
-
) && typeof (sourceOrTypeName as { lazy?: unknown }).lazy === 'function';
|
|
281
|
-
if (mayBeSource) {
|
|
282
|
-
const source = sourceOrTypeName as { lazy: LazyAbsorber };
|
|
283
|
-
const sourceLazyResult = source.lazy(
|
|
284
|
-
TypeNameOrGetter as string,
|
|
285
|
-
getterOrConfig as LazyDef<object>,
|
|
286
|
-
config as constructorOptions
|
|
287
|
-
) as unknown;
|
|
288
|
-
return sourceLazyResult;
|
|
289
|
-
}
|
|
290
|
-
|
|
291
386
|
const types = checkThis(this) ? defaultTypes : this || defaultTypes;
|
|
292
387
|
// Type assertion needed because TypesCollectionProxy is a Proxy
|
|
293
388
|
const lazyResult = (types as { lazy: LazyAbsorber })
|
|
294
389
|
.lazy(
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
390
|
+
TypeName,
|
|
391
|
+
getter as LazyDef<object>,
|
|
392
|
+
config
|
|
298
393
|
) as unknown;
|
|
299
394
|
return lazyResult;
|
|
300
395
|
}
|
|
@@ -303,133 +398,135 @@ export function lookup<const K extends keyof TypeRegistry>(
|
|
|
303
398
|
this: unknown,
|
|
304
399
|
TypeNestedPath: K
|
|
305
400
|
): FreeLookupResult<K>;
|
|
306
|
-
export function lookup<Reg extends object, const K extends keyof Reg & string>(
|
|
307
|
-
// explicit-source form: lookup(source, path)
|
|
308
|
-
// resolves against the registry carried by a builder/collection value
|
|
309
|
-
source: { lookup: TypeLookup<Reg> },
|
|
310
|
-
TypeNestedPath: K
|
|
311
|
-
): WithHookRegister<LookedUpConstructor<Reg, K>>;
|
|
312
401
|
export function lookup(this: unknown, TypeNestedPath: string): TypeClass | undefined;
|
|
313
|
-
export function lookup(
|
|
314
|
-
source: LookupSource,
|
|
315
|
-
TypeNestedPath: string
|
|
316
|
-
): TypeClass | undefined;
|
|
317
402
|
export function lookup(
|
|
318
403
|
this: unknown,
|
|
319
|
-
|
|
320
|
-
TypeNestedPath?: unknown
|
|
404
|
+
TypeNestedPath: string
|
|
321
405
|
): unknown {
|
|
322
|
-
|
|
323
|
-
// explicit-source form: lookup(source, path)
|
|
324
|
-
if (typeof sourceOrPath !== 'string' && typeof TypeNestedPath === 'string') {
|
|
325
|
-
const source = sourceOrPath as LookupSource;
|
|
326
|
-
const sourceResult = source.lookup(TypeNestedPath);
|
|
327
|
-
return sourceResult;
|
|
328
|
-
}
|
|
329
|
-
|
|
330
406
|
const types = checkThis(this) ? defaultTypes : this || defaultTypes;
|
|
331
407
|
// Type assertion needed because TypesCollectionProxy is a Proxy
|
|
332
|
-
const lookupResult = (types as LookupSource).lookup(
|
|
408
|
+
const lookupResult = (types as LookupSource).lookup(TypeNestedPath);
|
|
333
409
|
return lookupResult;
|
|
334
410
|
}
|
|
335
411
|
|
|
336
412
|
|
|
337
|
-
|
|
413
|
+
// returns the constructed instance as `object`: apply/call/bind name it as
|
|
414
|
+
// their public merged result type
|
|
415
|
+
const $run = function <E extends object, T extends object, Args extends unknown[]>(
|
|
338
416
|
entity: E,
|
|
339
417
|
Ctor: CtorParameter<T, Args>,
|
|
340
418
|
args: unknown[]
|
|
341
|
-
): {
|
|
342
|
-
[key in keyof S]: S[key]
|
|
343
|
-
} {
|
|
419
|
+
): object {
|
|
344
420
|
|
|
345
421
|
// debugger;
|
|
346
422
|
// CtorParameter deliberately accepts constructors without a TypeName
|
|
347
|
-
// declaration; runtime mnemonica constructors carry it.
|
|
348
|
-
//
|
|
349
|
-
//
|
|
350
|
-
const
|
|
423
|
+
// declaration; runtime mnemonica constructors carry it. A constructor
|
|
424
|
+
// without one (a plain class or function) resolves no subtype and
|
|
425
|
+
// throws the same error below as an unknown TypeName
|
|
426
|
+
const TypeName: unknown = Reflect.get(
|
|
427
|
+
Ctor,
|
|
428
|
+
'TypeName'
|
|
429
|
+
);
|
|
351
430
|
// the returned SubTypeProxy is a plain function at runtime — constructable —
|
|
352
431
|
// so TypeConstructorBase (the named minimal constructor shape) is its
|
|
353
432
|
// honest static type here
|
|
354
|
-
const Cstr =
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
433
|
+
const Cstr = typeof TypeName === 'string' ?
|
|
434
|
+
prepareSubtypeForConstruction(
|
|
435
|
+
TypeName,
|
|
436
|
+
entity
|
|
437
|
+
) as TypeConstructorBase :
|
|
438
|
+
undefined;
|
|
358
439
|
if (Cstr === undefined) {
|
|
359
440
|
throw new WRONG_MODIFICATION_PATTERN(`[ ${TypeName} ] is not defined as a Type Constructor on used instance`);
|
|
360
441
|
}
|
|
361
442
|
const runResult = new Cstr(...args);
|
|
362
|
-
// new Cstr returns object here; S is the caller's merged entity/constructor
|
|
363
|
-
// shape and cannot be recovered by narrowing without changing the public
|
|
364
|
-
// result type, so the return stays suppressed.
|
|
365
|
-
// @ts-expect-error -- the merged result shape cannot be recovered by narrowing
|
|
366
443
|
return runResult;
|
|
367
444
|
};
|
|
368
445
|
|
|
369
446
|
// TODO: apply instance .to type .with arguments
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
>
|
|
447
|
+
// the overloads carry the merged entity+ctor shape, or its Promise for an
|
|
448
|
+
// async constructor; the implementation returns what $run constructs
|
|
449
|
+
export function apply <E extends object, C extends Constructor<PromiseLike<object>>>(
|
|
450
|
+
entity: E,
|
|
451
|
+
Ctor: C & NotAny<C>,
|
|
452
|
+
... [ args ]: [] extends ConstructorParameters<C>
|
|
453
|
+
? [args?: ConstructorParameters<C>]
|
|
454
|
+
: [args: ConstructorParameters<C>]
|
|
455
|
+
): Promise<InstanceResult<Merge<E, Awaited<InstanceType<C>>>>>;
|
|
456
|
+
export function apply <E extends object, T extends object, Args extends unknown[]>(
|
|
457
|
+
entity: E,
|
|
458
|
+
Ctor: CtorParameter<T, Args>,
|
|
459
|
+
... [ args ]: [] extends Args ? [args?: NoInfer<Args>] : [args: NoInfer<Args>]
|
|
460
|
+
): InstanceResult<Merge<E, T>>;
|
|
461
|
+
export function apply <E extends object, T extends object, Args extends unknown[]>(
|
|
376
462
|
entity: E,
|
|
377
463
|
Ctor: CtorParameter<T, Args>,
|
|
378
464
|
args: unknown[] = []
|
|
379
|
-
):
|
|
380
|
-
const
|
|
465
|
+
): object {
|
|
466
|
+
const result = $run<E, T, Args>(
|
|
381
467
|
entity,
|
|
382
468
|
Ctor,
|
|
383
469
|
args
|
|
384
470
|
);
|
|
385
|
-
// S extends Proto<E, T>, which is the same merged entity+ctor shape
|
|
386
|
-
// apply/call/bind promise — the named public result type
|
|
387
|
-
const result = runResult as InstanceResult<Merge<E, T>>;
|
|
388
471
|
return result;
|
|
389
|
-
}
|
|
472
|
+
}
|
|
390
473
|
|
|
391
474
|
// TODO: call type .by instance .with arguments
|
|
392
|
-
export
|
|
475
|
+
export function call <E extends object, C extends Constructor<PromiseLike<object>>>(
|
|
476
|
+
entity: E,
|
|
477
|
+
Ctor: C & NotAny<C>,
|
|
478
|
+
...args: ConstructorParameters<C>
|
|
479
|
+
): Promise<InstanceResult<Merge<E, Awaited<InstanceType<C>>>>>;
|
|
480
|
+
export function call <E extends object, T extends object, Args extends unknown[]>(
|
|
393
481
|
entity: E,
|
|
394
482
|
Ctor: CtorParameter<T, Args>,
|
|
395
483
|
...args: NoInfer<Args>
|
|
396
|
-
): InstanceResult<Merge<E, T
|
|
397
|
-
|
|
484
|
+
): InstanceResult<Merge<E, T>>;
|
|
485
|
+
export function call <E extends object, T extends object, Args extends unknown[]>(
|
|
486
|
+
entity: E,
|
|
487
|
+
Ctor: CtorParameter<T, Args>,
|
|
488
|
+
...args: unknown[]
|
|
489
|
+
): object {
|
|
490
|
+
const result = $run<E, T, Args>(
|
|
398
491
|
entity,
|
|
399
492
|
Ctor,
|
|
400
493
|
args
|
|
401
494
|
);
|
|
402
|
-
// same merge narrowing as apply() above
|
|
403
|
-
const result = runResult as InstanceResult<Merge<E, T>>;
|
|
404
495
|
return result;
|
|
405
|
-
}
|
|
496
|
+
}
|
|
406
497
|
|
|
407
498
|
// TODO: bind type .with instance → (...args)
|
|
408
|
-
export
|
|
499
|
+
export function bind <E extends object, C extends Constructor<PromiseLike<object>>>(
|
|
500
|
+
entity: E,
|
|
501
|
+
Ctor: C & NotAny<C>
|
|
502
|
+
): (...args: ConstructorParameters<C>) => Promise<InstanceResult<Merge<E, Awaited<InstanceType<C>>>>>;
|
|
503
|
+
export function bind <E extends object, T extends object, Args extends unknown[]>(
|
|
504
|
+
entity: E,
|
|
505
|
+
Ctor: CtorParameter<T, Args>
|
|
506
|
+
): (...args: Args) => InstanceResult<Merge<E, T>>;
|
|
507
|
+
export function bind <E extends object, T extends object, Args extends unknown[]>(
|
|
409
508
|
entity: E,
|
|
410
509
|
Ctor: CtorParameter<T, Args>
|
|
411
|
-
): (...args: Args) =>
|
|
510
|
+
): (...args: Args) => object {
|
|
412
511
|
const result = (...args: Args) => {
|
|
413
|
-
const runResult = $run<E, T,
|
|
512
|
+
const runResult = $run<E, T, Args>(
|
|
414
513
|
entity,
|
|
415
514
|
Ctor,
|
|
416
515
|
args
|
|
417
516
|
);
|
|
418
|
-
|
|
419
|
-
const typedResult = runResult as InstanceResult<Merge<E, T>>;
|
|
420
|
-
return typedResult;
|
|
517
|
+
return runResult;
|
|
421
518
|
};
|
|
422
519
|
return result;
|
|
423
|
-
}
|
|
520
|
+
}
|
|
424
521
|
|
|
425
522
|
export const decorate = function <
|
|
426
|
-
T extends Constructor<object> |
|
|
523
|
+
T extends Constructor<object> | NoHandlerConfig | undefined = undefined
|
|
427
524
|
>(
|
|
428
525
|
target?: T,
|
|
429
|
-
config?:
|
|
526
|
+
config?: DefineConfig
|
|
430
527
|
): <U extends Constructor<object>>(cstr: U) => DecoratedClass<U> {
|
|
431
|
-
const opts = (config === undefined &&
|
|
432
|
-
? target as
|
|
528
|
+
const opts = (config === undefined && !(target instanceof Function))
|
|
529
|
+
? target as NoHandlerConfig | undefined
|
|
433
530
|
: config;
|
|
434
531
|
|
|
435
532
|
// decorate(SomeType, config?) expects a constructor already carrying
|
|
@@ -469,7 +566,7 @@ export const decorate = function <
|
|
|
469
566
|
|
|
470
567
|
export const registerHook = function <
|
|
471
568
|
T extends object,
|
|
472
|
-
HT extends
|
|
569
|
+
HT extends hookName = hooksTypes,
|
|
473
570
|
>(
|
|
474
571
|
Ctor: HookableConstructor<T>,
|
|
475
572
|
hookType: HT,
|
|
@@ -481,6 +578,18 @@ export const registerHook = function <
|
|
|
481
578
|
);
|
|
482
579
|
};
|
|
483
580
|
|
|
581
|
+
export const unregisterHook = function (
|
|
582
|
+
Ctor: Pick<HookMethods, 'unregisterHook'>,
|
|
583
|
+
hookType: hookName,
|
|
584
|
+
cb: AnyHookCallback
|
|
585
|
+
): boolean {
|
|
586
|
+
const result = Ctor.unregisterHook(
|
|
587
|
+
hookType,
|
|
588
|
+
cb
|
|
589
|
+
);
|
|
590
|
+
return result;
|
|
591
|
+
};
|
|
592
|
+
|
|
484
593
|
export const mnemonica = Object.entries({
|
|
485
594
|
|
|
486
595
|
define,
|
|
@@ -491,6 +600,7 @@ export const mnemonica = Object.entries({
|
|
|
491
600
|
bind,
|
|
492
601
|
decorate,
|
|
493
602
|
registerHook,
|
|
603
|
+
unregisterHook,
|
|
494
604
|
|
|
495
605
|
...descriptors,
|
|
496
606
|
|