All files / ima/page/state PageStateManagerDecorator.js

100% Statements 15/15
75% Branches 3/4
100% Functions 6/6
100% Lines 15/15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81        3x                           5x             5x             5x             1x             2x 2x 2x 2x     2x 1x               1x             2x             1x       3x  
import ns from '../../namespace';
import PageStateManager from './PageStateManager';
import GenericError from '../../error/GenericError';
 
ns.namespace('ima.page.state');
 
/**
 * Decorator for page state manager, which add logic for limiting Extension
 * competence.
 */
export default class PageStateManagerDecorator extends PageStateManager {
  /**
	 * Initializes the page state manager decorator.
	 *
	 * @param {PageStateManager} pageStateManager
	 * @param {string[]} allowedStateKeys
	 */
  constructor(pageStateManager, allowedStateKeys) {
    super();
 
    /**
		 * The current page state manager.
		 *
		 * @type {PageStateManager}
		 */
    this._pageStateManager = pageStateManager;
 
    /**
		 * Array of access keys for state.
		 *
		 * @type {string[]}
		 */
    this._allowedStateKeys = allowedStateKeys;
  }
 
  /**
	 * @inheritdoc
	 */
  clear() {
    this._pageStateManager.clear();
  }
 
  /**
	 * @inheritdoc
	 */
  setState(statePatch) {
    Eif ($Debug) {
      let patchKeys = Object.keys(statePatch);
      let deniedKeys = patchKeys.filter(patchKey => {
        return this._allowedStateKeys.indexOf(patchKey) === -1;
      });
 
      if (deniedKeys.length > 0) {
        throw new GenericError(
          `Extension can not set state for keys ` +
            `${deniedKeys.join()}. Check your extension or add keys ` +
            `${deniedKeys.join()} to getAllowedStateKeys.`
        );
      }
    }
 
    this._pageStateManager.setState(statePatch);
  }
 
  /**
	 * @inheritdoc
	 */
  getState() {
    return this._pageStateManager.getState();
  }
 
  /**
	 * @inheritdoc
	 */
  getAllStates() {
    return this._pageStateManager.getAllStates();
  }
}
 
ns.ima.page.state.PageStateManagerDecorator = PageStateManagerDecorator;