Commit 7880a84c53be4a312a09ca7cb7d1ac5bafc76d23
Add deinit method
Charles Lehner committed on 9/20/2016, 1:47:59 AMParent: 43d017788d2f57c366a1d4ccb85142ebba4ade90
Files changed
index.js | changed |
package.json | changed |
index.js | ||
---|---|---|
@@ -1,8 +1,9 @@ | ||
1 | 1 | var pull = require('pull-stream') |
2 | 2 | var paramap = require('pull-paramap') |
3 | 3 | var asyncMemo = require('asyncmemo') |
4 | 4 | var issueSchemas = require('./lib/schemas') |
5 | +var multicb = require('multicb') | |
5 | 6 | |
6 | 7 | function Cache(fn, ssb) { |
7 | 8 | return asyncMemo(fn) |
8 | 9 | return function (key, cb) { ac.get(key, cb) } |
@@ -58,8 +59,9 @@ | ||
58 | 59 | |
59 | 60 | exports.init = function (ssb) { |
60 | 61 | |
61 | 62 | var ssbGet = asyncMemo(ssb.get) |
63 | + var liveStreams = [] | |
62 | 64 | |
63 | 65 | var getIssue = asyncMemo(function (id, cb) { |
64 | 66 | var issue = {} |
65 | 67 | var issueMsg |
@@ -95,11 +97,13 @@ | ||
95 | 97 | old: true, live: false, sync: false}), |
96 | 98 | pull.drain(onOldMsg, onOldEnd) |
97 | 99 | ) |
98 | 100 | // keep the results up-to-date in the future |
101 | + var read = ssb.links({dest: id, values: true, | |
102 | + old: false, live: true, sync: false}) | |
103 | + liveStreams.push(read) | |
99 | 104 | pull( |
100 | - ssb.links({dest: id, values: true, | |
101 | - old: false, live: true, sync: false}), | |
105 | + read, | |
102 | 106 | pull.drain(onNewMsg, onNewEnd) |
103 | 107 | ) |
104 | 108 | } |
105 | 109 | |
@@ -191,8 +195,17 @@ | ||
191 | 195 | } |
192 | 196 | } |
193 | 197 | }) |
194 | 198 | |
199 | + function deinit(cb) { | |
200 | + var done = multicb() | |
201 | + // cancel all live streams | |
202 | + liveStreams.forEach(function (read) { | |
203 | + read(true, done()) | |
204 | + }) | |
205 | + done(cb) | |
206 | + } | |
207 | + | |
195 | 208 | function listIssues(opts) { |
196 | 209 | opts.type = 'issue' |
197 | 210 | return pull( |
198 | 211 | // TODO: use links2 for this |
@@ -239,8 +252,9 @@ | ||
239 | 252 | }) |
240 | 253 | } |
241 | 254 | |
242 | 255 | return { |
256 | + deinit: deinit, | |
243 | 257 | get: getIssue, |
244 | 258 | list: listIssues, |
245 | 259 | new: newIssue, |
246 | 260 | edit: editIssue, |
Built with git-ssb-web