-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(plugin): use standard dfs instead of topological for stable …
…sort
- Loading branch information
1 parent
6e591b4
commit a06f8e5
Showing
5 changed files
with
48 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,17 @@ | ||
import { topologicalSort } from './common'; | ||
import { sortPlugins } from './common'; | ||
import { Plugin } from './impl'; | ||
import { PluginConfigItem, PluginCreateOptions, PluginMap, PluginType } from './types'; | ||
|
||
export class PluginFactory { | ||
static async create(name: string, item: PluginConfigItem, opts?: PluginCreateOptions): Promise<PluginType> { | ||
const pluginInstance = new Plugin(name, item, opts); | ||
await pluginInstance.init(); | ||
return pluginInstance; | ||
} | ||
|
||
static async createFromConfig(config: Record<string, PluginConfigItem>, opts?: PluginCreateOptions): Promise<PluginType[]> { | ||
const pluginInstanceMap: PluginMap = new Map(); | ||
for (const [name, item] of Object.entries(config)) { | ||
const pluginInstance = await PluginFactory.create(name, item, opts); | ||
if (pluginInstance.enable) { | ||
if (item.enable) { | ||
const pluginInstance = new Plugin(name, item); | ||
await pluginInstance.init(); | ||
pluginInstanceMap.set(name, pluginInstance); | ||
} | ||
} | ||
let pluginDepEdgeList: [string, string][] = []; | ||
// Topological sort plugins | ||
for (const [_name, pluginInstance] of pluginInstanceMap) { | ||
pluginInstance.checkDepExisted(pluginInstanceMap); | ||
pluginDepEdgeList = pluginDepEdgeList.concat(pluginInstance.getDepEdgeList()); | ||
} | ||
const pluginSortResult: string[] = topologicalSort(pluginInstanceMap, pluginDepEdgeList); | ||
if (pluginSortResult.length !== pluginInstanceMap.size) { | ||
const diffPlugin = [...pluginInstanceMap.keys()].filter(name => !pluginSortResult.includes(name)); | ||
throw new Error(`There is a cycle in the dependencies, wrong plugin is ${diffPlugin.join(',')}.`); | ||
} | ||
return pluginSortResult.map(name => pluginInstanceMap.get(name)!); | ||
return sortPlugins(pluginInstanceMap, opts?.logger); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters