mnemonica 1.3.7 → 1.3.8
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 +11 -1
- package/build/api/errors/exceptionConstructor.js +3 -3
- package/build/api/errors/index.js +1 -1
- package/build/api/errors/throwModificationError.js +1 -1
- package/build/api/hooks/flowCheckers.d.ts +1 -1
- package/build/api/hooks/flowCheckers.js +1 -1
- package/build/api/hooks/invokeHook.js +1 -1
- package/build/api/types/InstanceCreator.js +6 -7
- package/build/api/types/InstanceModificator.js +6 -3
- package/build/api/types/TypeProxy.js +2 -2
- package/build/api/types/compileNewModificatorFunctionBody.js +1 -1
- package/build/api/types/index.js +18 -5
- package/build/constants/index.js +4 -1
- package/build/descriptors/errors/index.js +1 -1
- package/build/descriptors/types/index.js +14 -1
- package/build/index.js +6 -7
- package/build/types/index.d.ts +35 -0
- package/build/types/index.js +1 -1
- package/build/utils/index.js +3 -1
- package/build/utils/lineage.d.ts +2 -0
- package/build/utils/lineage.js +156 -0
- package/docs/UTILS.md +41 -2
- package/docs/async-constructors.md +6 -0
- package/package.json +4 -2
- package/src/api/errors/exceptionConstructor.ts +7 -5
- package/src/api/errors/index.ts +5 -1
- package/src/api/errors/throwModificationError.ts +1 -1
- package/src/api/hooks/flowCheckers.ts +3 -1
- package/src/api/hooks/invokeHook.ts +1 -1
- package/src/api/types/InstanceCreator.ts +20 -10
- package/src/api/types/InstanceModificator.ts +19 -4
- package/src/api/types/TypeProxy.ts +11 -2
- package/src/api/types/compileNewModificatorFunctionBody.ts +6 -0
- package/src/api/types/index.ts +58 -5
- package/src/constants/index.ts +6 -0
- package/src/descriptors/errors/index.ts +3 -0
- package/src/descriptors/types/index.ts +24 -1
- package/src/index.ts +33 -14
- package/src/types/index.ts +51 -0
- package/src/utils/index.ts +5 -0
- package/src/utils/lineage.ts +220 -0
package/src/api/types/index.ts
CHANGED
|
@@ -64,6 +64,7 @@ const {
|
|
|
64
64
|
WRONG_TYPE_DEFINITION,
|
|
65
65
|
TYPENAME_MUST_BE_A_STRING,
|
|
66
66
|
HANDLER_MUST_BE_A_FUNCTION,
|
|
67
|
+
OPTIONS_ERROR,
|
|
67
68
|
} = ErrorsTypes;
|
|
68
69
|
|
|
69
70
|
// invokeHook
|
|
@@ -95,7 +96,7 @@ export type TypesMap = Map<string, object> & {
|
|
|
95
96
|
[MNEMOSYNE]?: CollectionDef;
|
|
96
97
|
};
|
|
97
98
|
|
|
98
|
-
const
|
|
99
|
+
const typeDescriptorCreator = function (
|
|
99
100
|
this: TypeDescriptorInstance,
|
|
100
101
|
defineOrigin: TypeAbsorber,
|
|
101
102
|
types: TypesMap,
|
|
@@ -124,11 +125,20 @@ const TypeDescriptor = function (
|
|
|
124
125
|
|
|
125
126
|
const title = `${TYPE_TITLE_PREFIX}${TypeName}`;
|
|
126
127
|
|
|
128
|
+
// 'name' is a COLLECTION option only: a user passing it here meant to
|
|
129
|
+
// name the collection, not the type — say so readably
|
|
130
|
+
if ( config !== undefined && typeof config === 'object' && 'name' in config ) {
|
|
131
|
+
const nameError = '\"name\" is a collection option ' +
|
|
132
|
+
'(createTypesCollection({ name })) — it cannot be set on a type';
|
|
133
|
+
throw new OPTIONS_ERROR( nameError );
|
|
134
|
+
}
|
|
127
135
|
config = Object.assign(
|
|
128
136
|
{},
|
|
129
137
|
(collection as Record<symbol, unknown>)[ SymbolConfig ],
|
|
130
138
|
config
|
|
131
139
|
);
|
|
140
|
+
// never inherited into the type's config either
|
|
141
|
+
Reflect.deleteProperty( config, 'name' );
|
|
132
142
|
|
|
133
143
|
const type = Object.assign(
|
|
134
144
|
this,
|
|
@@ -188,7 +198,13 @@ const TypeDescriptor = function (
|
|
|
188
198
|
const result = types.get(TypeName);
|
|
189
199
|
return result;
|
|
190
200
|
|
|
191
|
-
}
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
// the creator is only ever invoked with `new` (the createFrom* call sites
|
|
204
|
+
// below); a function expression carries no construct signature, so it is
|
|
205
|
+
// named once as the constructor interface it is used through — the
|
|
206
|
+
// prototype contract is attached immediately below
|
|
207
|
+
const TypeDescriptor = typeDescriptorCreator as _Internal_TC_<TypeDescriptorInstance>;
|
|
192
208
|
|
|
193
209
|
Object.assign(
|
|
194
210
|
TypeDescriptor.prototype,
|
|
@@ -283,13 +299,14 @@ odp(
|
|
|
283
299
|
const result = function (options?: object) {
|
|
284
300
|
const decorator = function (cstr: CallableFunction) {
|
|
285
301
|
const { name } = cstr;
|
|
302
|
+
// define() returns the registered constructor itself —
|
|
303
|
+
// the full TypeClass is a richer honest type than any cast
|
|
286
304
|
const defineResult = self.define(
|
|
287
305
|
name,
|
|
288
306
|
cstr,
|
|
289
307
|
options
|
|
290
308
|
);
|
|
291
|
-
|
|
292
|
-
return decoratedResult;
|
|
309
|
+
return defineResult;
|
|
293
310
|
};
|
|
294
311
|
return decorator;
|
|
295
312
|
};
|
|
@@ -428,6 +445,9 @@ const createFromDirectHandler = function (
|
|
|
428
445
|
makeConstructHandler,
|
|
429
446
|
proto,
|
|
430
447
|
config
|
|
448
|
+
// new TypeDescriptor() returns the TypeProxy it registers in the
|
|
449
|
+
// subtypes map — a Proxy materializing the TypeClass surface
|
|
450
|
+
// dynamically — so the public constructor contract is named here
|
|
431
451
|
) as unknown as TypeClass;
|
|
432
452
|
return result;
|
|
433
453
|
};
|
|
@@ -456,8 +476,22 @@ const createFromLazyGetter = function (
|
|
|
456
476
|
TypeName
|
|
457
477
|
);
|
|
458
478
|
|
|
479
|
+
// reject the unsupported handler shapes at DEFINE time too — the lazy
|
|
480
|
+
// path used to check only typeof/name, so a getter returning an arrow,
|
|
481
|
+
// a method, a bound function or a generator defined fine and failed
|
|
482
|
+
// (or misbehaved) at construction. Same readable errors as the direct
|
|
483
|
+
// path; checkDuplicate runs first so a taken name still wins with the
|
|
484
|
+
// canonical ALREADY_DECLARED
|
|
485
|
+
classifyConstructHandler(
|
|
486
|
+
TypeName,
|
|
487
|
+
type as ConstructHandler
|
|
488
|
+
);
|
|
489
|
+
|
|
459
490
|
const asClass = isClass(type);
|
|
460
491
|
|
|
492
|
+
// classifyConstructHandler above rejects only no-own-prototype
|
|
493
|
+
// functions; a function with an own NON-OBJECT prototype (prototype = 123)
|
|
494
|
+
// still passes and needs the default-prototype fallback
|
|
461
495
|
const proto = (
|
|
462
496
|
hop(
|
|
463
497
|
type,
|
|
@@ -469,6 +503,14 @@ const createFromLazyGetter = function (
|
|
|
469
503
|
const makeConstructHandler = () => {
|
|
470
504
|
const constructHandler = getter();
|
|
471
505
|
|
|
506
|
+
// and on EVERY construction: the getter may return a new function
|
|
507
|
+
// each time, so the shape check cannot be cached from define time.
|
|
508
|
+
// This is what makes lazy slower than a direct define
|
|
509
|
+
classifyConstructHandler(
|
|
510
|
+
TypeName,
|
|
511
|
+
constructHandler as ConstructHandler
|
|
512
|
+
);
|
|
513
|
+
|
|
472
514
|
odp(
|
|
473
515
|
constructHandler,
|
|
474
516
|
SymbolConstructorName, {
|
|
@@ -491,6 +533,12 @@ const createFromLazyGetter = function (
|
|
|
491
533
|
return handlerResult;
|
|
492
534
|
};
|
|
493
535
|
|
|
536
|
+
// 'name' is a COLLECTION option only (same rejection as the direct path)
|
|
537
|
+
if ( config !== undefined && typeof config === 'object' && 'name' in config ) {
|
|
538
|
+
const nameError = '\"name\" is a collection option ' +
|
|
539
|
+
'(createTypesCollection({ name })) — it cannot be set on a type';
|
|
540
|
+
throw new OPTIONS_ERROR( nameError );
|
|
541
|
+
}
|
|
494
542
|
config = Object.assign(
|
|
495
543
|
{},
|
|
496
544
|
config
|
|
@@ -505,6 +553,8 @@ const createFromLazyGetter = function (
|
|
|
505
553
|
makeConstructHandler,
|
|
506
554
|
proto,
|
|
507
555
|
config
|
|
556
|
+
// same TypeProxy return as createFromDirectHandler: the public
|
|
557
|
+
// TypeClass contract is materialized dynamically by the Proxy
|
|
508
558
|
) as unknown as TypeClass;
|
|
509
559
|
return result;
|
|
510
560
|
};
|
|
@@ -648,8 +698,11 @@ export const lookup = function (
|
|
|
648
698
|
if (!type) {
|
|
649
699
|
return undefined;
|
|
650
700
|
}
|
|
701
|
+
// a type's subtypes map IS the runtime TypesMap (SymbolParentType and
|
|
702
|
+
// MNEMOSYNE are installed at definition time), so the single cast only
|
|
703
|
+
// names that view
|
|
651
704
|
const result = lookup.call(
|
|
652
|
-
type.subtypes as
|
|
705
|
+
type.subtypes as TypesMap,
|
|
653
706
|
NextNestedPath
|
|
654
707
|
);
|
|
655
708
|
return result;
|
package/src/constants/index.ts
CHANGED
|
@@ -81,6 +81,12 @@ const defaultOptions = {
|
|
|
81
81
|
return false;
|
|
82
82
|
},
|
|
83
83
|
|
|
84
|
+
// the collection's name for lineage exports (lethe type.collection);
|
|
85
|
+
// unnamed collections export as 'defaultTypes'
|
|
86
|
+
get name () {
|
|
87
|
+
return '';
|
|
88
|
+
},
|
|
89
|
+
|
|
84
90
|
} as Record<string, unknown>;
|
|
85
91
|
|
|
86
92
|
export const constants = {
|
|
@@ -10,6 +10,9 @@ const { ErrorMessages, } = constants;
|
|
|
10
10
|
|
|
11
11
|
// ErrorsTypes is dynamically built - using MnemonicaErrorConstructor to indicate these are constructable
|
|
12
12
|
export const ErrorsTypes: { [ index: string ]: MnemonicaErrorConstructor } = {
|
|
13
|
+
// BASE_MNEMONICA_ERROR is a class: its construct signature matches the
|
|
14
|
+
// interface, only the (unused) call signature is missing — the map is
|
|
15
|
+
// only ever `new`-ed, so the bridge is safe
|
|
13
16
|
BASE_MNEMONICA_ERROR : BASE_MNEMONICA_ERROR as unknown as MnemonicaErrorConstructor
|
|
14
17
|
};
|
|
15
18
|
|
|
@@ -258,7 +258,10 @@ odp(
|
|
|
258
258
|
TypeNestedPath: string
|
|
259
259
|
) {
|
|
260
260
|
const lookupResult = lookup.call(
|
|
261
|
-
|
|
261
|
+
// a collection's subtypes map IS the runtime TypesMap (the
|
|
262
|
+
// MNEMOSYNE/SymbolParentType props are installed at
|
|
263
|
+
// construction), so the single cast only names that view
|
|
264
|
+
this.subtypes as TypesMap,
|
|
262
265
|
TypeNestedPath
|
|
263
266
|
);
|
|
264
267
|
return lookupResult;
|
|
@@ -373,9 +376,29 @@ const typesCollectionProxyHandler = {
|
|
|
373
376
|
}
|
|
374
377
|
};
|
|
375
378
|
|
|
379
|
+
// every collection exports under exactly one name (lethe type.collection):
|
|
380
|
+
// the first collection — the default one — is 'defaultTypes'; later
|
|
381
|
+
// unnamed collections get 'collection_1', 'collection_2', … in creation
|
|
382
|
+
// order (the scheme tactica writes for mnemographica). No two collections
|
|
383
|
+
// may ever export the same name.
|
|
384
|
+
let defaultCollectionNamed = false;
|
|
385
|
+
let unnamedCollections = 0;
|
|
386
|
+
|
|
376
387
|
const createTypesCollection = (config: Record<string, unknown> = {}) => {
|
|
377
388
|
|
|
378
389
|
const typesCollection = new TypesCollection(config);
|
|
390
|
+
const collectionConfig = (typesCollection as { [ SymbolConfig ]?: Record<string, unknown> })[
|
|
391
|
+
SymbolConfig
|
|
392
|
+
];
|
|
393
|
+
if ( collectionConfig && !collectionConfig.name ) {
|
|
394
|
+
if ( !defaultCollectionNamed ) {
|
|
395
|
+
collectionConfig.name = 'defaultTypes';
|
|
396
|
+
defaultCollectionNamed = true;
|
|
397
|
+
} else {
|
|
398
|
+
unnamedCollections += 1;
|
|
399
|
+
collectionConfig.name = `collection_${unnamedCollections}`;
|
|
400
|
+
}
|
|
401
|
+
}
|
|
379
402
|
const typesCollectionProxy = new Proxy(
|
|
380
403
|
typesCollection,
|
|
381
404
|
typesCollectionProxyHandler
|
package/src/index.ts
CHANGED
|
@@ -13,6 +13,7 @@ import type {
|
|
|
13
13
|
Constructor,
|
|
14
14
|
DecoratedClass,
|
|
15
15
|
TypeClass,
|
|
16
|
+
TypeConstructorBase,
|
|
16
17
|
TypeAbsorber,
|
|
17
18
|
LazyAbsorber,
|
|
18
19
|
MnemonicaModule,
|
|
@@ -180,16 +181,21 @@ export function define <
|
|
|
180
181
|
// explicit-source form: define(source, name, handler, config?)
|
|
181
182
|
// the source may be a TypesCollection (object) or a TypeProxy (function),
|
|
182
183
|
// so detection is: has a callable .define AND the next arg is a string name
|
|
184
|
+
const sourceCandidate = TypeNameOrSource as { define?: unknown };
|
|
183
185
|
const mayBeSource = (
|
|
184
186
|
(typeof TypeNameOrSource === 'object' && TypeNameOrSource !== null) ||
|
|
185
187
|
typeof TypeNameOrSource === 'function'
|
|
186
|
-
) && typeof
|
|
188
|
+
) && typeof sourceCandidate.define === 'function';
|
|
187
189
|
if (mayBeSource && typeof constructHandlerOrName === 'string') {
|
|
188
|
-
|
|
190
|
+
// .define was verified callable above; the cast only adds its signature
|
|
191
|
+
const source = sourceCandidate as { define: TypeAbsorber };
|
|
189
192
|
const sourceDefineResult = source.define(
|
|
190
193
|
constructHandlerOrName,
|
|
191
194
|
configOrHandler as IDEF<T>,
|
|
192
195
|
config
|
|
196
|
+
// facade return: the overload promises R (the caller's constructor
|
|
197
|
+
// view); the delegated define produces that same runtime
|
|
198
|
+
// constructor under a different generic instantiation
|
|
193
199
|
) as unknown as R;
|
|
194
200
|
return sourceDefineResult;
|
|
195
201
|
}
|
|
@@ -201,6 +207,9 @@ export function define <
|
|
|
201
207
|
TypeNameOrSource as string,
|
|
202
208
|
constructHandlerOrName as IDEF<T>,
|
|
203
209
|
configOrHandler as constructorOptions
|
|
210
|
+
// facade return: the overload promises R (the caller's constructor
|
|
211
|
+
// view); the delegated define produces that same runtime
|
|
212
|
+
// constructor under a different generic instantiation
|
|
204
213
|
) as unknown as R;
|
|
205
214
|
return defineResult;
|
|
206
215
|
}
|
|
@@ -247,7 +256,7 @@ export function lazy(
|
|
|
247
256
|
typeof arg1 === 'function'
|
|
248
257
|
) && typeof (arg1 as { lazy?: unknown }).lazy === 'function';
|
|
249
258
|
if (mayBeSource) {
|
|
250
|
-
const source = arg1 as
|
|
259
|
+
const source = arg1 as { lazy: LazyAbsorber };
|
|
251
260
|
const sourceLazyResult = source.lazy(
|
|
252
261
|
arg2 as string | (() => IDEF<object>),
|
|
253
262
|
arg3 as (() => IDEF<object>) | constructorOptions,
|
|
@@ -313,14 +322,16 @@ const $run = function <E extends object, T extends object, S extends Proto<E, T>
|
|
|
313
322
|
// debugger;
|
|
314
323
|
// @ts-expect-error - extracting TypeName from function
|
|
315
324
|
const { TypeName } = Ctor;
|
|
325
|
+
// the returned SubTypeProxy is a plain function at runtime — constructable —
|
|
326
|
+
// so TypeConstructorBase (the named minimal constructor shape) is its
|
|
327
|
+
// honest static type here
|
|
316
328
|
const Cstr = prepareSubtypeForConstruction(
|
|
317
329
|
TypeName,
|
|
318
330
|
entity
|
|
319
|
-
) as
|
|
331
|
+
) as TypeConstructorBase;
|
|
320
332
|
// TODO: check lines below and if Constructor is not mnemonized ...
|
|
321
333
|
if (Cstr === undefined) {
|
|
322
|
-
|
|
323
|
-
throw new ErrorCtor(`[ ${TypeName} ] is not defined as a Type Constructor on used instance`);
|
|
334
|
+
throw new WRONG_MODIFICATION_PATTERN(`[ ${TypeName} ] is not defined as a Type Constructor on used instance`);
|
|
324
335
|
}
|
|
325
336
|
const runResult = new Cstr(...args);
|
|
326
337
|
// @ts-expect-error - returning result as merged proto type
|
|
@@ -338,7 +349,9 @@ export const apply = function <E extends object, T extends object, S extends Pro
|
|
|
338
349
|
Ctor,
|
|
339
350
|
args
|
|
340
351
|
);
|
|
341
|
-
|
|
352
|
+
// S extends Proto<E, T>, which is the same merged entity+ctor shape
|
|
353
|
+
// apply/call/bind promise — the named public result type
|
|
354
|
+
const result = runResult as InstanceResult<Merge<E, T>>;
|
|
342
355
|
return result;
|
|
343
356
|
};
|
|
344
357
|
|
|
@@ -353,7 +366,8 @@ export const call = function <E extends object, T extends object, S extends Prot
|
|
|
353
366
|
Ctor,
|
|
354
367
|
args
|
|
355
368
|
);
|
|
356
|
-
|
|
369
|
+
// same merge narrowing as apply() above
|
|
370
|
+
const result = runResult as InstanceResult<Merge<E, T>>;
|
|
357
371
|
return result;
|
|
358
372
|
};
|
|
359
373
|
|
|
@@ -368,7 +382,8 @@ export const bind = function <E extends object, T extends object, S extends Prot
|
|
|
368
382
|
Ctor,
|
|
369
383
|
args
|
|
370
384
|
);
|
|
371
|
-
|
|
385
|
+
// same merge narrowing as apply() above
|
|
386
|
+
const typedResult = runResult as InstanceResult<Merge<E, T>>;
|
|
372
387
|
return typedResult;
|
|
373
388
|
};
|
|
374
389
|
return result;
|
|
@@ -384,8 +399,11 @@ export const decorate = function <
|
|
|
384
399
|
? target as constructorOptions
|
|
385
400
|
: config;
|
|
386
401
|
|
|
402
|
+
// decorate(SomeType, config?) expects a constructor already carrying
|
|
403
|
+
// .define (a previously defined type) — declared on the value so the
|
|
404
|
+
// decorator body below needs no cast
|
|
387
405
|
const parentType = (target instanceof Function)
|
|
388
|
-
? target as Constructor<object>
|
|
406
|
+
? target as Constructor<object> & { define: TypeAbsorber }
|
|
389
407
|
: undefined;
|
|
390
408
|
|
|
391
409
|
const decorator = function <U extends Constructor<object>>(cstr: U): DecoratedClass<U> {
|
|
@@ -395,16 +413,17 @@ export const decorate = function <
|
|
|
395
413
|
name,
|
|
396
414
|
cstr as IDEF<object>,
|
|
397
415
|
opts
|
|
416
|
+
// decorator contract: define() returns the constructor that
|
|
417
|
+
// wraps cstr; the decorator replaces the class binding with
|
|
418
|
+
// it, so the result is presented as DecoratedClass<U>
|
|
398
419
|
) as unknown as DecoratedClass<U>;
|
|
399
420
|
return decoratorResult;
|
|
400
421
|
}
|
|
401
|
-
const
|
|
402
|
-
define: TypeAbsorber;
|
|
403
|
-
};
|
|
404
|
-
const defineResult = parent.define(
|
|
422
|
+
const defineResult = parentType.define(
|
|
405
423
|
name,
|
|
406
424
|
cstr as IDEF<object>,
|
|
407
425
|
opts
|
|
426
|
+
// same decorator contract as the branch above
|
|
408
427
|
) as unknown as DecoratedClass<U>;
|
|
409
428
|
return defineResult;
|
|
410
429
|
};
|
package/src/types/index.ts
CHANGED
|
@@ -587,6 +587,56 @@ export interface EmptyParsed {
|
|
|
587
587
|
parent: undefined;
|
|
588
588
|
}
|
|
589
589
|
|
|
590
|
+
// --- lineage (the lethe cross-language export) -----------------------------
|
|
591
|
+
|
|
592
|
+
// A JSON-safe value in the lethe lineage graph: instances export as $ref,
|
|
593
|
+
// non-JSON values as tagged placeholders — never an error.
|
|
594
|
+
export type LineageValue =
|
|
595
|
+
| null
|
|
596
|
+
| boolean
|
|
597
|
+
| string
|
|
598
|
+
| number
|
|
599
|
+
| LineageValue[]
|
|
600
|
+
| { $ref: string }
|
|
601
|
+
| { '$mnemonica': 'unsupported'; kind: string }
|
|
602
|
+
| { [ key: string ]: LineageValue };
|
|
603
|
+
|
|
604
|
+
// Where a level's type was DECLARED — not just its name (names collide
|
|
605
|
+
// across levels and collections).
|
|
606
|
+
export interface LineageTypeRef {
|
|
607
|
+
collection: string;
|
|
608
|
+
path: string;
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
export interface LineageNode {
|
|
612
|
+
type: LineageTypeRef;
|
|
613
|
+
// the fields this level set itself — shadowed values stay with their
|
|
614
|
+
// writer
|
|
615
|
+
own: { [ key: string ]: LineageValue };
|
|
616
|
+
// the parent's id, null at the root
|
|
617
|
+
parent: string | null;
|
|
618
|
+
// construction args — present only when lineage() gets { args: true }
|
|
619
|
+
args?: LineageValue;
|
|
620
|
+
// opt-in metadata (e.g. __timestamp__) — present only when
|
|
621
|
+
// lineage() gets { props: [...] }
|
|
622
|
+
props?: { [ key: string ]: LineageValue };
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
// The lethe lineage graph: version "1", heads in argument order, every
|
|
626
|
+
// reachable instance deduplicated at any depth.
|
|
627
|
+
export interface LineageGraph {
|
|
628
|
+
version: '1';
|
|
629
|
+
heads: string[];
|
|
630
|
+
nodes: { [ id: string ]: LineageNode };
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
export interface LineageOptions {
|
|
634
|
+
/** include construction args as node.args */
|
|
635
|
+
args?: boolean;
|
|
636
|
+
/** opt-in getProps keys exported as node.props (e.g. [ '__timestamp__' ]) */
|
|
637
|
+
props?: string[];
|
|
638
|
+
}
|
|
639
|
+
|
|
590
640
|
// Sibling type accessor
|
|
591
641
|
export interface SiblingAccessor {
|
|
592
642
|
(SiblingTypeName: string): TypeClass | undefined;
|
|
@@ -990,6 +1040,7 @@ export interface UtilsCollection {
|
|
|
990
1040
|
parse<T extends object>(self: T): Parsed<T>;
|
|
991
1041
|
parse(self: null): EmptyParsed;
|
|
992
1042
|
deepParse<T extends object>(instance: T): Array<Parsed<T>>;
|
|
1043
|
+
lineage(instances: object[], options?: LineageOptions): LineageGraph;
|
|
993
1044
|
parent<T extends object>(instance: T): object | null;
|
|
994
1045
|
parent<T extends object, K extends ParentPathOfInstance<T> & string>(
|
|
995
1046
|
instance: T,
|
package/src/utils/index.ts
CHANGED
|
@@ -16,6 +16,7 @@ import { clone } from './clone';
|
|
|
16
16
|
import { toJSON } from './toJSON';
|
|
17
17
|
import { parse } from './parse';
|
|
18
18
|
import { deepParse } from './deepParse';
|
|
19
|
+
import { lineage } from './lineage';
|
|
19
20
|
import { merge } from './merge';
|
|
20
21
|
|
|
21
22
|
const utilsUnWrapped = {
|
|
@@ -33,6 +34,7 @@ const utilsUnWrapped = {
|
|
|
33
34
|
|
|
34
35
|
parse,
|
|
35
36
|
deepParse,
|
|
37
|
+
lineage,
|
|
36
38
|
merge,
|
|
37
39
|
|
|
38
40
|
collectConstructors,
|
|
@@ -44,6 +46,9 @@ const wrapThis = ( method: WrappableMethod ) => {
|
|
|
44
46
|
const instanceContext = instance !== undefined ? instance : this;
|
|
45
47
|
let wrapResult: unknown;
|
|
46
48
|
if ( new.target ) {
|
|
49
|
+
// wrapThis forwards `new` to the util; WrappableMethod (a plain
|
|
50
|
+
// function type) declares no construct signature, so the cast
|
|
51
|
+
// bridges that construct call
|
|
47
52
|
wrapResult = new (method as unknown as new (...a: unknown[]) => unknown)(
|
|
48
53
|
instanceContext,
|
|
49
54
|
...args
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// utils.lineage — the lineage walker (lineage-export plan, layer 3).
|
|
4
|
+
// Exports the lineage graph of the given instances in the lethe format —
|
|
5
|
+
// the cross-language contract (@mnemonica/lethe): what survives when live
|
|
6
|
+
// instances are forgotten. Built on the parse() primitive (the one-level
|
|
7
|
+
// snapshot deepParse walks): the recursion follows parse().parent, the
|
|
8
|
+
// REAL parent-instance links, so the per-type prototype hops never appear.
|
|
9
|
+
|
|
10
|
+
import { parse } from './parse';
|
|
11
|
+
import {
|
|
12
|
+
_getProps as getPropsRef, Props
|
|
13
|
+
} from '../api/types/Props';
|
|
14
|
+
import { constants } from '../constants';
|
|
15
|
+
|
|
16
|
+
const { SymbolConfig } = constants;
|
|
17
|
+
|
|
18
|
+
import type {
|
|
19
|
+
LineageGraph,
|
|
20
|
+
LineageNode,
|
|
21
|
+
LineageOptions,
|
|
22
|
+
LineageValue
|
|
23
|
+
} from '../types';
|
|
24
|
+
|
|
25
|
+
// Instance ids are implementation-specific by contract: a per-realm random
|
|
26
|
+
// prefix plus a process-local counter, held in a WeakMap — stable within
|
|
27
|
+
// one realm, never reused, nothing retained, and never compared across
|
|
28
|
+
// processes or languages (the fixture comparison maps them 1:1 in
|
|
29
|
+
// first-encounter order — see @mnemonica/lethe testdata README)
|
|
30
|
+
const realmPrefix = Math.random().toString( 16 )
|
|
31
|
+
.slice( 2, 10 );
|
|
32
|
+
let idCounter = 0;
|
|
33
|
+
const instanceIds = new WeakMap<object, string>();
|
|
34
|
+
const idOf = ( instance: object ): string => {
|
|
35
|
+
let id = instanceIds.get( instance );
|
|
36
|
+
if ( id === undefined ) {
|
|
37
|
+
idCounter += 1;
|
|
38
|
+
id = `${realmPrefix}:${idCounter}`;
|
|
39
|
+
instanceIds.set( instance, id );
|
|
40
|
+
}
|
|
41
|
+
return id;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const unsupported = ( kind: string ): LineageValue => {
|
|
45
|
+
const result: LineageValue = {
|
|
46
|
+
'$mnemonica' : 'unsupported',
|
|
47
|
+
kind
|
|
48
|
+
};
|
|
49
|
+
return result;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const isInstance = ( value: object ): boolean => {
|
|
53
|
+
const instanceCheck = getPropsRef( value ) !== undefined;
|
|
54
|
+
return instanceCheck;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export const lineage = ( instances: object[], options?: LineageOptions ): LineageGraph => {
|
|
58
|
+
|
|
59
|
+
const nodes: Record<string, LineageNode> = {};
|
|
60
|
+
const nodePaths: Record<string, string> = {};
|
|
61
|
+
const visiting = new Set<string>();
|
|
62
|
+
|
|
63
|
+
const exportValue = ( value: unknown, seen: Set<unknown> ): LineageValue => {
|
|
64
|
+
|
|
65
|
+
if ( value === null || value === undefined ) {
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if ( typeof value === 'boolean' || typeof value === 'string' ) {
|
|
70
|
+
return value;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if ( typeof value === 'number' ) {
|
|
74
|
+
if ( Number.isNaN( value ) ) {
|
|
75
|
+
const nanResult = unsupported( 'nan' );
|
|
76
|
+
return nanResult;
|
|
77
|
+
}
|
|
78
|
+
if ( value === Infinity ) {
|
|
79
|
+
const infResult = unsupported( '+inf' );
|
|
80
|
+
return infResult;
|
|
81
|
+
}
|
|
82
|
+
if ( value === -Infinity ) {
|
|
83
|
+
const ninfResult = unsupported( '-inf' );
|
|
84
|
+
return ninfResult;
|
|
85
|
+
}
|
|
86
|
+
return value;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// the schema's kind enum is the cross-language set: JS-only shapes
|
|
90
|
+
// (symbols, bigints) report as 'invalid' — never an error
|
|
91
|
+
if ( typeof value === 'bigint' || typeof value === 'symbol' ) {
|
|
92
|
+
const invalidResult = unsupported( 'invalid' );
|
|
93
|
+
return invalidResult;
|
|
94
|
+
}
|
|
95
|
+
if ( typeof value === 'function' ) {
|
|
96
|
+
const funcResult = unsupported( 'func' );
|
|
97
|
+
return funcResult;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// a field holding another mnemonica instance exports as $ref; the
|
|
101
|
+
// referenced instance joins the graph at first encounter
|
|
102
|
+
if ( isInstance( value as object ) ) {
|
|
103
|
+
const refResult: LineageValue = {
|
|
104
|
+
$ref : visit( value as object )
|
|
105
|
+
};
|
|
106
|
+
return refResult;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if ( seen.has( value ) ) {
|
|
110
|
+
const cycleResult = unsupported( 'cycle' );
|
|
111
|
+
return cycleResult;
|
|
112
|
+
}
|
|
113
|
+
seen.add( value );
|
|
114
|
+
|
|
115
|
+
let objectResult: LineageValue;
|
|
116
|
+
if ( Array.isArray( value ) ) {
|
|
117
|
+
const arrayResult: LineageValue[] = [];
|
|
118
|
+
value.forEach( ( item ) => {
|
|
119
|
+
arrayResult.push( exportValue( item, seen ) );
|
|
120
|
+
} );
|
|
121
|
+
objectResult = arrayResult;
|
|
122
|
+
} else {
|
|
123
|
+
const recordResult: { [ key: string ]: LineageValue } = {};
|
|
124
|
+
Object.keys( value as object ).forEach( ( key ) => {
|
|
125
|
+
recordResult[ key ] = exportValue(
|
|
126
|
+
( value as Record<string, unknown> )[ key ],
|
|
127
|
+
seen
|
|
128
|
+
);
|
|
129
|
+
} );
|
|
130
|
+
objectResult = recordResult;
|
|
131
|
+
}
|
|
132
|
+
seen.delete( value );
|
|
133
|
+
return objectResult;
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
const visit = ( instance: object ): string => {
|
|
137
|
+
|
|
138
|
+
const id = idOf( instance );
|
|
139
|
+
// dedup at any depth; the visiting set also breaks instance-valued
|
|
140
|
+
// field cycles (a field pointing at its own instance)
|
|
141
|
+
if ( nodes[ id ] !== undefined || visiting.has( id ) ) {
|
|
142
|
+
return id;
|
|
143
|
+
}
|
|
144
|
+
visiting.add( id );
|
|
145
|
+
|
|
146
|
+
const parsed = parse( instance );
|
|
147
|
+
|
|
148
|
+
// own fields in declaration order FIRST — a $ref target is exported
|
|
149
|
+
// at first encounter, before the parent link (the encounter order
|
|
150
|
+
// the cross-language comparison maps ids by)
|
|
151
|
+
const own: { [ key: string ]: LineageValue } = {};
|
|
152
|
+
const ownProps = parsed.props as Record<string, unknown>;
|
|
153
|
+
Object.keys( ownProps ).forEach( ( key ) => {
|
|
154
|
+
own[ key ] = exportValue( ownProps[ key ], new Set() );
|
|
155
|
+
} );
|
|
156
|
+
|
|
157
|
+
const baseProps = getPropsRef( instance ) as Props;
|
|
158
|
+
|
|
159
|
+
let nodeArgs: LineageValue | undefined;
|
|
160
|
+
if ( options && options.args ) {
|
|
161
|
+
nodeArgs = exportValue( baseProps.__args__, new Set() );
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
let nodeProps: { [ key: string ]: LineageValue } | undefined;
|
|
165
|
+
if ( options && Array.isArray( options.props ) ) {
|
|
166
|
+
nodeProps = {};
|
|
167
|
+
options.props.forEach( ( key ) => {
|
|
168
|
+
nodeProps![ key ] = exportValue( baseProps[ key ], new Set() );
|
|
169
|
+
} );
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// then the parent — the type path is the parent's path plus this
|
|
173
|
+
// level's name (names alone collide across levels and collections)
|
|
174
|
+
let parentId: string | null = null;
|
|
175
|
+
let path = parsed.name;
|
|
176
|
+
if ( parsed.parent !== null ) {
|
|
177
|
+
parentId = visit( parsed.parent );
|
|
178
|
+
path = `${nodePaths[ parentId ]}.${parsed.name}`;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const { __collection__: collection } = baseProps;
|
|
182
|
+
// every collection carries a name by construction (the default
|
|
183
|
+
// collection is 'defaultTypes', unnamed customs are auto-named), so
|
|
184
|
+
// no fallback is needed here
|
|
185
|
+
const collectionConfig = ( collection as unknown as { [ SymbolConfig ]?: { name?: string } } )[
|
|
186
|
+
SymbolConfig
|
|
187
|
+
]!;
|
|
188
|
+
const collectionName: string = collectionConfig.name!;
|
|
189
|
+
|
|
190
|
+
const node: LineageNode = {
|
|
191
|
+
type : {
|
|
192
|
+
collection : collectionName,
|
|
193
|
+
path
|
|
194
|
+
},
|
|
195
|
+
own,
|
|
196
|
+
parent : parentId
|
|
197
|
+
};
|
|
198
|
+
if ( nodeArgs !== undefined ) {
|
|
199
|
+
node.args = nodeArgs;
|
|
200
|
+
}
|
|
201
|
+
if ( nodeProps !== undefined ) {
|
|
202
|
+
node.props = nodeProps;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
nodes[ id ] = node;
|
|
206
|
+
nodePaths[ id ] = path;
|
|
207
|
+
visiting.delete( id );
|
|
208
|
+
return id;
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
const heads = instances.map( ( instance ) => visit( instance ) );
|
|
212
|
+
|
|
213
|
+
const result: LineageGraph = {
|
|
214
|
+
version : '1',
|
|
215
|
+
heads,
|
|
216
|
+
nodes
|
|
217
|
+
};
|
|
218
|
+
return result;
|
|
219
|
+
|
|
220
|
+
};
|