Files: 5c49f6009ad5b21074c77817ed53d140a4b0f9b6 / _async.js
1382 bytesRaw
1 | var Obv = require('obv') |
2 | var V = require('./') |
3 | |
4 | module.exports = function (state, log) { |
5 | |
6 | var writing = Obv() |
7 | writing.set(false) |
8 | var author |
9 | function queue (msg) { |
10 | state = V.queue(state, msg) |
11 | if(author && (msg.author !== author) && (state.feeds[author].queue.length > 20)) { |
12 | state = V.validate(state, author) |
13 | state = V.queue(state, msg) |
14 | } |
15 | else |
16 | state = V.queue(state, msg) |
17 | author = msg.author |
18 | } |
19 | |
20 | function flush (id, cb) { |
21 | if(!cb) |
22 | cb = id, id = null |
23 | |
24 | if(writing.value) |
25 | return writing.once(function () { flush(id, cb) }, false) |
26 | else |
27 | writing.set(true) |
28 | |
29 | if(id === true) { //flush everything |
30 | for(var k in state.feeds) |
31 | if(state.feeds[k].queue.length) |
32 | state = V.validate(state, k) |
33 | } |
34 | else if(id) |
35 | state = V.validate(state, id) |
36 | |
37 | if(state.queue.length) { |
38 | state.writing = state.queue |
39 | state.queue = [] |
40 | log.append(state.writing, function (err, value) { |
41 | state.writing = [] |
42 | writing.set(false) |
43 | cb(err, value) |
44 | }) |
45 | } |
46 | else |
47 | cb() |
48 | } |
49 | |
50 | return { |
51 | writing: writing, |
52 | append: function (msg, cb) { |
53 | queue(msg) |
54 | flush(msg.author, cb) |
55 | }, |
56 | queue: queue, |
57 | flush: flush, |
58 | validated: function () { |
59 | return state.validated |
60 | }, |
61 | queued: function () { |
62 | return state.queued |
63 | } |
64 | } |
65 | } |
66 | |
67 |
Built with git-ssb-web