Skip to content

Commit

Permalink
fix: 修复同步方法调用异常
Browse files Browse the repository at this point in the history
  • Loading branch information
zyao89 committed Mar 11, 2020
1 parent 08b4253 commit 612d55b
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/core/Service/Service.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class Service extends PluginService {
return;
}

this._initPlugins();
this._initPluginsSync();

this.initialized = true; // 再此之前可重新 init

Expand Down Expand Up @@ -157,6 +157,7 @@ class Service extends PluginService {
});
logger.debug('[Plugin]', `run ${name} with args: `, args);

console.warn(this.commands);
const command = this.commands[name];
if (!command) {
logger.throw('[core]', `Command "${name}" does not exists!`);
Expand Down Expand Up @@ -187,7 +188,7 @@ class Service extends PluginService {
}

runSync(name = 'help', args = { _: [] }) {
this.init();
this.initSync();
return this.runCommand(name, args);
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/core/Service/Service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,12 @@ describe('Service', () => {
console.warn(service.microsPackageGraph.rawPackageList);
});

it('new constructor runSync', () => {
const service = new Service();
service.runSync();

expect(service.version).not.toBeUndefined();
expect(service.version).not.toBeNull();
});

});
71 changes: 68 additions & 3 deletions src/core/Service/libs/PluginService.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ class PluginService extends MethodService {
return pluginsObj;
}

async _initPlugin(plugin) {
const { id, apply, opts = {}, mode, alias, target } = plugin;
_initPluginAPI(plugin) {
const { id, apply, mode, alias, target } = plugin;

// --skip-plugins
const skipPlugins = this.context.skipPlugins;
Expand Down Expand Up @@ -135,6 +135,12 @@ class PluginService extends MethodService {
);
plugin._onOptionChange = fn;
};
return api;
}

async _initPlugin(plugin) {
const api = this._initPluginAPI(plugin);
const { apply, opts = {} } = plugin;

if (apply.__isMicroAppCommand) {
const _apply = new apply(api, opts);
Expand All @@ -146,7 +152,21 @@ class PluginService extends MethodService {
plugin[Symbol.for('api')] = api;
}

async _initPlugins() {
_initPluginSync(plugin) {
const api = this._initPluginAPI(plugin);
const { apply, opts = {} } = plugin;

if (apply.__isMicroAppCommand) {
const _apply = new apply(api, opts);
_apply.initialize(api, opts);
} else {
apply(api, opts);
}

plugin[Symbol.for('api')] = api;
}

_sortPlugins() {
this.plugins.push(...this._getPlugins());
const builtIn = Symbol.for('built-in');

Expand Down Expand Up @@ -183,6 +203,10 @@ class PluginService extends MethodService {
// enforce: post
postPlugins
);
}

async _initPlugins() {
this._sortPlugins();

await this.plugins.reduce((_chain, plugin) => _chain.then(() => this._initPlugin(plugin)), Promise.resolve());

Expand Down Expand Up @@ -220,6 +244,47 @@ class PluginService extends MethodService {
logger.debug('[Plugin]', '_initPlugins() End!');
}

_initPluginsSync() {
this._sortPlugins();

this.plugins.forEach(plugin => {
this._initPluginSync(plugin);
});

let count = 0;
while (this.extraPlugins.length) {
const extraPlugins = _.cloneDeep(this.extraPlugins);
this.extraPlugins = [];
extraPlugins.forEach(plugin => {
this._initPluginSync(plugin);
this.plugins.push(plugin);
});
count += 1;
assert(count <= 10, '插件注册死循环?');
}

// TODO 排序重组, reload();


// 过滤掉没有初始化的 plugin
this.plugins = this.plugins.filter(plugin => !!plugin[Symbol.for('api')]);

// Throw error for methods that can't be called after plugins is initialized
this.plugins.forEach(plugin => {
Object.keys(plugin[Symbol.for('api')]).forEach(method => {
if (/^register/i.test(method) || [
'onOptionChange',
].includes(method)) {
plugin[Symbol.for('api')][method] = () => {
logger.throw('[Plugin]', `api.${method}() should not be called after plugin is initialized.`);
};
}
});
});

logger.debug('[Plugin]', '_initPlugins() End!');
}

// ZAP 与 PluginAPI 相似
registerPlugin(opts) {
assert(_.isPlainObject(opts), `opts should be plain object, but got ${opts}`);
Expand Down

0 comments on commit 612d55b

Please sign in to comment.