<commit_before>// Copyright (c) ppy Pty Ltd <<EMAIL>>. Licensed under the GNU Affero General Public License v3.0.
// See the LICENCE file in the repository root for full licence text.

import Layzr, { LayzrInstance } from 'layzr';

export default class OsuLayzr {
  private layzr?: LayzrInstance;
  private observer: MutationObserver;

  constructor() {
    this.observer = new MutationObserver(this.reinit);

    $(document).on('turbolinks:load', this.init);
  }

  init = () => {
    this.layzr ??= Layzr();

    this.reinit();
    this.observer.observe(document.body, { childList: true, subtree: true });
  };

  reinit = () => {
    this.layzr?.update().check().handlers(true);
  };
}<commit_msg>Reduce image lazy-loading related works<commit_after>// Copyright (c) ppy Pty Ltd <<EMAIL>>. Licensed under the GNU Affero General Public License v3.0.
// See the LICENCE file in the repository root for full licence text.

import Layzr, { LayzrInstance } from 'layzr';

export default class OsuLayzr {
  private layzr?: LayzrInstance;
  private observer: MutationObserver;

  constructor() {
    this.observer = new MutationObserver(this.observePage);
    this.observer.observe(document, { childList: true, subtree: true });

    // Layzr depends on document.body which is only available after document is ready.
    $(() => {
      this.layzr ??= Layzr();
      this.reinit();
    });
  }

  private observePage = (mutations: MutationRecord[]) => {
    if (this.layzr == null) return;

    for (const mutation of mutations) {
      for (const node of mutation.addedNodes) {
        if (node instanceof HTMLElement && (node.dataset.normal != null || node.querySelector('[data-normal]') != null)) {
          this.reinit();

          return;
        }
      }
    }
  };

  private reinit() {
    this.layzr?.update().check().handlers(true);
  }
}
{'fraction_non_alphanumeric': 0.13589604764482946, 'fraction_numerical': 0.002165674066053059, 'mean_word_length': 3.2678983833718247, 'pattern_counts': {'":': 0, '<': 7, '<?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': '5543613', 'n_tokens_mistral': 600, 'n_tokens_neox': 560, 'n_words': 162}