Separate SSB interface from model code

This commit is contained in:
Christian Bundy 2020-01-09 09:04:46 -08:00
parent ccbc877b8d
commit 9297b5f198
3 changed files with 990 additions and 975 deletions

View File

@ -34,6 +34,12 @@ const ssbMentions = require('ssb-mentions')
const ssbRef = require('ssb-ref')
const { themeNames } = require('@fraction/base16-css')
const ssb = require('./ssb')
// Create "cooler"-style interface from SSB connection.
// This handle is passed to the models for their convenience.
const cooler = ssb()
const {
about,
blob,
@ -41,7 +47,7 @@ const {
meta,
post,
vote
} = require('./models')
} = require('./models')(cooler)
const {
authorView,

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,10 @@
'use strict'
// This module exports a function that connects to SSB and returns a "cooler"
// interface. This interface is poorly defined and should be replaced with
// native support for Promises in the MuxRPC module and auto-generated manifest
// files in the SSB-Client module.
const debug = require('debug')('oasis')
const ssbClient = require('ssb-client')
const ssbConfig = require('ssb-config')
@ -48,45 +53,6 @@ const rawConnect = () => new Promise((resolve, reject) => {
})
})
const db = {
connect () {
// This has interesting behavior that may be unexpected.
//
// If `handle` is already an active [non-closed] connection, return that.
//
// If the connection is closed, we need to restart it. It's important to
// note that if we're depending on an external service (like Patchwork) and
// that app is closed, then Oasis will seamlessly start its own SSB service.
return new Promise((resolve, reject) => {
handle.then((ssb) => {
if (ssb.closed) {
createConnection()
}
resolve(handle)
})
})
},
/**
* @param {function} method
*/
get (method, ...opts) {
return new Promise((resolve, reject) => {
method(...opts, (err, val) => {
if (err) {
reject(err)
} else {
resolve(val)
}
})
})
},
read (method, ...args) {
return new Promise((resolve) => {
resolve(method(...args))
})
}
}
debug.enabled = true
let handle
@ -117,6 +83,44 @@ const createConnection = () => {
return handle
}
createConnection()
module.exports = db
module.exports = () => {
createConnection()
return {
connect () {
// This has interesting behavior that may be unexpected.
//
// If `handle` is already an active [non-closed] connection, return that.
//
// If the connection is closed, we need to restart it. It's important to
// note that if we're depending on an external service (like Patchwork) and
// that app is closed, then Oasis will seamlessly start its own SSB service.
return new Promise((resolve, reject) => {
handle.then((ssb) => {
if (ssb.closed) {
createConnection()
}
resolve(handle)
})
})
},
/**
* @param {function} method
*/
get (method, ...opts) {
return new Promise((resolve, reject) => {
method(...opts, (err, val) => {
if (err) {
reject(err)
} else {
resolve(val)
}
})
})
},
read (method, ...args) {
return new Promise((resolve) => {
resolve(method(...args))
})
}
}
}