mnemonica 1.4.9 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,36 @@
1
+ 'use strict';
2
+
3
+ import type {
4
+ Hookable, hook
5
+ } from '../../types';
6
+
7
+ import { resolveHookType } from './hookTypes';
8
+
9
+ // true when the callback was registered; an unknown callback is not an error.
10
+ // The exact callback is removed: a Set holds it by reference.
11
+ export const unregisterHook = function ( this: Hookable, name: string, cb: hook ): boolean {
12
+
13
+ const hookType = resolveHookType(
14
+ name,
15
+ cb
16
+ );
17
+
18
+ const callbacks = this.hooks[ hookType ];
19
+ if ( callbacks === undefined ) {
20
+ return false;
21
+ }
22
+
23
+ const removed = callbacks.delete( cb );
24
+
25
+ // the last one out: an empty entry would still run the flow checker
26
+ // on every construction (see invokeHook)
27
+ if ( callbacks.size === 0 ) {
28
+ Reflect.deleteProperty(
29
+ this.hooks,
30
+ hookType
31
+ );
32
+ }
33
+
34
+ return removed;
35
+
36
+ };
package/src/api/index.ts CHANGED
@@ -3,6 +3,7 @@
3
3
  import {
4
4
  invokeHook,
5
5
  registerHook,
6
+ unregisterHook,
6
7
  registerFlowChecker,
7
8
  } from './hooks';
