Commit 9b4392d8b0d1d6a14f5ba5b1d6b4897c62e9d864
update thread in realtime
Dominic Tarr committed on 5/25/2016, 1:23:37 AMParent: 305ee7215bd7c6886901aa12488342f4a7c91193
Files changed
modules/thread.js | changed |
modules/thread.js | ||
---|---|---|
@@ -40,35 +40,62 @@ | ||
40 | 40 | } |
41 | 41 | |
42 | 42 | exports.screen_view = function (id, sbot) { |
43 | 43 | if(ref.isMsg(id)) { |
44 | - var div = h('div.column', {style: {'overflow-y': 'auto'}}) | |
44 | + var meta = { | |
45 | + type: 'post', | |
46 | + root: id, | |
47 | + branch: id //mutated when thread is loaded. | |
48 | + } | |
49 | + | |
50 | + var content = h('div') | |
51 | + var div = h('div.column', | |
52 | + {style: {'overflow-y': 'auto'}}, | |
53 | + content, | |
54 | + h('div.editor', u.firstPlug(exports.message_compose, meta, sbot)) | |
55 | + ) | |
56 | + | |
45 | 57 | var render = ui.createRenderers(exports.message_render, sbot) |
46 | 58 | |
47 | - getThread(id, sbot, function (err, thread) { | |
48 | - thread = thread.map(function (msg) { | |
49 | - return 'string' === typeof msg.value.content ? unbox(msg) : msg | |
50 | - }) | |
59 | + pull( | |
60 | + sbot.links({ | |
61 | + rel: 'root', dest: id, keys: true, old: false | |
62 | + }), | |
63 | + pull.drain(function (msg) { | |
64 | + console.log('new message in thread', msg) | |
65 | + //redraw thread | |
66 | + loadThread() | |
67 | + }, function () {} ) | |
68 | + ) | |
51 | 69 | |
52 | - if(err) return div.appendChild(h('pre', err.stack)) | |
53 | - sort(thread).map(render).forEach(function (el) { | |
54 | - div.appendChild(el) | |
70 | + | |
71 | + function loadThread () { | |
72 | + console.log("LOAD THREAD", id) | |
73 | + getThread(id, sbot, function (err, thread) { | |
74 | + //would probably be better keep an id for each message element | |
75 | + //(i.e. message key) and then update it if necessary. | |
76 | + //also, it may have moved (say, if you received a missing message) | |
77 | + content.innerHTML = '' | |
78 | + thread = thread.map(function (msg) { | |
79 | + return 'string' === typeof msg.value.content ? unbox(msg) : msg | |
80 | + }) | |
81 | + | |
82 | + if(err) return content.appendChild(h('pre', err.stack)) | |
83 | + sort(thread).map(render).forEach(function (el) { | |
84 | + content.appendChild(el) | |
85 | + }) | |
86 | + | |
87 | + var branches = sort.heads(thread) | |
88 | + meta.branch = branches.length > 1 ? branches : branches[0] | |
89 | + | |
90 | + var recps = thread[0].value.content.recps | |
91 | + if(recps && thread[0].value.private) | |
92 | + meta.recps = recps | |
55 | 93 | }) |
56 | 94 | |
57 | - var branches = sort.heads(thread) | |
58 | - var meta = { | |
59 | - type: 'post', | |
60 | - root: id, | |
61 | - branch: branches.length > 1 ? branches : branches[0] | |
62 | - } | |
63 | - var recps = thread[0].value.content.recps | |
64 | - if(recps && thread[0].value.private) | |
65 | - meta.recps = recps | |
95 | + } | |
66 | 96 | |
67 | - div.appendChild( | |
68 | - h('div', u.firstPlug(exports.message_compose, meta, sbot)) | |
69 | - ) | |
70 | - }) | |
97 | + loadThread() | |
71 | 98 | |
72 | 99 | return div |
73 | 100 | } |
74 | 101 | |
@@ -78,4 +105,20 @@ | ||
78 | 105 | exports.message_compose = [] |
79 | 106 | exports.message_unbox = [] |
80 | 107 | |
81 | 108 | |
109 | + | |
110 | + | |
111 | + | |
112 | + | |
113 | + | |
114 | + | |
115 | + | |
116 | + | |
117 | + | |
118 | + | |
119 | + | |
120 | + | |
121 | + | |
122 | + | |
123 | + | |
124 | + |
Built with git-ssb-web