UNPKG

skypager-project

Version:
48 lines (36 loc) 1.38 kB
import Helper from '../helper' export class Renderer extends Helper { static dirname = __dirname static createRegistry(options = {}) { return Helper.createContextRegistry('renderers', { context: require.context('../renderers', true, /\.js$/), ...options, }) } static attach(project, options = {}) { return Helper.attach(project, Renderer, { registryProp: 'renderers', lookupProp: 'renderer', registry: options.registry || Renderer.createRegistry(options), ...options }) } render(options = {}, context = {}) { const sync = options.sync || this.tryGet('sync') return sync ? this.renderSync(options, context) : this.renderAsync(options, context) } async renderAsync(options = {}, context = {}) { const getInitialProps = options.getInitialProps || this.tryGet('getInitialProps') const method = this.tryGet('renderAsync', this.tryGet('render')) if (getInitialProps) { options = await getInitialProps.call(this, options, context) } const output = await method.call(this, {...this.options, ...options}, {...this.context, ...context}) return output } renderSync(options = {}, context = {}) { const method = this.tryGet('renderSync', this.tryGet('render')) return method.call(this, {...this.options, ...options}, {...this.context, ...context}) } } export default Renderer