-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathPerspectivesController.ts
98 lines (86 loc) · 3.14 KB
/
PerspectivesController.ts
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import path from 'path'
import fs from 'fs'
import { v4 as uuidv4 } from 'uuid'
import * as PubSub from './PubSub'
import type PerspectiveContext from './PerspectiveContext'
import type PerspectiveID from './PerspectiveID'
import Perspective from './Perspective'
export default class PerspectivesController {
#perspectiveIDs: Map<string, PerspectiveID>
#perspectiveInstances: Map<string, Perspective>
#rootConfigPath
#pubsub
#context
constructor(rootConfigPath: string, context: PerspectiveContext) {
this.#context = context
this.#rootConfigPath = rootConfigPath
this.#pubsub = PubSub.get()
this.#perspectiveIDs = new Map<string, PerspectiveID>()
this.#perspectiveInstances = new Map<string, Perspective>()
const FILENAME = 'perspectives.json'
const FILEPATH = path.join(rootConfigPath, FILENAME)
if(fs.existsSync(FILEPATH)) {
const fileObject = JSON.parse(fs.readFileSync(FILEPATH).toString())
const entries = Object.keys(fileObject).map(k => {
console.debug(`Found Perspective "${k}":`, fileObject[k])
this.#perspectiveIDs.set(k, fileObject[k])
})
}
}
private save() {
const FILENAME = 'perspectives.json'
const FILEPATH = path.join(this.#rootConfigPath, FILENAME)
const obj = {}
this.#perspectiveIDs.forEach((perspectiveID, uuid) => {
obj[uuid] = perspectiveID
})
fs.writeFileSync(FILEPATH, JSON.stringify(obj))
}
perspectiveID(uuid: string): PerspectiveID|void {
const pID = this.#perspectiveIDs.get(uuid)
console.log("pID:", pID)
return pID
}
allPerspectiveIDs(): PerspectiveID[] {
const alluuids = Array.from(this.#perspectiveIDs.values())
console.log("ALL PerspectiveIDs:", alluuids)
return alluuids
}
perspective(uuid: string): Perspective {
const foundInstance = this.#perspectiveInstances.get(uuid)
if(foundInstance) {
return foundInstance
} else {
const foundID = this.#perspectiveIDs.get(uuid)
if(foundID) {
return new Perspective(foundID, this.#context)
} else {
throw Error(`Perspective not found: ${uuid}`)
}
}
}
add(perspective) {
if(!perspective.uuid) {
perspective.uuid = uuidv4()
}
this.#perspectiveIDs.set(perspective.uuid, perspective)
this.save()
this.#pubsub.publish(PubSub.PERSPECTIVE_ADDED_TOPIC, { perspective })
return perspective
}
remove(uuid) {
this.#perspectiveIDs.delete(uuid)
this.save()
this.#pubsub.publish(PubSub.PERSPECTIVE_REMOVED_TOPIC, { uuid })
}
update(perspective) {
const uuid = perspective.uuid
this.#perspectiveIDs.set(uuid, perspective)
this.save()
const instance = this.#perspectiveInstances.get(uuid)
if(instance) {
instance.updateFromId(perspective as PerspectiveID)
}
this.#pubsub.publish(PubSub.PERSPECTIVE_UPDATED_TOPIC, { perspective })
}
}