<commit_before>import { capitalize } from '@ember/string';
import { scheduleOnce, next } from '@ember/runloop';
import { A } from '@ember/array';
import Mixin from '@ember/object/mixin';
import { computed, set, get } from '@ember/object';
import { MDCRippleFoundation } from '@material/ripple';
import getElementProperty from '../utils/get-element-property';
import { createRippleAdapter } from '../utils/mdc-ripple-adapter';
import styleComputed from '../utils/style-computed';

export const addClass = (className, component) => {
  get(component, 'mdcClasses').addObject(className);
};

export const removeClass = (className, component) => {
  get(component, 'mdcClasses').removeObject(className);
};

/**
 * @typedef {Ember.Mixin} MDCComponent
 */
export const MDCComponent = Mixin.create({
  //region Ember Hooks
  init() {
    this._super(...arguments);
    set(this, 'mdcClasses', A([]));
    set(this, 'mdcInteractionHandlers', A([]));
    set(this, 'mdcStyles', {});
  },

  didInsertElement() {
    this._super(...arguments);
    // We don't want to init the foundation until the next run loop, because
    // many components rely on child components registering themselves, which
    // tend to happen in their own didInsertElement hooks that run _after_ the
    // parent's didInsertElement.
    scheduleOnce('afterRender', this, () => {
      this._attachMdcInteractionHandlers();
      if (get(this, 'createFoundation')) {
        const foundation = this.createFoundation();
        set(this, 'foundation', foundation);
        foundation.init();
        this.afterFoundationCreation(foundation);
      }
      if (get(this, 'ripple')) {
        const rippleFoundation = new MDCRippleFoundation(
          createRippleAdapter(this, this.rippleOptions())
        );
        set(this, 'rippleFoundation', rippleFoundation);
        rippleFoundation.init();
      }
    });
  },

  willDestroyElement() {
    this._super(...arguments);
    const foundation = get(this, 'foundation');
    const rippleFoundation = get(this, 'rippleFoundation');
    if (foundation) {
      foundation.destroy();
    }
    if (rippleFoundation) {
      rippleFoundation.destroy();
    }
  },
  //endregion
<commit_msg>:bug: Don't set foundations on destroyed components (#60)<commit_after>import { capitalize } from '@ember/string';
import { scheduleOnce, next } from '@ember/runloop';
import { A } from '@ember/array';
import Mixin from '@ember/object/mixin';
import { computed, set, get } from '@ember/object';
import { MDCRippleFoundation } from '@material/ripple';
import getElementProperty from '../utils/get-element-property';
import { createRippleAdapter } from '../utils/mdc-ripple-adapter';
import styleComputed from '../utils/style-computed';

export const addClass = (className, component) => {
  get(component, 'mdcClasses').addObject(className);
};

export const removeClass = (className, component) => {
  get(component, 'mdcClasses').removeObject(className);
};

/**
 * @typedef {Ember.Mixin} MDCComponent
 */
export const MDCComponent = Mixin.create({
  //region Ember Hooks
  init() {
    this._super(...arguments);
    set(this, 'mdcClasses', A([]));
    set(this, 'mdcInteractionHandlers', A([]));
    set(this, 'mdcStyles', {});
  },

  didInsertElement() {
    this._super(...arguments);
    // We don't want to init the foundation until the next run loop, because
    // many components rely on child components registering themselves, which
    // tend to happen in their own didInsertElement hooks that run _after_ the
    // parent's didInsertElement.
    scheduleOnce('afterRender', this, () => {
      this._attachMdcInteractionHandlers();
      if (get(this, 'createFoundation') && !get(this, 'isDestroyed')) {
        const foundation = this.createFoundation();
        set(this, 'foundation', foundation);
        foundation.init();
        this.afterFoundationCreation(foundation);
      }
      if (get(this, 'ripple')) {
        const rippleFoundation = new MDCRippleFoundation(
          createRippleAdapter(this, this.rippleOptions())
        );
        set(this, 'rippleFoundation', rippleFoundation);
        rippleFoundation.init();
      }
    });
  },

  willDestroyElement() {
    this._super(...arguments);
    const foundation = get(this, 'foundation');
    const rippleFoundation = get(this, 'rippleFoundation');
    if (foundation) {
      foundation.destroy();
    }
    if (rippleFoundation) {
      rippleFoundation.destroy();
    }
  },
  //endregion

{'fraction_non_alphanumeric': 0.14339790692496104, 'fraction_numerical': 0.00044533511467379205, 'mean_word_length': 3.579001019367992, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1854586', 'n_tokens_mistral': 1317, 'n_tokens_neox': 1273, 'n_words': 319}