All Downloads are FREE. Search and download functionalities are using the official Maven repository.

package.src.controller-mixin.js Maven / Gradle / Ivy

There is a newer version: 24.5.3
Show newest version
/**
 * @license
 * Copyright (c) 2021 - 2024 Vaadin Ltd.
 * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
 */
import { dedupingMixin } from '@polymer/polymer/lib/utils/mixin.js';

/**
 * @typedef ReactiveController
 * @type {import('lit').ReactiveController}
 */

/**
 * A mixin for connecting controllers to the element.
 *
 * @polymerMixin
 */
export const ControllerMixin = dedupingMixin((superClass) => {
  // If the superclass extends from LitElement,
  // use its own controllers implementation.
  if (typeof superClass.prototype.addController === 'function') {
    return superClass;
  }

  return class ControllerMixinClass extends superClass {
    constructor() {
      super();

      /**
       * @type {Set}
       */
      this.__controllers = new Set();
    }

    /** @protected */
    connectedCallback() {
      super.connectedCallback();

      this.__controllers.forEach((c) => {
        if (c.hostConnected) {
          c.hostConnected();
        }
      });
    }

    /** @protected */
    disconnectedCallback() {
      super.disconnectedCallback();

      this.__controllers.forEach((c) => {
        if (c.hostDisconnected) {
          c.hostDisconnected();
        }
      });
    }

    /**
     * Registers a controller to participate in the element update cycle.
     *
     * @param {ReactiveController} controller
     * @protected
     */
    addController(controller) {
      this.__controllers.add(controller);
      // Call hostConnected if a controller is added after the element is attached.
      if (this.$ !== undefined && this.isConnected && controller.hostConnected) {
        controller.hostConnected();
      }
    }

    /**
     * Removes a controller from the element.
     *
     * @param {ReactiveController} controller
     * @protected
     */
    removeController(controller) {
      this.__controllers.delete(controller);
    }
  };
});




© 2015 - 2024 Weber Informatics LLC | Privacy Policy