<commit_before>import { Inject, Injectable } from '@angular/core';

import { GalleryRef } from './gallery-ref';
import { GALLERY_CONFIG } from '../utils/gallery.token';
import { defaultConfig } from '../utils/gallery.default';
import { GalleryConfig } from '../models';

@Injectable()
export class Gallery {

  /** Store gallery instances */
  private readonly _instances = new Map<string, GalleryRef>();

  /** Global config */
  config: GalleryConfig;

  constructor(@Inject(GALLERY_CONFIG) config: GalleryConfig) {
    this.config = {...defaultConfig, ...config};
  }

  /**
   * Get or create gallery by ID
   * @param id
   * @param config
   */
  ref(id = 'root', config?: GalleryConfig): GalleryRef {
    if (this._instances.has(id)) {
      const galleryRef = this._instances.get(id);
      if (config) {
        galleryRef.setConfig({...this.config, ...config});
      }
      return galleryRef;
    } else {
      return this._instances.set(id, new GalleryRef({...this.config, ...config}, this.deleteInstance(id))).get(id);
    }
  }

  /**
   * Destroy all gallery instances
   */
  destroyAll() {
    this._instances.forEach((ref: GalleryRef) => ref.destroy());
  }

  /**
   * Reset all gallery instances
   */
  resetAll() {
    this._instances.forEach((ref: GalleryRef) => ref.reset());
  }

  /**
   * A destroyer function for each gallery instance
   */
  private deleteInstance(id: string) {
    return () => {
      this._instances.delete(id);
    };
  }

}
<commit_msg>fix(Gallery): Check galleryRef exists before destroying<commit_after>import { Inject, Injectable } from '@angular/core';

import { GalleryRef } from './gallery-ref';
import { GALLERY_CONFIG } from '../utils/gallery.token';
import { defaultConfig } from '../utils/gallery.default';
import { GalleryConfig } from '../models';

@Injectable()
export class Gallery {

  /** Store gallery instances */
  private readonly _instances = new Map<string, GalleryRef>();

  /** Global config */
  config: GalleryConfig;

  constructor(@Inject(GALLERY_CONFIG) config: GalleryConfig) {
    this.config = {...defaultConfig, ...config};
  }

  /**
   * Get or create gallery by ID
   * @param id
   * @param config
   */
  ref(id = 'root', config?: GalleryConfig): GalleryRef {
    if (this._instances.has(id)) {
      const galleryRef = this._instances.get(id);
      if (config) {
        galleryRef.setConfig({...this.config, ...config});
      }
      return galleryRef;
    } else {
      return this._instances.set(id, new GalleryRef({...this.config, ...config}, this.deleteInstance(id))).get(id);
    }
  }

  /**
   * Destroy all gallery instances
   */
  destroyAll() {
    this._instances.forEach((ref: GalleryRef) => ref.destroy());
  }

  /**
   * Reset all gallery instances
   */
  resetAll() {
    this._instances.forEach((ref: GalleryRef) => ref.reset());
  }

  /**
   * A destroyer function for each gallery instance
   */
  private deleteInstance(id: string) {
    return () => {
      if (this._instances.has(id)) {
        this._instances.delete(id);
      }
    };
  }

}

{'fraction_non_alphanumeric': 0.16074339745679816, 'fraction_numerical': 0.0, 'mean_word_length': 2.9033078880407124, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, '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': '3864402', 'n_tokens_mistral': 946, 'n_tokens_neox': 927, 'n_words': 217}