Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

73
Visualizações
Alternative to destructor?

I am aware that, as of 2022 Q2, JavaScript does not have destructors.

However, it looks like my workaround in the following function to leverage closures' GC may have some compatibility impact.

function to_async(f) {
    let src = f.toString();
    let src_worker = [ // TODO: pass exceptions, too
        "self.addEventListener('message', async e => { let arguments = e.data.arguments; let result = await (\n",
        src,
        "\n)(...arguments); self.postMessage({result: result}, [result].filter(x => typeof x === 'object')); }, {once: true});"
    ];

    // MEMORY LEAK; ENABLE AT YOUR OWN RISK!!
    //let src_worker_url = URL.createObjectURL(new Blob(src_worker, {type: 'application/javascript'}));

    // Workaround / alternative implementation in case function is called non-finite number of times
    let src_worker_url = 'data:application/javascript,' + encodeURIComponent(src_worker.join(""));

    async function g() {
        let w = new Worker(src_worker_url);
        let p = new Promise(res => w.addEventListener('message', e => res(e.data.result), {once: true}));
        w.postMessage({arguments: Array.from(arguments)}, Array.from(arguments).filter(x => typeof x === 'object'));
        let result = await p;
        return result;
    }
    return g;
}

I would like to set g[Symbol.destructor] = () => URL.revokeObjectURL(src_worker_url); before returning it.

Is there any way I can achieve something like this in ES6? Or is my current workaround with let-inside-a-closure of a data: URI the best it gets for this use-case?

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda