All files / src SSVGComputed.js

92.85% Statements 13/14
87.5% Branches 7/8
100% Functions 3/3
92.85% Lines 13/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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            1x                       9x 9x   9x 9x 8x           7x             100x   100x   16x   84x 78x     6x                
// @ts-check
 
import { parseDomCallback } from './parser/utils';
import { defaultTo } from './utils';
import d from 'debug';
 
const debug = d('ssvg:computed');
 
/**
 * @typedef {import('./SSVGProperty').default} SSVGProperty
 */
 
export default class SSVGComputed {
  /**
   * @param {SSVGProperty} property
   * @param {Element} element
   */
  constructor(property, element) {
    this.property = property;
    this.element = element;
 
    const attr = this.element.getAttribute('onupdate');
    if (attr) {
      this._onupdate = parseDomCallback(attr, [ '$state' ],
        defaultTo(element.ownerDocument, document).defaultView || undefined);
    }
  }
 
  disconnect() {
    this.property.disconnect();
  }
 
  /**
   * @param {ssvg.$State} state
   */
  compute(state) {
    try {
      // @ts-ignore: added to the DOM
      if (this.element.onupdate) {
        // @ts-ignore: added to the DOM
        this.property.value = this.element.onupdate(state);
      }
      else if (this._onupdate) {
        this.property.value = this._onupdate.call(this.element, state);
      }
      else {
        debug('onupdate not set on %s', this.property.name);
      }
    }
    catch (ex) {
      debug("failed to compute on %s: %s", this.property.name, ex);
    }
  }
}