mnemonica 1.3.8 → 1.4.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 +101 -31
- package/README.md +2 -2
- package/build/api/errors/index.d.ts +3 -7
- package/build/api/errors/index.js +3 -5
- package/build/api/errors/throwModificationError.js +2 -4
- package/build/api/hooks/HookInvocation.d.ts +2 -2
- package/build/api/hooks/HookInvocation.js +1 -1
- package/build/api/hooks/flowCheckers.d.ts +3 -3
- package/build/api/hooks/flowCheckers.js +1 -1
- package/build/api/index.d.ts +3 -3
- package/build/api/types/index.d.ts +3 -3
- package/build/api/types/index.js +18 -26
- package/build/api/utils/index.d.ts +2 -2
- package/build/api/utils/index.js +31 -18
- package/build/descriptors/errors/index.js +1 -1
- package/build/descriptors/types/index.js +8 -8
- package/build/index.d.ts +10 -13
- package/build/index.js +13 -16
- package/build/types/index.d.ts +89 -25
- package/build/types/index.js +1 -1
- package/build/utils/collectConstructors.js +1 -1
- package/build/utils/fork.d.ts +2 -1
- package/build/utils/fork.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 +5 -3
- package/docs/empathy-in-ai.md +2 -2
- package/docs/prototype-chain.md +1 -1
- package/docs/typed-lookup.md +3 -1
- package/package.json +12 -12
- package/src/api/errors/index.ts +12 -15
- package/src/api/errors/throwModificationError.ts +3 -4
- package/src/api/hooks/HookInvocation.ts +6 -5
- package/src/api/hooks/flowCheckers.ts +6 -3
- package/src/api/types/index.ts +45 -44
- package/src/api/utils/index.ts +60 -24
- package/src/descriptors/errors/index.ts +4 -5
- package/src/descriptors/types/index.ts +20 -16
- package/src/index.ts +55 -47
- package/src/types/index.ts +252 -39
- package/src/utils/collectConstructors.ts +0 -9
- package/src/utils/fork.ts +2 -1
- package/src/utils/index.ts +15 -14
- package/src/utils/parse.ts +9 -1
package/src/index.ts
CHANGED
|
@@ -5,7 +5,6 @@ import type {
|
|
|
5
5
|
CreateTypesCollectionFunction,
|
|
6
6
|
CtorParameter,
|
|
7
7
|
IDEF,
|
|
8
|
-
hook,
|
|
9
8
|
hooksTypes,
|
|
10
9
|
constructorOptions,
|
|
11
10
|
Proto,
|
|
@@ -22,7 +21,13 @@ import type {
|
|
|
22
21
|
TypeLookup,
|
|
23
22
|
RegistryHolderBase,
|
|
24
23
|
RegistryEntry,
|
|
25
|
-
LookedUpConstructor
|
|
24
|
+
LookedUpConstructor,
|
|
25
|
+
WithHookRegister,
|
|
26
|
+
typedHook,
|
|
27
|
+
HookableConstructor,
|
|
28
|
+
DefineNewableOrCallable,
|
|
29
|
+
LookupSource,
|
|
30
|
+
LazyDef
|
|
26
31
|
} from './types';
|
|
27
32
|
|
|
28
33
|
import TypesUtils from './api/utils/index';
|
|
@@ -41,6 +46,7 @@ export type {
|
|
|
41
46
|
Proto,
|
|
42
47
|
ProtoFlat,
|
|
43
48
|
constructorOptions,
|
|
49
|
+
deliveredHookOpts,
|
|
44
50
|
hooksOpts,
|
|
45
51
|
hook,
|
|
46
52
|
hooksTypes,
|
|
@@ -62,6 +68,13 @@ export type {
|
|
|
62
68
|
RegistryEntry,
|
|
63
69
|
LookedUpConstructor,
|
|
64
70
|
LookedUpInstance,
|
|
71
|
+
typedHook,
|
|
72
|
+
typedHookOpts,
|
|
73
|
+
WithHookRegister,
|
|
74
|
+
HookableConstructor,
|
|
75
|
+
// what a failed construction throws — the catch side of `new T()`
|
|
76
|
+
CreationError,
|
|
77
|
+
ErrorProps,
|
|
65
78
|
} from './types';
|
|
66
79
|
|
|
67
80
|
/**
|
|
@@ -149,8 +162,9 @@ export function define <
|
|
|
149
162
|
): IDefinitorInstance<
|
|
150
163
|
F,
|
|
151
164
|
InstanceResult<F>,
|
|
152
|
-
Reg & Record<ChildPath, RegistryEntry<F, ChildPath>>,
|
|
153
|
-
ChildPath
|
|
165
|
+
Reg & Record<ChildPath, RegistryEntry<F, ChildPath, Parent>>,
|
|
166
|
+
ChildPath,
|
|
167
|
+
Parent
|
|
154
168
|
>;
|
|
155
169
|
export function define <
|
|
156
170
|
T extends object,
|
|
@@ -160,9 +174,9 @@ export function define <
|
|
|
160
174
|
R extends IDefinitorInstance<N> = IDefinitorInstance<N>,
|
|
161
175
|
>(
|
|
162
176
|
this: unknown,
|
|
163
|
-
TypeName?: string |
|
|
177
|
+
TypeName?: string | DefineNewableOrCallable,
|
|
164
178
|
// Allow both strict IDEF and more flexible function signatures
|
|
165
|
-
constructHandler?: IDEF<T> |
|
|
179
|
+
constructHandler?: IDEF<T> | DefineNewableOrCallable | object | boolean,
|
|
166
180
|
config?: constructorOptions,
|
|
167
181
|
): R;
|
|
168
182
|
export function define <
|
|
@@ -172,9 +186,9 @@ export function define <
|
|
|
172
186
|
R extends IDefinitorInstance<N> = IDefinitorInstance<N>,
|
|
173
187
|
>(
|
|
174
188
|
this: unknown,
|
|
175
|
-
TypeNameOrSource?: string |
|
|
176
|
-
constructHandlerOrName?: IDEF<T> |
|
|
177
|
-
configOrHandler?: constructorOptions | IDEF<T> |
|
|
189
|
+
TypeNameOrSource?: string | DefineNewableOrCallable | RegistryHolderBase<object, object, string>,
|
|
190
|
+
constructHandlerOrName?: IDEF<T> | DefineNewableOrCallable | object | boolean | string,
|
|
191
|
+
configOrHandler?: constructorOptions | IDEF<T> | DefineNewableOrCallable | object | boolean,
|
|
178
192
|
config?: constructorOptions,
|
|
179
193
|
): R {
|
|
180
194
|
|
|
@@ -231,36 +245,36 @@ export function lazy<T extends object>(
|
|
|
231
245
|
// named form: lazy(TypeName, getter, config?)
|
|
232
246
|
this: unknown,
|
|
233
247
|
TypeName: string,
|
|
234
|
-
getter:
|
|
248
|
+
getter: LazyDef<T>,
|
|
235
249
|
config?: constructorOptions
|
|
236
250
|
): IDefinitorInstance<T>;
|
|
237
251
|
export function lazy<T extends object>(
|
|
238
252
|
// unnamed form: lazy(getter, config?)
|
|
239
253
|
this: unknown,
|
|
240
|
-
getter:
|
|
254
|
+
getter: LazyDef<T>,
|
|
241
255
|
config?: constructorOptions
|
|
242
256
|
): IDefinitorInstance<T>;
|
|
243
257
|
export function lazy(
|
|
244
258
|
this: unknown,
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
259
|
+
sourceOrTypeNameOrGetter: unknown,
|
|
260
|
+
TypeNameOrGetterOrConfig?: unknown,
|
|
261
|
+
getterOrConfig?: unknown,
|
|
262
|
+
config?: unknown
|
|
249
263
|
): unknown {
|
|
250
264
|
|
|
251
265
|
// explicit-source form: lazy(source, name?, getter, config?)
|
|
252
266
|
// the source may be a TypesCollection (object) or a TypeProxy (function),
|
|
253
267
|
// so detection is: has a callable .lazy
|
|
254
268
|
const mayBeSource = (
|
|
255
|
-
(typeof
|
|
256
|
-
typeof
|
|
257
|
-
) && typeof (
|
|
269
|
+
(typeof sourceOrTypeNameOrGetter === 'object' && sourceOrTypeNameOrGetter !== null) ||
|
|
270
|
+
typeof sourceOrTypeNameOrGetter === 'function'
|
|
271
|
+
) && typeof (sourceOrTypeNameOrGetter as { lazy?: unknown }).lazy === 'function';
|
|
258
272
|
if (mayBeSource) {
|
|
259
|
-
const source =
|
|
273
|
+
const source = sourceOrTypeNameOrGetter as { lazy: LazyAbsorber };
|
|
260
274
|
const sourceLazyResult = source.lazy(
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
275
|
+
TypeNameOrGetterOrConfig as string | LazyDef<object>,
|
|
276
|
+
getterOrConfig as LazyDef<object> | constructorOptions,
|
|
277
|
+
config as constructorOptions
|
|
264
278
|
) as unknown;
|
|
265
279
|
return sourceLazyResult;
|
|
266
280
|
}
|
|
@@ -269,9 +283,9 @@ export function lazy(
|
|
|
269
283
|
// Type assertion needed because TypesCollectionProxy is a Proxy
|
|
270
284
|
const lazyResult = (types as { lazy: LazyAbsorber })
|
|
271
285
|
.lazy(
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
286
|
+
sourceOrTypeNameOrGetter as string | LazyDef<object>,
|
|
287
|
+
TypeNameOrGetterOrConfig as LazyDef<object> | constructorOptions,
|
|
288
|
+
getterOrConfig as constructorOptions
|
|
275
289
|
) as unknown;
|
|
276
290
|
return lazyResult;
|
|
277
291
|
}
|
|
@@ -279,34 +293,34 @@ export function lazy(
|
|
|
279
293
|
export function lookup<const K extends keyof TypeRegistry>(
|
|
280
294
|
this: unknown,
|
|
281
295
|
TypeNestedPath: K
|
|
282
|
-
): TypeRegistry[K]
|
|
296
|
+
): WithHookRegister<TypeRegistry[K]>;
|
|
283
297
|
export function lookup<Reg extends object, const K extends keyof Reg & string>(
|
|
284
298
|
// explicit-source form: lookup(source, path)
|
|
285
299
|
// resolves against the registry carried by a builder/collection value
|
|
286
300
|
source: { lookup: TypeLookup<Reg> },
|
|
287
301
|
TypeNestedPath: K
|
|
288
|
-
): LookedUpConstructor<Reg, K
|
|
302
|
+
): WithHookRegister<LookedUpConstructor<Reg, K>>;
|
|
289
303
|
export function lookup(this: unknown, TypeNestedPath: string): TypeClass | undefined;
|
|
290
304
|
export function lookup(
|
|
291
|
-
source:
|
|
305
|
+
source: LookupSource,
|
|
292
306
|
TypeNestedPath: string
|
|
293
307
|
): TypeClass | undefined;
|
|
294
308
|
export function lookup(
|
|
295
309
|
this: unknown,
|
|
296
|
-
|
|
297
|
-
|
|
310
|
+
sourceOrPath: unknown,
|
|
311
|
+
TypeNestedPath?: unknown
|
|
298
312
|
): unknown {
|
|
299
313
|
|
|
300
314
|
// explicit-source form: lookup(source, path)
|
|
301
|
-
if (typeof
|
|
302
|
-
const source =
|
|
303
|
-
const sourceResult = source.lookup(
|
|
315
|
+
if (typeof sourceOrPath !== 'string' && typeof TypeNestedPath === 'string') {
|
|
316
|
+
const source = sourceOrPath as LookupSource;
|
|
317
|
+
const sourceResult = source.lookup(TypeNestedPath);
|
|
304
318
|
return sourceResult;
|
|
305
319
|
}
|
|
306
320
|
|
|
307
321
|
const types = checkThis(this) ? defaultTypes : this || defaultTypes;
|
|
308
322
|
// Type assertion needed because TypesCollectionProxy is a Proxy
|
|
309
|
-
const lookupResult = (types as
|
|
323
|
+
const lookupResult = (types as LookupSource).lookup(sourceOrPath as string);
|
|
310
324
|
return lookupResult;
|
|
311
325
|
}
|
|
312
326
|
|
|
@@ -329,7 +343,6 @@ const $run = function <E extends object, T extends object, S extends Proto<E, T>
|
|
|
329
343
|
TypeName,
|
|
330
344
|
entity
|
|
331
345
|
) as TypeConstructorBase;
|
|
332
|
-
// TODO: check lines below and if Constructor is not mnemonized ...
|
|
333
346
|
if (Cstr === undefined) {
|
|
334
347
|
throw new WRONG_MODIFICATION_PATTERN(`[ ${TypeName} ] is not defined as a Type Constructor on used instance`);
|
|
335
348
|
}
|
|
@@ -431,10 +444,13 @@ export const decorate = function <
|
|
|
431
444
|
};
|
|
432
445
|
|
|
433
446
|
|
|
434
|
-
export const registerHook = function <
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
447
|
+
export const registerHook = function <
|
|
448
|
+
T extends object,
|
|
449
|
+
HT extends hooksTypes = hooksTypes,
|
|
450
|
+
>(
|
|
451
|
+
Ctor: HookableConstructor<T>,
|
|
452
|
+
hookType: HT,
|
|
453
|
+
cb: typedHook<HT, object, T>
|
|
438
454
|
): void {
|
|
439
455
|
Ctor.registerHook(
|
|
440
456
|
hookType,
|
|
@@ -476,14 +492,6 @@ export const mnemonica = Object.entries({
|
|
|
476
492
|
{}
|
|
477
493
|
) as MnemonicaModule;
|
|
478
494
|
|
|
479
|
-
import * as api from './api';
|
|
480
|
-
|
|
481
|
-
export const {
|
|
482
|
-
define: _define,
|
|
483
|
-
lazy: _lazy,
|
|
484
|
-
lookup: _lookup
|
|
485
|
-
} = api.types;
|
|
486
|
-
|
|
487
495
|
export const {
|
|
488
496
|
|
|
489
497
|
SymbolParentType,
|