8
9
  import {
@@ -16,6 +17,7 @@ export * as errors from './errors';
16
17
  export const hooks = {
17
18
  invokeHook,
18
19
  registerHook,
20
+ unregisterHook,
19
21
  registerFlowChecker,
20
22
  };
21
23
 
@@ -27,6 +27,7 @@ import {
27
27
  TypeDef,
28
28
  CollectionDef,
29
29
  constructorOptions,
30
+ TypeConfig,
30
31
  TypeAbsorber,
31
32
  ModificationConstructorFactory,
32
33
  MnemonicaConstructorFactory,
@@ -396,8 +397,8 @@ Object.assign(
396
397
  TypeDescriptor.prototype.define = function (
397
398
  this: TypeDescriptorInstance,
398
399
  TypeOrTypeName: string | DefineNewableOrCallable,
399
- constructHandlerOrConfig?: DefineNewableOrCallable | object,
400
- config?: object
400
+ constructHandlerOrConfig?: DefineNewableOrCallable | object | boolean,
401
+ config?: object | boolean
401
402
  ) {
402
403
  const result = define.call(
403
404
  define,
@@ -413,7 +414,7 @@ TypeDescriptor.prototype.lazy = function (
413
414
  this: TypeDescriptorInstance,
414
415
  TypeName: string,
415
416
  getter?: LazyDef<object>,
416
- config?: object
417
+ config?: object | boolean
417
418
  ) {
418
419
  const result = lazy.call(
419
420
  this,
@@ -457,7 +458,7 @@ odp(
457
458
  'decorate', {
458
459
  get (this: TypeDescriptorInstance) {
459
460
  const self = this;
460
- const result = function (options?: object) {
461
+ const result = function (options?: object | boolean) {
461
462
  const decorator = function (cstr: DefineNewable) {
462
463
  const { name } = cstr;
463
464
  // define() returns the registered constructor itself —
@@ -551,14 +552,30 @@ const checkDuplicate = function (target: TypesMap, name: string): void {
551
552
  // Both receive the resolved context and return TypeDescriptor.
552
553
  // ============================================================
553
554
 
555
+ // what is given in the config place overlaps defaultOptions (see constants):
556
+ // an object its own keys, a boolean strictChain alone, and a function
557
+ // ModificationConstructor alone. Anything else overlaps nothing
558
+ const makeOwnConfig = function (config: unknown): constructorOptions {
559
+ if (typeof config === 'boolean') {
560
+ const strictChainResult = { strictChain : config };
561
+ return strictChainResult;
562
+ }
563
+ if (typeof config === 'function') {
564
+ const modificatorResult = {
565
+ ModificationConstructor : config as ModificationConstructorFactory
566
+ };
567
+ return modificatorResult;
568
+ }
569
+ const result = typeof config === 'object' && config !== null ? config : {};
570
+ return result;
571
+ };
572
+
554
573
  const createFromDirectHandler = function (
555
574
  defineOrigin: TypeAbsorber,
556
575
  target: TypesMap,
557
576
  name: string,
558
577
  handler: ConstructHandler | undefined,
559
- // always an object here: the define() dispatcher replaces a missing
560
- // config with {}, the lazy path passes its own
561
- config: constructorOptions,
578
+ config: unknown,
562
579
  lazyGetter?: LazyDef<object>
563
580
  ) {
564
581
  // for a lazy type the handler stays the empty shim: the real constructor
@@ -574,13 +591,13 @@ const createFromDirectHandler = function (
574
591
  SymbolConstructorName
575
592
  );
576
593
 
577
- if (config instanceof Function) {
578
- config = {
579
- ModificationConstructor : config as ModificationConstructorFactory
580
- };
581
- }
582
-
583
- config.asClass = asClass;
594
+ // the detected mode goes into the type's own copy, never into the
595
+ // caller's options object
596
+ const typeConfig: TypeConfig = Object.assign(
597
+ {},
598
+ makeOwnConfig(config),
599
+ { asClass }
600
+ );
584
601
 
585
602
  const proto = (
586
603
  hop(
@@ -608,7 +625,7 @@ const createFromDirectHandler = function (
608
625
  name,
609
626
  makeConstructHandler,
610
627
  proto,
611
- config,
628
+ typeConfig,
612
629
  lazyGetter
613
630
  );
614
631
  return result;
@@ -625,7 +642,7 @@ const createFromLazyDefinition = function (
625
642
  target: TypesMap,
626
643
  name: string,
627
644
  getter: LazyDef<object>,
628
- config: constructorOptions
645
+ config: unknown
629
646
  ) {
630
647
 
631
648
  const result = createFromDirectHandler(
@@ -650,15 +667,13 @@ export const define = function (
650
667
  this: unknown,
651
668
  subtypes: TypesMap,
652
669
  TypeOrTypeName: string | DefineNewableOrCallable,
653
- constructHandlerOrConfig?: DefineNewableOrCallable | object,
654
- config?: object
670
+ constructHandlerOrConfig?: DefineNewableOrCallable | object | boolean,
671
+ config?: object | boolean
655
672
  ): TypeClass {
656
673
 
657
- if (!config || (typeof config !== 'object' && typeof config !== 'function')) {
658
- config = {};
659
- }
660
-
661
- // --- Branch: function passed as first arg ---
674
+ // --- Branch: function or class passed as first arg ---
675
+ // its own name is the TypeName, so the arguments shift left by one:
676
+ // the second is the config, and nothing after the config is read
662
677
  if (typeof TypeOrTypeName === 'function') {
663
678
  const fn = TypeOrTypeName;
664
679
  if (fn.name) {
@@ -671,7 +686,7 @@ export const define = function (
671
686
  subtypes,
672
687
  fn.name,
673
688
  fn,
674
- config
689
+ constructHandlerOrConfig
675
690
  );
676
691
  return defineResult;
677
692
  }
@@ -689,8 +704,8 @@ export const define = function (
689
704
  let handler: ConstructHandler | undefined;
690
705
  if (typeof constructHandlerOrConfig === 'function') {
691
706
  handler = constructHandlerOrConfig as ConstructHandler;
692
- } else if (typeof constructHandlerOrConfig === 'object') {
693
- config = constructHandlerOrConfig as constructorOptions;
707
+ } else if (typeof constructHandlerOrConfig === 'object' || typeof constructHandlerOrConfig === 'boolean') {
708
+ config = constructHandlerOrConfig;
694
709
  }
695
710
 
696
711
  const {
@@ -710,7 +725,7 @@ export const define = function (
710
725
  target,
711
726
  name,
712
727
  handler,
713
- config as constructorOptions
728
+ config
714
729
  );
715
730
  return result;
716
731
  };
@@ -720,7 +735,7 @@ export const lazy = function (
720
735
  subtypes: TypesMap,
721
736
  TypeName: string,
722
737
  getter?: LazyDef<object>,
723
- config?: object
738
+ config?: object | boolean
724
739
  ): TypeClass {
725
740
 
726
741
  // the unnamed lazy(() => Constructor) form is dropped: the callback is
@@ -748,10 +763,6 @@ export const lazy = function (
748
763
  throw new WRONG_MODIFICATION_PATTERN( msg );
749
764
  }
750
765
 
751
- if (!config || (typeof config !== 'object' && typeof config !== 'function')) {
752
- config = {};
753
- }
754
-
755
766
  // the same dotted-path resolution as define's string branch:
756
767
  // lazy('Parent.Child', ...) lands under Parent, and a taken name fails
757
768
  // with the descriptor's ALREADY_DECLARED, exactly like define
@@ -767,7 +778,7 @@ export const lazy = function (
767
778
  target,
768
779
  name,
769
780
  getter,
770
- config as constructorOptions
781
+ config
771
782
  );
772
783
  return lazyResult;
773
784
  };
@@ -35,6 +35,7 @@ import * as hooksAPI from '../../api/hooks';
35
35
 
36
36
  const {
37
37
  registerHook,
38
+ unregisterHook,
38
39
  invokeHook,
39
40
  registerFlowChecker,
40
41
  } = hooksAPI;
@@ -216,7 +217,7 @@ odp(
216
217
  {
217
218
  get (this: TypesCollection) {
218
219
  const self = this;
219
- const result = function (config?: object) {
220
+ const result = function (config?: object | boolean) {
220
221
  const decorator = function (cstr: DefineNewable) {
221
222
  const { name } = cstr;
222
223
  const defineResult = self.define(
@@ -258,27 +259,35 @@ odp(
258
259
  }
259
260
  );
260
261
 
261
- odp(
262
- TypesCollection.prototype,
263
- 'registerHook',
264
- {
265
- get (this: TypesCollection) {
266
-
267
- const self = this;
268
- const result = function (hookName: string, hookCallback: hook) {
269
- // return proto.registerHook.call( typesCollections.get( self ), hookName, hookCallback );
270
- const hookResult = registerHook.call(
271
- self,
272
- hookName,
273
- hookCallback
274
- );
275
- return hookResult;
276
- }.bind(this);
277
- return result;
278
- },
279
- enumerable : true
280
- }
281
- );
262
+ // on / off are the same two functions under short names
263
+ Object.entries({
264
+ registerHook,
265
+ on : registerHook,
266
+ unregisterHook,
267
+ off : unregisterHook,
268
+ }).forEach(([ methodName, hookMethod ]) => {
269
+ odp(
270
+ TypesCollection.prototype,
271
+ methodName,
272
+ {
273
+ get (this: TypesCollection) {
274
+
275
+ const self = this;
276
+ const result = function (hookName: string, hookCallback: hook) {
277
+ // return proto.registerHook.call( typesCollections.get( self ), hookName, hookCallback );
278
+ const hookResult = hookMethod.call(
279
+ self,
280
+ hookName,
281
+ hookCallback
282
+ );
283
+ return hookResult;
284
+ }.bind(this);
285
+ return result;
286
+ },
287
+ enumerable : true
288
+ }
289
+ );
290
+ });
282
291
 
283
292
  odp(
284
293
  TypesCollection.prototype,