-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatcherVirtual.js
153 lines (133 loc) · 4.39 KB
/
watcherVirtual.js
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
const VirtualPath = require('./virtualPath')
const { ExtraPayload, MetaData } = require('./types')
const { Event, EventTypes } = require('./event')
const FileNode = require('./fileNode')
const EventTransaction = require('./eventTransaction')
class VirtualTree {
FileTree = new FileNode()
Path = new VirtualPath()
ParentPath = new VirtualPath()
constructor(fileTree = new FileNode(), path = new VirtualPath(), parentPath = new VirtualPath()) {
this.FileTree = fileTree
this.Path = path
this.ParentPath = parentPath
}
/**
* params event = new Event(), extra = new ExtraPayload()
*/
Handler(event, extra) {
let error = null,
node = new FileNode(),
bExtra = null,
response
if (extra) {
bExtra = extra
}
switch (event.Type) {
case EventTypes.Remove:
response = this.Remove(event.FromPath)
node = response.fileNode
error = response.error
break
// case EventTypes.Write:
// response = this.Write(event.FromPath)
// node = response.fileNode
// error = response.error
// break
case EventTypes.Create:
response = this.Create(event.FromPath, bExtra)
node = response.fileNode
error = response.error
break
case EventTypes.Rename:
response = this.Rename(event.FromPath, event.ToPath)
node = response.fileNode
error = response.error
break
case EventTypes.Move:
response = this.Move(event.FromPath, event.ToPath)
node = response.fileNode
error = response.error
break
default:
error = new Error(`unhandled event: ${event.String()}`)
break
}
if (error) {
return { eventTransaction: null, error: error }
}
return { eventTransaction: this.MakeEventTransaction(node, event.Type), error: null }
}
Restore(tree = new FileNode()) {
this.FileTree = tree
return this
}
SearchByPath(p = '') {
return this.FileTree.Search(p)
}
SearchByUUID(uUID = '') {
return this.FileTree.SearchByUUID(uUID)
}
PrintTree(label = '') {
console.log(`----------------${label}----------------`)
console.log(JSON.stringify(this.FileTree.ToObject(), null, 2))
console.log(`----------------${label}----------------\n\n`)
}
Create(fromPath = new VirtualPath(), extra = new ExtraPayload()) {
const eventPath = fromPath.ExcludePath(this.ParentPath),
{ fileNode, error } = this.FileTree.Create(eventPath, fromPath)
if (error) {
return { fileNode: null, error: error }
}
fileNode.UpdateWithExtra(extra)
return { fileNode: fileNode, error: null }
}
Remove(fromPath = new VirtualPath()) {
const { fileNode, error } = this.FileTree.Remove(fromPath.ExcludePath(this.ParentPath))
if (error) {
return { fileNode: null, error: error }
}
return { fileNode: fileNode, error: null }
}
Rename(fromPath = new VirtualPath(), toPath = new VirtualPath()) {
const { fileNode, error } = this.FileTree.Rename(fromPath.ExcludePath(this.ParentPath),
toPath.ExcludePath(this.ParentPath))
if (error) {
return { fileNode: null, error: error }
}
return { fileNode: fileNode, error: null }
}
Move(fromPath = new VirtualPath(), toPath = new VirtualPath()) {
const { fileNode, error } = this.FileTree.Move(fromPath.ExcludePath(this.ParentPath),
toPath.ExcludePath(this.ParentPath))
if (error) {
return { fileNode: null, error: error }
}
return { fileNode: fileNode, error: null }
}
Write(fromPath = new VirtualPath()) {
return { fileNode: null, error: null }
}
MakeEventTransaction(node = new FileNode(), event = EventTypes.Create) {
return new EventTransaction(node.Name, event, node.UUID, node.ParentUUID, node.Meta)
}
}
const NewVirtualPathWatcher = (uUID = '', virtualPath = '', extra = new ExtraPayload()) => {
const path = new VirtualPath(virtualPath, true),
root = new FileNode([],
path.Name(),
'',
'',
new MetaData(true)),
virtualTree = new VirtualTree(root, path, path.ParentPath()),
e = new Event(EventTypes.Create, path),
{ eventTransaction, error } = virtualTree.Handler(e, extra)
if (error) {
return { watcher: null, eventTransaction: null, error: error }
}
return { watcher: virtualTree, eventTransaction: eventTransaction, error: null }
}
module.exports = {
VirtualTree,
NewVirtualPathWatcher
}