This repository has been archived by the owner on Oct 22, 2021. It is now read-only.
/
factory.ts
232 lines (190 loc) · 7.48 KB
/
factory.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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import Repository from '../'
import Fiber from './'
import Level from 'level'
import npath from 'path'
import klaw from 'klaw'
import through2 from 'through2'
import fs from 'fs-extra'
import stream from 'stream'
import PandoError from '../error'
export default class FiberFactory {
public repository: Repository
public db: Level
constructor(repository: Repository) {
this.repository = repository
this.db = Level(npath.join(repository.paths.fibers, 'db'), { valueEncoding: 'json' })
}
public async uuid(name: string): Promise<string> {
let uuid: string|undefined = undefined
return new Promise<string>((resolve, reject) => {
this.db
.createReadStream()
.on('data', fiber => {
if (fiber.value.name === name) { uuid = fiber.key }
})
.on('end', () => {
if (typeof uuid === 'undefined') {
reject(new PandoError('E_FIBER_NOT_FOUND', name))
} else {
resolve(uuid)
}
})
.on('error', (err) => {
reject (err)
})
})
}
public async current({ uuid = false }: { uuid?: boolean} = {}): Promise<Fiber|string|undefined> {
let done: boolean = false
return new Promise<Fiber|string|undefined>((resolve, reject) => {
this.db
.createReadStream()
.on('data', async fiber => {
if (fiber.value.current) {
done = true
if (uuid) {
resolve(fiber.key)
} else {
resolve(await this.load(fiber.key, { uuid: true }))
}
}
})
.on('end', async () => {
if (!done) {
reject(new Error('Unknow current fiber'))
}
})
.on('error', err => {
reject(err)
})
})
}
public async create(name: string, { fork = true, open = false }: { fork?: boolean, open?: boolean } = {}): Promise<Fiber> {
let fiber = await Fiber.create(this.repository)
if (fork) {
const current: string = await this.current({ uuid: true }) as string
await fs.copy(npath.join(this.repository.paths.fibers, current, 'snapshots'), npath.join(this.repository.paths.fibers, fiber.uuid, 'snapshots'))
await this._stash(fiber.uuid, { copy: true })
}
await this.db.put(fiber.uuid, { name, wdir: 'null', snapshot: 'null', current: false })
if (open) {
await fiber._open()
}
return fiber
}
public async load(name: string, { uuid = false }: { uuid?: boolean } = {}): Promise<Fiber> {
name = uuid ? name : await this.uuid(name)
return Fiber.load(this.repository, name)
}
public async list(): Promise<any> {
const fibers: any[] = []
return new Promise<any>((resolve, reject) => {
this.db
.createReadStream()
.on('data', async fiber => {
fibers.push(fiber.value)
})
.on('end', async () => {
resolve(fibers)
})
.on('error', err => {
reject(err)
})
})
}
public async switch(name: string, { stash = true }: { stash?: boolean} = {}): Promise<any> {
let from: string|undefined = undefined
let to: string|undefined = undefined
let ops: any[] = []
return new Promise<Fiber>((resolve, reject) => {
const read = this.db.createReadStream()
const write = new stream.Writable({
objectMode: true,
write: async (fiber, encoding, next) => {
if (fiber.value.current) {
from = fiber.key
ops.push({ type: 'put', key: fiber.key, value: { ...fiber.value, ...{ current: false } } })
}
if (fiber.value.name === name) {
to = fiber.key
ops.push({ type: 'put', key: fiber.key, value: { ...fiber.value, ...{ current: true } } })
}
next()
}
})
write
.on('finish', async () => {
if (stash) {
// throw if uuid is undefined
await this._stash(from as string)
await Promise.all([
// this._stash(from as string),
this._unstash(to as string),
this.db.batch(ops)
])
} else {
await this.db.batch(ops)
}
resolve()
})
.on('error', err => { reject(err) })
read
.pipe(write)
.on('error', err => { reject(err) })
})
}
private async _stash(uuid: string, { copy = false }: { copy?: boolean} = {}): Promise<any> {
const ops:any[] = []
const files: string[] = []
return new Promise<any>((resolve, reject) => {
klaw(this.repository.paths.root, { depthLimit: 0 })
.pipe(through2.obj(function (item, enc, next) { // ignore .pando directory
if (item.path.indexOf('.pando') >= 0) {
next()
} else {
this.push(item)
next()
}
}))
.on('data', file => {
files.push(file.path)
})
.on('error', (err) => {
reject(err)
})
.on('end', async () => {
files.shift()
for (let file of files) {
if (copy) {
ops.push(fs.copy(file, npath.join(this.repository.paths.fibers, uuid, 'stash', npath.basename(file))))
} else {
ops.push(fs.move(file, npath.join(this.repository.paths.fibers, uuid, 'stash', npath.basename(file)), { overwrite: true }))
}
}
await Promise.all(ops)
resolve()
})
})
}
private async _unstash(uuid: string): Promise<any> {
const ops:any[] = []
const files: string[] = []
return new Promise<any>((resolve, reject) => {
klaw(npath.join(this.repository.paths.fibers, uuid, 'stash'), { depthLimit: 0 })
.on('data', file => {
files.push(file.path)
})
.on('error', (err) => {
reject(err)
})
.on('end', async () => {
files.shift()
for (let file of files) {
ops.push(fs.move(file, npath.join(this.repository.paths.root, npath.basename(file)), { overwrite: true }))
}
await Promise.all(ops)
resolve()
})
})
}
}