git ssb

7+

dinoworm 🐛 / patchcore



Commit 94a864d41161acab3c2937769986c0037321d093

contact.obs: use new reducer pattern from about.obs

Matt McKegg committed on 6/20/2017, 4:47:09 AM
Parent: 043b2e20476159dcff94dd46890be1c353e9132e

Files changed

contact/obs.jschanged
contact/obs.jsView
@@ -1,7 +1,6 @@
11 var nest = require('depnest')
2-var {Value, onceTrue, computed} = require('mutant')
3-var defer = require('pull-defer')
2 +var {Value, computed} = require('mutant')
43 var pull = require('pull-stream')
54 var ref = require('ssb-ref')
65
76 exports.needs = nest({
@@ -19,22 +18,23 @@
1918 var sync = Value(false)
2019
2120 return nest({
2221 'contact.obs': {
23- following: (id) => get(id).following,
24- followers: (id) => get(id).followers
22 + following: (id) => values(get(id), 'following', true),
23 + followers: (id) => values(get(id), 'followers', true)
2524 },
2625 'sbot.hook.publish': function (msg) {
2726 if (isContact(msg)) {
27 + // HACK: make interface more responsive when sbot is busy
2828 var source = msg.value.author
2929 var dest = msg.value.content.contact
3030 if (typeof msg.value.content.following === 'boolean') {
31- get(source).push({
31 + update(source, {
3232 following: {
3333 [dest]: [msg.value.content]
3434 }
3535 })
36- get(dest).push({
36 + update(dest, {
3737 followers: {
3838 [source]: [msg.value.content]
3939 }
4040 })
@@ -42,16 +42,20 @@
4242 }
4343 }
4444 })
4545
46 + function values (state, key, compare) {
47 + var obs = computed([state, key, compare], getIds)
48 + obs.sync = sync
49 + return obs
50 + }
51 +
4652 function loadCache () {
4753 pull(
4854 api.sbot.pull.stream(sbot => sbot.contacts.stream({live: true})),
4955 pull.drain(item => {
5056 for (var target in item) {
51- if (ref.isFeed(target)) {
52- get(target).push(item[target])
53- }
57 + if (ref.isFeed(target)) update(target, item[target])
5458 }
5559
5660 if (!sync()) {
5761 sync.set(true)
@@ -59,52 +63,40 @@
5963 })
6064 )
6165 }
6266
67 + function update (id, values) {
68 + var state = get(id)
69 + var lastState = state()
70 + var changed = false
71 + for (var key in values) {
72 + var valuesForKey = lastState[key] = lastState[key] || {}
73 + for (var dest in values[key]) {
74 + var value = values[key][dest]
75 + if (!valuesForKey[dest] || value[1] > valuesForKey[dest][1] || !values[1] || !valuesForKey[dest[1]]) {
76 + valuesForKey[dest] = value
77 + changed = true
78 + }
79 + }
80 + }
81 + if (changed) {
82 + state.set(lastState)
83 + }
84 + }
85 +
6386 function get (id) {
6487 if (!ref.isFeed(id)) throw new Error('Contact state requires an id!')
6588 if (!cacheLoading) {
6689 cacheLoading = true
6790 loadCache()
6891 }
6992 if (!cache[id]) {
70- cache[id] = Contact(api, id, sync)
93 + cache[id] = Value({})
7194 }
7295 return cache[id]
7396 }
7497 }
7598
76-function Contact (api, id, sync) {
77- var state = Value({})
78- return {
79- following: computedIds(state, 'following', true, sync),
80- followers: computedIds(state, 'followers', true, sync),
81- push: function (values) {
82- var lastState = state()
83- var changed = false
84- for (var key in values) {
85- var valuesForKey = lastState[key] = lastState[key] || {}
86- for (var dest in values[key]) {
87- var value = values[key][dest]
88- if (!valuesForKey[dest] || value[1] > valuesForKey[dest][1] || !values[1] || !valuesForKey[dest[1]]) {
89- valuesForKey[dest] = value
90- changed = true
91- }
92- }
93- }
94- if (changed) {
95- state.set(lastState)
96- }
97- }
98- }
99-}
100-
101-function computedIds (state, key, compare, sync) {
102- var obs = computed([state, key, true], getIds)
103- obs.sync = sync
104- return obs
105-}
106-
10799 function getIds (state, key, compare) {
108100 var result = new Set()
109101 if (state[key]) {
110102 for (var dest in state[key]) {
@@ -112,12 +104,10 @@
112104 result.add(dest)
113105 }
114106 }
115107 }
116-
117108 return result
118109 }
119110
120111 function isContact (msg) {
121112 return msg.value && msg.value.content && msg.value.content.type === 'contact'
122113 }
123-

Built with git-ssb-web