Skip to content

Commit

Permalink
Merge pull request #427 from ssbc/compat-hooks
Browse files Browse the repository at this point in the history
Add hooks to compat functions
  • Loading branch information
Powersource authored Nov 21, 2023
2 parents 9d3dcd9 + c344520 commit b22f4de
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 20 deletions.
16 changes: 9 additions & 7 deletions compat/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// SPDX-License-Identifier: LGPL-3.0-only

const pull = require('pull-stream')
const Hookable = require('hoox')

// exports.name is blank to merge into global namespace

Expand All @@ -13,21 +14,22 @@ exports.manifest = {
}

exports.init = function (sbot, config) {
sbot.add = sbot.db.add
sbot.get = function get(idOrObject, cb) {
sbot.add = Hookable(sbot.db.add)
sbot.get = Hookable(function get(idOrObject, cb) {
if (typeof idOrObject === 'object' && idOrObject.meta) {
sbot.db.getMsg(idOrObject.id, cb)
} else if (typeof idOrObject === 'object') {
sbot.db.get(idOrObject.id, cb)
} else {
sbot.db.get(idOrObject, cb)
}
}
})
// already hooked in the publish compat plugin (if loaded)
sbot.publish = sbot.db.publish
sbot.whoami = () => ({ id: sbot.id })
sbot.ready = () => true
sbot.whoami = Hookable(() => ({ id: sbot.id }))
sbot.ready = Hookable(() => true)
sbot.keys = config.keys
sbot.createWriteStream = function createWriteStream(cb) {
sbot.createWriteStream = Hookable(function createWriteStream(cb) {
return pull(
pull.asyncMap(sbot.db.add),
pull.drain(
Expand All @@ -36,5 +38,5 @@ exports.init = function (sbot, config) {
cb || ((err) => console.error(new Error('ssb-db2 createWriteStream failed to add messages'), {cause: err}))
)
)
}
})
}
15 changes: 8 additions & 7 deletions compat/ebt.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,27 @@
// SPDX-License-Identifier: LGPL-3.0-only

const pull = require('pull-stream')
const Hookable = require('hoox')
const EBTIndex = require('../indexes/ebt')
const { onceWhen } = require('../utils')

exports.init = function (sbot, config) {
sbot.db.registerIndex(EBTIndex)
if (!sbot.post) sbot.post = sbot.db.post
sbot.getAtSequence = (key, cb) => {
sbot.getAtSequence = Hookable((key, cb) => {
sbot.db.onDrain('ebt', () => {
sbot.db.getIndex('ebt').getMessageFromAuthorSequence(key, cb)
})
}
sbot.getAtSequenceNativeMsg = (key, feedFormat, cb) => {
})
sbot.getAtSequenceNativeMsg = Hookable((key, feedFormat, cb) => {
sbot.db.onDrain('ebt', () => {
sbot.db
.getIndex('ebt')
.getNativeMsgFromAuthorSequence(key, feedFormat, cb)
})
}
sbot.add = sbot.db.add
sbot.getVectorClock = function getVectorClock(cb) {
})
sbot.add = Hookable(sbot.db.add)
sbot.getVectorClock = Hookable(function getVectorClock(cb) {
onceWhen(
sbot.db2migrate && sbot.db2migrate.synchronized,
(isSynced) => isSynced === true,
Expand All @@ -45,5 +46,5 @@ exports.init = function (sbot, config) {
})
}
)
}
})
}
5 changes: 3 additions & 2 deletions compat/feedstate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
//
// SPDX-License-Identifier: LGPL-3.0-only

const Hookable = require('hoox')
const { onceWhen } = require('../utils')

exports.init = function (sbot, config) {
sbot.getFeedState = function (feedId, cb) {
sbot.getFeedState = Hookable(function (feedId, cb) {
onceWhen(
sbot.db.stateFeedsReady,
(ready) => ready === true,
Expand All @@ -21,5 +22,5 @@ exports.init = function (sbot, config) {
})
}
)
}
})
}
5 changes: 3 additions & 2 deletions compat/log-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

const pull = require('pull-stream')
const cat = require('pull-cat')
const Hookable = require('hoox')
const { descending, live, toPullStream } = require('../operators')

// exports.name is blank to merge into global namespace
Expand All @@ -13,7 +14,7 @@ exports.manifest = {
}

exports.init = function (sbot) {
sbot.createLogStream = function createLogStream(opts) {
sbot.createLogStream = Hookable(function createLogStream(opts) {
// Apply default values
opts = opts || {}
const optsKeys = opts.keys === false ? false : true
Expand Down Expand Up @@ -48,5 +49,5 @@ exports.init = function (sbot) {
if (optsOld && !optsSync) return applyLimit(cat([old$, live$]))
if (!optsOld && optsSync) return applyLimit(cat([sync$, live$]))
if (!optsOld && !optsSync) return applyLimit(live$)
}
})
}
6 changes: 4 additions & 2 deletions compat/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
//
// SPDX-License-Identifier: LGPL-3.0-only

const Hookable = require('hoox')

exports.init = function (sbot, config) {
function publish(content, cb) {
publishAs(config.keys, content, cb)
Expand All @@ -19,6 +21,6 @@ exports.init = function (sbot, config) {
)
}

sbot.db.publish = publish
sbot.db.publishAs = publishAs
sbot.db.publish = Hookable(publish)
sbot.db.publishAs = Hookable(publishAs)
}

0 comments on commit b22f4de

Please sign in to comment.