git ssb

30+

cel / git-ssb-web



Tree: f4de6185a9ab8d859093c6270327cf70d43533c7

Files: f4de6185a9ab8d859093c6270327cf70d43533c7 / lib / index-cache.js

3157 bytesRaw
1var pull = require('pull-stream')
2var cat = require('pull-cat')
3
4// work around sync not getting emitted
5function logt(sbot, opt) {
6 if (!opt.live) return sbot.messagesByType(opt)
7 var optOld = {}, optNew = {}
8 for (var k in opt) optOld[k] = opt[k], optNew[k] = opt[k]
9 optOld.live = false, optOld.old = true
10 optNew.live = true, optNew.old = false
11 return cat([
12 sbot.messagesByType(optOld),
13 pull.once({sync: true}),
14 sbot.messagesByType(optNew)
15 ])
16}
17
18// Keep track of the set of open issues and pull-requests for each repo.
19// This is like a 2P-set, where an issue/pull-request message adds to the set
20// and a link rel issues with property close: true is a remove from the set.
21
22module.exports = function (sbot) {
23 var repos = {/* issue/pr id : repo id */}
24 var repoIssues = {/* repo id : {issue id} */}
25 var repoPRs = {/* repo id : {pr id} */}
26 var closed = {/* issue/pr id */}
27 var waiting = 3
28 var start = new Date
29
30 pull(
31 logt(sbot, {
32 type: 'issue',
33 live: true,
34 }),
35 pull.drain(function (msg) {
36 if (msg.sync) return sync()
37 if (msg.key in closed) return delete closed[msg.key]
38 var repoId = msg.value.content.project || msg.value.content.repo
39 var issues = repoIssues[repoId] || (repoIssues[repoId] = {count: 0})
40 issues[msg.key] = true
41 repos[msg.key] = repoId
42 issues.count++
43 }, function (err) {
44 if (err) console.error(err.stack || err)
45 })
46 )
47
48 pull(
49 logt(sbot, {
50 type: 'pull-request',
51 live: true,
52 }),
53 pull.drain(function (msg) {
54 if (msg.sync) return sync()
55 if (msg.key in closed) return delete closed[msg.key]
56 var repoId = msg.value.content.repo
57 var prs = repoPRs[repoId] || (repoPRs[repoId] = {count: 0})
58 prs[msg.key] = true
59 repos[msg.key] = repoId
60 prs.count++
61 }, function (err) {
62 if (err) console.error(err.stack || err)
63 })
64 )
65
66 pull(
67 sbot.links({
68 rel: 'issues',
69 values: true,
70 live: true,
71 }),
72 pull.map(function (msg) {
73 if (msg.sync) return sync()
74 return msg.value.content.issues
75 }),
76 pull.flatten(),
77 pull.filter(function (issue) {
78 return issue.open === false
79 }),
80 pull.drain(function (update) {
81 var id = update.link
82 var repoId = repos[id]
83 if (repoId) {
84 var issues, prs
85 if ((issues = repoIssues[repoId]) && issues[id])
86 delete issues[id], issues.count--
87 if ((prs = repoPRs[repoId]) && prs[id])
88 delete prs[id], prs.count--
89 } else {
90 closed[id] = true
91 }
92 }, function (err) {
93 if (err) console.error(err.stack || err)
94 })
95 )
96
97 function sync() {
98 if (--waiting) return
99 console.log('Issues state synced', -(start -= new Date)/1000 + 's')
100 }
101
102 return {
103 getIssuesCount: function (repoId, placeholder) {
104 if (waiting) return placeholder || NaN
105 var issue = repoIssues[repoId]
106 return issue ? issue.count : 0
107 },
108 getPRsCount: function (repoId, placeholder) {
109 if (waiting) return placeholder || NaN
110 var pr = repoPRs[repoId]
111 return pr ? pr.count : 0
112 },
113 }
114}
115

Built with git-ssb-web