-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathssb-singleton.js
205 lines (171 loc) · 5.43 KB
/
ssb-singleton.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
module.exports.setup = function (dir, config, extraModules) {
const { WindowController } = require("./window-controller.js")
const path = require('path')
window.windowController = new WindowController()
window.windowList = (window.opener && window.opener.windowList ? window.opener.windowList : [ window ])
module.exports.initSSB = function() {
// Before we start up ssb-browser-core, let's check to see if we
// do not yet have an id, since this would mean that we need to
// display the onboarding screen.
const ssbKeys = require('ssb-keys')
window.firstTimeLoading = false
try {
ssbKeys.loadSync(path.join(dir, 'secret'))
} catch(err) {
window.firstTimeLoading = true
}
if (window.updateFirstTimeLoading)
window.updateFirstTimeLoading()
require('./core').init(dir, config, extraModules)
// Using a different name so that anything trying to use the
// non-singleton global will fail so we can find them.
window.singletonSSB = {
uniqueID: (new Date()).getTime()
}
SSBLOADER.on('ready', () => {
window.singletonSSB.SSB = SSB
})
}
}
var onErrorCallbacks = []
var onSuccessCallbacks = []
var ssbChangedCallbacks = []
var lastSSB = null
function runOnChangeIfNeeded(SSB) {
if (lastSSB != SSB.uniqueID) {
lastSSB = SSB.uniqueID
for (f in ssbChangedCallbacks)
ssbChangedCallbacks[f]()
}
}
function runOnError(err) {
for (f in onErrorCallbacks)
onErrorCallbacks[f](err)
}
function runOnSuccess() {
for (f in onSuccessCallbacks)
onSuccessCallbacks[f]()
}
// Allows for registering callbacks which run any time the active SSB
// is switched, including if we initialize or we have to register with
// a new SSB in another window.
module.exports.onChangeSSB = function(cb) {
ssbChangedCallbacks.push(cb)
}
module.exports.onError = function(cb) {
onErrorCallbacks.push(cb)
}
module.exports.onSuccess = function(cb) {
onSuccessCallbacks.push(cb)
}
module.exports.getSSB = function() {
const r = module.exports.getSSBInternal()
if (r[1])
return [r[0], r[1].SSB]
else
return r
}
module.exports.getSSBInternal = function() {
if (window.singletonSSB) {
if (windowController.isMaster) {
runOnChangeIfNeeded(window.singletonSSB)
runOnSuccess()
return [ null, window.singletonSSB ]
} else {
// We have an initialized SSB but lost our WindowController
// status, which means we probably froze up for long enough that
// another window gave up on listening for our heatbeat pings.
//
// We need to get rid of our SSB object as soon as possible and
// then fall back to trying to get it from another window.
delete window.singletonSSB
}
}
var err = "Acquiring database lock - Only one instance of ssb-browser is allowed to run at a time."
if (windowController.isMaster) {
// We've been elected as the SSB holder window but have no SSB
// yet. Initialize an SSB object.
module.exports.initSSB()
runOnChangeIfNeeded(window.singletonSSB)
runOnSuccess()
return [null, window.singletonSSB]
} else {
// We're not supposed to be running an SSB. But there might be
// another window with one.
for (w in window.windowList) {
var otherWindow = window.windowList[w]
if (otherWindow != window && otherWindow.windowController && otherWindow.getSSBSingleton) {
if (window.windowController.others && window.windowController.others[otherWindow.windowController.id]) {
// They're still responding to pings.
let [ err, otherSingleton ] = otherWindow.getSSBSingleton().getSSBInternal()
if (otherSingleton) {
runOnChangeIfNeeded(otherSingleton)
runOnSuccess()
return [null, otherSingleton]
}
}
}
}
}
runOnError(err)
return [ err, null ]
}
var ssbEventuallyCB = []
function checkSSBEventually()
{
let [ err, maybeSSB ] = module.exports.getSSB()
for (let i = 0; i < ssbEventuallyCB.length; ++i)
{
const check = ssbEventuallyCB[i]
if (check.isRelevant && !check.isRelevant())
ssbEventuallyCB.splice(i, 1)
let isOk = false
if (!err)
try { isOk = check.ssbCheck(maybeSSB) } catch (e) {}
if (isOk) {
try { check.cb(err, maybeSSB) } catch (e) {}
ssbEventuallyCB.splice(i, 1)
} else if (check.retries > 0) {
check.retries -= 1
} else {
try { check.cb("Could not lock database", null) } catch (e) {}
ssbEventuallyCB.splice(i, 1)
}
}
if (ssbEventuallyCB.length > 0)
setTimeout(checkSSBEventually, 250)
}
module.exports.getSSBEventually = function(timeout, isRelevant, ssbCheck, cb) {
let [ err, maybeSSB ] = this.getSSB()
const isOk = ssbCheck(maybeSSB)
if (!isOk) {
if (timeout != 0) {
ssbEventuallyCB.push({
retries: timeout === -1 ? 10000 : timeout / 250,
isRelevant,
ssbCheck,
cb
})
if (ssbEventuallyCB.length === 1)
setTimeout(checkSSBEventually, 250)
return
}
}
cb(err, isOk ? maybeSSB : null)
}
module.exports.getSimpleSSBEventually = function(isRelevant, cb) {
if (!cb) {
cb = isRelevant
isRelevant = () => { return true }
}
module.exports.getSSBEventually(
-1,
isRelevant,
(SSB) => { return SSB && SSB.db },
cb
)
}
module.exports.openWindow = function(href) {
window.windowList.push(window.open(href))
}
window.getSSBSingleton = function() { return module.exports }