From: Nick Downing Date: Sun, 2 Dec 2018 00:37:18 +0000 (+1100) Subject: Update to build_cache.git commit 1cc703b X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=90d5f10665159f6395fc429651859130c45900e7;p=jst_cache.git Update to build_cache.git commit 1cc703b --- diff --git a/JSTCache.js b/JSTCache.js index a2998e5..be400c9 100644 --- a/JSTCache.js +++ b/JSTCache.js @@ -32,9 +32,11 @@ let util = require('util') let fs_readFile = util.promisify(fs.readFile) let fs_writeFile = util.promisify(fs.writeFile) -let JSTCache = function(root, args, diag) { +let JSTCache = function(root, args, diag1, diag) { if (!this instanceof JSTCache) - throw Error('JSTCache is a constructor') + throw new Error('JSTCache is a constructor') + BuildCache.call(this, diag) + this.diag1 = diag1 this.root = root || '.' this.arg_names = [] this.arg_values = [] @@ -44,46 +46,40 @@ let JSTCache = function(root, args, diag) { this.arg_names.push(i) this.arg_values.push(args[i]) } - this.diag = diag || false - - this.build_cache = new BuildCache() } -JSTCache.prototype.get = function(key) { - return /*await*/ this.build_cache.get( +JSTCache.prototype = Object.create(BuildCache.prototype) + +JSTCache.prototype.build = async function(key, result) { + let render = await disk_build( key, - async result => { - let render = await disk_build( - key, - async temp_pathname => /*await*/ fs_writeFile( - temp_pathname, - jst( - `module.exports = async (${['_require', '_pathname', '_root'].concat(this.arg_names).join(', ')}) => {${await fs_readFile(key, {encoding: 'utf-8'})}}\n` - ), - {encoding: 'utf-8'} - ), - this.diag - ) - assert(render.deps.length === 0) - let full_pathname = require.resolve(render.pathname) - delete require.cache[full_pathname] - result.value = await (require(full_pathname)).apply( - null, - [ - async pathname => { - let temp = path.posix.dirname(key) - while (pathname.charAt(0) === '/') { - temp = this.root - pathname = pathname.slice(1) - } - pathname = path.posix.resolve(temp, pathname) - return this.get(pathname) - }, - key, - this.root - ].concat(this.arg_values) - ) - } + async temp_pathname => /*await*/ fs_writeFile( + temp_pathname, + jst( + `module.exports = async (${['_require', '_pathname', '_root'].concat(this.arg_names).join(', ')}) => {${await fs_readFile(key, {encoding: 'utf-8'})}}\n` + ), + {encoding: 'utf-8'} + ), + this.diag1 + ) + assert(render.deps.length === 0) + let full_pathname = require.resolve(render.pathname) + delete require.cache[full_pathname] + result.value = await (require(full_pathname)).apply( + null, + [ + async pathname => { + let temp = path.posix.dirname(key) + while (pathname.charAt(0) === '/') { + temp = this.root + pathname = pathname.slice(1) + } + pathname = path.posix.resolve(temp, pathname) + return this.get(pathname) + }, + key, + this.root + ].concat(this.arg_values) ) }