This repository has been archived by the owner on May 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
net.js
172 lines (140 loc) · 4.35 KB
/
net.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// this file is loaded from ui/browser.js when SSB is ready
const pull = require('pull-stream')
// this uses https://github.com/arj03/ssb-partial-replication
SSB.syncFeedAfterFollow = function(feedId) {
SSB.syncFeedFromSequence(feedId, 0)
}
SSB.syncFeedFromSequence = function(feedId, sequence, cb) {
let rpc = SSB.getPeer()
var seqStart = sequence - 100
if (seqStart < 0)
seqStart = 0
console.log(`seq ${seqStart} feedId: ${feedId}`)
console.time("downloading messages")
pull(
rpc.partialReplication.getFeed({ id: feedId, seq: seqStart, keys: false }),
pull.asyncMap(SSB.db.addOOO),
pull.collect((err, msgs) => {
if (err) throw err
console.timeEnd("downloading messages")
console.log(msgs.length)
if (cb) cb()
})
)
}
SSB.syncFeedFromLatest = function(feedId, cb) {
let rpc = SSB.getPeer()
console.time("downloading messages")
pull(
rpc.partialReplication.getFeedReverse({ id: feedId, keys: false, limit: 25 }),
pull.asyncMap(SSB.db.addOOO),
pull.collect((err, msgs) => {
if (err) throw err
console.timeEnd("downloading messages")
if (cb) cb()
})
)
}
syncThread = function(messages, cb) {
pull(
pull.values(messages),
pull.filter((msg) => msg && msg.content.type == "post"),
pull.asyncMap(SSB.db.addOOO),
pull.collect(cb)
)
}
// this uses https://github.com/arj03/ssb-partial-replication
SSB.getThread = function(msgId, cb) {
SSB.connectedWithData(() => {
let rpc = SSB.getPeer()
rpc.partialReplication.getTangle(msgId, (err, messages) => {
if (err) return cb(err)
syncThread(messages, cb)
})
})
}
SSB.activeConnections = 0
SSB.activeConnectionsWithData = 0
SSB.callbacksWaitingForConnection = []
SSB.callbacksWaitingForConnectionWithData = []
SSB.callbacksWaitingForDisconnect = []
function runConnectedCallbacks() {
while(SSB.callbacksWaitingForConnection.length > 0) {
const cb = SSB.callbacksWaitingForConnection.shift()
cb(SSB)
}
}
function runConnectedWithDataCallbacks() {
while(SSB.callbacksWaitingForConnectionWithData.length > 0) {
const cb = SSB.callbacksWaitingForConnectionWithData.shift()
cb(SSB)
}
}
function runDisconnectedCallbacks() {
while(SSB.callbacksWaitingForDisconnect.length > 0) {
const cb = SSB.callbacksWaitingForDisconnect.shift()
cb(SSB)
}
}
SSB.isConnected = function() {
return (SSB.activeConnections > 0)
}
SSB.isConnectedWithData = function() {
return (SSB.activeConnectionsWithData > 0)
}
SSB.connected = function(cb) {
// Add the callback to the list.
SSB.callbacksWaitingForConnection.push(cb);
if(SSB.isConnected()) {
// Already connected. Run all the callbacks.
runConnectedCallbacks()
}
}
SSB.connectedWithData = function(cb) {
// Register a callback for when we're connected to a peer with data (not a room).
SSB.callbacksWaitingForConnectionWithData.push(cb);
if(SSB.isConnectedWithData()) {
// Already connected. Run all the callbacks.
runConnectedWithDataCallbacks()
}
}
SSB.disconnected = function(cb) {
// Register a callback for when we're no longer connected to any peer.
SSB.callbacksWaitingForDisconnect.push(cb);
if(!SSB.isConnected()) {
// Already connected. Run all the callbacks.
runDisconnectedCallbacks()
}
}
// Register for the connect event so we can keep track of it.
SSB.net.on('rpc:connect', (rpc) => {
// Now we're connected. Run all the callbacks.
++SSB.activeConnections
runConnectedCallbacks()
// See if we're operating on a connection with actual data (not a room).
let connPeers = Array.from(SSB.net.conn.hub().entries())
connPeers = connPeers.filter(([,x])=>!!x.key).map(([address,data])=>({
address,
data
}))
var peer = connPeers.find(x=>x.data.key == rpc.id)
if (peer && peer.data.type != 'room') {
// It's not a room.
++SSB.activeConnectionsWithData
runConnectedWithDataCallbacks()
// Register another callback to decrement our "connections with data" reference count.
rpc.on('closed', () => --SSB.activeConnectionsWithData)
}
// Register an event handler for disconnects so we know to trigger waiting again.
rpc.on('closed', () => {
--SSB.activeConnections
if (SSB.activeConnections == 0) {
runDisconnectedCallbacks()
}
})
})
SSB.getOOO = function(msgId, cb) {
SSB.connectedWithData((rpc) => {
SSB.net.ooo.get(msgId, cb)
})
}