Source: tiny/core/utils/pluginTarget.js

/**
 * Mixins functionality to make an object have "plugins".
 *
 * @example
 * function MyObject() {}
 *
 * pluginTarget.mixin(MyObject);
 *
 * @memberof Tiny
 * @param {object} obj - The object to mix into.
 */
function pluginTarget(obj) {
  obj.__plugins = {};

  /**
   * Adds a plugin to an object
   *
   * @param {string} pluginName - The events that should be listed.
   * @param {function} ctor - The constructor function for the plugin.
   */
  obj.registerPlugin = function registerPlugin(pluginName, ctor) {
    obj.__plugins[pluginName] = ctor;
  };

  /**
   * Instantiates all the plugins of this object
   *
   */
  obj.prototype.initPlugins = function initPlugins() {
    this.plugins = this.plugins || {};

    for (const o in obj.__plugins) {
      this.plugins[o] = new (obj.__plugins[o])(this);
    }
  };

  /**
   * Removes all the plugins of this object
   *
   */
  obj.prototype.destroyPlugins = function destroyPlugins() {
    for (const o in this.plugins) {
      this.plugins[o].destroy();
      this.plugins[o] = null;
    }

    this.plugins = null;
  };
}

export default {
  /**
   * Mixes in the properties of the pluginTarget into another object
   *
   * @param {object} obj - The obj to mix into
   */
  mixin: function mixin(obj) {
    pluginTarget(obj);
  },
};
Documentation generated by JSDoc 3.4.3 on Fri Jul 09 2021 19:32:25 GMT+0800 (CST)