Skip to content

Commit

Permalink
Add hooks to compat functions
Browse files Browse the repository at this point in the history
Made by @cooldracula
  • Loading branch information
Powersource committed Nov 21, 2023
1 parent 9d3dcd9 commit 45c875f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 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}))
)
)
}
})
}
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 45c875f

Please sign in to comment.