-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmetaparticle-storage.js
More file actions
64 lines (60 loc) · 2 KB
/
Copy pathmetaparticle-storage.js
File metadata and controls
64 lines (60 loc) · 2 KB
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
(function () {
var storage = null;
var q = require('q');
var storageImpls = {
"file": './metaparticle-file-storage.js',
"redis": './metaparticle-redis-storage',
"mysql": './metaparticle-mysql-storage.js'
}
module.exports.setStorage = function(name, config) {
// Does this storage method exist?
const file = storageImpls[name];
if ('undefined' !== typeof (file)) {
// Yes, initialise it.
storage = require(file);
storage.configure(config);
}
}
module.exports.shutdown = () => {
if (storage && storage.shutdown) {
storage.shutdown();
}
}
module.exports.scoped = function(name, fn) {
if (storage == null) {
throw("You must initialize a storage implementation via setStorage first.");
}
var promise = q.defer();
loadExecuteStore(name, fn, promise);
return promise.promise;
}
var loadExecuteStore = function(name, fn, promise) {
storage.load(name).then(function(data) {
var dirty = false;
var Proxy = require('harmony-proxy');
var obj = new Proxy(data.data, {
set: function (target, property, value) {
data.data[property] = value;
dirty = true;
return true;
}
});
var result = fn(obj);
if (dirty) {
storage.store(name, data).then(function(success) {
if (!success) {
setTimeout(function() {
loadExecuteStore(name, fn, promise);
}, 100);
} else {
promise.resolve(result);
}
}).catch(function(error) {
console.log(error);
});
}
}).catch(function(error) {
console.log(error);
});
};
}());