Skip to content

Commit

Permalink
Fix singleton not working on a empty db, we need to wait for wasm init
Browse files Browse the repository at this point in the history
  • Loading branch information
arj03 committed Nov 12, 2021
1 parent 65721aa commit a3a34f5
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 20 deletions.
11 changes: 7 additions & 4 deletions core.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
exports.init = function (dir, config, extraModules) {
// init secret stack
SSB = require('./net').init(dir, config, extraModules)
console.log("my id: ", SSB.id)
const EventEmitter = require('events')
SSBLOADER = new EventEmitter()

// init secret stack
const s = require('sodium-browserify')
s.events.on('sodium-browserify:wasm loaded', () => {
console.log("wasm loaded")

SSB = require('./net').init(dir, config, extraModules)
console.log("my id: ", SSB.id)

const helpers = require('./core-helpers')

SSB.helpers = {
Expand All @@ -30,6 +33,6 @@ exports.init = function (dir, config, extraModules) {
}, 2500)
}

SSB.emit("SSB: loaded")
SSBLOADER.emit("ready")
})
}
2 changes: 1 addition & 1 deletion scripts/full-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fs.copyFileSync("scripts/secret", dir + "/secret")

require('../core.js').init(dir)

SSB.on('SSB: loaded', function() {
SSBLOADER.on('ready', function() {

process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0 // wtf
// var remoteAddress = 'wss://between-two-worlds.dk:8990~noauth:lbocEWqF2Fg6WMYLgmfYvqJlMfL7hiqVAV6ANjHWNw8='
Expand Down
60 changes: 45 additions & 15 deletions ssb-singleton.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ module.exports.setup = function (dir, config, extraModules) {
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.
// 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 {
Expand All @@ -20,8 +22,16 @@ module.exports.setup = function (dir, config, extraModules) {
window.updateFirstTimeLoading()

require('./core').init(dir, config, extraModules)
SSB.uniqueID = (new Date()).getTime()
window.singletonSSB = SSB // Using a different name so that anything trying to use the non-singleton global will fail so we can find them.

// 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
})
}
}

Expand All @@ -48,7 +58,9 @@ function runOnSuccess() {
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.
// 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)
}
Expand All @@ -62,38 +74,51 @@ module.exports.onSuccess = function(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) {
// We're already holding an SSB object, so we can return it right away.
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.
// 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.
// 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 ]
return [null, window.singletonSSB]
} else {
// We're not supposed to be running an SSB. But there might be another window with one.
// 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, otherSSB ] = otherWindow.getSSBSingleton().getSSB()
if (otherSSB) {
runOnChangeIfNeeded(otherSSB)
let [ err, otherSingleton ] = otherWindow.getSSBSingleton().getSSBInternal()
if (otherSingleton) {
runOnChangeIfNeeded(otherSingleton)
runOnSuccess()
return [ null, otherSSB ]
return [null, otherSingleton]
}
}
}
Expand Down Expand Up @@ -165,7 +190,12 @@ module.exports.getSimpleSSBEventually = function(isRelevant, cb) {
isRelevant = () => { return true }
}

module.exports.getSSBEventually(-1, isRelevant, (SSB) => { return SSB && SSB.db }, cb)
module.exports.getSSBEventually(
-1,
isRelevant,
(SSB) => { return SSB && SSB.db },
cb
)
}

module.exports.openWindow = function(href) {
Expand Down

0 comments on commit a3a34f5

Please sign in to comment.