mirror of
https://github.com/motajs/template.git
synced 2026-11-24 14:54:30 +08:00
refactor: 插件系统结构
This commit is contained in:
1 parent
3cc97e5ed3
commit
e6f4346924
30 files changed
+260
-169
No files matched your search
@@ -1,20 +1,8 @@
|
||||
//#region 插件排序接口
|
||||
|
||||
export interface IPluginDepends {
|
||||
/** 插件标识符 */
|
||||
readonly id: string;
|
||||
/** 插件所依赖的其他插件 id */
|
||||
readonly dependencies: readonly string[];
|
||||
}
|
||||
|
||||
export interface IPluginHolder<T extends IPluginDepends> {
|
||||
/** 插件内容 */
|
||||
readonly plugin: T;
|
||||
}
|
||||
|
||||
export interface IPluginOrderResult<T> {
|
||||
export interface IPluginOrderResult {
|
||||
/** 可初始化的插件拓扑序 */
|
||||
readonly order: T[];
|
||||
readonly order: string[];
|
||||
/** 处于依赖环中的插件 id */
|
||||
readonly cyclic: Set<string>;
|
||||
/** 直接引用了不存在依赖的插件 id,与其缺失的依赖 id 列表 */
|
||||
@@ -29,33 +17,31 @@ export interface IPluginOrderResult<T> {
|
||||
* 依据插件的依赖关系计算可初始化的拓扑序,并检测依赖环与不存在的依赖
|
||||
* @param plugins 以插件 id 为键的插件映射表
|
||||
*/
|
||||
export function orderPlugins<H extends IPluginHolder<IPluginDepends>>(
|
||||
plugins: ReadonlyMap<string, H>
|
||||
): IPluginOrderResult<H> {
|
||||
export function orderPlugins(
|
||||
plugins: ReadonlyMap<string, readonly string[]>
|
||||
): IPluginOrderResult {
|
||||
const cyclic = new Set<string>();
|
||||
const missing = new Map<string, string[]>();
|
||||
|
||||
// 依赖不存在检测:记录直接引用了不存在依赖的插件及其缺失的依赖
|
||||
for (const holder of plugins.values()) {
|
||||
const define = holder.plugin;
|
||||
for (const [id, deps] of plugins) {
|
||||
const lacked: string[] = [];
|
||||
for (const dependency of define.dependencies) {
|
||||
for (const dependency of deps) {
|
||||
if (!plugins.has(dependency)) {
|
||||
lacked.push(dependency);
|
||||
}
|
||||
}
|
||||
if (lacked.length > 0) {
|
||||
missing.set(define.id, lacked);
|
||||
missing.set(id, lacked);
|
||||
}
|
||||
}
|
||||
|
||||
// 环检测:从每个插件出发沿依赖边搜索,若能回到自身则其处于依赖环中
|
||||
for (const id of plugins.keys()) {
|
||||
const start = plugins.get(id)!;
|
||||
for (const [id, deps] of plugins) {
|
||||
const visited = new Set<string>();
|
||||
const pending: string[] = [];
|
||||
for (const dependency of start.plugin.dependencies) {
|
||||
pending.push(dependency);
|
||||
for (const dep of deps) {
|
||||
pending.push(dep);
|
||||
}
|
||||
while (pending.length > 0) {
|
||||
const current = pending.pop()!;
|
||||
@@ -67,7 +53,7 @@ export function orderPlugins<H extends IPluginHolder<IPluginDepends>>(
|
||||
visited.add(current);
|
||||
const node = plugins.get(current);
|
||||
if (!node) continue;
|
||||
for (const dependency of node.plugin.dependencies) {
|
||||
for (const dependency of node) {
|
||||
pending.push(dependency);
|
||||
}
|
||||
}
|
||||
@@ -81,12 +67,11 @@ export function orderPlugins<H extends IPluginHolder<IPluginDepends>>(
|
||||
let expanded = true;
|
||||
while (expanded) {
|
||||
expanded = false;
|
||||
for (const holder of plugins.values()) {
|
||||
const define = holder.plugin;
|
||||
if (blocked.has(define.id)) continue;
|
||||
for (const dependency of define.dependencies) {
|
||||
if (blocked.has(dependency)) {
|
||||
blocked.add(define.id);
|
||||
for (const [id, deps] of plugins) {
|
||||
if (blocked.has(id)) continue;
|
||||
for (const dep of deps) {
|
||||
if (blocked.has(dep)) {
|
||||
blocked.add(id);
|
||||
expanded = true;
|
||||
break;
|
||||
}
|
||||
@@ -95,24 +80,23 @@ export function orderPlugins<H extends IPluginHolder<IPluginDepends>>(
|
||||
}
|
||||
|
||||
// 拓扑排序:反复扫描,逐轮输出依赖均已就绪的插件,保证依赖先于被依赖者
|
||||
const order: H[] = [];
|
||||
const order: string[] = [];
|
||||
const emitted = new Set<string>();
|
||||
let progressed = true;
|
||||
while (progressed) {
|
||||
progressed = false;
|
||||
for (const holder of plugins.values()) {
|
||||
const define = holder.plugin;
|
||||
if (blocked.has(define.id) || emitted.has(define.id)) continue;
|
||||
for (const [id, deps] of plugins) {
|
||||
if (blocked.has(id) || emitted.has(id)) continue;
|
||||
let ready = true;
|
||||
for (const dependency of define.dependencies) {
|
||||
if (!emitted.has(dependency)) {
|
||||
for (const dep of deps) {
|
||||
if (!emitted.has(dep)) {
|
||||
ready = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!ready) continue;
|
||||
order.push(holder);
|
||||
emitted.add(define.id);
|
||||
order.push(id);
|
||||
emitted.add(id);
|
||||
progressed = true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user