src/lens_factory.js

  1. import CCCLens from './ccc_lens.js';
  2. import { DEFAULT_FACTORY as jsContainers } from './js_container_factory.js';
  3. /**
  4. * @interface ContainerFactory
  5. * @see Factory
  6. */
  7. /**
  8. * @function ContainerFactory#construct
  9. * @summary Construct a missing container
  10. * @param {Array} keys The keys up to and including the one indexing into the missing container
  11. * @returns {*} The missing container
  12. */
  13. class LensFactory {
  14. /**
  15. * @constructs Factory
  16. * @summary A factory for Lens-derived objects with customized container construction
  17. * @param {Object} spec
  18. * @param {ContainerFactory} spec.containerFactory Factory for containers, used by [Lenses]{@link Lens} created by this Factory, invoked when modify-cloning missing containers in subject data
  19. *
  20. * @description
  21. * When POD container types (Object and Array) are not the desired types to
  22. * construct in a clone -- as with use of immutable containers -- this class
  23. * can be used to build lenses that *do* build the desired types of objects.
  24. *
  25. * This class is often used in conjunction with a JsContainerFactory object
  26. * implementing the container factory.
  27. */
  28. constructor({containerFactory = jsContainers} = {}) {
  29. this.containerFactory = containerFactory;
  30. }
  31. /**
  32. * Construct a lens through the factory
  33. * @param {...*} key A key of the customized lens type
  34. * @returns {Lens} A lens with customized container creation behavior
  35. */
  36. lens(...keys) {
  37. const result = new CCCLens(...keys);
  38. result._containerFactory = this.containerFactory;
  39. return result;
  40. }
  41. }
  42. export default LensFactory;