@lumjs/core 1.25.1 → 1.25.2

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.
Files changed (2) hide show
  1. package/lib/traits.js +56 -2
  2. package/package.json +1 -1
package/lib/traits.js CHANGED
@@ -9,9 +9,9 @@ const getProp = require('./obj/getproperty');
9
9
 
10
10
  const
11
11
  {
12
- def,F,B,
12
+ def,F,B,S,
13
13
  isObj,isArray,isConstructor,isProperty,
14
- needObj,
14
+ needObj,needType,
15
15
  } = require('./types');
16
16
 
17
17
  // Symbol for private storage of composed traits.
@@ -693,10 +693,64 @@ class CoreTrait
693
693
 
694
694
  } // CoreTrait class
695
695
 
696
+ /**
697
+ * Build a Trait registry.
698
+ *
699
+ * @param {object} registry - Object for the registry.
700
+ *
701
+ * Generally the `exports` from a Node.js module would be good here.
702
+ *
703
+ * It will have a `Trait` property added, which is an alias to
704
+ * the `Trait` class constructor.
705
+ *
706
+ * It will also have a `registerTrait(name, value)` function added.
707
+ * This function will add new traits to the registry, using
708
+ * the `name` as its property key. The `value` is either the
709
+ * Trait sub-class constructor `function` itself, or a _lazy-loading_
710
+ * closure `function` that must load and return the actual sub-class
711
+ * constructor when executed. The registry DOES NOT support `object`
712
+ * type traits, or any class that doesn't extend the `Trait` class.
713
+ *
714
+ * @returns {function} The `registerTrait()` function created above.
715
+ * @alias module:@lumjs/core/traits.makeRegistry
716
+ */
717
+ function makeTraitRegistry(registry)
718
+ {
719
+ needObj(registry, false, 'invalid trait registry object');
720
+
721
+ def(registry, 'Trait', CoreTrait);
722
+
723
+ function registerTrait(name, value)
724
+ {
725
+ needType(S, name, 'invalid trait name');
726
+ needType(F, value, 'invalid trait loader value');
727
+
728
+ if (registry[name] !== undefined)
729
+ {
730
+ console.error("trait already registered", {name,value,registry});
731
+ return;
732
+ }
733
+
734
+ if (CoreTrait.isPrototypeOf(value))
735
+ { // Make it available directly.
736
+ def(registry, name, value, def.e);
737
+ }
738
+ else
739
+ { // Lazy-loading engaged.
740
+ def.lazy(registry, name, value, def.e);
741
+ }
742
+ }
743
+
744
+ def(registry, 'registerTrait', registerTrait);
745
+
746
+ return registerTrait;
747
+ }
748
+
696
749
  module.exports =
697
750
  {
698
751
  compose, composeFully, getComposed, decompose,
699
752
  Trait: CoreTrait, IGNORE_STATIC, ensureProto,
753
+ makeRegistry: makeTraitRegistry,
700
754
 
701
755
  // Undocumented:
702
756
  hasOwn,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lumjs/core",
3
- "version": "1.25.1",
3
+ "version": "1.25.2",
4
4
  "main": "lib/index.js",
5
5
  "exports":
6
6
  {