import compile from './compile' ; import { getConfig } from './config' ; import { promiseImpl } from './polyfills' ; import EtaErr from './err' ; import type { EtaConfig , PartialConfig } from './config' ; import type { TemplateFunction } from './compile' ; import type { CallbackFn } from './file-handlers' ; function handleCache ( template , options ) { if ( options . cache && options . name && options . templates . get ( options . name ) ) { return options . templates . get ( options . name ) ; } const templateFunc = typeof template === 'function' ? template : compile ( template , options ) ; if ( options . cache && options . name ) { options . templates . define ( options . name , templateFunc ) ; } return templateFunc ; } param template param data param config param cb export default function render ( template , data , config ? , cb ? ) { const options = getConfig ( config || { } ) ; if ( options . async ) { if ( cb ) { try { const templateFn = handleCache ( template , options ) ; templateFn ( data , options , cb ) ; } catch ( err ) { return cb ( err ) ; } } else { if ( typeof promiseImpl === 'function' ) { return new promiseImpl ( function ( resolve , reject ) { try { resolve ( handleCache ( template , options ) ( data , options ) ) ; } catch ( err ) { reject ( err ) ; } } ) ; } else { throw EtaErr ( "Please provide a callback function, this env doesn't support Promises" ) ; } } } else { return handleCache ( template , options ) ( data , options ) ; } } param template param data param config param cb export function renderAsync ( template , data , config ? , cb ? ) { return render ( template , data , Object . assign ( { } , config , { async : true } ) , cb ) ; }