From 5e527b45d1e73a3586a5a972e3f10cafe4661aeb Mon Sep 17 00:00:00 2001 From: nicco Date: Tue, 13 Feb 2018 22:13:34 +0100 Subject: [PATCH] Made core static --- dist/cometa.js | 43 ++++++++++++++++++++++++------------ src/cometa.ts | 60 ++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 73 insertions(+), 30 deletions(-) diff --git a/dist/cometa.js b/dist/cometa.js index a78a3ac..dff9e3e 100644 --- a/dist/cometa.js +++ b/dist/cometa.js @@ -5,33 +5,30 @@ const util = require("./util"); const parser = require("./parser"); const compiler = require("./compiler"); const options_1 = require("./options"); -module.exports = class { +module.exports = class Cometa { constructor(opt, rexp) { - this.options = options_1.options; - this.expressions = options_1.re; - this._express = this.renderFile; this.cache = new Map(); - this.options = Object.assign(this.options, opt); - this.expressions = Object.assign(this.expressions, rexp); + this.options = Object.assign(options_1.options, opt); + this.expressions = Object.assign(options_1.re, rexp); if (module.parent === null) throw new Error('Not imported'); this.options.views = path.join(path.dirname(module.parent.filename), this.options.views); } - renderFile(file, data, callback) { - console.log('Options', this.options); + static exec(file, data, callback, env) { util.readFile(file).then(html => { - console.log('Options', this.options); if (html === undefined) { callback(`No template found: ${file}`, ''); return; } util.checksum(html, true).then(hash => { - if (this.options.caching && !this.cache.get(html)) - this.cache.set(html, { - template: compiler.process(html, this.options, this.expressions), + if (env.options.caching && !env.cache.get(html)) { + process.stdout.write(`Compiling: ${hash}\n`); + env.cache.set(html, { + template: compiler.process(html, env.options, env.expressions), time: Date.now() }); - const compiled = this.cache.get(html); + } + const compiled = env.cache.get(html); if (compiled) callback(null, parser.computeParts(compiled.template, data)); else @@ -39,8 +36,26 @@ module.exports = class { }); }); } - renderTemplate(template_name, data, callback) { + render(template_name, data, callback) { const template_path = path.join(this.options.views, `${template_name}.${this.options.extension}`); this.renderFile(template_path, data, callback); } + renderFile(template_path, data, callback) { + Cometa.exec(template_path, data, callback, { + options: this.options, + expressions: this.expressions, + cache: this.cache + }); + } + static __express(file, data, callback) { + if (Cometa.permCache === undefined) { + process.stdout.write('Initializing cache map\n'); + Cometa.permCache = new Map(); + } + Cometa.exec(file, data, callback, { + options: options_1.options, + expressions: options_1.re, + cache: Cometa.permCache, + }); + } }; diff --git a/src/cometa.ts b/src/cometa.ts index 6c6c266..deed33e 100644 --- a/src/cometa.ts +++ b/src/cometa.ts @@ -4,16 +4,25 @@ import * as parser from './parser' import * as compiler from './compiler' import { Compiled, options, Options, Expressions, re } from './options' -module.exports = class { - private cache: Map - private options: Options = options - private expressions: Expressions = re +type RenderCallback = (err: any, render: string) => void + +interface Env { + options: Options + expressions: Expressions + cache: Map +} + +module.exports = class Cometa { + cache: Map = new Map() + options: Options + expressions: Expressions + + private static permCache: Map constructor(opt?: Options, rexp?: Expressions) { - this.cache = new Map() - this.options = Object.assign(this.options, opt) - this.expressions = Object.assign(this.expressions, rexp) + this.options = Object.assign(options, opt) + this.expressions = Object.assign(re, rexp) if (module.parent === null) throw new Error('Not imported') @@ -21,24 +30,24 @@ module.exports = class { this.options.views = path.join(path.dirname(module.parent.filename), this.options.views) } - renderFile(file: string, data: any, callback: (err: any, render: string) => void): void { - console.log('Options', this.options) + private static exec(file: string, data: any, callback: RenderCallback, env: Env): void { util.readFile(file).then(html => { - console.log('Options', this.options) if (html === undefined) { callback(`No template found: ${file}`, '') return } util.checksum(html, true).then(hash => { // Compile Template if is not in cache - if (this.options.caching && !this.cache.get(html)) - this.cache.set(html, { - template: compiler.process(html, this.options, this.expressions), + if (env.options.caching && !env.cache.get(html)) { + process.stdout.write(`Compiling: ${hash}\n`) + env.cache.set(html, { + template: compiler.process(html, env.options, env.expressions), time: Date.now() }) + } // Render the template and return the html - const compiled = this.cache.get(html) + const compiled = env.cache.get(html) if (compiled) callback(null, parser.computeParts(compiled.template, data)) else @@ -47,11 +56,30 @@ module.exports = class { }) } - renderTemplate(template_name: string, data: any, callback: (err: any, render: string) => void): void { + render(template_name: string, data: any, callback: RenderCallback): void { const template_path = path.join(this.options.views, `${template_name}.${this.options.extension}`) this.renderFile(template_path, data, callback) } - _express = this.renderFile + renderFile(template_path: string, data: any, callback: RenderCallback): void { + Cometa.exec(template_path, data, callback, { + options: this.options, + expressions: this.expressions, + cache: this.cache + }) + } + + static __express(file: string, data: any, callback: RenderCallback) { + if (Cometa.permCache === undefined) { + process.stdout.write('Initializing cache map\n') + Cometa.permCache = new Map() + } + + Cometa.exec(file, data, callback, { + options: options, + expressions: re, + cache: Cometa.permCache, + }) + } } \ No newline at end of file