<commit_before>import * as ko from "knockout";

import { StoredListing } from "../../common/Listable";
import { StatBlock } from "../../common/StatBlock";
import { AccountClient } from "../Account/AccountClient";
import { Store } from "../Utility/Store";
import { Listing, ListingOrigin } from "./Listing";

export class StatBlockLibrary {
  private statBlocks = ko.observableArray<Listing<StatBlock>>([]);
  private readonly StoreName = Store.StatBlocks;

  public GetStatBlocks = ko.pureComputed(() => this.statBlocks());

  constructor(private accountClient: AccountClient) {}

  public AddListings = (listings: StoredListing[], source: ListingOrigin) => {
    ko.utils.arrayPushAll<Listing<StatBlock>>(
      this.statBlocks,
      listings.map(c => {
        return new Listing<StatBlock>(c, source);
      })
    );
  };

  public DeleteListing = async (id: string) => {
    this.statBlocks.remove(s => s.Listing().Id == id);
    await Store.Delete(this.StoreName, id);
    await this.accountClient.DeleteStatBlock(id);
  };

  private saveStatBlock = async (
    listing: Listing<StatBlock>,
    newStatBlock: StatBlock
  ) => {
    listing.Listing().Id = newStatBlock.Id;
    this.statBlocks.push(listing);

    await Store.Save<StatBlock>(this.StoreName, newStatBlock.Id, newStatBlock);
    listing.SetValue(newStatBlock);

    const saveResult = await this.accountClient.SaveStatBlock(newStatBlock);
    if (!saveResult || listing.Origin === "account") {
      return;
    }
    const accountListing = new Listing<StatBlock>(
      {
        ...newStatBlock,
        SearchHint: StatBlock.GetSearchHint(newStatBlock),
        Metadata: StatBlock.GetMetadata(newStatBlock),
        Link: `/my/statblocks/${newStatBlock.Id}`
      },
      "account",<commit_msg>Ignore failed AJAX statblock listing delete<commit_after>import * as ko from "knockout";

import { StoredListing } from "../../common/Listable";
import { StatBlock } from "../../common/StatBlock";
import { AccountClient } from "../Account/AccountClient";
import { Store } from "../Utility/Store";
import { Listing, ListingOrigin } from "./Listing";

export class StatBlockLibrary {
  private statBlocks = ko.observableArray<Listing<StatBlock>>([]);
  private readonly StoreName = Store.StatBlocks;

  public GetStatBlocks = ko.pureComputed(() => this.statBlocks());

  constructor(private accountClient: AccountClient) {}

  public AddListings = (listings: StoredListing[], source: ListingOrigin) => {
    ko.utils.arrayPushAll<Listing<StatBlock>>(
      this.statBlocks,
      listings.map(c => {
        return new Listing<StatBlock>(c, source);
      })
    );
  };

  public DeleteListing = async (id: string) => {
    this.statBlocks.remove(s => s.Listing().Id == id);
    await Store.Delete(this.StoreName, id);
    try {
      await this.accountClient.DeleteStatBlock(id);
    } catch {}
  };

  private saveStatBlock = async (
    listing: Listing<StatBlock>,
    newStatBlock: StatBlock
  ) => {
    listing.Listing().Id = newStatBlock.Id;
    this.statBlocks.push(listing);

    await Store.Save<StatBlock>(this.StoreName, newStatBlock.Id, newStatBlock);
    listing.SetValue(newStatBlock);

    const saveResult = await this.accountClient.SaveStatBlock(newStatBlock);
    if (!saveResult || listing.Origin === "account") {
      return;
    }
    const accountListing = new Listing<StatBlock>(
      {
        ...newStatBlock,
        SearchHint: StatBlock.GetSearchHint(newStatBlock),
        Metadata: StatBlock.GetMetadata(newStatBlock),
        Link: `/my/statblocks/${newStatBlock.Id}`
      },
      "account",
{'fraction_non_alphanumeric': 0.14011676396997497, 'fraction_numerical': 0.0, 'mean_word_length': 3.59514687100894, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 31, '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': '6626482', 'n_tokens_mistral': 1078, 'n_tokens_neox': 1070, 'n_words': 216}