@travetto/registry
Advanced tools
Comparing version 0.0.33 to 0.0.34
@@ -18,3 +18,3 @@ { | ||
"scripts": {}, | ||
"version": "0.0.33" | ||
"version": "0.0.34" | ||
} |
import { Class } from '../model'; | ||
export const PendingRegister = new Map<string, Class<any>[]>(); | ||
class $PendingRegister { | ||
map = new Map<string, Class<any>[]>(); | ||
ordered: [string, Class<any>[]][] = []; | ||
export function Register() { | ||
return (target: Class<any>) => { | ||
if (!PendingRegister.has(target.__filename)) { | ||
PendingRegister.set(target.__filename, []); | ||
add(cls: Class<any>) { | ||
if (!this.map.has(cls.__filename)) { | ||
const sub: Class<any>[] = []; | ||
this.map.set(cls.__filename, sub); | ||
this.ordered.push([cls.__filename, sub]); | ||
} | ||
PendingRegister.get(target.__filename)!.push(target); | ||
this.map.get(cls.__filename)!.push(cls); | ||
} | ||
flush() { | ||
const out = this.ordered.slice(0); | ||
this.map.clear(); | ||
this.ordered = []; | ||
return out; | ||
} | ||
} | ||
export const PendingRegister = new $PendingRegister(); | ||
export function Register() { | ||
return (target: Class<any>) => PendingRegister.add(target); | ||
} |
@@ -35,36 +35,25 @@ import { EventEmitter } from 'events'; | ||
const extra: string[] = []; | ||
const requireListen = (file: string) => extra.push(file); | ||
Compiler.on('required-after', requireListen); | ||
for (const file of files) { | ||
this.processClasses(file, this.computeClasses(file)); | ||
for (const f of files) { // Load all files, class scanning | ||
require(f); | ||
} | ||
for (const file of extra) { | ||
if (PendingRegister.has(file)) { | ||
this.processClasses(file, PendingRegister.get(file)!); | ||
PendingRegister.delete(file); | ||
} | ||
} | ||
this.flush(); | ||
Compiler.off('required-after', requireListen); | ||
Compiler.on('changed', this.watch); | ||
Compiler.on('removed', this.watch); | ||
Compiler.on('added', this.watch); | ||
Compiler.on('required-after', f => this.processClasses(f, PendingRegister.get(f)!)); | ||
Compiler.on('required-after', f => this.flush()); | ||
} | ||
protected processClasses(file: string, classes?: Class[]) { | ||
if (!classes || !classes.length) { | ||
return; | ||
private flush() { | ||
for (const [file, classes] of PendingRegister.flush()) { | ||
if (!classes || !classes.length) { | ||
continue; | ||
} | ||
this.classes.set(file, new Map()); | ||
for (const cls of classes) { | ||
this.classes.get(cls.__filename)!.set(cls.__id, cls); | ||
this.emit({ type: 'added', curr: cls }); | ||
} | ||
} | ||
this.classes.set(file, new Map()); | ||
for (const cls of classes) { | ||
this.classes.get(file)!.set(cls.__id, cls); | ||
this.emit({ type: 'added', curr: cls }); | ||
} | ||
} | ||
@@ -76,5 +65,5 @@ | ||
protected async watch(file: string) { | ||
console.debug('Got file', file); | ||
const next = new Map(this.computeClasses(file).map(x => [x.__id, x] as [string, Class])); | ||
protected async handleFileChanges(file: string, classes: Class<any>[]) { | ||
const next = new Map(classes.map(cls => [cls.__id, cls] as [string, Class])); | ||
let prev = new Map<string, Class>(); | ||
@@ -106,13 +95,8 @@ if (this.classes.has(file)) { | ||
private computeClasses(file: string) { | ||
try { | ||
const out = require(file); | ||
// Get and clear after computed | ||
const classes: Class[] = PendingRegister.get(file)!; | ||
PendingRegister.delete(file); | ||
return classes || []; | ||
} catch (e) { | ||
return []; | ||
protected async watch(rfile: string) { | ||
require(rfile); | ||
for (const [file, classes] of PendingRegister.flush()) { | ||
this.handleFileChanges(file, classes); | ||
} | ||
} | ||
} |
@@ -27,3 +27,9 @@ import * as ts from 'typescript'; | ||
const registerPath = require.resolve('../src/decorator/register') | ||
function visitNode<T extends ts.Node>(context: ts.TransformationContext, node: T, state: IState): T { | ||
if (state.path === registerPath) { // Cannot process self | ||
return node; | ||
} | ||
if (ts.isClassDeclaration(node) && node.name && node.parent && ts.isSourceFile(node.parent)) { | ||
@@ -34,3 +40,3 @@ if (!state.imported) { | ||
ident: state.imported, | ||
path: require.resolve('../src/decorator/register') | ||
path: registerPath | ||
}); | ||
@@ -37,0 +43,0 @@ } |
@@ -5,2 +5,2 @@ require('@travetto/base/bin/travetto').run() | ||
require('./simple'); | ||
}); | ||
}); |
@@ -0,1 +1,3 @@ | ||
console.log('Loading'); | ||
export class Test { | ||
@@ -14,5 +16,5 @@ | ||
} | ||
nameze() { | ||
namered() { | ||
return 'hi'; | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
495
17268