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/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'
|
|
@@ -45,18 +108,29 @@ export type ErrorMessageKey =
|
|
|
45
108
|
// Error messages object type
|
|
46
109
|
export type ErrorMessages = Record<ErrorMessageKey, string>;
|
|
47
110
|
|
|
48
|
-
// Hook callback
|
|
111
|
+
// Hook callback as STORED and invoked by invokeHook — its parameter is the
|
|
112
|
+
// DELIVERED payload (deliveredHookOpts: HookInvocation.build()'s output).
|
|
113
|
+
// The registration surface (registerHook) types callbacks per hookType via
|
|
114
|
+
// typedHook instead.
|
|
49
115
|
export interface hook extends CallableFunction {
|
|
50
|
-
(opts:
|
|
116
|
+
(opts: deliveredHookOpts): unknown;
|
|
51
117
|
}
|
|
52
118
|
|
|
53
|
-
//
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
119
|
+
// Flow checker callback registered via registerFlowChecker: invoked with
|
|
120
|
+
// the hook invocation record after each hook run; its return value is
|
|
121
|
+
// collected into the invocation results but otherwise unused.
|
|
122
|
+
export interface FlowChecker extends CallableFunction {
|
|
123
|
+
(opts: object): unknown;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// An error class made by constructError(name, message) — the lib.es5
|
|
127
|
+
// ErrorConstructor idiom, `new`-only (a class cannot be called without it).
|
|
128
|
+
// Each class carries one fixed message (constants ErrorMessages); `addition`
|
|
129
|
+
// is the per-throw detail, appended as `${message} : ${addition}`; `stack`
|
|
130
|
+
// lines, when given, are prepended to the captured stack.
|
|
131
|
+
export interface MnemonicaErrorConstructor extends NewableFunction {
|
|
132
|
+
new (addition?: string, stack?: string[]): MnemonicaError;
|
|
133
|
+
readonly prototype: MnemonicaError;
|
|
60
134
|
}
|
|
61
135
|
|
|
62
136
|
// Errors types map - indexable record of error constructors
|
|
@@ -79,6 +153,29 @@ export interface ErrorProps {
|
|
|
79
153
|
error?: unknown;
|
|
80
154
|
}
|
|
81
155
|
|
|
156
|
+
/**
|
|
157
|
+
* What a failed construction of `T` throws. TypeScript has no checked
|
|
158
|
+
* exceptions, so `new T()` is typed by its happy path only; this names the
|
|
159
|
+
* other path for the `catch` side.
|
|
160
|
+
*
|
|
161
|
+
* - `T & Error` — the errored instance (`blockErrors: true`, the default):
|
|
162
|
+
* the failed construction still produces a `T` layer, and the original
|
|
163
|
+
* error is linked in as the end of its prototype chain, so the thrown
|
|
164
|
+
* value is `instanceof T` AND `instanceof Error`. Its data is read via
|
|
165
|
+
* `getProps(error)` (`ErrorProps`).
|
|
166
|
+
* - `Error` — the original error, propagated unwrapped
|
|
167
|
+
* (`blockErrors: false`), or a mnemonica error thrown before construction.
|
|
168
|
+
*
|
|
169
|
+
* Async constructors reject with the same values.
|
|
170
|
+
*
|
|
171
|
+
* @example
|
|
172
|
+
* function report (failed: CreationError<Widget>) {
|
|
173
|
+
* if (failed instanceof Widget) { failed.size; failed.message; }
|
|
174
|
+
* }
|
|
175
|
+
* try { new Widget(1); } catch (e) { if (e instanceof Error) report(e); }
|
|
176
|
+
*/
|
|
177
|
+
export type CreationError<T extends object> = ( T & Error ) | Error;
|
|
178
|
+
|
|
82
179
|
/**
|
|
83
180
|
* Internal Type Constructor.
|
|
84
181
|
* "Type" here is used in the Computer Science sense — an interface/contract
|
|
@@ -87,6 +184,7 @@ export interface ErrorProps {
|
|
|
87
184
|
* shape, distinct from user-facing TypeConstructor below.
|
|
88
185
|
*/
|
|
89
186
|
export interface _Internal_TC_<ConstructorInstance extends object> {
|
|
187
|
+
/** @throws {CreationError<ConstructorInstance>} when construction fails */
|
|
90
188
|
new(...args: unknown[]): ConstructorInstance;
|
|
91
189
|
(this: ConstructorInstance, ...args: unknown[]): ConstructorInstance;
|
|
92
190
|
readonly prototype: ConstructorInstance & {
|
|
@@ -193,6 +291,15 @@ export type hooksTypes = 'preCreation' | 'postCreation' | 'creationError';
|
|
|
193
291
|
// Hook options passed to hook callbacks
|
|
194
292
|
// P = parent / existent instance (proto)
|
|
195
293
|
// T = child / inherited instance (type being created)
|
|
294
|
+
//
|
|
295
|
+
// This is the payload at invokeHook's INPUT boundary: InstanceCreator's
|
|
296
|
+
// hookData carries `creator` (the InstanceCreator itself), and so may a
|
|
297
|
+
// manual invokeHook caller. HookInvocation converts creator into the bound
|
|
298
|
+
// throwModificationError of the DELIVERED payload (deliveredHookOpts below)
|
|
299
|
+
// — callbacks never see `creator`. Merged with optional post-hook fields on
|
|
300
|
+
// purpose: invokePreHooks' input has no instance yet, and a manual
|
|
301
|
+
// invokeHook caller may pass a partial payload. The CONSUMPTION side
|
|
302
|
+
// (registerHook callbacks) is typed per hookType by typedHookOpts below.
|
|
196
303
|
export type hooksOpts<P = object, T = P> = {
|
|
197
304
|
TypeName: string;
|
|
198
305
|
type: TypeDef;
|
|
@@ -202,6 +309,71 @@ export type hooksOpts<P = object, T = P> = {
|
|
|
202
309
|
creator?: { throwModificationError(error: Error): void };
|
|
203
310
|
};
|
|
204
311
|
|
|
312
|
+
// The DELIVERED hook payload — what invokeHook's HookInvocation rebuild
|
|
313
|
+
// actually hands to callbacks: the base quartet always; inheritedInstance
|
|
314
|
+
// and the bound throwModificationError only on post hooks. Both stay
|
|
315
|
+
// OPTIONAL here because a manual invokeHook caller may deliver a partial
|
|
316
|
+
// payload — the registration surface (typedHookOpts) types them per
|
|
317
|
+
// hookType instead.
|
|
318
|
+
export type deliveredHookOpts<P = object, T = P> = {
|
|
319
|
+
TypeName: string;
|
|
320
|
+
type: TypeDef;
|
|
321
|
+
args: unknown[];
|
|
322
|
+
existentInstance: P;
|
|
323
|
+
inheritedInstance?: T;
|
|
324
|
+
throwModificationError?: ThrowModificationError;
|
|
325
|
+
};
|
|
326
|
+
|
|
327
|
+
// Hook options typed per hookType — the compile-time face of the DELIVERED
|
|
328
|
+
// hook payload (HookInvocation's rebuild in invokeHook, which is what
|
|
329
|
+
// callbacks receive; InstanceCreator's own hookData is the input side,
|
|
330
|
+
// hooksOpts).
|
|
331
|
+
// P = existent (parent) instance, T = inherited (created) instance.
|
|
332
|
+
// preCreation fires BEFORE the created instance exists, so its opts carry
|
|
333
|
+
// no inheritedInstance and no throwModificationError — accessing them is a
|
|
334
|
+
// compile error.
|
|
335
|
+
export type typedHookOpts<HT extends hooksTypes, P extends object, T extends object> =
|
|
336
|
+
HT extends 'preCreation'
|
|
337
|
+
? {
|
|
338
|
+
TypeName: string;
|
|
339
|
+
type: TypeDef;
|
|
340
|
+
args: unknown[];
|
|
341
|
+
existentInstance: P;
|
|
342
|
+
}
|
|
343
|
+
: {
|
|
344
|
+
TypeName: string;
|
|
345
|
+
type: TypeDef;
|
|
346
|
+
args: unknown[];
|
|
347
|
+
existentInstance: P;
|
|
348
|
+
inheritedInstance: T;
|
|
349
|
+
throwModificationError: ThrowModificationError;
|
|
350
|
+
};
|
|
351
|
+
|
|
352
|
+
// Typed hook callback — the cb of EVERY registerHook call: the callback's
|
|
353
|
+
// opts are inferred from the hookType and the parent/instance pair the hook
|
|
354
|
+
// is registered on. On a TYPE (RegistryEntry, the free registerHook) P/T are
|
|
355
|
+
// the real parent/created instance types; on a COLLECTION (Hookable) they
|
|
356
|
+
// stay `object` — a collection mixes every registered type, so narrowing to
|
|
357
|
+
// a concrete instance is the callback's business (instanceof lookup('User'))
|
|
358
|
+
// — but the preCreation/post payload split holds at both levels.
|
|
359
|
+
export interface typedHook<
|
|
360
|
+
HT extends hooksTypes,
|
|
361
|
+
P extends object = object,
|
|
362
|
+
T extends object = object
|
|
363
|
+
> extends CallableFunction {
|
|
364
|
+
(opts: typedHookOpts<HT, P, T>): unknown;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
// Constructor surface the free registerHook() accepts: any constructor of T
|
|
368
|
+
// instances with a registerHook method. Deliberately NOT DecoratedClass<T> —
|
|
369
|
+
// inference through DecoratedClass's InstanceType<T> conditional picks a wrong
|
|
370
|
+
// T candidate, and inferring from a generic registerHook member resets T to
|
|
371
|
+
// its default; here T is inferred from Constructor<T>'s return position and
|
|
372
|
+
// the precise cb typing comes from the free function's own cb parameter.
|
|
373
|
+
export type HookableConstructor<T extends object> = Constructor<T> & {
|
|
374
|
+
registerHook(hookType: hooksTypes, cb: AnyHookCallback): void;
|
|
375
|
+
};
|
|
376
|
+
|
|
205
377
|
// Callback passed into ModificationConstructor to attach internal props to the prototype
|
|
206
378
|
export interface AddPropsCallback extends CallableFunction {
|
|
207
379
|
(proto: object): void;
|
|
@@ -407,6 +579,24 @@ export type LookedUpConstructor<
|
|
|
407
579
|
lookup: NestedTypeLookup<Registry, Path>;
|
|
408
580
|
};
|
|
409
581
|
|
|
582
|
+
// Typed registerHook surface for lookup results whose registry entries are
|
|
583
|
+
// bare constructors (tactica-emitted or hand-written TypeRegistry entries):
|
|
584
|
+
// their lookup results would otherwise lack registerHook entirely. The
|
|
585
|
+
// created-instance type is recovered from the entry's construct/call return.
|
|
586
|
+
// Constructors that already carry a typed registerHook (builder-created
|
|
587
|
+
// RegistryEntry values) pass through unchanged.
|
|
588
|
+
export type WithHookRegister<C> = C extends { registerHook: AnyHookCallback }
|
|
589
|
+
? C
|
|
590
|
+
: C extends { new (...args: never[]): infer R }
|
|
591
|
+
? R extends object
|
|
592
|
+
? C & { registerHook<HT extends hooksTypes>(hookType: HT, cb: typedHook<HT, object, R>): void }
|
|
593
|
+
: C
|
|
594
|
+
: C extends { (...args: never[]): infer R }
|
|
595
|
+
? R extends object
|
|
596
|
+
? C & { registerHook<HT extends hooksTypes>(hookType: HT, cb: typedHook<HT, object, R>): void }
|
|
597
|
+
: C
|
|
598
|
+
: C;
|
|
599
|
+
|
|
410
600
|
// Result of a typed lookup: the augmented constructor plus a `lookup` method
|
|
411
601
|
// scoped to the constructor's own type path for relative subtype lookups.
|
|
412
602
|
export type LookupResult<
|
|
@@ -698,10 +888,19 @@ export type InstanceResult<
|
|
|
698
888
|
// It preserves the dotted `Path` so that `.define()` on a looked-up constructor
|
|
699
889
|
// computes the correct child path, but it omits the full `GlobalRegistry`
|
|
700
890
|
// generic to keep hover tooltips readable.
|
|
891
|
+
// `Parent` is the instance type of the type's parent (object for root types),
|
|
892
|
+
// threaded so looked-up constructors type their registerHook callbacks'
|
|
893
|
+
// existentInstance.
|
|
701
894
|
export interface RegistryEntry<
|
|
702
895
|
F extends object,
|
|
703
|
-
Path extends string = ''
|
|
704
|
-
|
|
896
|
+
Path extends string = '',
|
|
897
|
+
Parent extends object = object
|
|
898
|
+
> extends _Internal_TC_<F>, RegistryHolderBase<{}, F, Path> {
|
|
899
|
+
// typed hook registration — preserved through ReplaceConstructorInstance's
|
|
900
|
+
// member preservation, so lookup() results of builder-typed registries
|
|
901
|
+
// get registerHook with inferred opts
|
|
902
|
+
registerHook<HT extends hooksTypes>(hookType: HT, cb: typedHook<HT, Parent, F>): void;
|
|
903
|
+
}
|
|
705
904
|
|
|
706
905
|
// Registry holder base - provides the accumulating .define() method.
|
|
707
906
|
// `Parent` tracks the instance type of the last defined constructor, so chained
|
|
@@ -715,9 +914,9 @@ export interface RegistryHolderBase<
|
|
|
715
914
|
// Legacy overload: define(constructHandler, config?)
|
|
716
915
|
define<SubType extends object>(
|
|
717
916
|
this: RegistryHolderBase<T, Parent, Path>,
|
|
718
|
-
TypeOrTypeName:
|
|
719
|
-
constructHandlerOrConfig?: IDEF<SubType> | object | boolean |
|
|
720
|
-
configOrUndefined?: constructorOptions |
|
|
917
|
+
TypeOrTypeName: DefineNewableOrCallable,
|
|
918
|
+
constructHandlerOrConfig?: IDEF<SubType> | object | boolean | DefineNewableOrCallable,
|
|
919
|
+
configOrUndefined?: constructorOptions | DefineNewableOrCallable | boolean
|
|
721
920
|
): IDefinitorInstance<SubType>;
|
|
722
921
|
|
|
723
922
|
// Modern overload: define(TypeName, constructHandler, config?)
|
|
@@ -735,8 +934,9 @@ export interface RegistryHolderBase<
|
|
|
735
934
|
): IDefinitorInstance<
|
|
736
935
|
F,
|
|
737
936
|
InstanceResult<F>,
|
|
738
|
-
T & Record<ChildPath, RegistryEntry<F, ChildPath>>,
|
|
739
|
-
ChildPath
|
|
937
|
+
T & Record<ChildPath, RegistryEntry<F, ChildPath, Parent>>,
|
|
938
|
+
ChildPath,
|
|
939
|
+
Parent
|
|
740
940
|
>;
|
|
741
941
|
|
|
742
942
|
// Explicit lazy getter: .lazy(() => Constructor, config?)
|
|
@@ -760,8 +960,9 @@ export interface RegistryHolderBase<
|
|
|
760
960
|
): IDefinitorInstance<
|
|
761
961
|
F,
|
|
762
962
|
InstanceResult<F>,
|
|
763
|
-
T & Record<ChildPath, RegistryEntry<F, ChildPath>>,
|
|
764
|
-
ChildPath
|
|
963
|
+
T & Record<ChildPath, RegistryEntry<F, ChildPath, Parent>>,
|
|
964
|
+
ChildPath,
|
|
965
|
+
Parent
|
|
765
966
|
>;
|
|
766
967
|
}
|
|
767
968
|
|
|
@@ -781,11 +982,14 @@ export interface RegistryHolder<
|
|
|
781
982
|
// R = wrapped result instance type
|
|
782
983
|
// Registry = typed registry map for lookup()
|
|
783
984
|
// Path = dotted type path of this constructor (empty for root types)
|
|
985
|
+
// Parent = instance type of the parent type (object for root types);
|
|
986
|
+
// threaded into registerHook's existentInstance typing
|
|
784
987
|
export interface IDefinitorInstance<
|
|
785
988
|
N extends object,
|
|
786
989
|
R extends InstanceResult<N> = InstanceResult<N>,
|
|
787
990
|
Registry extends object = GlobalRegistry,
|
|
788
|
-
Path extends string = ''
|
|
991
|
+
Path extends string = '',
|
|
992
|
+
Parent extends object = object,
|
|
789
993
|
>
|
|
790
994
|
extends RegistryHolderBase<Registry, N, Path> {
|
|
791
995
|
|
|
@@ -796,6 +1000,7 @@ export interface IDefinitorInstance<
|
|
|
796
1000
|
// the line below declares a `new ...` invocation,
|
|
797
1001
|
// so makes interface constructible
|
|
798
1002
|
// new(...args: unknown[]): { [key in keyof R]: R[key] };
|
|
1003
|
+
/** @throws {CreationError<R>} when construction fails */
|
|
799
1004
|
new(...args: unknown[]): R;
|
|
800
1005
|
|
|
801
1006
|
|
|
@@ -808,7 +1013,7 @@ export interface IDefinitorInstance<
|
|
|
808
1013
|
|
|
809
1014
|
decorate: (config?: constructorOptions) => <U extends Constructor<object>>(cstr: U) => DecoratedClass<U, Registry>;
|
|
810
1015
|
|
|
811
|
-
registerHook(hookType:
|
|
1016
|
+
registerHook<HT extends hooksTypes>(hookType: HT, cb: typedHook<HT, Parent, N>): void;
|
|
812
1017
|
|
|
813
1018
|
subtypes: SubtypesMap;
|
|
814
1019
|
|
|
@@ -830,9 +1035,9 @@ export interface IDefinitorInstance<
|
|
|
830
1035
|
export interface TypeAbsorber extends CallableFunction {
|
|
831
1036
|
<T extends object>(
|
|
832
1037
|
this: unknown,
|
|
833
|
-
TypeOrTypeName: string |
|
|
834
|
-
constructHandlerOrConfig?: IDEF<T> | object | boolean |
|
|
835
|
-
configOrUndefined?: constructorOptions |
|
|
1038
|
+
TypeOrTypeName: string | DefineNewableOrCallable,
|
|
1039
|
+
constructHandlerOrConfig?: IDEF<T> | object | boolean | DefineNewableOrCallable,
|
|
1040
|
+
configOrUndefined?: constructorOptions | DefineNewableOrCallable | boolean
|
|
836
1041
|
): IDefinitorInstance<T>;
|
|
837
1042
|
}
|
|
838
1043
|
|
|
@@ -851,9 +1056,9 @@ export interface LazyAbsorber extends CallableFunction {
|
|
|
851
1056
|
): IDefinitorInstance<T>;
|
|
852
1057
|
<T extends object>(
|
|
853
1058
|
this: unknown,
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
1059
|
+
TypeNameOrGetter: string | LazyDef<T>,
|
|
1060
|
+
getterOrConfig: LazyDef<T> | constructorOptions,
|
|
1061
|
+
namedFormConfig?: constructorOptions
|
|
857
1062
|
): IDefinitorInstance<T>;
|
|
858
1063
|
<T extends object>(
|
|
859
1064
|
this: unknown,
|
|
@@ -871,9 +1076,9 @@ export interface LazyAbsorber extends CallableFunction {
|
|
|
871
1076
|
<T extends object>(
|
|
872
1077
|
this: unknown,
|
|
873
1078
|
source: RegistryHolderBase<object, object, string>,
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
1079
|
+
TypeNameOrGetter: string | LazyDef<T>,
|
|
1080
|
+
getterOrConfig: LazyDef<T> | constructorOptions,
|
|
1081
|
+
namedFormConfig?: constructorOptions
|
|
877
1082
|
): IDefinitorInstance<T>;
|
|
878
1083
|
}
|
|
879
1084
|
|
|
@@ -903,8 +1108,13 @@ export interface TypesCollection<
|
|
|
903
1108
|
export interface Hookable {
|
|
904
1109
|
hooks: Record<string, Set<hook>>;
|
|
905
1110
|
invokeHook(hookType: hooksTypes, opts: hooksOpts): Set<unknown>;
|
|
906
|
-
|
|
907
|
-
|
|
1111
|
+
// Collection-level registration: the payload split by hookType holds
|
|
1112
|
+
// (preCreation has no inheritedInstance/throwModificationError; post
|
|
1113
|
+
// hooks carry both, required) but P/T stay `object` — a collection
|
|
1114
|
+
// mixes every registered type, so narrowing to a concrete instance
|
|
1115
|
+
// happens in the callback.
|
|
1116
|
+
registerHook<HT extends hooksTypes>(hookType: HT, cb: typedHook<HT>): void;
|
|
1117
|
+
registerFlowChecker(cb: FlowChecker): void;
|
|
908
1118
|
}
|
|
909
1119
|
|
|
910
1120
|
// createTypesCollection function type
|
|
@@ -931,15 +1141,15 @@ export interface MnemonicaConstructor extends NewableFunction {
|
|
|
931
1141
|
// Type descriptor instance — internal shape of TypeDescriptor objects
|
|
932
1142
|
export interface TypeDescriptorDefine extends CallableFunction {
|
|
933
1143
|
(
|
|
934
|
-
TypeOrTypeName: string |
|
|
935
|
-
constructHandlerOrConfig?:
|
|
1144
|
+
TypeOrTypeName: string | DefineNewableOrCallable,
|
|
1145
|
+
constructHandlerOrConfig?: DefineNewableOrCallable | object,
|
|
936
1146
|
config?: object
|
|
937
1147
|
): TypeClass;
|
|
938
1148
|
}
|
|
939
1149
|
|
|
940
1150
|
export interface TypeDescriptorLazy extends CallableFunction {
|
|
941
1151
|
(
|
|
942
|
-
getter:
|
|
1152
|
+
getter: LazyDef<object>,
|
|
943
1153
|
config?: object
|
|
944
1154
|
): TypeClass;
|
|
945
1155
|
}
|
|
@@ -1029,9 +1239,9 @@ export interface UtilsCollection {
|
|
|
1029
1239
|
pick<T extends object, K extends keyof T>(instance: T, ...args: (K | K[])[]): { [P in K]: T[P] } & {};
|
|
1030
1240
|
pick<T extends object>(instance: T, ...args: (string | string[])[]): Record<string, unknown>;
|
|
1031
1241
|
clone<T extends object>(instance: T): T;
|
|
1032
|
-
fork<T extends object>(instance: T):
|
|
1242
|
+
fork<T extends object>(instance: T): ForkInvoker<T>;
|
|
1033
1243
|
sibling(instance: object): SiblingAccessor;
|
|
1034
|
-
collectConstructors: (instance: object, flat?: boolean) => (
|
|
1244
|
+
collectConstructors: (instance: object, flat?: boolean) => (DefineNewableOrCallable | string)[];
|
|
1035
1245
|
merge<A extends object, B extends object>(
|
|
1036
1246
|
a: A,
|
|
1037
1247
|
b: B,
|
|
@@ -1048,7 +1258,7 @@ export interface UtilsCollection {
|
|
|
1048
1258
|
): InstanceOfTypeRegistry<K> | undefined;
|
|
1049
1259
|
parent<T extends object>(instance: T, path: string): object | undefined;
|
|
1050
1260
|
toJSON<T extends object>(instance: T): string;
|
|
1051
|
-
[key: string]:
|
|
1261
|
+
[key: string]: UtilFunction;
|
|
1052
1262
|
}
|
|
1053
1263
|
|
|
1054
1264
|
// Main mnemonica module interface - represents the exported module object.
|
|
@@ -1064,7 +1274,10 @@ export interface MnemonicaModule<Registry extends object = {}>
|
|
|
1064
1274
|
target?: T,
|
|
1065
1275
|
config?: constructorOptions
|
|
1066
1276
|
) => <U extends Constructor<object>>(cstr: U) => DecoratedClass<U, Registry>;
|
|
1067
|
-
registerHook: <
|
|
1277
|
+
registerHook: <
|
|
1278
|
+
T extends object,
|
|
1279
|
+
HT extends hooksTypes = hooksTypes
|
|
1280
|
+
>(Constructor: IDEF<T>, hookType: HT, cb: typedHook<HT, object, T>) => void;
|
|
1068
1281
|
|
|
1069
1282
|
// Descriptors
|
|
1070
1283
|
defaultTypes: TypesCollection<Registry>;
|
|
@@ -1100,7 +1313,7 @@ export interface MnemonicaModule<Registry extends object = {}>
|
|
|
1100
1313
|
getProps: (instance: object) => PropsType | undefined;
|
|
1101
1314
|
setProps: (instance: object, values: object) => string[] | false;
|
|
1102
1315
|
findSubTypeFromParent: (instance: object | undefined, subType: string) => object | null;
|
|
1103
|
-
isClass: (fn:
|
|
1316
|
+
isClass: (fn: DefineNewableOrCallable) => boolean;
|
|
1104
1317
|
|
|
1105
1318
|
// createTypesCollection
|
|
1106
1319
|
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
|
@@ -41,9 +41,13 @@ const utilsUnWrapped = {
|
|
|
41
41
|
|
|
42
42
|
};
|
|
43
43
|
|
|
44
|
-
const wrapThis = ( method: WrappableMethod ) => {
|
|
44
|
+
const wrapThis = ( collection: object, method: WrappableMethod ) => {
|
|
45
45
|
const result = function ( this: object, instance: object | undefined, ...args: unknown[] ) {
|
|
46
|
-
|
|
46
|
+
// utils.X(undefined) must not silently operate on the utils
|
|
47
|
+
// collection itself: `this` substitutes only for a genuine method
|
|
48
|
+
// call on some OTHER object (the prototype opt-in form)
|
|
49
|
+
const instanceContext = instance !== undefined ? instance
|
|
50
|
+
: ( this !== collection ? this : undefined );
|
|
47
51
|
let wrapResult: unknown;
|
|
48
52
|
if ( new.target ) {
|
|
49
53
|
// wrapThis forwards `new` to the util; WrappableMethod (a plain
|
|
@@ -64,18 +68,15 @@ const wrapThis = ( method: WrappableMethod ) => {
|
|
|
64
68
|
return result;
|
|
65
69
|
};
|
|
66
70
|
|
|
67
|
-
|
|
71
|
+
// one object serves both roles: the exported collection AND the identity
|
|
72
|
+
// wrapThis compares `this` against (a spread copy would defeat the check)
|
|
73
|
+
const wrappedUtils: { [ index: string ]: WrappableMethod } = {};
|
|
74
|
+
Object.entries( utilsUnWrapped )
|
|
75
|
+
.forEach(( util ) => {
|
|
76
|
+
const [ name, fn ] = util;
|
|
77
|
+
wrappedUtils[ name ] = wrapThis( wrappedUtils, fn );
|
|
78
|
+
});
|
|
68
79
|
|
|
69
|
-
|
|
70
|
-
.reduce(
|
|
71
|
-
( methods: { [ index: string ]: CallableFunction }, util ) => {
|
|
72
|
-
const [ name, fn ] = util;
|
|
73
|
-
methods[ name ] = wrapThis( fn );
|
|
74
|
-
return methods;
|
|
75
|
-
},
|
|
76
|
-
{}
|
|
77
|
-
),
|
|
78
|
-
|
|
79
|
-
} as UtilsCollection;
|
|
80
|
+
export const utils = wrappedUtils as UtilsCollection;
|
|
80
81
|
|
|
81
82
|
export { defineStackCleaner } from './defineStackCleaner';
|
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,14 @@ export function parse<T extends object> ( self: T | null ): Parsed<T> | EmptyPar
|
|
|
41
42
|
return nullResult;
|
|
42
43
|
}
|
|
43
44
|
|
|
44
|
-
if (
|
|
45
|
+
if ( typeof self !== 'object' && typeof self !== 'function' ) {
|
|
46
|
+
// every non-object except null (handled above): undefined,
|
|
47
|
+
// primitives — these used to die on a plain TypeError from
|
|
48
|
+
// Reflect.getPrototypeOf instead of the documented error
|
|
49
|
+
throw new WRONG_MODIFICATION_PATTERN;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if ( !( self as { constructor?: InstanceConstructor } ).constructor ) {
|
|
45
53
|
throw new WRONG_MODIFICATION_PATTERN;
|
|
46
54
|
}
|
|
47
55
|
|