Skip to content

Commit 2e9141b

Browse files
committed
[Plugin] refactor plugins to order by id.
Signed-off-by: Eric Wang <[email protected]>
1 parent e0cddd0 commit 2e9141b

File tree

6 files changed

+34
-22
lines changed

6 files changed

+34
-22
lines changed

lib/commands/version.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ cmd.handler = function(argv) {
6565

6666
log.info('\n[Plugins]');
6767
_.each(Plugin.plugins, function(p, k) {
68-
prettyLine(p.id, 'v' + p.ver);
68+
prettyLine(p.name, 'v' + p.ver);
6969
});
7070
};
7171

lib/plugin.js

+17-9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
var _ = require('underscore');
2+
13
var h = require('./helper');
24
var log = require('./log');
35

@@ -16,26 +18,32 @@ Plugin.prototype.setNext = function(next) {
1618
this.next = this.__proto__ = next;
1719
};
1820

19-
Plugin.plugins = {};
21+
Plugin.plugins = [];
2022

21-
Plugin.init = function(root) {
23+
Plugin.init = function(head) {
24+
var plugins = [];
2225
h.getDirData(['lib', 'plugins']).forEach(function(f) {
2326
var p = f.data;
2427
if (!p) return;
2528

26-
log.trace('found plugin: ' + p.id + '=' + p.ver);
29+
log.trace('found plugin: ' + p.name + '=' + p.ver);
2730
p.init();
28-
Plugin.plugins[f.name] = p;
29-
log.trace('inited plugin: ' + p.id);
31+
plugins.push(p);
32+
log.trace('inited plugin: ' + p.name);
33+
});
34+
35+
// chain the plugins together
36+
// the one has bigger `id` comes first
37+
plugins = _.sortBy(plugins, function(p) {
38+
return -p.id;
3039
});
3140

32-
var last = root;
33-
var chains = ['retry', 'cache', 'leetcode'];
34-
chains.forEach(function(name) {
35-
var p = Plugin.plugins[name];
41+
var last = head;
42+
plugins.forEach(function(p) {
3643
last.setNext(p);
3744
last = p;
3845
});
46+
Plugin.plugins = plugins;
3947
};
4048

4149
module.exports = Plugin;

lib/plugins/cache.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ var log = require('../log');
66
var Plugin = require('../plugin');
77
var session = require('../session');
88

9-
var plugin = new Plugin('cache', 'Cache', '20170722', 'Plugin to provide local cache.');
9+
var plugin = new Plugin(20, 'Cache', '2017.07.24', 'Plugin to provide local cache.');
1010

1111
plugin.init = function() {
1212
cache.init();

lib/plugins/leetcode.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ var Plugin = require('../plugin');
1010
var queue = require('../queue');
1111
var session = require('../session');
1212

13-
var plugin = new Plugin('leetcode', 'Leetcode Client', '20170722',
14-
'Plugin to access leetcode APIs.');
13+
var plugin = new Plugin(10, 'Leetcode Client', '2017.07.24',
14+
'Plugin to talk with leetcode APIs.');
1515

1616
// update options with user credentials
1717
function signOpts(opts, user) {

lib/plugins/retry.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ var log = require('../log');
33
var Plugin = require('../plugin');
44
var session = require('../session');
55

6-
var plugin = new Plugin('retry', 'Retry on error', '20170722',
7-
'Plugin to retry last requests on error.');
6+
var plugin = new Plugin(30, 'Retry', '2017.07.24',
7+
'Plugin to retry last failed request if AUTO_LOGIN is on.');
88

99
var count = {};
1010

test/test_plugin.js

+11-7
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ var Plugin = rewire('../lib/plugin');
77
var h = rewire('../lib/helper');
88

99
describe('plugin', function() {
10-
var cache = new Plugin('cache', 'Cache', '1.0', '');
11-
var leetcode = new Plugin('leetcode', 'Leetcode', '2.0', '');
12-
var retry = new Plugin('retry', 'Retry', '3.0', '');
13-
var core = new Plugin('core', 'Core', '4.0', '');
10+
var leetcode = new Plugin(0, 'Leetcode', '2.0', '');
11+
var cache = new Plugin(1, 'Cache', '1.0', '');
12+
var retry = new Plugin(2, 'Retry', '3.0', '');
13+
var core = new Plugin(3, 'Core', '4.0', '');
1414

1515
before(function() {
1616
log.init();
@@ -26,17 +26,21 @@ describe('plugin', function() {
2626
{name: 'cache', data: cache},
2727
{name: 'leetcode', data: leetcode},
2828
{name: 'retry', data: retry},
29-
{name: 'core', data: core},
3029
{name: 'bad', data: null}
3130
];
3231
};
3332
Plugin.__set__('h', h);
3433
});
3534

3635
it('should init ok', function() {
37-
assert.deepEqual(_.keys(Plugin.plugins), []);
36+
assert.deepEqual(Plugin.plugins, []);
3837
Plugin.init(core);
39-
assert.deepEqual(_.keys(Plugin.plugins), ['cache', 'leetcode', 'retry', 'core']);
38+
assert.deepEqual(Plugin.plugins.length, 3);
39+
40+
var names = Plugin.plugins.map(function(p) {
41+
return p.name;
42+
});
43+
assert.deepEqual(names, ['Retry', 'Cache', 'Leetcode']);
4044

4145
assert.equal(core.next, retry);
4246
assert.equal(retry.next, cache);

0 commit comments

Comments
 (0)