<commit_before>import * as mio from '../module';

/**
 * Represents a series section.
 */
export class SeriesSection implements mio.ISeriesLibrary {
  private _context: mio.IContext;

  /**
   * Initializes a new instance of the SeriesSection class.
   * @param context The context.
   */
  constructor(context: mio.IContext) {
    this._context = context;
  }

  /**
   * Promises the chapter library.
   * @param seriesId The series identifier.
   * @return The promise for the chapter library.
   */
  chaptersAsync(seriesId: number): Promise<mio.IOption<mio.IChapterLibrary>> {
    let match = mio.findChild(this._context.providers, provider => provider.series, series => series.id === seriesId);
    if (match.value != null) {
      return Promise.resolve(mio.option(new mio.ChapterSection(match.value[2])));
    } else {
      return Promise.resolve(mio.option<mio.IChapterLibrary>());
    }
  }

  /**
   * Promises to create the series.
   * @param seriesAddress The series address.
   * @return The promise to create the series.
   */
  async createAsync(seriesAddress: string): Promise<mio.IOption<number>> {
    return await mio.taskService.enqueue(mio.PriorityType.High, async function(): Promise<mio.IOption<number>> {
      // Initialize the provider and series.
      let provider = mio.openProvider(seriesAddress);
      let series = await provider.seriesAsync(seriesAddress);

      // Create the provider when applicable.
      if (!this._context.providers[provider.name]) {
        this._context.providers[provider.name] = {series: {}};
      }

      // Create the series when applicable.
      if (!this._context.providers[provider.name].series[seriesAddress]) {
        this._context.providers[provider.name].series[seriesAddress] = {
          addedAt: Date.now(),
          chapters: mio.mapObject(series.chapters, mio.adaptChapterToKey, mio.adaptChapterToContext),
          checkedAt: Date.now(),
          id: ++this._context.lastId,
          metadata: mio.copySeriesMetadata(series)
        };
      }

      // Return the series identifier.
      return mio.option(this._context.providers[provider.name].series[seriesAddress].id);
    });
  }

  /**
   * Promises to delete the series.
   * @param seriesId The series identifier.
   * @return The promise to delete the series.
   */
  async deleteAsync(seriesId: number): Promise<boolean> {
    let match = mio.findChild(this._context.providers, provider => provider.series, series => series.id === seriesId);
    if (match.value != null) {<commit_msg>[Library] SectionService persist series creation<commit_after>import * as mio from '../module';

/**
 * Represents a series section.
 */
export class SeriesSection implements mio.ISeriesLibrary {
  private _context: mio.IContext;

  /**
   * Initializes a new instance of the SeriesSection class.
   * @param context The context.
   */
  constructor(context: mio.IContext) {
    this._context = context;
  }

  /**
   * Promises the chapter library.
   * @param seriesId The series identifier.
   * @return The promise for the chapter library.
   */
  chaptersAsync(seriesId: number): Promise<mio.IOption<mio.IChapterLibrary>> {
    let match = mio.findChild(this._context.providers, provider => provider.series, series => series.id === seriesId);
    if (match.value != null) {
      return Promise.resolve(mio.option(new mio.ChapterSection(match.value[2])));
    } else {
      return Promise.resolve(mio.option<mio.IChapterLibrary>());
    }
  }

  /**
   * Promises to create the series.
   * @param seriesAddress The series address.
   * @return The promise to create the series.
   */
  async createAsync(seriesAddress: string): Promise<mio.IOption<number>> {
    return await mio.taskService.enqueue(mio.PriorityType.High, async function(): Promise<mio.IOption<number>> {
      // Initialize the provider.
      let provider = mio.openProvider(seriesAddress);

      // Check if the provider exists.
      if (!this._context.providers[provider.name]) {
        this._context.providers[provider.name] = {series: {}};
      }

      // Check if the series exists.
      if (this._context.providers[provider.name].series[seriesAddress]) {
        return mio.option(this._context.providers[provider.name].series[seriesAddress].id)
      } else {
        // Initialize the identifier and series.
        let id = ++this._context.lastId;
        let series = await provider.seriesAsync(seriesAddress);

        // Add the series.
        this._context.providers[provider.name].series[seriesAddress] = {
          addedAt: Date.now(),
          chapters: mio.mapObject(series.chapters, mio.adaptChapterToKey, mio.adaptChapterToContext),
          checkedAt: Date.now(),
          id: id,
          metadata: mio.copySeriesMetadata(series)
        };

        // Save the context.
        await mio.contextService.writeContext();
        return mio.option(id);
      }
    });
  }

  /**
   * Promises to delete the series.
   * @param seriesId The series identifier.
   * @return The promise to delete the series.
   */
  async deleteAsync(seriesId: number): Promise<boolean> {
    let match = mio.findChild(this._context.providers, provider => provider.series, series => series.id === seriesId);
    if (match.value != null) {
{'fraction_non_alphanumeric': 0.1230271914812702, 'fraction_numerical': 0.00038030043734550294, 'mean_word_length': 3.068058778035576, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 27, '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': '2142561', 'n_tokens_mistral': 1531, 'n_tokens_neox': 1504, 'n_words': 367}