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

Support sync rendering even of async transforms #210

Open
ForbesLindesay opened this issue Aug 1, 2017 · 0 comments
Open

Support sync rendering even of async transforms #210

ForbesLindesay opened this issue Aug 1, 2017 · 0 comments
Assignees

Comments

@ForbesLindesay
Copy link
Member

We could use sync-rpc to render transforms that only support async, synchronously. It would be relatively slow, but if you need it then at least it would work.

The code would look something like:

worker.js

const jstransformer = require('./');

function init(transformName) {
  const t = jstransformer(require('jstransformer-' + transformName));
  return function render({str, options, locals}) {
    // sync-rpc converts this promise into a synchronous result
    return t.renderAsync(str, options, locals);
  }
}

module.exports = init;

index.js

const syncRPC = require('sync-rpc');

// ...

Transformer.prototype.render = function (str, options, locals) {
  if (!this.can('render') && this.can('renderAsync')) {
    if (!this._syncWorker) {
      this._syncWorker = syncRPC(require.resolve('./worker.js'), this.name);
    }
    return this._syncWorker({str, options, locals});
  }

  // ... normal sync implementation here ...
}

// ...
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

3 participants