Made core static

This commit is contained in:
nicco 2018-02-13 22:13:34 +01:00
parent 483b4d625c
commit 5e527b45d1
2 changed files with 73 additions and 30 deletions

43
dist/cometa.js vendored
View File

@ -5,33 +5,30 @@ const util = require("./util");
const parser = require("./parser"); const parser = require("./parser");
const compiler = require("./compiler"); const compiler = require("./compiler");
const options_1 = require("./options"); const options_1 = require("./options");
module.exports = class { module.exports = class Cometa {
constructor(opt, rexp) { constructor(opt, rexp) {
this.options = options_1.options;
this.expressions = options_1.re;
this._express = this.renderFile;
this.cache = new Map(); this.cache = new Map();
this.options = Object.assign(this.options, opt); this.options = Object.assign(options_1.options, opt);
this.expressions = Object.assign(this.expressions, rexp); this.expressions = Object.assign(options_1.re, rexp);
if (module.parent === null) if (module.parent === null)
throw new Error('Not imported'); throw new Error('Not imported');
this.options.views = path.join(path.dirname(module.parent.filename), this.options.views); this.options.views = path.join(path.dirname(module.parent.filename), this.options.views);
} }
renderFile(file, data, callback) { static exec(file, data, callback, env) {
console.log('Options', this.options);
util.readFile(file).then(html => { util.readFile(file).then(html => {
console.log('Options', this.options);
if (html === undefined) { if (html === undefined) {
callback(`No template found: ${file}`, ''); callback(`No template found: ${file}`, '');
return; return;
} }
util.checksum(html, true).then(hash => { util.checksum(html, true).then(hash => {
if (this.options.caching && !this.cache.get(html)) if (env.options.caching && !env.cache.get(html)) {
this.cache.set(html, { process.stdout.write(`Compiling: ${hash}\n`);
template: compiler.process(html, this.options, this.expressions), env.cache.set(html, {
template: compiler.process(html, env.options, env.expressions),
time: Date.now() time: Date.now()
}); });
const compiled = this.cache.get(html); }
const compiled = env.cache.get(html);
if (compiled) if (compiled)
callback(null, parser.computeParts(compiled.template, data)); callback(null, parser.computeParts(compiled.template, data));
else 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}`); const template_path = path.join(this.options.views, `${template_name}.${this.options.extension}`);
this.renderFile(template_path, data, callback); 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,
});
}
}; };

View File

@ -4,16 +4,25 @@ import * as parser from './parser'
import * as compiler from './compiler' import * as compiler from './compiler'
import { Compiled, options, Options, Expressions, re } from './options' import { Compiled, options, Options, Expressions, re } from './options'
module.exports = class { type RenderCallback = (err: any, render: string) => void
private cache: Map<string, Compiled>
private options: Options = options interface Env {
private expressions: Expressions = re options: Options
expressions: Expressions
cache: Map<string, Compiled>
}
module.exports = class Cometa {
cache: Map<string, Compiled> = new Map()
options: Options
expressions: Expressions
private static permCache: Map<string, Compiled>
constructor(opt?: Options, rexp?: Expressions) { constructor(opt?: Options, rexp?: Expressions) {
this.cache = new Map()
this.options = Object.assign(this.options, opt) this.options = Object.assign(options, opt)
this.expressions = Object.assign(this.expressions, rexp) this.expressions = Object.assign(re, rexp)
if (module.parent === null) if (module.parent === null)
throw new Error('Not imported') 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) 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 { private static exec(file: string, data: any, callback: RenderCallback, env: Env): void {
console.log('Options', this.options)
util.readFile(file).then(html => { util.readFile(file).then(html => {
console.log('Options', this.options)
if (html === undefined) { if (html === undefined) {
callback(`No template found: ${file}`, '') callback(`No template found: ${file}`, '')
return return
} }
util.checksum(html, true).then(hash => { util.checksum(html, true).then(hash => {
// Compile Template if is not in cache // Compile Template if is not in cache
if (this.options.caching && !this.cache.get(html)) if (env.options.caching && !env.cache.get(html)) {
this.cache.set(html, { process.stdout.write(`Compiling: ${hash}\n`)
template: compiler.process(html, this.options, this.expressions), env.cache.set(html, {
template: compiler.process(html, env.options, env.expressions),
time: Date.now() time: Date.now()
}) })
}
// Render the template and return the html // Render the template and return the html
const compiled = this.cache.get(html) const compiled = env.cache.get(html)
if (compiled) if (compiled)
callback(null, parser.computeParts(compiled.template, data)) callback(null, parser.computeParts(compiled.template, data))
else 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}`) const template_path = path.join(this.options.views, `${template_name}.${this.options.extension}`)
this.renderFile(template_path, data, callback) 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,
})
}
} }