teleport-code-generators icon indicating copy to clipboard operation
teleport-code-generators copied to clipboard

Async generators can be made sync?

Open alexnm opened this issue 5 years ago • 2 comments

Initially we developed the component generators as async functions, but since the flow is sync, it might not be needed after all.

A resolution would be great before the official release

alexnm avatar Feb 24 '19 15:02 alexnm

Some parts can, but there's one or two packages that are async. I think we are using the babel generator with a async callback that gets called when babel is done with it.

I'm certain we CAN make them sync as even the babel generator has a sync mode.

I'm also certain we SHOULD NOT make them sync as sync scales less well, and my assumption is that we might want to multithread the architecture to allow multiple components to be generated concurrently via worker threads / service workers.

vladnicula avatar Feb 27 '19 08:02 vladnicula

@vladnicula what about having both like in the node API?

This would allow to transform this:

const teleport = require('@teleporthq/teleport-code-generators')

const reactGenerator = teleport.createReactComponentGenerator()
const uidl = {
  name: 'DemoComponent',
  content: {
    type: 'text',
    children: ['Teleport World!']
  }
}

reactGenerator
  .generateComponent(uidl)
  .then(({ dependencies, code }) => {
    console.log(dependencies)
    console.log(code)
  })
  .catch(err => {
    console.log(err)
  })

into this

const teleport = require('@teleporthq/teleport-code-generators')

const reactGenerator = teleport.createReactComponentGenerator()
const uidl = {
  name: 'DemoComponent',
  content: {
    type: 'text',
    children: ['Teleport World!']
  }
}

const { dependencies, code } = reactGenerator.generateComponentSync(uidl)

paulbrie avatar Mar 12 '19 10:03 paulbrie