Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run HTML conversion in web worker thread #4

Closed
nerk opened this issue May 19, 2014 · 3 comments
Closed

Run HTML conversion in web worker thread #4

nerk opened this issue May 19, 2014 · 3 comments
Assignees

Comments

@nerk
Copy link
Member

nerk commented May 19, 2014

Investigate if HTML conversion can be performed in web worker thread to avoid blocking of editor UI events during conversion.

@nerk nerk self-assigned this May 19, 2014
@mojavelinux
Copy link
Member

Great idea!

@nerk
Copy link
Member Author

nerk commented May 20, 2014

Got it working with a quick hack as follows.

In main.js:

var converter = new Worker('converter.js'); // may be constructed only once

// structure to pass docText, options, and attributes. Opal hash won't work 
var data = {
    docText: docText,
    basedir: basedir,
    pwd: FileUtils.getDirectoryPath(window.location.href),
    attributes: attributes
};

converter.postMessage(data);
converter.onmessage = function (e) {

    var bodyText = e.data;
    // add header, footer etc.
    htmlSource = ... + bodyText + ...;
    // update view
    $iframe.attr("srcdoc", htmlSource);
};

All code doing the conversion is in self-contained, separate file 'converter.js'. No access to console, DOM, document, or window objects.

// converter.js

// Fake console, Opal needs it.

var console = {
    log: function(m) { // Create HTML and prepend to generated output? ...},
    warn: function(m) {// something ...}
};

===> include opal.js code
===> include asciidoctor.js code 

var onmessage = function (e) {
    postMessage(convert(e.data));
}

// data must contain all options and attributes, can't pass Opal.hash2
function convert(data) {

    Opal.ENV['$[]=']("PWD", data.pwd);
    var opts = Opal.hash2(['base_dir', 'safe', 'doctype', 'attributes'], {
                'base_dir': data.basedir,
                'safe': 'safe',
                'doctype': 'article',
                'attributes': data.attributes
            });

    return Opal.Asciidoctor.$convert(data.docText, opts);
}

Smooth editing!

converter.js could be made usable for other projects requiring background conversion. Either a separate build step is needed to create this single file from its parts, or the build process for Asciidoctor.js could create and distribute such a combined file with a proper interface.

@nerk
Copy link
Member Author

nerk commented May 22, 2014

Implemented for version 1.0.4

@nerk nerk closed this as completed May 22, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants