Skip to content
This repository was archived by the owner on Mar 6, 2020. It is now read-only.

Commit 098dbf6

Browse files
author
René Kooi
committed
plugin: generate hooks for less duplication
1 parent 097765f commit 098dbf6

1 file changed

Lines changed: 16 additions & 24 deletions

File tree

src/Plugin.js

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ define(function (require, exports, module) {
1111
const quote = require('regexp-quote');
1212

1313
const stubHook = function () {};
14+
const hooks = [ 'enable', 'disable' ];
1415

1516
const Plugin = Class.extend({
1617
init(id, ext) {
@@ -36,33 +37,24 @@ define(function (require, exports, module) {
3637
// dis/enable hooks used to require _super() calls which were easy to
3738
// forget. now, we attach events if the methods have been defined.
3839
// it's all a bit ugly but...
39-
if (this.enable !== stubHook) {
40-
this.on('enable', this.enable, this);
41-
}
42-
if (this.disable !== stubHook) {
43-
this.on('disable', this.disable, this);
44-
}
45-
46-
// prevent overwriting dis/enable hooks later
47-
// use the events if you need to do additional work
48-
Object.defineProperties(this, {
49-
enable: {
50-
value: () => {
51-
this.trigger('enable');
52-
Plugin.trigger('enable', this);
53-
}
54-
},
55-
disable: {
56-
value: () => {
57-
// auto-remove event handlers added by the plugin, if the plugin
58-
// used `.listenTo()`
59-
this.stopListening();
60-
this.trigger('disable');
61-
Plugin.trigger('disable', this);
62-
}
40+
hooks.forEach(hookName => {
41+
if (this[hookName] !== stubHook) {
42+
this.on(hookName, this[hookName], this);
43+
// prevent overwriting dis/enable hooks later
44+
// use the events if you need to do additional work
45+
Object.defineProperty(this, hookName, {
46+
value: () => {
47+
this.trigger(hookName);
48+
Plugin.trigger(hookName, this);
49+
}
50+
});
6351
}
6452
});
6553

54+
// auto-remove event handlers added by the plugin, if the plugin
55+
// used `.listenTo()`
56+
this.on('disable', this.stopListening);
57+
6658
// Styles API
6759
this._styles = [];
6860
if (this.style) {

0 commit comments

Comments
 (0)