Commit 673c7a2d4beb3d140d4a72dc921e63e7f985f873
disable localStorage thread cache
Dominic Tarr committed on 8/14/2017, 1:06:17 PMParent: 4dbb2a6693e61517c65a6eb549c9f0d8acf437b7
Files changed
state/obs.js | changed |
state/obs.js | ||
---|---|---|
@@ -8,35 +8,38 @@ | ||
8 | 8 | function isObject (o) { |
9 | 9 | return 'object' === typeof o |
10 | 10 | } |
11 | 11 | |
12 | -exports.gives = nest('state.obs.threads', true) | |
12 | +exports.gives = nest({ | |
13 | + 'state.obs.threads': true, | |
14 | + 'state.obs.channel': true | |
15 | +}) | |
13 | 16 | |
14 | 17 | exports.needs = nest({ |
15 | 18 | 'message.sync.unbox': 'first', |
16 | - 'sbot.pull.log': 'first' | |
19 | + 'sbot.pull.log': 'first', | |
20 | + 'feed.pull.channel': 'first' | |
17 | 21 | }) |
18 | 22 | |
19 | 23 | exports.create = function (api) { |
20 | 24 | var threadsObs |
21 | 25 | |
22 | - function createStateObs (threadReduce, createStream, initial) { | |
23 | - var lastTimestamp = initial ? initial.last : Date.now() | |
24 | - var firstTimestamp = initial ? initial.first || Date.now() : Date.now() | |
26 | + function createStateObs (reduce, createStream, opts, initial) { | |
27 | + var lastTimestamp = opts.last || Date.now() | |
28 | + var firstTimestamp = opts.first || Date.now() | |
25 | 29 | |
26 | 30 | function unbox () { |
27 | 31 | return pull( |
28 | 32 | pull.map(function (data) { |
29 | -// lastTimestamp = data.timestamp | |
30 | 33 | if(isObject(data.value.content)) return data |
31 | 34 | return api.message.sync.unbox(data) |
32 | 35 | }), |
33 | 36 | pull.filter(Boolean) |
34 | 37 | ) |
35 | 38 | } |
36 | 39 | |
37 | - var threadsObs = PullObv( | |
38 | - threadReduce, | |
40 | + var obs = PullObv( | |
41 | + reduce, | |
39 | 42 | pull( |
40 | 43 | Next(function () { |
41 | 44 | return createStream({reverse: true, limit: 500, lt: lastTimestamp}) |
42 | 45 | }), |
@@ -58,47 +61,72 @@ | ||
58 | 61 | }), |
59 | 62 | pull.drain(function (data) { |
60 | 63 | if(data.sync) return |
61 | 64 | firstTimestamp = data.timestamp |
62 | - threadsObs.set(threadReduce(threadsObs.value, data)) | |
65 | + obs.set(reduce(threadsObs.value, data)) | |
63 | 66 | }) |
64 | 67 | ) |
65 | 68 | |
66 | - return threadsObs | |
69 | + return obs | |
67 | 70 | } |
68 | 71 | |
69 | 72 | |
70 | - return nest('state.obs.threads', function buildThreadObs() { | |
71 | - if(threadsObs) return threadsObs | |
73 | + return nest({ | |
74 | + 'state.obs.channel': function (channel) { | |
72 | 75 | |
73 | -// var initial | |
74 | -// try { initial = JSON.parse(localStorage.threadsState) } | |
75 | -// catch (_) { } | |
76 | -// | |
76 | + return createStateObs( | |
77 | + threadReduce, | |
78 | + function (opts) { | |
79 | + return opts.reverse ? | |
80 | + api.feed.pull.channel(channel)(opts): | |
81 | + pull(api.sbot.pull.log(opts), pull.filter(function (data) { | |
82 | + if(data.sync) return false | |
83 | + return data.value.content.channel === channel | |
84 | + })) | |
85 | + }, | |
86 | + {} | |
87 | + ) | |
77 | 88 | |
78 | - initial = {} | |
89 | + var channelObs = PullObv( | |
90 | + threadReduce, | |
91 | + createChannelStream({reverse: true, limit: 1000}) | |
92 | + ) | |
79 | 93 | |
80 | - threadsObs = createStateObs(threadReduce, api.sbot.pull.log, initial) | |
81 | 94 | |
82 | - threadsObs(function (threadsState) { | |
83 | - if(threadsState.ended && threadsState.ended !== true) | |
84 | - console.error('threadObs error:', threadsState.ended) | |
85 | - }) | |
95 | + }, | |
96 | + 'state.obs.threads': function buildThreadObs() { | |
97 | + if(threadsObs) return threadsObs | |
86 | 98 | |
87 | -// var timer | |
88 | -// //keep localStorage up to date | |
89 | -// threadsObs(function (threadsState) { | |
90 | -// if(timer) return | |
91 | -// timer = setTimeout(function () { | |
92 | -// timer = null | |
93 | -// threadsState.last = lastTimestamp | |
94 | -// console.log('save state') | |
95 | -// localStorage.threadsState = JSON.stringify(threadsState) | |
96 | -// }, 1000) | |
97 | -// }) | |
98 | -// | |
99 | + // DISABLE localStorage cache. mainly disabling this to make debugging the other stuff | |
100 | + // easier. maybe re-enable this later? also, should this be for every channel too? not sure. | |
99 | 101 | |
100 | - return threadsObs | |
102 | + // var initial | |
103 | + // try { initial = JSON.parse(localStorage.threadsState) } | |
104 | + // catch (_) { } | |
105 | + | |
106 | + initial = {} | |
107 | + | |
108 | + threadsObs = createStateObs(threadReduce, api.sbot.pull.log, initial, {}) | |
109 | + | |
110 | + threadsObs(function (threadsState) { | |
111 | + if(threadsState.ended && threadsState.ended !== true) | |
112 | + console.error('threadObs error:', threadsState.ended) | |
113 | + }) | |
114 | + | |
115 | + // var timer | |
116 | + // //keep localStorage up to date | |
117 | + // threadsObs(function (threadsState) { | |
118 | + // if(timer) return | |
119 | + // timer = setTimeout(function () { | |
120 | + // timer = null | |
121 | + // threadsState.last = lastTimestamp | |
122 | + // console.log('save state') | |
123 | + // localStorage.threadsState = JSON.stringify(threadsState) | |
124 | + // }, 1000) | |
125 | + // }) | |
126 | + | |
127 | + return threadsObs | |
128 | + } | |
101 | 129 | }) |
102 | 130 | } |
103 | 131 | |
104 | 132 |
Built with git-ssb-web