Closed vweevers closed 9 months ago
A simple solution is to change the hook signature from:
db.hooks.newsub.add(function (sub, opts) {})
To:
db.hooks.newsub.add(function (sub, name, opts) {})
The plugin can then do:
db.hooks.newsub.add(function (sub, name, opts) {
if (opts.separator !== '!') {
throw new Error('I do not support custom separators, sorry')
}
// ..
})
Done in https://github.com/Level/abstract-level/pull/78 (didn't auto-close because it's not on the default branch).
Context
I'm working on a rewrite of
level-ttl
, to make it use hooks and find out what gaps we in that API. Say a plugin like that wants to add methods to a db and also to (deeply) nested sublevels that the user might create. For optimal performance, the plugin wishes to bypass intermediate sublevels on its own operations. E.g.:Usage:
Problem
There's no suitable value for
child.xxx
atm. We don't expose the name(s) of a sublevel (e.g.'templates'
and'html'
) and even if we did, the sublevel might be using custom separators. We do expose the prefix of a sublevel (e.g.!templates!!html!
). But passing this along to.sublevel()
would only work one level deep, because theAbstractSublevel
constructor trims separators from e.g..sublevel('!templates!)
resulting in a valid name, but.sublevel('!templates!!html!')
results in a name that's'templates!!html'
. Which is then rejected by:https://github.com/Level/abstract-level/blob/69c5ce7350e6d77eaf07158e23d77f4b84459e46/lib/abstract-sublevel.js#L47-L50