Skip to content

Support sync rendering even of async transforms #210

@ForbesLindesay

Description

@ForbesLindesay

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

// ...

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions