@travetto/model
Advanced tools
Comparing version 0.0.31 to 0.0.32
@@ -23,3 +23,3 @@ { | ||
}, | ||
"version": "0.0.31" | ||
"version": "0.0.32" | ||
} |
@@ -31,4 +31,9 @@ import { Class } from '@travetto/registry'; | ||
await ModelRegistry.init(); | ||
if (AppEnv.watch && this.source.onChange) { | ||
ModelRegistry.on(this.source.onChange.bind(this.source)); | ||
if (AppEnv.watch) { | ||
if (this.source.onSchemaChange) { | ||
ModelRegistry.onSchemaChange(this.source.onSchemaChange.bind(this.source)); | ||
} | ||
if (this.source.onChange) { | ||
ModelRegistry.on(this.source.onChange.bind(this.source)); | ||
} | ||
} | ||
@@ -35,0 +40,0 @@ } |
@@ -1,12 +0,64 @@ | ||
import { SchemaRegistry } from '@travetto/schema'; | ||
import { SchemaRegistry, ClassConfig, FieldConfig } from '@travetto/schema'; | ||
import { ModelOptions } from './types'; | ||
import { EventEmitter } from 'events'; | ||
import { MetadataRegistry, Class } from '@travetto/registry'; | ||
import { MetadataRegistry, Class, ChangeEvent } from '@travetto/registry'; | ||
import { DependencyRegistry } from '@travetto/di'; | ||
import { AppEnv } from '@travetto/base'; | ||
type ModelChanges = { path: string[], changes: ChangeEvent<FieldConfig>[] }[]; | ||
export type SchemaChangeEvent = { cls: Class, changes: ModelChanges }; | ||
export class $ModelRegistry extends MetadataRegistry<ModelOptions<any>> { | ||
private schemaDependencies = new Map<string, Map<string, string[]>>(); | ||
constructor() { | ||
super(SchemaRegistry, DependencyRegistry); | ||
SchemaRegistry.onFieldChange(this.trackSchemaChanges.bind(this)); | ||
} | ||
trackSchemaChanges({ cls, changes }: { cls: Class, changes: ChangeEvent<FieldConfig>[] }) { | ||
const updates = new Map<string, ModelChanges>(); | ||
if (this.schemaDependencies.has(cls.__id)) { | ||
const deps = this.schemaDependencies.get(cls.__id)!; | ||
for (const id of deps.keys()) { | ||
if (!updates.has(id)) { | ||
updates.set(id, []); | ||
} | ||
const path = deps.get(id)!; | ||
updates.get(id)!.push({ path, changes }); | ||
} | ||
} | ||
for (const key of updates.keys()) { | ||
this.events.emit('model:schema-change', { | ||
cls: SchemaRegistry.get(key).class, | ||
delta: updates.get(key)! | ||
}); | ||
} | ||
} | ||
onSchemaChange(cb: (ev: { cls: Class, changes: ModelChanges }) => void) { | ||
this.events.on('model:schema-change', cb); | ||
} | ||
trackSchemaDependencies(cls: Class, curr: Class = cls, path: string[] = []) { | ||
const config = SchemaRegistry.get(curr); | ||
// Store current value as mapping | ||
if (!this.schemaDependencies.has(curr.__id)) { | ||
this.schemaDependencies.set(curr.__id, new Map()); | ||
} | ||
this.schemaDependencies.get(curr.__id)!.set(cls.__id, path); | ||
// Read children | ||
const view = config.views[SchemaRegistry.DEFAULT_VIEW]; | ||
for (const k of view.fields) { | ||
if (SchemaRegistry.has(view.schema[k].declared.type)) { | ||
this.trackSchemaDependencies(cls, view.schema[k].declared.type, [...path, k]); | ||
} | ||
} | ||
} | ||
createPending(cls: Class) { | ||
@@ -17,6 +69,14 @@ return { class: cls }; | ||
onInstallFinalize<T>(cls: Class<T>) { | ||
if (AppEnv.watch) { | ||
this.trackSchemaDependencies(cls); | ||
} | ||
return this.pending.get(cls.__id)! as ModelOptions<T>; | ||
} | ||
onUninstallFinalize<T>(cls: Class<T>) { | ||
super.onUninstallFinalize(cls); | ||
this.schemaDependencies.delete(cls.__id); | ||
} | ||
} | ||
export const ModelRegistry = new $ModelRegistry(); |
@@ -6,5 +6,7 @@ import { Class, ChangeEvent } from '@travetto/registry'; | ||
import { ModelQuery } from '../model/query'; | ||
import { SchemaChangeEvent } from './registry'; | ||
export abstract class ModelSource { | ||
onChange?<T extends ModelCore>(e: ChangeEvent<Class<T>>): void; | ||
onSchemaChange?(e: SchemaChangeEvent): void; | ||
@@ -11,0 +13,0 @@ abstract prePersist<T extends ModelCore>(cls: Class<T>, model: Partial<T>): Partial<T>; |
Sorry, the diff of this file is not supported yet
39451
987