Allow plugins to have multiple bridge versions
This commit is contained in:
parent
1cc74918e3
commit
f6fde2ea2b
|
@ -25,9 +25,7 @@ import DiscordApi from './discordapi';
|
|||
import { ReactComponents, ReactHelpers } from './reactcomponents';
|
||||
import { Patcher, MonkeyPatch } from './patcher';
|
||||
import GlobalAc from '../ui/autocomplete';
|
||||
import Vue from 'vue';
|
||||
import path from 'path';
|
||||
import Globals from './globals';
|
||||
import semver from 'semver';
|
||||
|
||||
export default class PluginApi {
|
||||
|
||||
|
@ -47,8 +45,18 @@ export default class PluginApi {
|
|||
return PluginManager.getPluginByPath(this.pluginPath);
|
||||
}
|
||||
|
||||
async bridge(plugin_id) {
|
||||
async bridge(plugin_id, request_version) {
|
||||
const plugin = await PluginManager.waitForPlugin(plugin_id);
|
||||
if (!request_version) return plugin.bridge;
|
||||
|
||||
if (plugin.bridges) for (const version of Object.keys(plugin.bridges)) {
|
||||
if (semver.satisfies(version, request_version)) return plugin.bridges[version];
|
||||
}
|
||||
|
||||
if (!semver.satisfies(plugin.version, request_version)) {
|
||||
throw new Error(`Requested version ${request_version} not satisfied by plugin.`);
|
||||
}
|
||||
|
||||
return plugin.bridge;
|
||||
}
|
||||
|
||||
|
|
|
@ -5,12 +5,13 @@ module.exports = (Plugin, Api, Vendor) => {
|
|||
|
||||
return class extends Plugin {
|
||||
async onstart() {
|
||||
const example_plugin = await Api.bridge('example-plugin');
|
||||
// Use version 1 of Example Plugin's bridge
|
||||
const example_plugin = await Api.bridge('example-plugin', '^1');
|
||||
console.log('Example plugin exports:', example_plugin.test1());
|
||||
}
|
||||
|
||||
async onstop() {
|
||||
const example_plugin = await Api.bridge('example-plugin');
|
||||
const example_plugin = await Api.bridge('example-plugin', '^1');
|
||||
console.log('Example plugin exports:', example_plugin.test2());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,7 +22,7 @@
|
|||
},
|
||||
"Just a string"
|
||||
],
|
||||
"version": "1.0",
|
||||
"version": "2.0",
|
||||
"description": "Example Plugin Description.\n\nDescriptions are preformatted (you can use newlines).",
|
||||
"icon": "icon.svg",
|
||||
"icon_type": "image/svg+xml"
|
||||
|
|
|
@ -87,15 +87,37 @@ module.exports = class extends Plugin {
|
|||
Logger.log(e);
|
||||
}
|
||||
|
||||
get bridge() {
|
||||
return {
|
||||
test1: this.test1.bind(this),
|
||||
test2: this.test2.bind(this)
|
||||
};
|
||||
/**
|
||||
* Allows plugins to support plugins using older versions of their bridge.
|
||||
* The bridge property will be used when plugins don't ask for a version.
|
||||
* This uses getters to avoid creating every available versions.
|
||||
*/
|
||||
get bridges() {
|
||||
return Object.defineProperty(this, 'bridges', {value: Object.defineProperties({}, {
|
||||
'2.0.0': {get: () => this.bridge},
|
||||
'1.0.0': {get: () => this.v1bridge}
|
||||
})}).bridges;
|
||||
}
|
||||
|
||||
test1() { return 'It works!'; }
|
||||
test2() { return 'This works too!'; }
|
||||
get bridge() {
|
||||
return Object.defineProperty(this, 'bridge', {value: {
|
||||
test1: this.test1.bind(this),
|
||||
test2: this.test2.bind(this)
|
||||
}}).bridge;
|
||||
}
|
||||
|
||||
async test1() { return 'It works!'; }
|
||||
async test2() { return 'This works too!'; }
|
||||
|
||||
get v1bridge() {
|
||||
return Object.defineProperty(this, 'v1bridge', {value: {
|
||||
test1: this.test1Sync.bind(this),
|
||||
test2: this.test2Sync.bind(this)
|
||||
}}).v1bridge;
|
||||
}
|
||||
|
||||
test1Sync() { return 'It works!'; }
|
||||
test2Sync() { return 'This works too!'; }
|
||||
|
||||
settingChanged(event) {
|
||||
if (!this.enabled) return;
|
||||
|
|
Loading…
Reference in New Issue