-
Notifications
You must be signed in to change notification settings - Fork 194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[discussion] Overridable public API. #160
Comments
@trusktr @ChiperSoft I'm just wondering – have you seen such plugin architectures in the wild? It sounds awesomely simple, but I'm having second thoughts if it scales well. I mean – as I see it, it would be unsafe to use two of such "plugins" at once. |
@tomekwi Good question! Can you think of a scenario where it might not scale? |
var dox0 = require("dox");
var dox1 = require("dox-css")(dox0);
var dox = require("dox-markdown")(dox1); Suppose |
BTW, I've picked up this |
Having an explicit api for extension isn't nessisary. Simply changing dox to invoke functions via var dox = Object.assign(
require('dox'),
require('dox-css')
require('dox-markdown')
); |
Good idea! As a matter of fact, also with // dox-css/index.js
export default (originalDox) => {
return Object.assign({}
, originalDox
// We want to extend `dox.parseComments`
, { parseComments (...args) {
// First we apply the original `dox` function
let result = originalDox.parseComments(...args);
// Here we can process the result
return processedResult;
}
}
);
}; This way we leave It would also solve the problem of scalability to some extent. As long as plugin authors extend functions this way, we can safely do: let dox = require("dox-markdown")(
require("dox-css")(
require("dox")
)
) – in this case |
It would probably be better if extensions were added via module.exports = exports = function (dox) {
dox = Object.create(dox);
return Object.assign(dox, exports);
}
exports.parseComments = function () {
var result = Object.getPrototypeOf(this).parseComments.apply(this, arguments);
} This way you're able to expose the new functions in case other extensions want to use them. |
Good point – that'd be lighter as well as the functions wouldn't need to be copied over and created. Only the prototype chain might get quite deep – so you never really know if you're taking the real // dox-css/index.js
module.exports = exports = function (dox) {
dox = Object.create(dox); // This is indeed dox
return Object.assign(dox, exports);
}
exports.parseComments = function () {
var result = Object.getPrototypeOf(this).parseComments.apply(this, arguments);
// dox.parseComments gets called.
}
// dox-markdown/index.js
module.exports = exports = function (dox) {
dox = Object.create(dox); // This is dox-css
return Object.assign(dox, exports);
}
exports.parseComments = function () {
var result = Object.getPrototypeOf(this).parseComments.apply(this, arguments);
// doxCSS.parseComments gets called.
} I don't think that's bad however. After all, every plugin will likely change one aspect of a function. No chance someone will use |
👍 |
Based from discussions in #152 and #159, making the public API an adapter to a private API might be nice so that people can create "plugins" for the community by simply overriding some or all of dox's public methods.
To restore dox's functionality (i.e. to be able to use dox with multiple types of code in the same running program) I was thinking there could be a simple function that restores all the public methods to their original state simply by re-adapting to the internal API (not by reference, but by closure, for privacy).
The text was updated successfully, but these errors were encountered: