<commit_before>import {Encoder} from './codec';

import mozjpeg_enc from '../../../codecs/mozjpeg_enc/mozjpeg_enc';
// Using require() so TypeScript doesn’t complain about this not being a module.
const wasmBinaryUrl = require('../../../codecs/mozjpeg_enc/mozjpeg_enc.wasm');

export class MozJpegEncoder implements Encoder {
  private emscriptenModule: Promise<EmscriptenWasm.Module>;
  constructor() {
    this.emscriptenModule = new Promise(resolve => {
      const m = mozjpeg_enc({
        // Just to be safe, don’t automatically invoke any wasm functions
        // noInitialRun: false,
        locateFile(url: string): string {
          // Redirect the request for the wasm binary to whatever webpack gave us.
          if(url.endsWith('.wasm')) {
            return wasmBinaryUrl;
          }
          return url;
        },
        onRuntimeInitialized() {
          resolve(m);
        }
      });
    });
  }

  async encode(bitmap: ImageBitmap): Promise<ArrayBuffer> {
    console.log('awaiting module');
    debugger;
    const m = await this.emscriptenModule;<commit_msg>Fixed the freeze bug thing<commit_after>import {Encoder} from './codec';

import mozjpeg_enc from '../../../codecs/mozjpeg_enc/mozjpeg_enc';
// Using require() so TypeScript doesn’t complain about this not being a module.
const wasmBinaryUrl = require('../../../codecs/mozjpeg_enc/mozjpeg_enc.wasm');

export class MozJpegEncoder implements Encoder {
  private emscriptenModule: Promise<EmscriptenWasm.Module>;
  constructor() {
    this.emscriptenModule = new Promise(resolve => {
      const m = mozjpeg_enc({
        // Just to be safe, don’t automatically invoke any wasm functions
        // noInitialRun: false,
        locateFile(url: string): string {
          // Redirect the request for the wasm binary to whatever webpack gave us.
          if(url.endsWith('.wasm')) {
            return wasmBinaryUrl;
          }
          return url;
        },
        onRuntimeInitialized() {
          // This is a bug I discovered. To be filed.
          delete (m as any).then;
          resolve(m);
        }
      });
    });
  }

  async encode(bitmap: ImageBitmap): Promise<ArrayBuffer> {
    const m = await this.emscriptenModule;
{'fraction_non_alphanumeric': 0.1146067415730337, 'fraction_numerical': 0.0, 'mean_word_length': 2.5445859872611467, 'pattern_counts': {'":': 0, '<': 7, '<?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': '6597570', 'n_tokens_mistral': 636, 'n_tokens_neox': 631, 'n_words': 179}