-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNode.ts
More file actions
26 lines (25 loc) · 785 Bytes
/
Node.ts
File metadata and controls
26 lines (25 loc) · 785 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import { mendly } from "mendly"
import { Variables } from "./Variables"
export abstract class Node {
abstract readonly class: string
get variables(): Variables {
return {}
}
protected constructor(readonly region?: mendly.Error.Region) {}
toObject(): { class: string } & any {
return { class: this.class }
}
toJson(indent: string = ""): string {
return JSON.stringify(this.toObject(), null, indent)
}
static create(data: { class: string } & any): Node | undefined {
const creator = creators[data.class]
return creator?.(data)
}
}
export namespace Node {}
export type Creator = (data: { class: string } & any) => Node
const creators: { [name: string]: Creator | undefined } = {}
export function register(name: string, creator: Creator) {
creators[name] = creator
}