mnemonica 1.3.8 → 1.3.9
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 +68 -6
- 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.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 +15 -15
- 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 +11 -13
- package/build/index.js +12 -12
- package/build/types/index.d.ts +81 -23
- 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 +1 -1
- package/build/utils/parse.js +1 -1
- package/package.json +1 -1
- package/src/api/errors/index.ts +12 -15
- package/src/api/errors/throwModificationError.ts +3 -4
- package/src/api/hooks/HookInvocation.ts +2 -1
- package/src/api/hooks/flowCheckers.ts +6 -3
- package/src/api/types/index.ts +26 -23
- 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 +54 -39
- package/src/types/index.ts +211 -36
- package/src/utils/collectConstructors.ts +0 -9
- package/src/utils/fork.ts +2 -1
- package/src/utils/index.ts +1 -1
- package/src/utils/parse.ts +2 -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';
|
|
@@ -62,6 +67,13 @@ export type {
|
|
|
62
67
|
RegistryEntry,
|
|
63
68
|
LookedUpConstructor,
|
|
64
69
|
LookedUpInstance,
|
|
70
|
+
typedHook,
|
|
71
|
+
typedHookOpts,
|
|
72
|
+
WithHookRegister,
|
|
73
|
+
HookableConstructor,
|
|
74
|
+
// what a failed construction throws — the catch side of `new T()`
|
|
75
|
+
CreationError,
|
|
76
|
+
ErrorProps,
|
|
65
77
|
} from './types';
|
|
66
78
|
|
|
67
79
|
/**
|
|
@@ -149,8 +161,9 @@ export function define <
|
|
|
149
161
|
): IDefinitorInstance<
|
|
150
162
|
F,
|
|
151
163
|
InstanceResult<F>,
|
|
152
|
-
Reg & Record<ChildPath, RegistryEntry<F, ChildPath>>,
|
|
153
|
-
ChildPath
|
|
164
|
+
Reg & Record<ChildPath, RegistryEntry<F, ChildPath, Parent>>,
|
|
165
|
+
ChildPath,
|
|
166
|
+
Parent
|
|
154
167
|
>;
|
|
155
168
|
export function define <
|
|
156
169
|
T extends object,
|
|
@@ -160,9 +173,9 @@ export function define <
|
|
|
160
173
|
R extends IDefinitorInstance<N> = IDefinitorInstance<N>,
|
|
161
174
|
>(
|
|
162
175
|
this: unknown,
|
|
163
|
-
TypeName?: string |
|
|
176
|
+
TypeName?: string | DefineNewableOrCallable,
|
|
164
177
|
// Allow both strict IDEF and more flexible function signatures
|
|
165
|
-
constructHandler?: IDEF<T> |
|
|
178
|
+
constructHandler?: IDEF<T> | DefineNewableOrCallable | object | boolean,
|
|
166
179
|
config?: constructorOptions,
|
|
167
180
|
): R;
|
|
168
181
|
export function define <
|
|
@@ -172,9 +185,9 @@ export function define <
|
|
|
172
185
|
R extends IDefinitorInstance<N> = IDefinitorInstance<N>,
|
|
173
186
|
>(
|
|
174
187
|
this: unknown,
|
|
175
|
-
TypeNameOrSource?: string |
|
|
176
|
-
constructHandlerOrName?: IDEF<T> |
|
|
177
|
-
configOrHandler?: constructorOptions | IDEF<T> |
|
|
188
|
+
TypeNameOrSource?: string | DefineNewableOrCallable | RegistryHolderBase<object, object, string>,
|
|
189
|
+
constructHandlerOrName?: IDEF<T> | DefineNewableOrCallable | object | boolean | string,
|
|
190
|
+
configOrHandler?: constructorOptions | IDEF<T> | DefineNewableOrCallable | object | boolean,
|
|
178
191
|
config?: constructorOptions,
|
|
179
192
|
): R {
|
|
180
193
|
|
|
@@ -231,36 +244,36 @@ export function lazy<T extends object>(
|
|
|
231
244
|
// named form: lazy(TypeName, getter, config?)
|
|
232
245
|
this: unknown,
|
|
233
246
|
TypeName: string,
|
|
234
|
-
getter:
|
|
247
|
+
getter: LazyDef<T>,
|
|
235
248
|
config?: constructorOptions
|
|
236
249
|
): IDefinitorInstance<T>;
|
|
237
250
|
export function lazy<T extends object>(
|
|
238
251
|
// unnamed form: lazy(getter, config?)
|
|
239
252
|
this: unknown,
|
|
240
|
-
getter:
|
|
253
|
+
getter: LazyDef<T>,
|
|
241
254
|
config?: constructorOptions
|
|
242
255
|
): IDefinitorInstance<T>;
|
|
243
256
|
export function lazy(
|
|
244
257
|
this: unknown,
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
258
|
+
sourceOrTypeNameOrGetter: unknown,
|
|
259
|
+
TypeNameOrGetterOrConfig?: unknown,
|
|
260
|
+
getterOrConfig?: unknown,
|
|
261
|
+
config?: unknown
|
|
249
262
|
): unknown {
|
|
250
263
|
|
|
251
264
|
// explicit-source form: lazy(source, name?, getter, config?)
|
|
252
265
|
// the source may be a TypesCollection (object) or a TypeProxy (function),
|
|
253
266
|
// so detection is: has a callable .lazy
|
|
254
267
|
const mayBeSource = (
|
|
255
|
-
(typeof
|
|
256
|
-
typeof
|
|
257
|
-
) && typeof (
|
|
268
|
+
(typeof sourceOrTypeNameOrGetter === 'object' && sourceOrTypeNameOrGetter !== null) ||
|
|
269
|
+
typeof sourceOrTypeNameOrGetter === 'function'
|
|
270
|
+
) && typeof (sourceOrTypeNameOrGetter as { lazy?: unknown }).lazy === 'function';
|
|
258
271
|
if (mayBeSource) {
|
|
259
|
-
const source =
|
|
272
|
+
const source = sourceOrTypeNameOrGetter as { lazy: LazyAbsorber };
|
|
260
273
|
const sourceLazyResult = source.lazy(
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
274
|
+
TypeNameOrGetterOrConfig as string | LazyDef<object>,
|
|
275
|
+
getterOrConfig as LazyDef<object> | constructorOptions,
|
|
276
|
+
config as constructorOptions
|
|
264
277
|
) as unknown;
|
|
265
278
|
return sourceLazyResult;
|
|
266
279
|
}
|
|
@@ -269,9 +282,9 @@ export function lazy(
|
|
|
269
282
|
// Type assertion needed because TypesCollectionProxy is a Proxy
|
|
270
283
|
const lazyResult = (types as { lazy: LazyAbsorber })
|
|
271
284
|
.lazy(
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
285
|
+
sourceOrTypeNameOrGetter as string | LazyDef<object>,
|
|
286
|
+
TypeNameOrGetterOrConfig as LazyDef<object> | constructorOptions,
|
|
287
|
+
getterOrConfig as constructorOptions
|
|
275
288
|
) as unknown;
|
|
276
289
|
return lazyResult;
|
|
277
290
|
}
|
|
@@ -279,34 +292,34 @@ export function lazy(
|
|
|
279
292
|
export function lookup<const K extends keyof TypeRegistry>(
|
|
280
293
|
this: unknown,
|
|
281
294
|
TypeNestedPath: K
|
|
282
|
-
): TypeRegistry[K]
|
|
295
|
+
): WithHookRegister<TypeRegistry[K]>;
|
|
283
296
|
export function lookup<Reg extends object, const K extends keyof Reg & string>(
|
|
284
297
|
// explicit-source form: lookup(source, path)
|
|
285
298
|
// resolves against the registry carried by a builder/collection value
|
|
286
299
|
source: { lookup: TypeLookup<Reg> },
|
|
287
300
|
TypeNestedPath: K
|
|
288
|
-
): LookedUpConstructor<Reg, K
|
|
301
|
+
): WithHookRegister<LookedUpConstructor<Reg, K>>;
|
|
289
302
|
export function lookup(this: unknown, TypeNestedPath: string): TypeClass | undefined;
|
|
290
303
|
export function lookup(
|
|
291
|
-
source:
|
|
304
|
+
source: LookupSource,
|
|
292
305
|
TypeNestedPath: string
|
|
293
306
|
): TypeClass | undefined;
|
|
294
307
|
export function lookup(
|
|
295
308
|
this: unknown,
|
|
296
|
-
|
|
297
|
-
|
|
309
|
+
sourceOrPath: unknown,
|
|
310
|
+
TypeNestedPath?: unknown
|
|
298
311
|
): unknown {
|
|
299
312
|
|
|
300
313
|
// explicit-source form: lookup(source, path)
|
|
301
|
-
if (typeof
|
|
302
|
-
const source =
|
|
303
|
-
const sourceResult = source.lookup(
|
|
314
|
+
if (typeof sourceOrPath !== 'string' && typeof TypeNestedPath === 'string') {
|
|
315
|
+
const source = sourceOrPath as LookupSource;
|
|
316
|
+
const sourceResult = source.lookup(TypeNestedPath);
|
|
304
317
|
return sourceResult;
|
|
305
318
|
}
|
|
306
319
|
|
|
307
320
|
const types = checkThis(this) ? defaultTypes : this || defaultTypes;
|
|
308
321
|
// Type assertion needed because TypesCollectionProxy is a Proxy
|
|
309
|
-
const lookupResult = (types as
|
|
322
|
+
const lookupResult = (types as LookupSource).lookup(sourceOrPath as string);
|
|
310
323
|
return lookupResult;
|
|
311
324
|
}
|
|
312
325
|
|
|
@@ -329,7 +342,6 @@ const $run = function <E extends object, T extends object, S extends Proto<E, T>
|
|
|
329
342
|
TypeName,
|
|
330
343
|
entity
|
|
331
344
|
) as TypeConstructorBase;
|
|
332
|
-
// TODO: check lines below and if Constructor is not mnemonized ...
|
|
333
345
|
if (Cstr === undefined) {
|
|
334
346
|
throw new WRONG_MODIFICATION_PATTERN(`[ ${TypeName} ] is not defined as a Type Constructor on used instance`);
|
|
335
347
|
}
|
|
@@ -431,10 +443,13 @@ export const decorate = function <
|
|
|
431
443
|
};
|
|
432
444
|
|
|
433
445
|
|
|
434
|
-
export const registerHook = function <
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
446
|
+
export const registerHook = function <
|
|
447
|
+
T extends object,
|
|
448
|
+
HT extends hooksTypes = hooksTypes,
|
|
449
|
+
>(
|
|
450
|
+
Ctor: HookableConstructor<T>,
|
|
451
|
+
hookType: HT,
|
|
452
|
+
cb: typedHook<HT, object, T>
|
|
438
453
|
): void {
|
|
439
454
|
Ctor.registerHook(
|
|
440
455
|
hookType,
|
package/src/types/index.ts
CHANGED
|
@@ -25,6 +25,69 @@ export type CtorParameter<T> = IDEF<T> | { new (...args: never[]): T };
|
|
|
25
25
|
// Used by the explicit .lazy() API.
|
|
26
26
|
export type LazyDef<T, Args extends unknown[] = unknown[]> = () => IDEF<T, Args>;
|
|
27
27
|
|
|
28
|
+
// define() handler in callable (plain function) form: the runtime invokes
|
|
29
|
+
// it with the fresh instance as `this` and the construction arguments as
|
|
30
|
+
// args. Parameters are never[] so every handler parameter list stays
|
|
31
|
+
// assignable by contravariance (the AnyConstructor trick); no `this`
|
|
32
|
+
// annotation so handlers declaring their own `this: Instance` assign too.
|
|
33
|
+
export interface DefineCallable extends CallableFunction {
|
|
34
|
+
(...args: never[]): unknown;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// define() handler in newable (class) form: the runtime constructs it with
|
|
38
|
+
// the construction arguments; the constructed object becomes the instance.
|
|
39
|
+
// never[] parameters accept every constructor parameter list, same
|
|
40
|
+
// contravariance reason as DefineCallable.
|
|
41
|
+
export interface DefineNewable extends NewableFunction {
|
|
42
|
+
new (...args: never[]): object;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// A define() handler in either form — the pair named once for the
|
|
46
|
+
// define()/lookup()/decorate overload unions.
|
|
47
|
+
export type DefineNewableOrCallable = DefineNewable | DefineCallable;
|
|
48
|
+
|
|
49
|
+
// The constructor reference every instance/prototype carries as
|
|
50
|
+
// `.constructor`. Signature-free on purpose: the Object interface types
|
|
51
|
+
// `.constructor` as bare Function (no construct signature of its own), and
|
|
52
|
+
// that reference must stay assignable here.
|
|
53
|
+
export interface InstanceConstructor extends NewableFunction {}
|
|
54
|
+
|
|
55
|
+
// A hook callback as seen by a REGISTERING surface that only forwards it
|
|
56
|
+
// (HookableConstructor's registerHook, the WithHookRegister member check):
|
|
57
|
+
// it consumes whatever opts the caller's typed hook carries and returns
|
|
58
|
+
// unknown. never[] parameters keep every hook shape assignable by
|
|
59
|
+
// contravariance, including generic typedHook<HT> instantiations (a
|
|
60
|
+
// parameter typed as the plain `hook` interface would reject those).
|
|
61
|
+
export interface AnyHookCallback extends CallableFunction {
|
|
62
|
+
(...args: never[]): unknown;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// A utility method as exposed on the `utils` collection — the
|
|
66
|
+
// index-signature fallback for utilities beyond the declared set. Plain
|
|
67
|
+
// callables: any argument list, unknown result.
|
|
68
|
+
export interface UtilFunction extends CallableFunction {
|
|
69
|
+
(...args: never[]): unknown;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// The minimal source shape the free lookup() resolves against: any value
|
|
73
|
+
// carrying a lookup method with the untyped fallback signature
|
|
74
|
+
// (a TypesCollection, a type constructor, or the mnemonica module object).
|
|
75
|
+
export interface LookupSource {
|
|
76
|
+
lookup(path: string): TypeClass | undefined;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// The modification-error sink handed through the creator object: consumes
|
|
80
|
+
// the construction error, produces nothing (the construction aborts).
|
|
81
|
+
export interface ThrowModificationError extends CallableFunction {
|
|
82
|
+
(error: Error): void;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// The bound fork invoker that instance.fork() returns: consumes fork
|
|
86
|
+
// arguments, produces a fresh instance of the forked type.
|
|
87
|
+
export interface ForkInvoker<T extends object> extends CallableFunction {
|
|
88
|
+
(this: object, ...forkArgs: unknown[]): T;
|
|
89
|
+
}
|
|
90
|
+
|
|
28
91
|
// Error message types - all error messages are strings
|
|
29
92
|
export type ErrorMessageKey =
|
|
30
93
|
| 'BASE_ERROR_MESSAGE'
|
|
@@ -50,13 +113,21 @@ export interface hook extends CallableFunction {
|
|
|
50
113
|
(opts: hooksOpts): unknown;
|
|
51
114
|
}
|
|
52
115
|
|
|
53
|
-
//
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
116
|
+
// Flow checker callback registered via registerFlowChecker: invoked with
|
|
117
|
+
// the hook invocation record after each hook run; its return value is
|
|
118
|
+
// collected into the invocation results but otherwise unused.
|
|
119
|
+
export interface FlowChecker extends CallableFunction {
|
|
120
|
+
(opts: object): unknown;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// An error class made by constructError(name, message) — the lib.es5
|
|
124
|
+
// ErrorConstructor idiom, `new`-only (a class cannot be called without it).
|
|
125
|
+
// Each class carries one fixed message (constants ErrorMessages); `addition`
|
|
126
|
+
// is the per-throw detail, appended as `${message} : ${addition}`; `stack`
|
|
127
|
+
// lines, when given, are prepended to the captured stack.
|
|
128
|
+
export interface MnemonicaErrorConstructor extends NewableFunction {
|
|
129
|
+
new (addition?: string, stack?: string[]): MnemonicaError;
|
|
130
|
+
readonly prototype: MnemonicaError;
|
|
60
131
|
}
|
|
61
132
|
|
|
62
133
|
// Errors types map - indexable record of error constructors
|
|
@@ -79,6 +150,29 @@ export interface ErrorProps {
|
|
|
79
150
|
error?: unknown;
|
|
80
151
|
}
|
|
81
152
|
|
|
153
|
+
/**
|
|
154
|
+
* What a failed construction of `T` throws. TypeScript has no checked
|
|
155
|
+
* exceptions, so `new T()` is typed by its happy path only; this names the
|
|
156
|
+
* other path for the `catch` side.
|
|
157
|
+
*
|
|
158
|
+
* - `T & Error` — the errored instance (`blockErrors: true`, the default):
|
|
159
|
+
* the failed construction still produces a `T` layer, and the original
|
|
160
|
+
* error is linked in as the end of its prototype chain, so the thrown
|
|
161
|
+
* value is `instanceof T` AND `instanceof Error`. Its data is read via
|
|
162
|
+
* `getProps(error)` (`ErrorProps`).
|
|
163
|
+
* - `Error` — the original error, propagated unwrapped
|
|
164
|
+
* (`blockErrors: false`), or a mnemonica error thrown before construction.
|
|
165
|
+
*
|
|
166
|
+
* Async constructors reject with the same values.
|
|
167
|
+
*
|
|
168
|
+
* @example
|
|
169
|
+
* function report (failed: CreationError<Widget>) {
|
|
170
|
+
* if (failed instanceof Widget) { failed.size; failed.message; }
|
|
171
|
+
* }
|
|
172
|
+
* try { new Widget(1); } catch (e) { if (e instanceof Error) report(e); }
|
|
173
|
+
*/
|
|
174
|
+
export type CreationError<T extends object> = ( T & Error ) | Error;
|
|
175
|
+
|
|
82
176
|
/**
|
|
83
177
|
* Internal Type Constructor.
|
|
84
178
|
* "Type" here is used in the Computer Science sense — an interface/contract
|
|
@@ -87,6 +181,7 @@ export interface ErrorProps {
|
|
|
87
181
|
* shape, distinct from user-facing TypeConstructor below.
|
|
88
182
|
*/
|
|
89
183
|
export interface _Internal_TC_<ConstructorInstance extends object> {
|
|
184
|
+
/** @throws {CreationError<ConstructorInstance>} when construction fails */
|
|
90
185
|
new(...args: unknown[]): ConstructorInstance;
|
|
91
186
|
(this: ConstructorInstance, ...args: unknown[]): ConstructorInstance;
|
|
92
187
|
readonly prototype: ConstructorInstance & {
|
|
@@ -202,6 +297,50 @@ export type hooksOpts<P = object, T = P> = {
|
|
|
202
297
|
creator?: { throwModificationError(error: Error): void };
|
|
203
298
|
};
|
|
204
299
|
|
|
300
|
+
// Hook options typed per hookType — the compile-time face of the runtime
|
|
301
|
+
// hookData built in InstanceCreator (invokePreHooks / invokePostHooks).
|
|
302
|
+
// P = existent (parent) instance, T = inherited (created) instance.
|
|
303
|
+
// preCreation fires BEFORE the created instance exists, so its opts carry
|
|
304
|
+
// no inheritedInstance and no creator — accessing them is a compile error.
|
|
305
|
+
export type typedHookOpts<HT extends hooksTypes, P extends object, T extends object> =
|
|
306
|
+
HT extends 'preCreation'
|
|
307
|
+
? {
|
|
308
|
+
TypeName: string;
|
|
309
|
+
type: TypeDef;
|
|
310
|
+
args: unknown[];
|
|
311
|
+
existentInstance: P;
|
|
312
|
+
}
|
|
313
|
+
: {
|
|
314
|
+
TypeName: string;
|
|
315
|
+
type: TypeDef;
|
|
316
|
+
args: unknown[];
|
|
317
|
+
existentInstance: P;
|
|
318
|
+
inheritedInstance: T;
|
|
319
|
+
creator: { throwModificationError(error: Error): void };
|
|
320
|
+
};
|
|
321
|
+
|
|
322
|
+
// Typed hook callback — the cb of a registerHook call bound to a known type:
|
|
323
|
+
// the callback's opts are inferred from the hookType and the parent/instance
|
|
324
|
+
// pair the hook is registered on. Collection-wide (global) handlers stay on
|
|
325
|
+
// the untyped `hook` above by design.
|
|
326
|
+
export interface typedHook<
|
|
327
|
+
HT extends hooksTypes,
|
|
328
|
+
P extends object = object,
|
|
329
|
+
T extends object = object
|
|
330
|
+
> extends CallableFunction {
|
|
331
|
+
(opts: typedHookOpts<HT, P, T>): unknown;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
// Constructor surface the free registerHook() accepts: any constructor of T
|
|
335
|
+
// instances with a registerHook method. Deliberately NOT DecoratedClass<T> —
|
|
336
|
+
// inference through DecoratedClass's InstanceType<T> conditional picks a wrong
|
|
337
|
+
// T candidate, and inferring from a generic registerHook member resets T to
|
|
338
|
+
// its default; here T is inferred from Constructor<T>'s return position and
|
|
339
|
+
// the precise cb typing comes from the free function's own cb parameter.
|
|
340
|
+
export type HookableConstructor<T extends object> = Constructor<T> & {
|
|
341
|
+
registerHook(hookType: hooksTypes, cb: AnyHookCallback): void;
|
|
342
|
+
};
|
|
343
|
+
|
|
205
344
|
// Callback passed into ModificationConstructor to attach internal props to the prototype
|
|
206
345
|
export interface AddPropsCallback extends CallableFunction {
|
|
207
346
|
(proto: object): void;
|
|
@@ -407,6 +546,24 @@ export type LookedUpConstructor<
|
|
|
407
546
|
lookup: NestedTypeLookup<Registry, Path>;
|
|
408
547
|
};
|
|
409
548
|
|
|
549
|
+
// Typed registerHook surface for lookup results whose registry entries are
|
|
550
|
+
// bare constructors (tactica-emitted or hand-written TypeRegistry entries):
|
|
551
|
+
// their lookup results would otherwise lack registerHook entirely. The
|
|
552
|
+
// created-instance type is recovered from the entry's construct/call return.
|
|
553
|
+
// Constructors that already carry a typed registerHook (builder-created
|
|
554
|
+
// RegistryEntry values) pass through unchanged.
|
|
555
|
+
export type WithHookRegister<C> = C extends { registerHook: AnyHookCallback }
|
|
556
|
+
? C
|
|
557
|
+
: C extends { new (...args: never[]): infer R }
|
|
558
|
+
? R extends object
|
|
559
|
+
? C & { registerHook<HT extends hooksTypes>(hookType: HT, cb: typedHook<HT, object, R>): void }
|
|
560
|
+
: C
|
|
561
|
+
: C extends { (...args: never[]): infer R }
|
|
562
|
+
? R extends object
|
|
563
|
+
? C & { registerHook<HT extends hooksTypes>(hookType: HT, cb: typedHook<HT, object, R>): void }
|
|
564
|
+
: C
|
|
565
|
+
: C;
|
|
566
|
+
|
|
410
567
|
// Result of a typed lookup: the augmented constructor plus a `lookup` method
|
|
411
568
|
// scoped to the constructor's own type path for relative subtype lookups.
|
|
412
569
|
export type LookupResult<
|
|
@@ -698,10 +855,19 @@ export type InstanceResult<
|
|
|
698
855
|
// It preserves the dotted `Path` so that `.define()` on a looked-up constructor
|
|
699
856
|
// computes the correct child path, but it omits the full `GlobalRegistry`
|
|
700
857
|
// generic to keep hover tooltips readable.
|
|
858
|
+
// `Parent` is the instance type of the type's parent (object for root types),
|
|
859
|
+
// threaded so looked-up constructors type their registerHook callbacks'
|
|
860
|
+
// existentInstance.
|
|
701
861
|
export interface RegistryEntry<
|
|
702
862
|
F extends object,
|
|
703
|
-
Path extends string = ''
|
|
704
|
-
|
|
863
|
+
Path extends string = '',
|
|
864
|
+
Parent extends object = object
|
|
865
|
+
> extends _Internal_TC_<F>, RegistryHolderBase<{}, F, Path> {
|
|
866
|
+
// typed hook registration — preserved through ReplaceConstructorInstance's
|
|
867
|
+
// member preservation, so lookup() results of builder-typed registries
|
|
868
|
+
// get registerHook with inferred opts
|
|
869
|
+
registerHook<HT extends hooksTypes>(hookType: HT, cb: typedHook<HT, Parent, F>): void;
|
|
870
|
+
}
|
|
705
871
|
|
|
706
872
|
// Registry holder base - provides the accumulating .define() method.
|
|
707
873
|
// `Parent` tracks the instance type of the last defined constructor, so chained
|
|
@@ -715,9 +881,9 @@ export interface RegistryHolderBase<
|
|
|
715
881
|
// Legacy overload: define(constructHandler, config?)
|
|
716
882
|
define<SubType extends object>(
|
|
717
883
|
this: RegistryHolderBase<T, Parent, Path>,
|
|
718
|
-
TypeOrTypeName:
|
|
719
|
-
constructHandlerOrConfig?: IDEF<SubType> | object | boolean |
|
|
720
|
-
configOrUndefined?: constructorOptions |
|
|
884
|
+
TypeOrTypeName: DefineNewableOrCallable,
|
|
885
|
+
constructHandlerOrConfig?: IDEF<SubType> | object | boolean | DefineNewableOrCallable,
|
|
886
|
+
configOrUndefined?: constructorOptions | DefineNewableOrCallable | boolean
|
|
721
887
|
): IDefinitorInstance<SubType>;
|
|
722
888
|
|
|
723
889
|
// Modern overload: define(TypeName, constructHandler, config?)
|
|
@@ -735,8 +901,9 @@ export interface RegistryHolderBase<
|
|
|
735
901
|
): IDefinitorInstance<
|
|
736
902
|
F,
|
|
737
903
|
InstanceResult<F>,
|
|
738
|
-
T & Record<ChildPath, RegistryEntry<F, ChildPath>>,
|
|
739
|
-
ChildPath
|
|
904
|
+
T & Record<ChildPath, RegistryEntry<F, ChildPath, Parent>>,
|
|
905
|
+
ChildPath,
|
|
906
|
+
Parent
|
|
740
907
|
>;
|
|
741
908
|
|
|
742
909
|
// Explicit lazy getter: .lazy(() => Constructor, config?)
|
|
@@ -760,8 +927,9 @@ export interface RegistryHolderBase<
|
|
|
760
927
|
): IDefinitorInstance<
|
|
761
928
|
F,
|
|
762
929
|
InstanceResult<F>,
|
|
763
|
-
T & Record<ChildPath, RegistryEntry<F, ChildPath>>,
|
|
764
|
-
ChildPath
|
|
930
|
+
T & Record<ChildPath, RegistryEntry<F, ChildPath, Parent>>,
|
|
931
|
+
ChildPath,
|
|
932
|
+
Parent
|
|
765
933
|
>;
|
|
766
934
|
}
|
|
767
935
|
|
|
@@ -781,11 +949,14 @@ export interface RegistryHolder<
|
|
|
781
949
|
// R = wrapped result instance type
|
|
782
950
|
// Registry = typed registry map for lookup()
|
|
783
951
|
// Path = dotted type path of this constructor (empty for root types)
|
|
952
|
+
// Parent = instance type of the parent type (object for root types);
|
|
953
|
+
// threaded into registerHook's existentInstance typing
|
|
784
954
|
export interface IDefinitorInstance<
|
|
785
955
|
N extends object,
|
|
786
956
|
R extends InstanceResult<N> = InstanceResult<N>,
|
|
787
957
|
Registry extends object = GlobalRegistry,
|
|
788
|
-
Path extends string = ''
|
|
958
|
+
Path extends string = '',
|
|
959
|
+
Parent extends object = object,
|
|
789
960
|
>
|
|
790
961
|
extends RegistryHolderBase<Registry, N, Path> {
|
|
791
962
|
|
|
@@ -796,6 +967,7 @@ export interface IDefinitorInstance<
|
|
|
796
967
|
// the line below declares a `new ...` invocation,
|
|
797
968
|
// so makes interface constructible
|
|
798
969
|
// new(...args: unknown[]): { [key in keyof R]: R[key] };
|
|
970
|
+
/** @throws {CreationError<R>} when construction fails */
|
|
799
971
|
new(...args: unknown[]): R;
|
|
800
972
|
|
|
801
973
|
|
|
@@ -808,7 +980,7 @@ export interface IDefinitorInstance<
|
|
|
808
980
|
|
|
809
981
|
decorate: (config?: constructorOptions) => <U extends Constructor<object>>(cstr: U) => DecoratedClass<U, Registry>;
|
|
810
982
|
|
|
811
|
-
registerHook(hookType:
|
|
983
|
+
registerHook<HT extends hooksTypes>(hookType: HT, cb: typedHook<HT, Parent, N>): void;
|
|
812
984
|
|
|
813
985
|
subtypes: SubtypesMap;
|
|
814
986
|
|
|
@@ -830,9 +1002,9 @@ export interface IDefinitorInstance<
|
|
|
830
1002
|
export interface TypeAbsorber extends CallableFunction {
|
|
831
1003
|
<T extends object>(
|
|
832
1004
|
this: unknown,
|
|
833
|
-
TypeOrTypeName: string |
|
|
834
|
-
constructHandlerOrConfig?: IDEF<T> | object | boolean |
|
|
835
|
-
configOrUndefined?: constructorOptions |
|
|
1005
|
+
TypeOrTypeName: string | DefineNewableOrCallable,
|
|
1006
|
+
constructHandlerOrConfig?: IDEF<T> | object | boolean | DefineNewableOrCallable,
|
|
1007
|
+
configOrUndefined?: constructorOptions | DefineNewableOrCallable | boolean
|
|
836
1008
|
): IDefinitorInstance<T>;
|
|
837
1009
|
}
|
|
838
1010
|
|
|
@@ -851,9 +1023,9 @@ export interface LazyAbsorber extends CallableFunction {
|
|
|
851
1023
|
): IDefinitorInstance<T>;
|
|
852
1024
|
<T extends object>(
|
|
853
1025
|
this: unknown,
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
1026
|
+
TypeNameOrGetter: string | LazyDef<T>,
|
|
1027
|
+
getterOrConfig: LazyDef<T> | constructorOptions,
|
|
1028
|
+
namedFormConfig?: constructorOptions
|
|
857
1029
|
): IDefinitorInstance<T>;
|
|
858
1030
|
<T extends object>(
|
|
859
1031
|
this: unknown,
|
|
@@ -871,9 +1043,9 @@ export interface LazyAbsorber extends CallableFunction {
|
|
|
871
1043
|
<T extends object>(
|
|
872
1044
|
this: unknown,
|
|
873
1045
|
source: RegistryHolderBase<object, object, string>,
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
1046
|
+
TypeNameOrGetter: string | LazyDef<T>,
|
|
1047
|
+
getterOrConfig: LazyDef<T> | constructorOptions,
|
|
1048
|
+
namedFormConfig?: constructorOptions
|
|
877
1049
|
): IDefinitorInstance<T>;
|
|
878
1050
|
}
|
|
879
1051
|
|
|
@@ -904,7 +1076,7 @@ export interface Hookable {
|
|
|
904
1076
|
hooks: Record<string, Set<hook>>;
|
|
905
1077
|
invokeHook(hookType: hooksTypes, opts: hooksOpts): Set<unknown>;
|
|
906
1078
|
registerHook(hookType: hooksTypes, cb: hook): void;
|
|
907
|
-
registerFlowChecker(cb:
|
|
1079
|
+
registerFlowChecker(cb: FlowChecker): void;
|
|
908
1080
|
}
|
|
909
1081
|
|
|
910
1082
|
// createTypesCollection function type
|
|
@@ -931,15 +1103,15 @@ export interface MnemonicaConstructor extends NewableFunction {
|
|
|
931
1103
|
// Type descriptor instance — internal shape of TypeDescriptor objects
|
|
932
1104
|
export interface TypeDescriptorDefine extends CallableFunction {
|
|
933
1105
|
(
|
|
934
|
-
TypeOrTypeName: string |
|
|
935
|
-
constructHandlerOrConfig?:
|
|
1106
|
+
TypeOrTypeName: string | DefineNewableOrCallable,
|
|
1107
|
+
constructHandlerOrConfig?: DefineNewableOrCallable | object,
|
|
936
1108
|
config?: object
|
|
937
1109
|
): TypeClass;
|
|
938
1110
|
}
|
|
939
1111
|
|
|
940
1112
|
export interface TypeDescriptorLazy extends CallableFunction {
|
|
941
1113
|
(
|
|
942
|
-
getter:
|
|
1114
|
+
getter: LazyDef<object>,
|
|
943
1115
|
config?: object
|
|
944
1116
|
): TypeClass;
|
|
945
1117
|
}
|
|
@@ -1029,9 +1201,9 @@ export interface UtilsCollection {
|
|
|
1029
1201
|
pick<T extends object, K extends keyof T>(instance: T, ...args: (K | K[])[]): { [P in K]: T[P] } & {};
|
|
1030
1202
|
pick<T extends object>(instance: T, ...args: (string | string[])[]): Record<string, unknown>;
|
|
1031
1203
|
clone<T extends object>(instance: T): T;
|
|
1032
|
-
fork<T extends object>(instance: T):
|
|
1204
|
+
fork<T extends object>(instance: T): ForkInvoker<T>;
|
|
1033
1205
|
sibling(instance: object): SiblingAccessor;
|
|
1034
|
-
collectConstructors: (instance: object, flat?: boolean) => (
|
|
1206
|
+
collectConstructors: (instance: object, flat?: boolean) => (DefineNewableOrCallable | string)[];
|
|
1035
1207
|
merge<A extends object, B extends object>(
|
|
1036
1208
|
a: A,
|
|
1037
1209
|
b: B,
|
|
@@ -1048,7 +1220,7 @@ export interface UtilsCollection {
|
|
|
1048
1220
|
): InstanceOfTypeRegistry<K> | undefined;
|
|
1049
1221
|
parent<T extends object>(instance: T, path: string): object | undefined;
|
|
1050
1222
|
toJSON<T extends object>(instance: T): string;
|
|
1051
|
-
[key: string]:
|
|
1223
|
+
[key: string]: UtilFunction;
|
|
1052
1224
|
}
|
|
1053
1225
|
|
|
1054
1226
|
// Main mnemonica module interface - represents the exported module object.
|
|
@@ -1064,7 +1236,10 @@ export interface MnemonicaModule<Registry extends object = {}>
|
|
|
1064
1236
|
target?: T,
|
|
1065
1237
|
config?: constructorOptions
|
|
1066
1238
|
) => <U extends Constructor<object>>(cstr: U) => DecoratedClass<U, Registry>;
|
|
1067
|
-
registerHook: <
|
|
1239
|
+
registerHook: <
|
|
1240
|
+
T extends object,
|
|
1241
|
+
HT extends hooksTypes = hooksTypes
|
|
1242
|
+
>(Constructor: IDEF<T>, hookType: HT, cb: typedHook<HT, object, T>) => void;
|
|
1068
1243
|
|
|
1069
1244
|
// Descriptors
|
|
1070
1245
|
defaultTypes: TypesCollection<Registry>;
|
|
@@ -1100,7 +1275,7 @@ export interface MnemonicaModule<Registry extends object = {}>
|
|
|
1100
1275
|
getProps: (instance: object) => PropsType | undefined;
|
|
1101
1276
|
setProps: (instance: object, values: object) => string[] | false;
|
|
1102
1277
|
findSubTypeFromParent: (instance: object | undefined, subType: string) => object | null;
|
|
1103
|
-
isClass: (fn:
|
|
1278
|
+
isClass: (fn: DefineNewableOrCallable) => boolean;
|
|
1104
1279
|
|
|
1105
1280
|
// createTypesCollection
|
|
1106
1281
|
createTypesCollection: CreateTypesCollectionFunction;
|
|
@@ -70,15 +70,6 @@ export const collectConstructors = (self: object, asSequence = false) => {
|
|
|
70
70
|
addToSequence('Object: null prototype');
|
|
71
71
|
break;
|
|
72
72
|
}
|
|
73
|
-
/*
|
|
74
|
-
TODO: show full chain and test it !!!
|
|
75
|
-
else {
|
|
76
|
-
// proto may be either empty object or null
|
|
77
|
-
// so typeof will always be 'object'
|
|
78
|
-
// eslint-disable-next-line no-lonely-if
|
|
79
|
-
addToSequence('... plain object ...');
|
|
80
|
-
}
|
|
81
|
-
*/
|
|
82
73
|
// so here we go deeper to the chain
|
|
83
74
|
}
|
|
84
75
|
return constructors;
|
package/src/utils/fork.ts
CHANGED
|
@@ -5,10 +5,11 @@ import {
|
|
|
5
5
|
} from '../api/types/Props';
|
|
6
6
|
import { InstanceCreator } from '../api/types/InstanceCreator';
|
|
7
7
|
import TypesUtils from '../api/utils/index';
|
|
8
|
+
import type { ForkInvoker } from '../types';
|
|
8
9
|
|
|
9
10
|
const { reflectPrimitiveWrappers } = TypesUtils;
|
|
10
11
|
|
|
11
|
-
export const fork = <T extends object>(instance: T):
|
|
12
|
+
export const fork = <T extends object>(instance: T): ForkInvoker<T> => {
|
|
12
13
|
|
|
13
14
|
const props = getProps(instance) as Props;
|
|
14
15
|
|
package/src/utils/index.ts
CHANGED
|
@@ -68,7 +68,7 @@ export const utils = {
|
|
|
68
68
|
|
|
69
69
|
...Object.entries( utilsUnWrapped )
|
|
70
70
|
.reduce(
|
|
71
|
-
( methods: { [ index: string ]:
|
|
71
|
+
( methods: { [ index: string ]: WrappableMethod }, util ) => {
|
|
72
72
|
const [ name, fn ] = util;
|
|
73
73
|
methods[ name ] = wrapThis( fn );
|
|
74
74
|
return methods;
|
package/src/utils/parse.ts
CHANGED
|
@@ -15,6 +15,7 @@ const {
|
|
|
15
15
|
|
|
16
16
|
import { extract } from './extract';
|
|
17
17
|
import type {
|
|
18
|
+
InstanceConstructor,
|
|
18
19
|
EmptyParsed, Parsed
|
|
19
20
|
} from '../types';
|
|
20
21
|
|
|
@@ -41,7 +42,7 @@ export function parse<T extends object> ( self: T | null ): Parsed<T> | EmptyPar
|
|
|
41
42
|
return nullResult;
|
|
42
43
|
}
|
|
43
44
|
|
|
44
|
-
if ( !self || !( self as { constructor?:
|
|
45
|
+
if ( !self || !( self as { constructor?: InstanceConstructor } ).constructor ) {
|
|
45
46
|
throw new WRONG_MODIFICATION_PATTERN;
|
|
46
47
|
}
|
|
47
48
|
|