mirror of
https://github.com/misode/misode.github.io.git
synced 2026-04-23 07:10:41 +00:00
51 lines
1.0 KiB
TypeScript
51 lines
1.0 KiB
TypeScript
import { RootNode } from "../nodes/RootNode"
|
|
import { Path } from "./Path"
|
|
|
|
export interface ModelListener {
|
|
invalidated(model: DataModel): void
|
|
}
|
|
|
|
export class DataModel {
|
|
data: any
|
|
schema: RootNode
|
|
listeners: ModelListener[]
|
|
|
|
constructor(schema: RootNode) {
|
|
this.schema = schema
|
|
this.data = schema.default()
|
|
this.listeners = []
|
|
}
|
|
|
|
addListener(listener: ModelListener) {
|
|
this.listeners.push(listener)
|
|
}
|
|
|
|
invalidate() {
|
|
this.listeners.forEach(listener => listener.invalidated(this))
|
|
}
|
|
|
|
set(path: Path, value: any) {
|
|
let node = this.data;
|
|
for (let index of path.pop()) {
|
|
if (node[index] === undefined) {
|
|
node[index] = {}
|
|
}
|
|
node = node[index]
|
|
}
|
|
|
|
console.log('Set', path.toString(), JSON.stringify(value))
|
|
|
|
if (value === undefined) {
|
|
if (typeof path.last() === 'number') {
|
|
node.splice(path.last(), 1)
|
|
} else {
|
|
delete node[path.last()]
|
|
}
|
|
} else {
|
|
node[path.last()] = value
|
|
}
|
|
|
|
this.invalidate()
|
|
}
|
|
}
|