inner-state.js 977 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. const {addReadProp} = require('./utils');
  2. /**
  3. * @private
  4. * @class InnerState
  5. * @description
  6. * Implements support for private/inner state object inside the class,
  7. * which can be accessed by a derived class via hidden read-only property _inner.
  8. */
  9. class InnerState {
  10. constructor(initialState) {
  11. addReadProp(this, '_inner', {}, true);
  12. if (initialState && typeof initialState === 'object') {
  13. this.extendState(initialState);
  14. }
  15. }
  16. /**
  17. * Extends or overrides inner state with the specified properties.
  18. *
  19. * Only own properties are used, i.e. inherited ones are skipped.
  20. */
  21. extendState(state) {
  22. for (const a in state) {
  23. // istanbul ignore else
  24. if (Object.prototype.hasOwnProperty.call(state, a)) {
  25. this._inner[a] = state[a];
  26. }
  27. }
  28. }
  29. }
  30. /**
  31. * @member InnerState#_inner
  32. * Private/Inner object state.
  33. */
  34. module.exports = {InnerState};