@travetto/schema
Advanced tools
Comparing version 0.0.24 to 0.0.25
@@ -28,3 +28,3 @@ { | ||
}, | ||
"version": "0.0.24" | ||
"version": "0.0.25" | ||
} |
@@ -1,9 +0,12 @@ | ||
import { ClassList, FieldConfig, ClassConfig, ViewConfig } from './types'; | ||
import { MetadataRegistry, RootRegistry, Class, ChangeEvent } from '@travetto/registry'; | ||
import { AppEnv } from '@travetto/base'; | ||
import { ClassList, FieldConfig, ClassConfig, ViewConfig, DEFAULT_VIEW } from './types'; | ||
import { | ||
SchemaChangeListener, | ||
SchemaChangeEvent, FieldChangeEvent, | ||
SCHEMA_CHANGE_EVENT, FIELD_CHANGE_EVENT | ||
} from './changes'; | ||
export class $SchemaRegistry extends MetadataRegistry<ClassConfig, FieldConfig> { | ||
static DEFAULT_VIEW = '__all'; | ||
DEFAULT_VIEW = $SchemaRegistry.DEFAULT_VIEW; | ||
constructor() { | ||
@@ -13,2 +16,16 @@ super(RootRegistry); | ||
computeSchemaDependencies(cls: Class, curr: Class = cls, path: string[] = []) { | ||
const config = this.get(curr); | ||
SchemaChangeListener.trackSchemaDependency(curr, cls, path, this.get(cls)); | ||
// Read children | ||
const view = config.views[DEFAULT_VIEW]; | ||
for (const k of view.fields) { | ||
if (this.has(view.schema[k].declared.type)) { | ||
this.computeSchemaDependencies(cls, view.schema[k].declared.type, [...path, k]); | ||
} | ||
} | ||
} | ||
createPending(cls: Class) { | ||
@@ -19,3 +36,3 @@ return { | ||
views: { | ||
[$SchemaRegistry.DEFAULT_VIEW]: { | ||
[DEFAULT_VIEW]: { | ||
schema: {}, | ||
@@ -29,3 +46,3 @@ fields: [] | ||
getPendingViewSchema<T>(cls: Class<T>, view?: string) { | ||
view = view || $SchemaRegistry.DEFAULT_VIEW; | ||
view = view || DEFAULT_VIEW; | ||
@@ -41,5 +58,5 @@ if (cls.__id) { | ||
getViewSchema<T>(cls: Class<T>, view?: string) { | ||
const res = this.get(cls)!.views[view || SchemaRegistry.DEFAULT_VIEW]; | ||
const res = this.get(cls)!.views[view || DEFAULT_VIEW]; | ||
if (!res) { | ||
throw new Error(`Unknown view ${view || SchemaRegistry.DEFAULT_VIEW} for ${cls.name}`); | ||
throw new Error(`Unknown view ${view || DEFAULT_VIEW} for ${cls.name}`); | ||
} | ||
@@ -50,3 +67,3 @@ return res; | ||
getOrCreatePendingViewConfig<T>(target: Class<T>, view?: string) { | ||
view = view || $SchemaRegistry.DEFAULT_VIEW; | ||
view = view || DEFAULT_VIEW; | ||
@@ -66,3 +83,3 @@ const conf = this.getOrCreatePending(target); | ||
registerPendingFieldFacet(target: Class, prop: string, config: any, view?: string) { | ||
view = view || $SchemaRegistry.DEFAULT_VIEW; | ||
view = view || DEFAULT_VIEW; | ||
@@ -76,3 +93,3 @@ const defViewConf = this.getOrCreatePendingViewConfig(target); | ||
if (view !== $SchemaRegistry.DEFAULT_VIEW) { | ||
if (view !== DEFAULT_VIEW) { | ||
const viewConf = this.getOrCreatePendingViewConfig(target, view); | ||
@@ -135,43 +152,2 @@ if (!viewConf.schema[prop]) { | ||
emitFieldDelta(cls: Class) { | ||
const prev = this.getExpired(cls); | ||
const curr = this.get(cls); | ||
const prevView = prev.views[this.DEFAULT_VIEW]; | ||
const currView = curr.views[this.DEFAULT_VIEW]; | ||
const prevFields = new Set(prevView.fields); | ||
const currFields = new Set(currView.fields); | ||
const changes: ChangeEvent<FieldConfig>[] = []; | ||
for (const c of currFields) { | ||
if (!prevFields.has(c)) { | ||
changes.push({ curr: currView.schema[c], type: 'added' }); | ||
} | ||
} | ||
for (const c of prevFields) { | ||
if (!currFields.has(c)) { | ||
changes.push({ prev: prevView.schema[c], type: 'removing' }); | ||
} | ||
} | ||
for (const c of currFields) { | ||
if (prevFields.has(c)) { | ||
const { type, ...prevSchema } = prevView.schema[c]; | ||
const { type: type2, ...currSchema } = currView.schema[c]; | ||
if (JSON.stringify(prevSchema) !== JSON.stringify(currSchema)) { | ||
changes.push({ prev: prevView.schema[c], curr: currView.schema[c], type: 'changed' }); | ||
} | ||
} | ||
} | ||
this.events.emit('field:change', { cls, changes }); | ||
} | ||
onFieldChange<T>(callback: (e: { cls: Class, changes: ChangeEvent<FieldConfig>[] }) => any): void { | ||
this.events.on('field:change', callback); | ||
} | ||
onInstallFinalize(cls: Class) { | ||
@@ -199,10 +175,38 @@ | ||
onInstall(cls: Class, e: ChangeEvent<Class>) { | ||
super.onInstall(cls, e); | ||
if (AppEnv.watch && this.has(cls)) { | ||
this.computeSchemaDependencies(cls); | ||
} | ||
} | ||
onUninstall<T>(cls: Class<T>, e: ChangeEvent<Class>) { | ||
super.onUninstall(cls, e); | ||
if (e.type === 'removing' && this.hasExpired(cls)) { | ||
SchemaChangeListener.clearSchemaDependency(cls); | ||
} | ||
} | ||
emit(ev: ChangeEvent<Class>) { | ||
super.emit(ev); | ||
if (ev.type === 'changed') { | ||
this.emitFieldDelta(ev.curr!); | ||
SchemaChangeListener.emitFieldChanges({ | ||
type: 'changed', | ||
curr: this.get(ev.curr!), | ||
prev: this.getExpired(ev.curr!) | ||
}); | ||
} | ||
} | ||
onSchemaChange(cb: (ev: SchemaChangeEvent) => void) { | ||
SchemaChangeListener.on(SCHEMA_CHANGE_EVENT, cb); | ||
} | ||
onFieldChange<T>(callback: (e: FieldChangeEvent) => any): void { | ||
SchemaChangeListener.on(FIELD_CHANGE_EVENT, callback); | ||
} | ||
} | ||
export const SchemaRegistry = new $SchemaRegistry(); |
import { Class } from '@travetto/registry'; | ||
import { ValidationError } from '.'; | ||
import { ValidationError } from './validator'; | ||
export const DEFAULT_VIEW = '__all'; | ||
export type ClassList = Class | [Class]; | ||
@@ -5,0 +7,0 @@ |
@@ -1,2 +0,2 @@ | ||
import { SchemaRegistry, FieldConfig } from '../service'; | ||
import { SchemaRegistry, FieldConfig, DEFAULT_VIEW } from '../service'; | ||
import { Class } from '@travetto/registry'; | ||
@@ -78,3 +78,3 @@ | ||
static bindSchema<T>(cons: Class, obj: T, data?: any, view?: string): T { | ||
view = view || SchemaRegistry.DEFAULT_VIEW; | ||
view = view || DEFAULT_VIEW; | ||
@@ -81,0 +81,0 @@ if (!!data) { |
@@ -5,4 +5,7 @@ require('@travetto/base/bin/travetto').run() | ||
require('../src').SchemaRegistry.onFieldChange((e) => { | ||
console.log(e); | ||
console.log('Field', e); | ||
}); | ||
require('../src').SchemaRegistry.onSchemaChange((e) => { | ||
console.log('Schema', e); | ||
}); | ||
}); |
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
47170
29
1360