mnemonica 1.4.9 → 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 +76 -13
- package/README.md +1 -1
- 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/index.d.ts +2 -2
- package/build/api/types/index.js +19 -16
- package/build/descriptors/types/index.js +19 -12
- package/build/index.d.ts +24 -10
- package/build/index.js +27 -24
- package/build/types/index.d.ts +66 -39
- package/build/types/index.js +1 -1
- package/docs/UTILS.md +1 -0
- package/docs/async-constructors.md +24 -8
- package/docs/typed-lookup.md +8 -8
- package/module/index.js +2 -0
- package/package.json +1 -1
- 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/index.ts +44 -33
- package/src/descriptors/types/index.ts +31 -22
- package/src/index.ts +251 -46
- package/src/types/index.ts +309 -80
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,6 +22,7 @@ import type {
|
|
|
17
22
|
LazyAbsorber,
|
|
18
23
|
MnemonicaModule,
|
|
19
24
|
InstanceResult,
|
|
25
|
+
NotAny,
|
|
20
26
|
Merge,
|
|
21
27
|
GlobalRegistry,
|
|
22
28
|
FreeLookupResult,
|
|
@@ -24,8 +30,7 @@ import type {
|
|
|
24
30
|
HookableConstructor,
|
|
25
31
|
DefineNewableOrCallable,
|
|
26
32
|
LookupSource,
|
|
27
|
-
LazyDef
|
|
28
|
-
ApplyFunction
|
|
33
|
+
LazyDef
|
|
29
34
|
} from './types';
|
|
30
35
|
|
|
31
36
|
import TypesUtils from './api/utils/index';
|
|
@@ -44,10 +49,13 @@ export type {
|
|
|
44
49
|
Proto,
|
|
45
50
|
ProtoFlat,
|
|
46
51
|
constructorOptions,
|
|
52
|
+
DefineConfig,
|
|
47
53
|
deliveredHookOpts,
|
|
48
54
|
hooksOpts,
|
|
49
55
|
hook,
|
|
50
56
|
hooksTypes,
|
|
57
|
+
hookName,
|
|
58
|
+
HookMethods,
|
|
51
59
|
IDefinitorInstance,
|
|
52
60
|
InstanceResult,
|
|
53
61
|
Merge,
|
|
@@ -143,14 +151,115 @@ function checkThis(pointer: typeof mnemonica | typeof exports | unknown): boolea
|
|
|
143
151
|
|
|
144
152
|
|
|
145
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
|
|
161
|
+
export function define <
|
|
162
|
+
T extends object,
|
|
163
|
+
Args extends unknown[] = unknown[]
|
|
164
|
+
>(
|
|
165
|
+
this: unknown,
|
|
166
|
+
TypeName: string,
|
|
167
|
+
constructHandler: (this: T, ...args: Args) => never,
|
|
168
|
+
config?: DefineConfig
|
|
169
|
+
): IDefinitorInstance<
|
|
170
|
+
T,
|
|
171
|
+
InstanceResult<T>,
|
|
172
|
+
GlobalRegistry,
|
|
173
|
+
'',
|
|
174
|
+
object,
|
|
175
|
+
Args
|
|
176
|
+
>;
|
|
177
|
+
export function define <
|
|
178
|
+
T extends object,
|
|
179
|
+
Args extends unknown[] = unknown[]
|
|
180
|
+
>(
|
|
181
|
+
this: unknown,
|
|
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
|
|
201
|
+
): IDefinitorInstance<
|
|
202
|
+
T,
|
|
203
|
+
InstanceResult<T>,
|
|
204
|
+
GlobalRegistry,
|
|
205
|
+
'',
|
|
206
|
+
object,
|
|
207
|
+
Args
|
|
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
|
+
>;
|
|
146
256
|
export function define <
|
|
147
257
|
T extends object,
|
|
148
258
|
Args extends unknown[] = unknown[]
|
|
149
259
|
>(
|
|
150
260
|
this: unknown,
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
config?: constructorOptions
|
|
261
|
+
constructHandler: IDEF<T, Args>,
|
|
262
|
+
config?: DefineConfig
|
|
154
263
|
): IDefinitorInstance<
|
|
155
264
|
T,
|
|
156
265
|
InstanceResult<T>,
|
|
@@ -159,6 +268,7 @@ export function define <
|
|
|
159
268
|
object,
|
|
160
269
|
Args
|
|
161
270
|
>;
|
|
271
|
+
// the same three forms for explicit generics and flexible handlers
|
|
162
272
|
export function define <
|
|
163
273
|
T extends object,
|
|
164
274
|
P extends object = object,
|
|
@@ -174,43 +284,104 @@ export function define <
|
|
|
174
284
|
> = IDefinitorInstance<N>,
|
|
175
285
|
>(
|
|
176
286
|
this: unknown,
|
|
177
|
-
TypeName
|
|
287
|
+
TypeName: string,
|
|
178
288
|
// Allow both strict IDEF and more flexible function signatures
|
|
179
|
-
constructHandler
|
|
180
|
-
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,
|
|
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,
|
|
181
325
|
): R;
|
|
182
326
|
// `this` is the collection: defaultTypes for a plain call, the given one for
|
|
183
|
-
// define.call(collection, …);
|
|
184
|
-
// itself (TypeName from its own name)
|
|
327
|
+
// define.call(collection, …); the arguments pass through as they are
|
|
185
328
|
export function define <T extends object>(
|
|
186
329
|
this: unknown,
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
config?:
|
|
330
|
+
TypeOrTypeName: string | DefineNewableOrCallable,
|
|
331
|
+
constructHandlerOrConfig?: IDEF<T> | DefineNewableOrCallable | DefineConfig,
|
|
332
|
+
config?: DefineConfig,
|
|
190
333
|
): IDefinitorInstance<T> {
|
|
191
334
|
const types = checkThis(this) ? defaultTypes : this || defaultTypes;
|
|
192
335
|
// Type assertion needed because TypesCollectionProxy is a Proxy
|
|
193
336
|
// The implementation returns the delegated constructor directly.
|
|
194
337
|
const defineResult = (types as { define: TypeAbsorber })
|
|
195
338
|
.define(
|
|
196
|
-
|
|
197
|
-
|
|
339
|
+
TypeOrTypeName as string,
|
|
340
|
+
constructHandlerOrConfig as IDEF<T>,
|
|
198
341
|
config
|
|
199
342
|
);
|
|
200
343
|
return defineResult;
|
|
201
344
|
}
|
|
202
345
|
|
|
346
|
+
// the same order as in define() above
|
|
347
|
+
export function lazy<T extends object, Args extends unknown[] = unknown[]>(
|
|
348
|
+
this: unknown,
|
|
349
|
+
TypeName: string,
|
|
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
|
+
>;
|
|
203
374
|
export function lazy<T extends object, Args extends unknown[] = unknown[]>(
|
|
204
375
|
this: unknown,
|
|
205
376
|
TypeName: string,
|
|
206
377
|
getter: LazyDef<T, Args>,
|
|
207
|
-
config?:
|
|
378
|
+
config?: DefineConfig
|
|
208
379
|
): IDefinitorInstance<T, InstanceResult<T>, GlobalRegistry, '', object, Args>;
|
|
209
380
|
export function lazy(
|
|
210
381
|
this: unknown,
|
|
211
382
|
TypeName: string,
|
|
212
383
|
getter: unknown,
|
|
213
|
-
config?:
|
|
384
|
+
config?: DefineConfig
|
|
214
385
|
): unknown {
|
|
215
386
|
const types = checkThis(this) ? defaultTypes : this || defaultTypes;
|
|
216
387
|
// Type assertion needed because TypesCollectionProxy is a Proxy
|
|
@@ -273,68 +444,89 @@ const $run = function <E extends object, T extends object, Args extends unknown[
|
|
|
273
444
|
};
|
|
274
445
|
|
|
275
446
|
// TODO: apply instance .to type .with arguments
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
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[]>(
|
|
281
462
|
entity: E,
|
|
282
463
|
Ctor: CtorParameter<T, Args>,
|
|
283
464
|
args: unknown[] = []
|
|
284
|
-
):
|
|
285
|
-
const
|
|
465
|
+
): object {
|
|
466
|
+
const result = $run<E, T, Args>(
|
|
286
467
|
entity,
|
|
287
468
|
Ctor,
|
|
288
469
|
args
|
|
289
470
|
);
|
|
290
|
-
// the instance constructed from entity by Ctor carries the merged
|
|
291
|
-
// entity+ctor shape apply/call/bind promise — the named public result type
|
|
292
|
-
const result = runResult as InstanceResult<Merge<E, T>>;
|
|
293
471
|
return result;
|
|
294
|
-
}
|
|
472
|
+
}
|
|
295
473
|
|
|
296
474
|
// TODO: call type .by instance .with arguments
|
|
297
|
-
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[]>(
|
|
298
481
|
entity: E,
|
|
299
482
|
Ctor: CtorParameter<T, Args>,
|
|
300
483
|
...args: NoInfer<Args>
|
|
301
|
-
): InstanceResult<Merge<E, T
|
|
302
|
-
|
|
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>(
|
|
303
491
|
entity,
|
|
304
492
|
Ctor,
|
|
305
493
|
args
|
|
306
494
|
);
|
|
307
|
-
// same merge narrowing as apply() above
|
|
308
|
-
const result = runResult as InstanceResult<Merge<E, T>>;
|
|
309
495
|
return result;
|
|
310
|
-
}
|
|
496
|
+
}
|
|
311
497
|
|
|
312
498
|
// TODO: bind type .with instance → (...args)
|
|
313
|
-
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[]>(
|
|
314
504
|
entity: E,
|
|
315
505
|
Ctor: CtorParameter<T, Args>
|
|
316
|
-
): (...args: Args) => InstanceResult<Merge<E, T
|
|
506
|
+
): (...args: Args) => InstanceResult<Merge<E, T>>;
|
|
507
|
+
export function bind <E extends object, T extends object, Args extends unknown[]>(
|
|
508
|
+
entity: E,
|
|
509
|
+
Ctor: CtorParameter<T, Args>
|
|
510
|
+
): (...args: Args) => object {
|
|
317
511
|
const result = (...args: Args) => {
|
|
318
512
|
const runResult = $run<E, T, Args>(
|
|
319
513
|
entity,
|
|
320
514
|
Ctor,
|
|
321
515
|
args
|
|
322
516
|
);
|
|
323
|
-
|
|
324
|
-
const typedResult = runResult as InstanceResult<Merge<E, T>>;
|
|
325
|
-
return typedResult;
|
|
517
|
+
return runResult;
|
|
326
518
|
};
|
|
327
519
|
return result;
|
|
328
|
-
}
|
|
520
|
+
}
|
|
329
521
|
|
|
330
522
|
export const decorate = function <
|
|
331
|
-
T extends Constructor<object> |
|
|
523
|
+
T extends Constructor<object> | NoHandlerConfig | undefined = undefined
|
|
332
524
|
>(
|
|
333
525
|
target?: T,
|
|
334
|
-
config?:
|
|
526
|
+
config?: DefineConfig
|
|
335
527
|
): <U extends Constructor<object>>(cstr: U) => DecoratedClass<U> {
|
|
336
|
-
const opts = (config === undefined &&
|
|
337
|
-
? target as
|
|
528
|
+
const opts = (config === undefined && !(target instanceof Function))
|
|
529
|
+
? target as NoHandlerConfig | undefined
|
|
338
530
|
: config;
|
|
339
531
|
|
|
340
532
|
// decorate(SomeType, config?) expects a constructor already carrying
|
|
@@ -374,7 +566,7 @@ export const decorate = function <
|
|
|
374
566
|
|
|
375
567
|
export const registerHook = function <
|
|
376
568
|
T extends object,
|
|
377
|
-
HT extends
|
|
569
|
+
HT extends hookName = hooksTypes,
|
|
378
570
|
>(
|
|
379
571
|
Ctor: HookableConstructor<T>,
|
|
380
572
|
hookType: HT,
|
|
@@ -386,6 +578,18 @@ export const registerHook = function <
|
|
|
386
578
|
);
|
|
387
579
|
};
|
|
388
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
|
+
|
|
389
593
|
export const mnemonica = Object.entries({
|
|
390
594
|
|
|
391
595
|
define,
|
|
@@ -396,6 +600,7 @@ export const mnemonica = Object.entries({
|
|
|
396
600
|
bind,
|
|
397
601
|
decorate,
|
|
398
602
|
registerHook,
|
|
603
|
+
unregisterHook,
|
|
399
604
|
|
|
400
605
|
...descriptors,
|
|
401
606
|
|