mirror of
https://gitee.com/ByteDance/flowgram.ai.git
synced 2025-07-07 17:43:29 +08:00
fix(core): deduplication before singleton plugin init (#142)
This commit is contained in:
parent
e75c2f8607
commit
f43d70ee88
@ -65,6 +65,7 @@ export const Plugin = Symbol('Plugin');
|
|||||||
export type Plugin<Options = any> = {
|
export type Plugin<Options = any> = {
|
||||||
options: Options;
|
options: Options;
|
||||||
pluginId: string;
|
pluginId: string;
|
||||||
|
singleton: boolean;
|
||||||
initPlugin: () => void;
|
initPlugin: () => void;
|
||||||
contributionKeys?: interfaces.ServiceIdentifier[];
|
contributionKeys?: interfaces.ServiceIdentifier[];
|
||||||
containerModules?: interfaces.ContainerModule[];
|
containerModules?: interfaces.ContainerModule[];
|
||||||
@ -78,22 +79,31 @@ export type PluginCreator<Options> = (opts: Options) => Plugin<Options>;
|
|||||||
|
|
||||||
export function loadPlugins(plugins: Plugin[], container: interfaces.Container): void {
|
export function loadPlugins(plugins: Plugin[], container: interfaces.Container): void {
|
||||||
const pluginInitSet = new Set<string>();
|
const pluginInitSet = new Set<string>();
|
||||||
const modules: interfaces.ContainerModule[] = plugins.reduce((res, plugin) => {
|
const singletonPluginIds = new Set<string>();
|
||||||
if (!pluginInitSet.has(plugin.pluginId)) {
|
const modules: interfaces.ContainerModule[] = plugins
|
||||||
plugin.initPlugin();
|
.reduceRight((result: Plugin[], plugin: Plugin) => {
|
||||||
pluginInitSet.add(plugin.pluginId);
|
const shouldSkip = plugin.singleton && singletonPluginIds.has(plugin.pluginId);
|
||||||
}
|
if (plugin.singleton) {
|
||||||
if (plugin.containerModules && plugin.containerModules.length > 0) {
|
singletonPluginIds.add(plugin.pluginId);
|
||||||
for (let module of plugin.containerModules) {
|
}
|
||||||
// 去重
|
return shouldSkip ? result : [plugin, ...result];
|
||||||
if (!res.includes(module)) {
|
}, [])
|
||||||
res.push(module);
|
.reduce((res, plugin) => {
|
||||||
|
if (!pluginInitSet.has(plugin.pluginId)) {
|
||||||
|
plugin.initPlugin();
|
||||||
|
pluginInitSet.add(plugin.pluginId);
|
||||||
|
}
|
||||||
|
if (plugin.containerModules && plugin.containerModules.length > 0) {
|
||||||
|
for (let module of plugin.containerModules) {
|
||||||
|
// 去重
|
||||||
|
if (!res.includes(module)) {
|
||||||
|
res.push(module);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}, [] as interfaces.ContainerModule[]);
|
||||||
return res;
|
|
||||||
}, [] as interfaces.ContainerModule[]);
|
|
||||||
modules.forEach((module) => container.load(module));
|
modules.forEach((module) => container.load(module));
|
||||||
plugins.forEach((plugin) => {
|
plugins.forEach((plugin) => {
|
||||||
if (plugin.contributionKeys) {
|
if (plugin.contributionKeys) {
|
||||||
@ -139,9 +149,10 @@ export function definePluginCreator<Options, CTX extends PluginContext = PluginC
|
|||||||
config: {
|
config: {
|
||||||
containerModules?: interfaces.ContainerModule[];
|
containerModules?: interfaces.ContainerModule[];
|
||||||
contributionKeys?: interfaces.ServiceIdentifier[];
|
contributionKeys?: interfaces.ServiceIdentifier[];
|
||||||
|
singleton?: boolean;
|
||||||
} & PluginConfig<Options, CTX>
|
} & PluginConfig<Options, CTX>
|
||||||
): PluginCreator<Options> {
|
): PluginCreator<Options> {
|
||||||
const { contributionKeys } = config;
|
const { contributionKeys, singleton = false } = config;
|
||||||
pluginIndex += 1;
|
pluginIndex += 1;
|
||||||
const pluginId = `Playground_${pluginIndex}`;
|
const pluginId = `Playground_${pluginIndex}`;
|
||||||
return (opts: Options) => {
|
return (opts: Options) => {
|
||||||
@ -150,6 +161,7 @@ export function definePluginCreator<Options, CTX extends PluginContext = PluginC
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
pluginId,
|
pluginId,
|
||||||
|
singleton,
|
||||||
initPlugin: () => {
|
initPlugin: () => {
|
||||||
// 防止 plugin 被上层业务多次 init
|
// 防止 plugin 被上层业务多次 init
|
||||||
if (isInit) {
|
if (isInit) {
|
||||||
|
|||||||
@ -6,6 +6,7 @@ import { WorkflowLinesLayer } from './layer';
|
|||||||
import { WorkflowBezierLineContribution, WorkflowFoldLineContribution } from './contributions';
|
import { WorkflowBezierLineContribution, WorkflowFoldLineContribution } from './contributions';
|
||||||
|
|
||||||
export const createFreeLinesPlugin = definePluginCreator({
|
export const createFreeLinesPlugin = definePluginCreator({
|
||||||
|
singleton: true,
|
||||||
onInit: (ctx: PluginContext, opts: FreeLinesPluginOptions) => {
|
onInit: (ctx: PluginContext, opts: FreeLinesPluginOptions) => {
|
||||||
ctx.playground.registerLayer(WorkflowLinesLayer, {
|
ctx.playground.registerLayer(WorkflowLinesLayer, {
|
||||||
...opts,
|
...opts,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user