{"size": 1215, "ext": "js", "max_stars_repo_path": "js/core/hostcontroller.js", "max_stars_repo_name": "mutablelogic/js-components", "max_stars_repo_stars_event_min_datetime": "2021-10-04T16:58:02.000Z", "max_stars_repo_stars_event_max_datetime": "2021-10-04T16:58:02.000Z", "max_issues_repo_path": "js/core/hostcontroller.js", "max_issues_repo_name": "mutablelogic/js-components", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "js/core/hostcontroller.js", "max_forks_repo_name": "mutablelogic/js-components", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 24.7959183673, "max_line_length": 62, "alphanum_fraction": 0.6263374486}
/*
    js-components
    Web Components Design System
    github.com/mutablelogic/js-components
*/

import Event from './event';
import Model from './model';

/**
 * HostController binds a data source to a host web component.
 * @class
*/
export default class HostController {
  constructor(host, provider, renderer) {
    // Store a reference to the host and provider
    this.$host = host;
    this.$provider = provider;
    this.$renderer = renderer;

    // Register for lifecycle updates
    host.addController(this);
  }

  hostConnected() {
    this.$provider.addEventListener(Event.ADD, (e) => {
      const view = this.$host.add(e.detail);
      if (this.$renderer) {
        this.$renderer(e.detail, view);
      }
    });
    this.$provider.addEventListener(Event.CHANGE, (e) => {
      const view = this.$host.change(e.detail);
      if (this.$renderer) {
        this.$renderer(e.detail, view);
      }
    });
    this.$provider.addEventListener(Event.DELETE, (e) => {
      const view = this.$host.delete(e.detail);
      console.log(`deleted ${e.detail} => ${view}`);
    });
  }

  // eslint-disable-next-line class-methods-use-this
  hostDisconnected() {
    // TODO: Remove event listeners
  }
}
