Commit dca7b4f2e6012767054e42e07ff8ea31ea0b501c
merge conflict
Matt McKegg committed on 6/16/2017, 4:00:36 AMParent: 28657bea14b4c70a34c6bdb7528cbf991e4b20a0
Files changed
about/obs.js | changed |
about/obs.js | ||
---|---|---|
@@ -1,10 +1,10 @@ | ||
1 | -var {Value, computed, onceTrue} = require('mutant') | |
2 | -var defer = require('pull-defer') | |
1 … | +var {Value, computed} = require('mutant') | |
3 | 2 … | var pull = require('pull-stream') |
4 | 3 … | var nest = require('depnest') |
5 | 4 … | var ref = require('ssb-ref') |
6 | 5 … | var colorHash = new (require('color-hash'))() |
6 … | +var fallbackImageUrl = 'data:image/gif;base64,R0lGODlhAQABAPAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==' | |
7 | 7 … | |
8 | 8 … | exports.needs = nest({ |
9 | 9 … | 'sbot.pull.stream': 'first', |
10 | 10 … | 'blob.sync.url': 'first', |
@@ -18,99 +18,88 @@ | ||
18 | 18 … | 'image', |
19 | 19 … | 'imageUrl', |
20 | 20 … | 'names', |
21 | 21 … | 'images', |
22 | - 'color' | |
22 … | + 'color', | |
23 … | + 'value', | |
24 … | + 'values' | |
23 | 25 … | ] |
24 | 26 … | }) |
25 | 27 … | |
26 | 28 … | exports.create = function (api) { |
27 | 29 … | var sync = Value(false) |
28 | - var cache = {} | |
29 | - var cacheLoading = false | |
30 … | + var cache = null | |
30 | 31 … | |
31 | 32 … | return nest({ |
32 | 33 … | 'about.obs': { |
33 | - name: (id) => get(id).name, | |
34 | - description: (id) => get(id).description, | |
35 | - image: (id) => get(id).image, | |
36 | - imageUrl: (id) => get(id).imageUrl, | |
37 | - names: (id) => get(id).names, | |
38 | - images: (id) => get(id).images, | |
39 | - color: (id) => computed(id, (id) => colorHash.hex(id)) | |
34 … | + // quick helpers, probably should deprecate! | |
35 … | + name: (id) => value(id, 'name', id.slice(1, 10)), | |
36 … | + description: (id) => value(id, 'description'), | |
37 … | + image: (id) => value(id, 'image'), | |
38 … | + names: (id) => values(id, 'name'), | |
39 … | + images: (id) => values(id, 'images'), | |
40 … | + color: (id) => computed(id, (id) => colorHash.hex(id)), | |
41 … | + imageUrl: (id) => computed(value(id, 'image'), (blobId) => { | |
42 … | + return blobId ? api.blob.sync.url(blobId) : fallbackImageUrl | |
43 … | + }), | |
44 … | + | |
45 … | + // custom abouts (the future!) | |
46 … | + value, | |
47 … | + values | |
40 | 48 … | } |
41 | 49 … | }) |
42 | 50 … | |
51 … | + function value (id, key, defaultValue) { | |
52 … | + if (!ref.isLink(id)) throw new Error('About requires an ssb ref!') | |
53 … | + var yourId = api.keys.sync.id() | |
54 … | + return computed([get(id), key, id, yourId, defaultValue], socialValue) | |
55 … | + } | |
56 … | + | |
57 … | + function values (id, key) { | |
58 … | + if (!ref.isLink(id)) throw new Error('About requires an ssb ref!') | |
59 … | + return computed([get(id), 'name'], allValues) | |
60 … | + } | |
61 … | + | |
43 | 62 … | function get (id) { |
44 | - if (!ref.isFeed(id)) throw new Error('About requires an id!') | |
45 | - if (!cacheLoading) { | |
46 | - cacheLoading = true | |
47 | - loadCache() | |
48 | - } | |
63 … | + if (!ref.isLink(id)) throw new Error('About requires an ssb ref!') | |
64 … | + load() | |
49 | 65 … | if (!cache[id]) { |
50 | - cache[id] = About(api, id) | |
66 … | + cache[id] = Value({}) | |
51 | 67 … | } |
52 | 68 … | return cache[id] |
53 | 69 … | } |
54 | 70 … | |
55 | - function loadCache () { | |
56 | - pull( | |
57 | - api.sbot.pull.stream(sbot => sbot.about.stream({live: true})), | |
58 | - pull.drain(item => { | |
59 | - for (var target in item) { | |
60 | - if (ref.isFeed(target)) { | |
61 | - get(target).push(item[target]) | |
71 … | + function load () { | |
72 … | + if (!cache) { | |
73 … | + cache = {} | |
74 … | + pull( | |
75 … | + api.sbot.pull.stream(sbot => sbot.about.stream({live: true})), | |
76 … | + pull.drain(item => { | |
77 … | + for (var target in item) { | |
78 … | + var state = get(target) | |
79 … | + var lastState = state() | |
80 … | + var values = item[target] | |
81 … | + var changed = false | |
82 … | + for (var key in values) { | |
83 … | + var valuesForKey = lastState[key] = lastState[key] || {} | |
84 … | + for (var author in values[key]) { | |
85 … | + var value = values[key][author] | |
86 … | + if (!valuesForKey[author] || value[1] > valuesForKey[author][1]) { | |
87 … | + valuesForKey[author] = value | |
88 … | + changed = true | |
89 … | + } | |
90 … | + } | |
91 … | + } | |
92 … | + if (changed) { | |
93 … | + state.set(lastState) | |
94 … | + } | |
62 | 95 … | } |
63 | - } | |
64 | 96 … | |
65 | - if (!sync()) { | |
66 | - sync.set(true) | |
67 | - } | |
68 | - }) | |
69 | - ) | |
70 | - } | |
71 | -} | |
72 | - | |
73 | -function About (api, id) { | |
74 | - // transparent image | |
75 | - var fallbackImageUrl = 'data:image/gif;base64,R0lGODlhAQABAPAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==' | |
76 | - | |
77 | - var state = Value({}) | |
78 | - var yourId = api.keys.sync.id() | |
79 | - var image = computed([state, 'image', id, yourId], socialValue) | |
80 | - var name = computed([state, 'name', id, yourId, id.slice(1, 10)], socialValue) | |
81 | - var description = computed([state, 'description', id, yourId], socialValue) | |
82 | - | |
83 | - return { | |
84 | - name, | |
85 | - image, | |
86 | - description, | |
87 | - names: computed([state, 'name', id, yourId, id.slice(1, 10)], allValues), | |
88 | - images: computed([state, 'image', id, yourId], allValues), | |
89 | - descriptions: computed([state, 'description', id, yourId], allValues), | |
90 | - imageUrl: computed(image, (blobId) => { | |
91 | - if (blobId) { | |
92 | - return api.blob.sync.url(blobId) | |
93 | - } else { | |
94 | - return fallbackImageUrl | |
95 | - } | |
96 | - }), | |
97 | - push: function (values) { | |
98 | - var lastState = state() | |
99 | - var changed = false | |
100 | - for (var key in values) { | |
101 | - var valuesForKey = lastState[key] = lastState[key] || {} | |
102 | - for (var author in values[key]) { | |
103 | - var value = values[key][author] | |
104 | - if (!valuesForKey[author] || value[1] > valuesForKey[author][1]) { | |
105 | - valuesForKey[author] = value | |
106 | - changed = true | |
97 … | + if (!sync()) { | |
98 … | + sync.set(true) | |
107 | 99 … | } |
108 | - } | |
109 | - } | |
110 | - if (changed) { | |
111 | - state.set(lastState) | |
112 | - } | |
100 … | + }) | |
101 … | + ) | |
113 | 102 … | } |
114 | 103 … | } |
115 | 104 … | } |
116 | 105 … | |
@@ -122,9 +111,9 @@ | ||
122 | 111 … | return fallback || null |
123 | 112 … | } |
124 | 113 … | } |
125 | 114 … | |
126 | -function allValues (lookup, key, id, yourId) { | |
115 … | +function allValues (lookup, key) { | |
127 | 116 … | var values = {} |
128 | 117 … | for (var author in lookup[key]) { |
129 | 118 … | var value = getValue(lookup[key][author]) |
130 | 119 … | if (value != null) { |
@@ -160,5 +149,4 @@ | ||
160 | 149 … | return item[0].link |
161 | 150 … | } |
162 | 151 … | } |
163 | 152 … | } |
164 | - |
Built with git-ssb-web