Merge pull request #116 from JsSucks/plugin-api

Plugin api
This commit is contained in:
Alexei Stukov 2018-02-12 01:10:34 +02:00 committed by GitHub
commit 06b3b70272
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 80 additions and 11 deletions

View File

@ -0,0 +1,67 @@
/**
* BetterDiscord Plugin Api
* Copyright (c) 2015-present Jiiks/JsSucks - https://github.com/Jiiks / https://github.com/JsSucks
* All rights reserved.
* https://betterdiscord.net
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import { ClientLogger as Logger } from 'common';
import Events from './events';
export default class PluginApi {
constructor(pluginInfo) {
this.pluginInfo = pluginInfo;
}
loggerLog(message) { Logger.log(this.pluginInfo.name, message) }
loggerErr(message) { Logger.err(this.pluginInfo.name, message) }
loggerWarn(message) { Logger.warn(this.pluginInfo.name, message) }
loggerInfo(message) { Logger.info(this.pluginInfo.name, message) }
loggerDbg(message) { Logger.dbg(this.pluginInfo.name, message) }
get Logger() {
return {
log: this.loggerLog.bind(this),
err: this.loggerErr.bind(this),
warn: this.loggerWarn.bind(this),
info: this.loggerInfo.bind(this),
dbg: this.loggerDbg.bind(this)
};
}
get eventSubs() {
return this._eventSubs || (this._eventSubs = []);
}
eventSubscribe(event, callback) {
if (this.eventSubs.find(e => e.event === event)) return;
this.eventSubs.push({
event,
callback
});
Events.on(event, callback);
}
eventUnsubscribe(event) {
const index = this.eventSubs.findIndex(e => e.event === event);
if (index < 0) return;
Events.off(event, this.eventSubs[0].callback);
this.eventSubs.splice(index, 1);
}
eventUnsubscribeAll() {
this.eventSubs.forEach(event => {
Events.off(event.event, event.callback);
});
this._eventSubs = [];
}
get Events() {
return {
subscribe: this.eventSubscribe.bind(this),
unsubscribe: this.eventUnsubscribe.bind(this),
unsubscribeAll: this.eventUnsubscribeAll.bind(this)
}
}
}

View File

@ -10,6 +10,8 @@
import ContentManager from './contentmanager';
import Plugin from './plugin';
import PluginApi from './pluginapi';
import Vendor from './vendor';
import { ClientLogger as Logger } from 'common';
import { Events } from 'modules';
@ -36,7 +38,7 @@ export default class extends ContentManager {
static get loadContent() { return this.loadPlugin }
static async loadPlugin(paths, configs, info, main) {
const plugin = window.require(paths.mainPath)(Plugin, {}, {});
const plugin = window.require(paths.mainPath)(Plugin, new PluginApi(info), Vendor);
const instance = new plugin({ configs, info, main, paths: { contentPath: paths.contentPath, dirName: paths.dirName } });
if (instance.enabled) instance.start();

View File

@ -1,24 +1,24 @@
module.exports = (Plugin, Api, Vendor) => {
const { $, moment } = Vendor;
const { Events } = Api;
const test = 'Testing';
const { $, moment, _ } = Vendor;
const { Events, Logger } = Api;
return class extends Plugin {
test() {
return test;
}
onStart() {
console.log('Example Plugin 1 onStart');
Events.subscribe('TEST_EVENT', this.eventTest);
Logger.log('onStart');
return true;
}
onStop() {
console.log('Example Plugin 1 onStop');
Events.unsubscribeAll();
Logger.log('onStop');
return true;
}
eventTest(e) {
Logger.log(e);
}
}
}