/
observables-Reactive-Extensions-RxJS.js
62 lines (58 loc) · 1.86 KB
/
observables-Reactive-Extensions-RxJS.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
global.useRx = true;
var Rx = require('rx');
require('../lib/fakesObservable');
module.exports = function upload(stream, idOrPath, tag, done) {
var blob = blobManager.create(account);
var tx = db.begin();
var blobIdP = blob.put(stream);
var fileP = self.byUuidOrPath(idOrPath).get();
var version, fileId, file;
Rx.Observable.forkJoin(blobIdP, fileP).flatMap(function(v) {
file = v[1];
var blobId = v[0];
var previousId = file ? file.version : null;
version = {
userAccountId: userAccount.id,
date: new Date(),
blobId: blobId,
creatorId: userAccount.id,
previousId: previousId,
};
version.id = Version.createHash(version);
return Version.insert(version).execWithin(tx);
}).flatMap(function() {
if (!file) {
var splitPath = idOrPath.split('/');
var fileName = splitPath[splitPath.length - 1];
var newId = uuid.v1();
return self.createQuery(idOrPath, {
id: newId,
userAccountId: userAccount.id,
name: fileName,
version: version.id
}).flatMap(function(q) {
return q.execWithin(tx);
}).map(function() {
return newId;
});
} else {
return Rx.Observable.return(file.id);
}
}).flatMap(function(fileIdV) {
fileId = fileIdV;
return FileVersion.insert({
fileId: fileId,
versionId: version.id
}).execWithin(tx);
}).flatMap(function() {
return File.whereUpdate({id: fileId}, {version: version.id})
.execWithin(tx);
}).subscribe(function() {
}, function() {
tx.rollback();
done(err);
}, function() {
tx.commit();
done();
});
}