-
-
Notifications
You must be signed in to change notification settings - Fork 698
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
JavaScript plugin hooks mechanism similar to pluggy #983
Comments
I had a look around and there isn't an obvious pluggy equivalent in JavaScript world at the moment. Lots of frameworks like jQuery and Vue have their own custom plugin mechanisms. https://github.com/rekit/js-plugin is a simple standalone plugin mechanism. Not quite as full-featured as Pluggy though - in particular I like how Pluggy supports multiple plugins returning results for the same hook that get concatenated into a list of results. https://css-tricks.com/designing-a-javascript-plugin-system/ has some ideas. |
If you don't mind a somewhat bonkers idea: how about a JS client-side plugin capability that allows any user looking at a Datasette site to pull in external plugins for data manipulation, even if the Datasette owner hasn't added them? (Yes, this may be much too ambitious. If you're remotely interested, maybe fork this discussion to a different issue.) This is some fascinating reading about what JS sandboxing looks like these days: |
@yozlet just spotted this comment. Wow that is interesting! With the right plugin hooks on the page (see also #987) one relatively simple way to do that could be with bookmarklets - users could install bookmarklets which, when executed against a Datasette page in their browser, use the existing JavaScript plugin integration points to add all kinds of functionality. Doing full sandboxing is certainly daunting, but it looks like Figma figured it out so TIL it's technically feasible. |
I think I'm going to try building a very lightweight clone of the core API design of Pluggy - not the advanced features, just the idea that plugins can register and a call to |
I'm going to introduce a global |
Pluggy does dependency injection by introspecting the named arguments to the Python function, which I really like. That's tricker in JavaScript. It looks like the only way to introspect a function is to look at the Even more challenging: JavaScript developers love minifying their code, and minification can shorten the function parameter names. From https://code-maven.com/dependency-injection-in-angularjs it looks like Angular.js does dependency injection and solves this by letting you optionally provide a separate list of the arguments your function uses: angular.module('DemoApp', [])
.controller('DemoController', ['$scope', '$log', function($scope, $log) {
$scope.message = "Hello World";
$log.debug('logging hello');
}]); I can copy that approach: I'll introspect by default, but provide a documented mechanism for explicitly listing your parameter names so that if you know your plugin code will be minified you can use that instead. |
Potential design: datasette.plugins.register('column_actions', function(database, table, column, actor) {
/* ... *l
}) Or if you want to be explicit to survive minification: datasette.plugins.register('column_actions', function(database, table, column, actor) {
/* ... *l
}, ['database', 'table', 'column', 'actor']) I'm making that list of parameter names an optional third argument to the |
Then to call the plugins: datasette.plugins.call('column_actions', {database: 'database', table: 'table'}) |
The
|
Initial prototype: window.datasette = {};
window.datasette.plugins = (function() {
var registry = {};
function extractParameters(fn) {
var match = /\((.*)\)/.exec(fn.toString());
if (match && match[1].trim()) {
return match[1].split(',').map(s => s.trim());
} else {
return [];
}
}
function register(hook, fn, parameters) {
parameters = parameters || extractParameters(fn);
if (!registry[hook]) {
registry[hook] = [];
}
registry[hook].push([fn, parameters]);
}
function call(hook, args) {
args = args || {};
var implementations = registry[hook] || [];
var results = [];
implementations.forEach(([fn, parameters]) => {
/* Call with the correct arguments */
var callWith = parameters.map(parameter => args[parameter]);
var result = fn.apply(fn, callWith);
if (result) {
results.push(result);
}
});
return results;
}
return {
register: register,
_registry: registry,
call: call
};
})(); Usage example: datasette.plugins.register('numbers', (a, b) => a + b)
datasette.plugins.register('numbers', (a, b) => a * b)
datasette.plugins.call('numbers', {a: 4, b: 6})
/* Returns [10, 24] */ |
This implementation doesn't have an equivalent of "hookspecs" which can identify if a registered plugin implementation matches a known signature. I should add that, it will provide a better developer experience if someone has a typo. |
This could work to define a plugin hook: datasette.plugins.define('numbers', ['a' ,'b']) |
This version adds window.datasette = {};
window.datasette.plugins = (function() {
var registry = {};
var definitions = {};
function extractParameters(fn) {
var match = /\((.*)\)/.exec(fn.toString());
if (match && match[1].trim()) {
return match[1].split(',').map(s => s.trim());
} else {
return [];
}
}
function define(hook, parameters) {
definitions[hook] = parameters || [];
}
function isSubSet(a, b) {
return a.every(parameter => b.includes(parameter))
}
function register(hook, fn, parameters) {
parameters = parameters || extractParameters(fn);
if (!definitions[hook]) {
throw new Error('"' + hook + '" is not a defined plugin hook');
}
if (!definitions[hook]) {
throw new Error('"' + hook + '" is not a defined plugin hook');
}
/* Check parameters is a subset of definitions[hook] */
var validParameters = definitions[hook];
if (!isSubSet(parameters, validParameters)) {
throw new Error('"' + hook + '" valid parameters are ' + JSON.stringify(validParameters));
}
if (!registry[hook]) {
registry[hook] = [];
}
registry[hook].push([fn, parameters]);
}
function call(hook, args) {
args = args || {};
if (!definitions[hook]) {
throw new Error('"' + hook + '" hook has not been defined');
}
if (!isSubSet(Object.keys(args), definitions[hook])) {
throw new Error('"' + hook + '" valid arguments are ' + JSON.stringify(definitions[hook]));
}
var implementations = registry[hook] || [];
var results = [];
implementations.forEach(([fn, parameters]) => {
/* Call with the correct arguments */
var callWith = parameters.map(parameter => args[parameter]);
var result = fn.apply(fn, callWith);
if (result) {
results.push(result);
}
});
return results;
}
return {
define: define,
register: register,
_registry: registry,
call: call
};
})(); Usage: datasette.plugins.define('numbers', ['a', 'b'])
datasette.plugins.register('numbers', (a, b) => a + b)
datasette.plugins.register('numbers', (a, b) => a * b)
datasette.plugins.call('numbers', {a: 4, b: 6}) |
I need to decide how this code is going to be loaded. Putting it in a blocking Running it through https://javascript-minifier.com/ produces this, which is 855 characters - so maybe I could inline that into the header of the page?
|
If I'm going to minify it I'll need to figure out a build step in Datasette itself so that I can easily work on that minified version. |
Using raw string exceptions, |
This version minifies to 702 characters: window.datasette = window.datasette || {};
window.datasette.plugins = (() => {
var registry = {};
var definitions = {};
var stringify = JSON.stringify;
function extractParameters(fn) {
var match = /\((.*)\)/.exec(fn.toString());
if (match && match[1].trim()) {
return match[1].split(',').map(s => s.trim());
} else {
return [];
}
}
function isSubSet(a, b) {
return a.every(parameter => b.includes(parameter))
}
return {
_registry: registry,
define: (hook, parameters) => {
definitions[hook] = parameters || [];
},
register: (hook, fn, parameters) => {
parameters = parameters || extractParameters(fn);
if (!definitions[hook]) {
throw '"' + hook + '" is not a defined hook';
}
/* Check parameters is a subset of definitions[hook] */
var validParameters = definitions[hook];
if (!isSubSet(parameters, validParameters)) {
throw '"' + hook + '" valid args are ' + stringify(validParameters);
}
if (!registry[hook]) {
registry[hook] = [];
}
registry[hook].push([fn, parameters]);
},
call: (hook, args) => {
args = args || {};
if (!definitions[hook]) {
throw '"' + hook + '" hook is not defined';
}
if (!isSubSet(Object.keys(args), definitions[hook])) {
throw '"' + hook + '" valid args: ' + stringify(definitions[hook]);
}
var implementations = registry[hook] || [];
var results = [];
implementations.forEach(([fn, parameters]) => {
/* Call with the correct arguments */
var callWith = parameters.map(parameter => args[parameter]);
var result = fn.apply(fn, callWith);
if (result) {
results.push(result);
}
});
return results;
}
};
})(); Or 701 characters using https://skalman.github.io/UglifyJS-online/ |
This one is 683 bytes with Uglify - I like how https://skalman.github.io/UglifyJS-online/ shows you the minified character count as you edit the script: window.datasette = window.datasette || {};
window.datasette.plugins = (() => {
var registry = {};
var definitions = {};
var stringify = JSON.stringify;
function extractParameters(fn) {
var match = /\((.*)\)/.exec(fn.toString());
if (match && match[1].trim()) {
return match[1].split(',').map(s => s.trim());
} else {
return [];
}
}
function isSubSet(a, b) {
return a.every(parameter => b.includes(parameter))
}
return {
_r: registry,
define: (hook, parameters) => {
definitions[hook] = parameters || [];
},
register: (hook, fn, parameters) => {
parameters = parameters || extractParameters(fn);
if (!definitions[hook]) {
throw 'Hook "' + hook + '" not defined';
}
/* Check parameters is a subset of definitions[hook] */
var validParameters = definitions[hook];
if (!isSubSet(parameters, validParameters)) {
throw '"' + hook + '" valid args: ' + stringify(validParameters);
}
if (!registry[hook]) {
registry[hook] = [];
}
registry[hook].push([fn, parameters]);
},
call: (hook, args) => {
args = args || {};
if (!definitions[hook]) {
throw '"' + hook + '" hook not defined';
}
if (!isSubSet(Object.keys(args), definitions[hook])) {
throw '"' + hook + '" valid args: ' + stringify(definitions[hook]);
}
var implementations = registry[hook] || [];
var results = [];
implementations.forEach(([fn, parameters]) => {
/* Call with the correct arguments */
var callWith = parameters.map(parameter => args[parameter]);
var result = fn.apply(fn, callWith);
if (result) {
results.push(result);
}
});
return results;
}
};
})();
|
I'm going to need to add JavaScript unit tests for this new plugin system. |
Removing the window.datasette = window.datasette || {};
window.datasette.plugins = (() => {
var registry = {};
function extractParameters(fn) {
var match = /\((.*)\)/.exec(fn.toString());
if (match && match[1].trim()) {
return match[1].split(',').map(s => s.trim());
} else {
return [];
}
}
return {
register: (hook, fn, parameters) => {
parameters = parameters || extractParameters(fn);
if (!registry[hook]) {
registry[hook] = [];
}
registry[hook].push([fn, parameters]);
},
call: (hook, args) => {
args = args || {};
var implementations = registry[hook] || [];
var results = [];
implementations.forEach(([fn, parameters]) => {
/* Call with the correct arguments */
var callWith = parameters.map(parameter => args[parameter]);
var result = fn.apply(fn, callWith);
if (result) {
results.push(result);
}
});
return results;
}
};
})();
|
262 bytes if I remove the parameter introspection code, instead requiring plugin authors to specify the arguments they take: window.datasette = window.datasette || {};
window.datasette.plugins = (() => {
var registry = {};
return {
register: (hook, fn, parameters) => {
if (!registry[hook]) {
registry[hook] = [];
}
registry[hook].push([fn, parameters]);
},
call: (hook, args) => {
args = args || {};
var results = [];
(registry[hook] || []).forEach(([fn, parameters]) => {
/* Call with the correct arguments */
var callWith = parameters.map(parameter => args[parameter]);
var result = fn.apply(fn, callWith);
if (result) {
results.push(result);
}
});
return results;
}
};
})();
|
I gotta admit that 262 byte version is pretty tempting, if it's going to end up in the |
I'm going to write a few example plugins and try them out against the longer and shorter versions of the script, to get a better feel for how useful the longer versions with the error handling and explicit definition actually are. |
Another option: have both "dev" and "production" versions of the plugin mechanism script. Make it easy to switch between the two. Build JavaScript unit tests that exercise the "production" APIs against the development version, and have extra tests that just work against the features in the development version. |
FixMyStreet inlines some JavaScript, and it's always a good idea to copy what they're doing when it comes to web performance: https://github.com/mysociety/fixmystreet/blob/23e9564b58a86b783ce47f3c0bf837cbd4fe7282/templates/web/base/common_header_tags.html#L19-L25 Note |
This one minifies to 241: var datasette = datasette || {};
datasette.plugins = (() => {
var registry = {};
return {
register: (hook, fn, parameters) => {
if (!registry[hook]) {
registry[hook] = [];
}
registry[hook].push([fn, parameters]);
},
call: (hook, args) => {
args = args || {};
var results = [];
(registry[hook] || []).forEach(([fn, parameters]) => {
/* Call with the correct arguments */
var result = fn.apply(fn, parameters.map(parameter => args[parameter]));
if (result) {
results.push(result);
}
});
return results;
}
};
})();
|
https://twitter.com/dracos/status/1344402639476424706 points out that plugins returning 0 will be ignored. This should probably check for |
If you're using arrow functions, you can presumably use default parameters, not much difference in support. That would save you 9 bytes. But OTOH you need Your latest 250-byte one, with use strict, gzips to 199 bytes. The following might be 292 bytes, but compresses to 204, basically the same, and works in any browser (well, IE9+) at all:
Source for that is below; I replaced the [fn,parameters] because closure-compiler includes a polyfill for that, and I ran var datasette = datasette || {};
datasette.plugins = (() => {
var registry = {};
return {
register: (hook, fn, parameters) => {
if (!registry[hook]) {
registry[hook] = [];
}
registry[hook].push([fn, parameters]);
},
call: (hook, args) => {
args = args || {};
var results = [];
(registry[hook] || []).forEach((data) => {
/* Call with the correct arguments */
var result = data[0].apply(data[0], data[1].map(parameter => args[parameter]));
if (result !== undefined) {
results.push(result);
}
});
return results;
}
};
})(); |
If you could say that all hook functions had to accept one options parameter (and could use object destructuring if they wished to only see a subset), you could have this, which minifies (to all-browser-JS) to 200 bytes, gzips to 146, and works practically the same: var datasette = datasette || {};
datasette.plugins = (() => {
var registry = {};
return {
register: (hook, fn) => {
registry[hook] = registry[hook] || [];
registry[hook].push(fn);
},
call: (hook, args) => {
var results = (registry[hook] || []).map(fn => fn(args||{}));
return results;
}
};
})();
Called the same, definitions tiny bit different: datasette.plugins.register('numbers', ({a, b}) => a + b)
datasette.plugins.register('numbers', o => o.a * o.b)
datasette.plugins.call('numbers', {a: 4, b: 6}) |
Using object destructuring like that is a great idea. I'm going to play with your version - it's delightfully succinct. |
I think I need to keep the mechanism whereby a plugin can return I'll write some example plugins to help me decide if the filtering-out-of-undefined mechanism is needed or not. |
Eventually I'd like to provide a whole bunch of other But I don't want to inline those into the page. So... I think the basic plugin system remains inline - maybe from an inlined file called If a plugin wants to take advantage of those APIs, maybe it registers itself using |
If I'm going to do that, it would be good if subsequent plugins that register against the Maybe the tiny bootstrap code could define a |
Amazing work! And you've put in far more work than I'd expect to reduce the payload (which is admirable). So, to add a plugin with the current design, it goes in (a) the template or (b) a bookmarklet, right? |
You'll be able to add JavaScript plugins using a bunch of different mechanisms:
|
For inlining the |
mishoo/UglifyJS#1905 (comment) says:
|
I have yet to build Datasette plugin and am unfamiliar with Pluggy. Since browsers have event handling builtin Datasette could communicate with plugins through it. Handlers register as listeners for custom Datasette events and Datasette's JS can then trigger said events. I was also wondering if you had looked at Javascript Modules for JS plugins? With services like Skypack (https://www.skypack.dev) NPM libraries can be loaded directly into browser, no build step needed. Same goes for local JS if you adhere to ES Module spec. If minification is required then tools such as Snowpack (https://www.snowpack.dev) could fit better. It uses https://github.com/evanw/esbuild for bundling and minification. On plugins you'd simply: import {register} from '/assets/js/datasette'
register.on({'click' : my_func}) In Datasette HTML pages' head you'd merely import these files as modules one by one. |
I thought about using browser events, but they don't quite match the API that I'm looking to provide. In particular, the great thing about Pluggy is that if you have multiple handlers registered for a specific plugin hook each of those handlers can return a value, and Pluggy will combine those values into a list of replies. This is great for things like plugin hooks that add extra menu items - each plugin can return a menu item (maybe as a label/URL/click-callback object) and the calling code can then add all of those items to the menu. See https://docs.datasette.io/en/stable/plugin_hooks.html#table-actions-datasette-actor-database-table for a Python example. I'm on the fence about relying on JavaScript modules. I need to think about browser compatibility for them - but I'm already commited to requiring support for |
Don't think you are :) (e.g. gzipped, using arrow functions in my example saves 2 bytes over spelling out function). On FMS, past month, looking at popular browsers, looks like we'd have 95.41% arrow support, 94.19% module support, and 4.58% (mostly IE9/IE11/Safari 9) supporting neither. |
With regards to JS/Browser events, given your example of menu items that plugins could add, I could imagine this code to work: // as part of datasette
datasette.events.AddMenuItem = 'DatasetteAddMenuItemEvent';
document.addEventListener(datasette.events.AddMenuItem, (e) => {
// do whatever is needed to add the menu item. Data comes from `e`
alert(e.title + ' ' + e.link);
});
// as part of a plugin
const event = new Event(datasette.events.AddMenuItem, {link: '/foo/bar', title: 'Go somewhere'});
Document.dispatchEvent(event) |
Oh that's interesting, I hadn't thought about plugins firing events - just responding to events fired by the rest of the application. |
I was thinking JavaScript plugins going with server side template extensions custom HTML. Attach my own widgets on there and listen for Datasette events to refresh when user interacts with main UI. Like a map view or table that updates according to selected column. There's certainly other ways to look at this. Perhaps you could list possible hooks or high level design doc on what would be possible with the plugin system? Re: modules. I would like to see modules supported at least in development. The developer experience is so much better than what JavaScript coding has been in the past. With large parts of NPM at your disposal I’d imagine even less experienced coder can whisk a custom plugin in no time. Proper production build system (like one you get with Pika or Parcel) could package everything up into bundles that older browsers can understand. Though that does come with performance and size penalties alongside the added complexity. |
For reasons I've written about elsewhere, I'm in favor of modules. It has several beneficial effects. One, old browsers just ignore it all together. Two, if you include the same plain script on the page more than once, it will be executed twice, but if you include the same module script on a page twice, it will only execute once. Three, you get a module local namespace, instead of having to use the global window namespace or a function private namespace. OTOH, if you are going to use an old style script, the code from before isn't ideal, because you wipe out your registry if the script it included more than once. Also you may as well use object methods and splat arguments. The event based architecture probably makes more sense though. Just make up some event names prefixed with function mycallback(){
// whatever
}
if (window.datasette) {
window.datasette.init(mycallback);
} else {
document.addEventListener('datasette:init', mycallback);
} |
I'm going to ship a version of this in Datasette 0.54 with a warning that the interface should be considered unstable (see #1202) so that we can start trying this out. |
... actually not going to include this in 0.54, I need to write a couple of plugins myself using it before I even make it available in preview. |
Originally posted by @simonw in #981 (comment)
The text was updated successfully, but these errors were encountered: