forked from FabricLabs/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstash.js
70 lines (55 loc) · 1.57 KB
/
stash.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
'use strict';
// TODO: note that generally, requirements are loosely ordered by
// their relative importance to the file in question
const util = require('util');
const localforage = require('localforage');
function Stash (vector) {
this.config = Object.assign({
path: './stores/store',
get: this.get,
set: this.set,
del: this.del,
transform: this.transform,
createReadStream: this.createReadStream
}, vector || {});
this.clock = 0;
this.stack = [];
this.known = {};
this.open();
this.init();
}
util.inherits(Stash, require('./vector'));
Stash.prototype.open = function load () {
this.db = localforage.createInstance({
name: 'fabric'
});
};
Stash.prototype.get = async function GET (key) {
var self = this;
var value = await self.db.getItem(key);
if (!value) return null;
// if (typeof value !== 'string') return JSON.parse(value);
return value;
};
Stash.prototype.set = async function PUT (key, value) {
var self = this;
if (typeof value !== 'string') {
value = self._serialize(value);
}
await self.db.setItem(key, value);
return await self.db.getItem(key);
};
Stash.prototype.del = async function DEL (key) {
return await this.db.setItem(key, null);
};
Stash.prototype.transform = function TRANSFORM (transaction, done) {
// this.db.del(batch, done);
return new Error('not yet implemented');
};
Stash.prototype.createReadStream = function createReadStream () {
return this.db.createReadStream();
};
Stash.prototype.close = async function close () {
return await this.db.close();
};
module.exports = Stash;