git ssb

0+

Dominic / ssb-validate



Commit 5c49f6009ad5b21074c77817ed53d140a4b0f9b6

rewrite async.js

Dominic Tarr committed on 9/22/2017, 8:54:57 PM
Parent: 302393313ab7ab84c0a33d2a455554450f2b902d

Files changed

async.jschanged
_async.jsadded
async.jsView
@@ -1,66 +1,31 @@
1-var Obv = require('obv')
21 var V = require('./')
2 +var AsyncWrite = require('async-write')
33
4-module.exports = function (state, log) {
4 +module.exports = function (state, log, hmac_key) {
5 + var queue = AsyncWrite(function (_, cb) {
6 + var batch = state.queue
7 + state.queue = []
8 + log.append(batch, cb)
9 + }, function reduce(_, msg) {
10 + return V.append(state, msg)
11 + }, function (_state) {
12 + return state.queue.length < 1000
13 + }, 200)
514
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-
5015 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
16 + add: queue,
17 + createFeed: function (keys) {
18 + function add (msg, cb) {
19 + queue(
20 + V.create(state, hmac_key, keys, content, timestamp()),
21 + cb
22 + )
23 + }
24 + return {
25 + add: add, publish: add,
26 + keys: keys, id: keys.id
27 + }
6328 }
6429 }
6530 }
6631
_async.jsView
@@ -1,0 +1,66 @@
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 +

Built with git-ssb-web