git ssb

7+

dinoworm 🐛 / patchcore



Commit a3b73a9d84561cf83c39c5ed661afb0241c47bfc

Merge branch 'strip_manifest'

Matt McKegg committed on 6/16/2017, 4:00:53 AM
Parent: 7f573664b3bbed9e297107fa1e8d8b09d9c01e90
Parent: dca7b4f2e6012767054e42e07ff8ea31ea0b501c

Files changed

about/obs.jschanged
about/obs.jsView
@@ -1,10 +1,10 @@
1-var {Value, computed, onceTrue} = require('mutant')
2-var defer = require('pull-defer')
1 +var {Value, computed} = require('mutant')
32 var pull = require('pull-stream')
43 var nest = require('depnest')
54 var ref = require('ssb-ref')
65 var colorHash = new (require('color-hash'))()
6 +var fallbackImageUrl = 'data:image/gif;base64,R0lGODlhAQABAPAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=='
77
88 exports.needs = nest({
99 'sbot.pull.stream': 'first',
1010 'blob.sync.url': 'first',
@@ -18,99 +18,88 @@
1818 'image',
1919 'imageUrl',
2020 'names',
2121 'images',
22- 'color'
22 + 'color',
23 + 'value',
24 + 'values'
2325 ]
2426 })
2527
2628 exports.create = function (api) {
2729 var sync = Value(false)
28- var cache = {}
29- var cacheLoading = false
30 + var cache = null
3031
3132 return nest({
3233 '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
4048 }
4149 })
4250
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 +
4362 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()
4965 if (!cache[id]) {
50- cache[id] = About(api, id)
66 + cache[id] = Value({})
5167 }
5268 return cache[id]
5369 }
5470
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 + }
6295 }
63- }
6496
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)
10799 }
108- }
109- }
110- if (changed) {
111- state.set(lastState)
112- }
100 + })
101 + )
113102 }
114103 }
115104 }
116105
@@ -122,9 +111,9 @@
122111 return fallback || null
123112 }
124113 }
125114
126-function allValues (lookup, key, id, yourId) {
115 +function allValues (lookup, key) {
127116 var values = {}
128117 for (var author in lookup[key]) {
129118 var value = getValue(lookup[key][author])
130119 if (value != null) {
@@ -160,5 +149,4 @@
160149 return item[0].link
161150 }
162151 }
163152 }
164-

Built with git-ssb-web