import { init, createWorker } from 'pmcrypto';
import { loadScript } from './dom';

export const initMain = async (openpgpContents) => {
    const mainUrl = URL.createObjectURL(new Blob([openpgpContents], { type: 'text/javascript' }));
    await loadScript(mainUrl);
    URL.revokeObjectURL(mainUrl);
    init(window.openpgp);
};

export const initWorker = async (openpgpContents, openpgpWorkerContents) => {
    const workerUrl = URL.createObjectURL(
        new Blob(['postMessage({ event: "load" });self.window = self;', openpgpContents, openpgpWorkerContents], {
            type: 'text/javascript'
        })
    );

    createWorker({
        path: workerUrl
    });

    if (window.openpgp.getWorker()) {
        // Wait until all workers are loaded
        await Promise.all(
            window.openpgp.getWorker().workers.map(
                (worker) =>
                    new Promise((resolve) => {
                        const { onmessage } = worker;
                        // eslint-disable-next-line
                        worker.onmessage = () => {
                            // eslint-disable-next-line
                            worker.onmessage = onmessage;
                            resolve();
                        };
                    })
            )
        );
    }

    URL.revokeObjectURL(workerUrl);
};

@@ -40,16 +40,17 @@
 rypto';%0A
+%0A
 import %7B
@@ -469,39 +469,8 @@
 b(%5B'
-postMessage(%7B event: %22load%22 %7D);
 self
@@ -584,21 +584,26 @@
 %0A    );%0A
-%0A
     
+await 
 createWo
@@ -644,638 +644,8 @@
  %7D);
-%0A%0A    if (window.openpgp.getWorker()) %7B%0A        // Wait until all workers are loaded%0A        await Promise.all(%0A            window.openpgp.getWorker().workers.map(%0A                (worker) =%3E%0A                    new Promise((resolve) =%3E %7B%0A                        const %7B onmessage %7D = worker;%0A                        // eslint-disable-next-line%0A                        worker.onmessage = () =%3E %7B%0A                            // eslint-disable-next-line%0A                            worker.onmessage = onmessage;%0A                            resolve();%0A                        %7D;%0A                    %7D)%0A            )%0A        );%0A    %7D%0A
 %0A   

Fix new version of openpgp